You definitely have to go offscreen if you running your program under Windows. Because if you running on-screen, you have to make sure the window is not partially or fully covered by another one: in this case, the pixel ownership test is taken into account and you will get garbage in the covered part of your image.
see figure 4.1 page 202 of OpenGL spec 2.1, chapter 4 “Per-Fragment Operations and the framebuffer”:
They are many approaches to initialize an offscreen window depending on the capabilities of your OpenGL implementation and the operating system you are using.
Going from best to worst:
- If framebuffer object is supported, go for it. This is flexible and hardware accelerated and it works within an existing OpenGL context.
If not, you will have to go with older solutions:
go with pbuffer (usually hardware accelerated), but you have to create a special pbuffer context.
if not, go with pixmap on Unix (glx) or DIB (device-independent bitmap) on Windows, both are software and slow:
ref for DIB: http://www.mesa3d.org/brianp/sig97/offscrn.htm#wgl
- or if you are using Mesa, use OSMesa (OS stands for Off Screen), software and slow, but you don’t have to use ReadPixel because you are giving color buffer to Mesa.