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が認識できるようにするため)

4

1 に答える 1

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)

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。必要がなければ、それぞれを個別にインポートする必要はありません。

于 2012-05-06T12:06:41.737 に答える