Haskell - 无法推断出......来自Context错误 - OpenGL AsUniform类类型

时间:2015-06-12 10:45:45

标签: class haskell opengl types uniform

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

现在,我已经到了设置统一变量的地步,但他们接受了GLfloat。我正在使用一个名为GLUtil的库,它使得它更容易,它提供了一个类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))的类型,我认为可能是这个问题,所以我不确定为什么它'表演。

这是该课程的来源:

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

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试将-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是罪魁祸首。