Need help using glReadPixels to read framebuffer

Can someone who has used glReadPixels help?

I need some suggestions for what’s wrong with the way I’m using glReadPixels or a code snippet showing how to read RGB values from the framebuffer.

Here’s the situtation:

  1. I’m using double buffering and rendering a gray object that completely fills my GLUT window.
  2. Material reflectance values are RGBA = (0.55, 0.55, 0.55, 1.0).
  3. I am trying to extract the RGB values of pixels in the framebuffer. I should get (0.55, 0.55, 0.55) for RGB, or (140, 140, 140) since I’m using an unsigned byte to store each R, G and B value and float values between 0 and 1 are scaled from 0 to 255.

Instead, the unsigned byte array I pass to glReadPixels appears to be unchanged after the call to glReadPixels.

The block of code that I’m using is:

// X location in pixels (relative to the screen origin) of the current window.

int x = glutGet(GLUT_WINDOW_X);

// Y location in pixels (relative to the screen origin) of the current window.

int y = glutGet(GLUT_WINDOW_Y); 

const size_t width = glutGet(GLUT_WINDOW_WIDTH);
const size_t height = glutGet(GLUT_WINDOW_HEIGHT);

// Number of bytes in array to hold RGB values
const size_t n_values = width*height*3;

GLubyte * pixels = new GLubyte [n_values];

size_t i;

// Initialize all values to 0
for (i = 0; i < n_values; i++)
{
	pixels[i] = 0;
}

glReadBuffer(GL_FRONT_LEFT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

// PROBLEM: At this point the pixels array still has zeros

Vishnu

Hello

Think I know what’s wrong here… The first four arguments in glReadPixels() is relative to the window, not the screen. If you window is placed at (100,100) and size is (50,50), you will have to call glReadPixels() as glReadPixels(0,0,50,50…), not glReadPixels(100,100,50,50…)

Bob

I modified the first two parameters to glReadPixel. The call is now:

glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

Now I get R=G=B=128 for all pixels. Shouldn’t it be 140 for r=g=b=0.55?

Also, the content of the pixel array set by glReadPixels doesn’t change even when I change the color of the object rendered in the GLUT window.

For example, I would expect that when I make the object filling the Glut window Red=1.0, Green=Blue= 0.0, the first byte in the pixels array should be 255, the second and third 0. But they are all 128.

Vishnu

Hello again

Hmm, has been thinking about this for a while now, and I can’t see anything wrong. I suggest you try to find some code on the net and have a look at it.

Bob

snippet from a function i currently use to grab the framebuffer:

byte pixmap=(byte )malloc(widthheight3);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glReadPixels(0,0,width,height,GL_RGB,GL_UNSIGNED_BYTE,pixmap);

if it don’t work, then the problem should be elsewhere…

instead of using glReadBuffe(GL_FRONT_LEFT) use GL_FRONT: specs state that there is no difference, but who knows… moreover, it’s more readable this way.

oh, unless you’re performing stereo rendering

Dolo//\ightY