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:
- I’m using double buffering and rendering a gray object that completely fills my GLUT window.
- Material reflectance values are RGBA = (0.55, 0.55, 0.55, 1.0).
- 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