Question

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 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.

Was it helpful?

Solution

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.

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top