Haskell OpenGL中的JuicyPixels纹理加载?

12

我该如何使用Haskell、OpenGL和JuicyPixels库加载纹理?

我已经做到了这一步:

loadImage :: IO ()
loadImage = do image <- readPng "data/Picture.png"
               case image of 
                 (Left s) -> do print s
                                exitWith (ExitFailure 1)
                 (Right d) -> do case (ImageRGBA i) -> do etc...

我该如何将这个转换为TextureObject?我认为我需要在Vector Word8和PixelData之间进行转换(以便OpenGL可以识别)


请注意,这部分内容JuicyPixels-Repa非常有帮助,但需要更新以适应Repa-3.0(GHC 7.4或更高版本)。 - Thomas M. DuBuisson
哎呀,我忘记了我已经为新的repa更新了JP-repa的一些工作。太好了。 - Thomas M. DuBuisson
1个回答

8
你需要使用 texImage2D 函数。你可以按照以下方式调用它:
import Data.Vector.Storable (unsafeWith)

import Graphics.Rendering.OpenGL.GL.Texturing.Specification (texImage2D, Level, Border, TextureSize2D(..))
import Graphics.Rendering.OpenGL.GL.PixelRectangles.ColorTable (Proxy(..), PixelInternalFormat(..))
import Graphics.Rendering.OpenGL.GL.PixelRectangles.Rasterization (PixelData(..))

-- ...

(ImageRGBA8 (Image width height dat)) ->
  -- Access the data vector pointer
  unsafeWith dat $ \ptr ->
    -- Generate the texture
    texImage2D
      -- No cube map
      Nothing
      -- No proxy
      NoProxy
      -- No mipmaps
      0
      -- Internal storage format: use R8G8B8A8 as internal storage
      RGBA8
      -- Size of the image
      (TextureSize2D width height)
      -- No borders
      0
      -- The pixel data: the vector contains Bytes, in RGBA order
      (PixelData RGBA UnsignedByte ptr)

请注意,Juicy并不总是返回RGBA格式的图像。您需要处理每个不同的图像变体:
ImageY8, ImageYA8, ImageRGB8, ImageRGBA8, ImageYCrCb8

此命令执行前,您必须绑定一个纹理对象以存储纹理数据。
import Data.ObjectName (genObjectNames)
import Graphics.Rendering.OpenGL.GL.Texturing.Objects (textureBinding)
import Graphics.Rendering.OpenGL.GL.Texturing.Specification (TextureTarget(..))

-- ...

-- Generate 1 texture object
[texObject] <- genObjectNames 1

-- Make it the "currently bound 2D texture"
textureBinding Texture2D $= Just texObject

顺便说一下,当你导入Graphics.Rendering.OpenGL时,许多这些导入将自动添加;如果不需要单独导入每个内容。


谢谢帮助!我认为应该是 UnsignedByte 而不是 Byte。除此之外,这个代码可以工作。 - Mark
对于OpenGL 3.x(Haskell包版本3.x,而不是OpenGL版本3.x),请将texImage2D的第一个参数从Nothing替换为Texture2D - kolen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接