I want to access the pixel data of what is being displayed to the window, but I have not had any luck finding such a function in gloss, nor by attempting to call OpenGL readPixels in a keyboard event callback. It looks like gloss renders a Picture to the window without exposing the rendered bitmap.
If this is hard to do in gloss, is there an alternative which has realtime high-level bitmap manipulation (translation, rotation, transparency)?
It turns out readPixels
can be used in this case. I found this snippet while digging through #haskell chat logs:
-- save a screenshot to a handle as binary PPM
snapshotWith :: (BS.ByteString -> IO b) -> Position -> Size -> IO b
snapshotWith f p0 vp@(Size vw vh) = do
let fi q = fromIntegral q
p6 = "P6\n" ++ show vw ++ " " ++ show vh ++ " 255\n"
allocaBytes (fi (vw*vh*3)) $ \ptr -> do
readPixels p0 vp $ PixelData RGB UnsignedByte ptr
px <- BSI.create (fi $ vw * vh * 3) $ \d -> forM_ [0..vh-1] $ \y ->
BSI.memcpy
(d`plusPtr`fi(y*vw*3))
(ptr`plusPtr`fi ((vh-1-y)*vw*3))
(fi(vw*3))
f $ BS.pack (map (toEnum . fromEnum) p6) `BS.append` px
writeSnapshot :: FilePath -> Position -> Size -> IO ()
writeSnapshot f = snapshotWith (BS.writeFile f)