1

我正在尝试将图像加载到 OpenGL 纹理中,但我不确定如何解决我遇到的类型错误。根据错误文本,我认为在我的GL.texImage2D通话中某处我搞砸了,但这里似乎没有任何问题。

import Graphics.Rendering.OpenGL as GL
import Graphics.Rendering.OpenGL (($=))

import Codec.Picture.Repa as Repa

newImage fname = do
    img <- Repa.readImage fname
    case img of
        Left  _ -> return Nothing
        Right x -> do
            let (dat, w, h) = Repa.toForeignPtr . Repa.reverseColorChannel $ x

            [tex] <- genObjectNames 1
            GL.textureBinding GL.Texture2D $= Just tex
            withForeignPtr dat $ \ptr -> do
                (GL.texImage2D
                    Nothing
                    GL.NoProxy
                    0
                    GL.RGBA8
                    (GL.TextureSize2D (fromIntegral w) (fromIntegral h))
                    0
                    (GL.PixelData GL.RGBA GL.UnsignedByte ptr))
            return $ Just tex

这是我得到的错误。

No instance for (TwoDimensionalTextureTarget (Maybe a0))
  arising from a use of `texImage2D'
Possible fix:
  add an instance declaration for
  (TwoDimensionalTextureTarget (Maybe a0))
In a stmt of a 'do' block:
  (texImage2D
     Nothing
     NoProxy
     0
     RGBA8
     (TextureSize2D (fromIntegral w) (fromIntegral h))
     0
     (PixelData RGBA UnsignedByte ptr))
In the expression:
  do { (texImage2D
          Nothing
          NoProxy
          0
          RGBA8
          (TextureSize2D (fromIntegral w) (fromIntegral h))
          0
          (PixelData RGBA UnsignedByte ptr)) }
In the second argument of `($)', namely
  `\ ptr
     -> do { (texImage2D
                Nothing
                NoProxy
                0
                RGBA8
                (TextureSize2D (fromIntegral w) (fromIntegral h))
                0
                (PixelData RGBA UnsignedByte ptr)) }'
4

1 回答 1

4

类型签名texImage2D

texImage2D :: TwoDimensionalTextureTarget t
           => t -> Proxy -> Level -> PixelInternalFormat -> TextureSize2D -> Border -> PixelData a -> IO ()

类的实例TwoDimensionalTextureTargetTextureTargetCubeMapFaceTextureTargetCubeMapTextureTarget2D。这些都不是任何形式的类型同义词Maybe t。因此,Nothing作为第一个参数提供textImage2D不会进行类型检查。(也可能有其他错误——我只是从Hackage上的错误消息中查找了相关类型。)

于 2013-10-20T03:08:40.227 回答