How to flip color buffer during/before glReadPixel

I’m adding a file save feature to my app that takes a snapshot of a 3D view and saves it to disk as a TIFF or JPEG.

I use glReadPixels to copy the pixel data from a framebuffer object to a data structure in main memory.

Unfortunately, pixel data is stored with 0,0 at the top left on my platform (Mac OS X). Thus, the image winds up flipped vertically once it winds up in main memory.

I could flip it in main memory by re-drawing it from one bitmap structure (actually an NSBitmapImageRep) to another, but the image is potentially huge (8000x8000 pixels or bigger) and I’d rather not create yet another copy of it if I can avoid it. I guess I could also write code that would line-by-line flip every scan line while only saving one scan line to temporary memory.

My preference, though, would be to have OpenGL either render the image flipped, or flip it as it copies it to main memory. I tried using glPixelZoom, but apparently that only affects glDrawPixels, not glReadPixels.

A transformation matrix won’t work (I don’t think) because this is a 2D flip of my scene after it’s been rendered to the pixel buffer, with lighting, projection, etc, etc.

I assume there is some fairly simple way to do this, but what is it? I guess I could use glPixelZoom plus glDrawPixels to copy and invert the contents of the front buffer to the back buffer, then use glReadPixels to copy the back buffer to main memory. Is there a cleaner way, however?

Duncan C

I would just do the “write code that would line-by-line flip every scan line while only saving one scan line to temporary memory.” - any other way is probably going to cause OpenGL to allocate a large buffer to do the flip in.

The other options are to redraw the scene upside down just before you save, or save in a bitmap format that has a flag indicating the orientation of the bitmap data. (I believe TGA has this flag, not sure about other formats)

From wikipedia : http://www.gamers.org/dEngine/quake3/TGA.txt