开发者

Loading a OpenGL Image with SDL-image

As it says above: How can I employ the image loading facilities of the SDL-image binding to Haskell to load a OpenGL texture, as you so often do in C. SDL-image supports a nice variety of image formats, but only delivers a Surface data type. For glTexImage2D, it seems, I need to provide something different, a PixelData data type.

Any way to get开发者_JS百科 the image data without resorting to C? I would even employ some other library, as long as it gives me PNG, JPG and TGA support.


To make this work with SDL surfaces & Haskell bindings use surfaceGetPixels it returns Pixels which is a type alias for Ptr PixelData and PixelData is an empty data declaration. The reason why this is, is because SDL surface pixel format & number of bits per pixel could be almost anything. So basically if you have 32bpp format you would cast the pointer to say Ptr Word32 using castPtr.

Here is an example of getting/putting a pixel:

getPixel32 :: MonadIO m => Surface -> Int -> Int -> m Pixel
getPixel32 s x y = liftIO $ do
    ps <- surfaceGetPixels s
    assert (x >= 0 && x < surfaceGetWidth s && y >= 0 && y < surfaceGetHeight s) $
        Pixel `liftM` peekElemOff (castPtr ps :: Ptr Word32) offset
 where offset = y * (fromIntegral $ surfaceGetPitch s `div` 4) + x

setPixel32 :: MonadIO m => Surface -> Int -> Int -> Pixel -> m ()
setPixel32 s x y (Pixel pixel) = liftIO $ do
    ps <- surfaceGetPixels s
    assert (x >= 0 && x < surfaceGetWidth s && y >= 0 && y < surfaceGetHeight s) $
        pokeElemOff (castPtr ps :: Ptr Word32) offset pixel
 where offset = y * (fromIntegral $ surfaceGetPitch s `div` 4) + x

So similarly you can cast the pointer to a particular pointer type and give that to glTexImage2D to upload the texture.


Perhaps Codec.Image.DevIL provides what you're looking for? I believe it should anyway.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜