简体   繁体   English

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

[英]Haskell - Could not deduce … from Context error - OpenGL AsUniform class type

I'm working on making my data types general instead of taking in the OpenGL type GLfloat . 我正在努力使我的数据类型通用,而不是采用OpenGL类型GLfloat So I started making it take in type a and then just replacing everything with that. 因此,我开始将其放入类型a ,然后将其替换为所有内容。

Now, I've come to a point where I'm setting uniform variables, but they take in GLfloat's. 现在,我已经到了设置统一变量的地步,但是它们采用了GLfloat的变量。 I'm using a library called GLUtil which makes it a bit easier, which has provided a class AsUniform , to check whether the type can be a uniform variable or not. 我正在使用一个名为GLUtil的库,该库使它更容易一些,它提供了一个类AsUniform ,以检查类型是否可以是统一变量。 I stick it in my type signature, but it stills gives me an error. 我将其粘贴在类型签名中,但仍然给我一个错误。 Here's the code: 这是代码:

-- | 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

and the error: 和错误:

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 is made an instance of AsUniform, as well as M44, which is a type for (V4 (V4 a)), which I thought might be the issue, so I'm not sure why it's acting up. V4既是AsUniform的实例,又是M44的实例,而M44是(V4(V4 a))的类型,我认为这可能是问题所在,所以我不确定为什么它起作用了。

Here's the source for the class: 这是该课程的源代码:

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

Thanks! 谢谢!

Try adding -XFlexibleContexts and the constraint, literally, to your existing answer: 尝试将-XFlexibleContexts和约束添加到现有答案中:

{-# LANGUAGE FlexibleContexts #-}

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

Usually this is the routine for constraints that aren't inferrable, or where constraints need to be transitively included in all call sites. 通常,这是无法避免的约束条件的例程,或者是需要在所有调用站点中传递约束条件的例程。 This happens to me all the time with MonadState et al. 我一直在MonadState等人身上发生这种情况。 In this case, setUniform is the culprit. 在这种情况下, setUniform是元凶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM