glReadPixels

Hi,
I’m trying to read the pixels from the backbuffer using gl_ReadPixels. I want to create a snapshot of the current back buffer and write it to a bitmap file. I can easy use the glReadPixels to read the data of the current opengl screen, but if another window is on top of this opengl window, then the pixels of the opengl window beneath it are not correctly read.
Does someone has a solution?

Thanks is advance,
Ward

Hi !

If you use double buffering you might want to read from the non visible back buffer instead, then you don’t get that problem.

Mikael

No mikael, the problem described happens both on front- and back-buffer. I seem to remember that it is written as “undefined behavior” in the GL spec.

Solutions :

  • create GL window as BITMAP-something mode, but will be software rendering. Basically you render to an invisible buffer, I don’t much about this.
  • use pbuffers to render to an accelerated invisible buffer. It is quite cumbersome to do by hand, but you will find some multi-plateform code here :
    http://www.gamedev.net/community/forums/topic.asp?topic_id=184777

(you can look at my messy-but-working code about pbuffers on windows, check website in my profile)

Hope it helped.

Thank you both for your inputs. As michael said I can use the glReadPixels from the bach buffer too. How can I do that?
ZBuffer, you say that in the backbuffer you I will have the same problem. Something to do with windows and clipping (some sort of optimalisation I presume). By the way, I’ve just downloaded your code examples. I don’t know what they do at the moment, but I will have a look at them soon.

greetz,
Ward

This falls under pixel ownership test.
Check the OpenGL 2.0 specs chapter 4.1.1
“The first test is to determine if the pixel at location (xw, yw) in the framebuffer
is currently owned by the GL (more precisely, by this GL context). If it is not,
the window system decides the fate the incoming fragment. Possible results are
that the fragment is discarded or that some subset of the subsequent per-fragment
operations are applied to the fragment. This test allows the window system to
control the GL’s behavior, for instance, when a GL window is obscured.”

All buffers composing a the bits of a “pixel” in OpenGL (front, back, depth, stencil, etc.) are affected.
Even if reading the backbuffer works on some implementations, that doesn’t mean it will work on others. Implementations which share the back and depth buffer among OpenGL windows will handle the pixel ownership according to the clipping situation.
Use pbuffers for unclipped rendering.

The current draw and read buffers are set with glDrawBuffer and glReadBuffer. For double buffered pixelformats both are initially set to back.