Haskell - 无法从上下文错误中推断出... - OpenGL AsUniform class 类型

Haskell - Could not deduce ... from Context error - OpenGL AsUniform class type

我正在努力使我的数据类型通用,而不是采用 OpenGL 类型 GLfloat。所以我开始让它接受 a 类型,然后用它替换所有内容。

现在,我已经到了要设置统一变量的地步,但它们采用了 GLfloat。我正在使用一个名为 GLUtil 的库,它使它变得更容易一些,它提供了 class AsUniform,来检查类型是否可以是统一变量。我把它贴在我的类型签名中,但它仍然给我一个错误。这是代码:

-- | Sets the modelview and projection matrix uniform variables.
mvpUnif :: (GL.UniformComponent a, Num a, Epsilon a, Floating a, AsUniform a) => (GLState a) -> ShaderProgram  -> IO ()
mvpUnif state p = do
-- Check if view and projection matrices are there, else set them to the identity.
let vMat = case vMatrix state of
    Just v -> v
    Nothing -> getIdentity
let pMat = case pMatrix state of
    Just p -> p
    Nothing -> getIdentity
-- Multiply model and view matrix together.
let mvMatrix = vMat !*! mMatrix state
setUniform p uModelViewMatrixVar mvMatrix
setUniform p uProjectionMatrixVar pMat

和错误:

Could not deduce (AsUniform (V4 (V4 a)))
  arising from a use of `setUniform'
from the context (GL.UniformComponent a,
                  Num a,
                  Epsilon a,
                  Floating a,
                  AsUniform a)
  bound by the type signature for
             mvpUnif :: (GL.UniformComponent a, Num a, Epsilon a, Floating a
,
                         AsUniform a) =>
                        GLState a -> ShaderProgram -> IO ()
  at src\Graphics\FreeD\Shaders\DefaultShaders.hs:194:12-119
In a stmt of a 'do' block:
  setUniform p uModelViewMatrixVar mvMatrix
In the expression:
  do { let vMat = ...;
       let pMat = ...;
       let mvMatrix = vMat !*! mMatrix state;
       setUniform p uModelViewMatrixVar mvMatrix;
       .... }
In an equation for `mvpUnif':
    mvpUnif state p
      = do { let vMat = ...;
             let pMat = ...;
             let mvMatrix = ...;
             .... }

V4 是 AsUniform 的一个实例,M44 是 (V4 (V4 a)) 的一个类型,我认为这可能是问题所在,所以我不确定它为什么会这样。

这是 class 的来源:

http://hackage.haskell.org/package/GLUtil-0.8.5/docs/Graphics-GLUtil-Linear.html

谢谢!

尝试将 -XFlexibleContexts 和约束添加到您现有的答案中:

{-# LANGUAGE FlexibleContexts #-}

mvpUnif :: ( GL.UniformComponent a
           , Num a
           , Epsilon a
           , Floating a
           , AsUniform a
           , AsUniform (V4 (V4 a))
           ) => (GLState a) -> ShaderProgram  -> IO ()

通常这是不可推断的约束的例程,或者约束需要传递地包含在所有调用站点中的地方。 MonadState 等人时常发生这种情况。在这种情况下,setUniform 是罪魁祸首。