【问题标题】:Haskell - Could not deduce ... from Context error - OpenGL AsUniform class typeHaskell - 无法从上下文错误中推断出...... OpenGL AsUniform 类类型
【发布时间】:2015-08-28 08:27:11
【问题描述】:

我正在努力使我的数据类型通用,而不是采用 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

谢谢!

【问题讨论】:

    标签: class haskell opengl types uniform


    【解决方案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 是罪魁祸首。

    【讨论】:

    • 不:/。同样的错误。将它放在顶部和我的 cabal 文件中,我尝试将 sig 更改为AsUniform (V4 (V4 a)),但这并没有影响它。也许 GLUtil 包页面可能会稍微说明它为什么不起作用,你认为?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多