在Haskell OpenGL中加载JuicyPixels纹理?

Mar*_*ark 12 opengl haskell

如何使用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...
Run Code Online (Sandbox Code Playgroud)

如何将其转换为TextureObject?我想我需要在Vector Word8和PixelData之间进行转换(用于OpenGL识别)

dfl*_*str 7

你使用该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)
Run Code Online (Sandbox Code Playgroud)

请注意,Juicy并不总是返回RGBA图像.您必须处理每种不同的图像变化:

ImageY8, ImageYA8, ImageRGB8, ImageRGBA8, ImageYCrCb8
Run Code Online (Sandbox Code Playgroud)

此外,在此命令之前,您必须绑定纹理对象以存储纹理数据.

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
Run Code Online (Sandbox Code Playgroud)

顺便说一句,导入时会自动添加其中许多导入Graphics.Rendering.OpenGL; 如果你不想要的话,你不必单独导入每件东西.