glReadPixels

Hi everyone,

I’m trying to implement selection mechanism using unique colors.
If I try to get color info for a single pixel everything works just fine and I always get correct color.

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(x, viewport[3] - y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);

But if I try to get color info for a rectangular group of pixels I don’t get correct colors

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(x, viewport[3] - y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);

According to the gotten results, it seems that some of the pixels are missed somehow…

Any ideas??

Additional information…
For example if a blue QUAD is drawn so that it is spread all over the viewport and if I read pixels with

GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glReadPixels(viewport[0],viewport[1],viewport[2],viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pixels);

I’ll get that color of some pixels is (0,0,255) and color of the other pixels is (255,255,255).
Shouldn’t it be (0,0,255) for all pixels? That’s why I said it seems that some pixels are missed…

See http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads

To fix: use GL_BGRA/GL_RGBA (GL_BGRA likely to be fastest on most platforms), or glPixelStorei(GL_PACK_ALIGNMENT, 1);

If you don’t call glPixelStorei(GL_PACK_ALIGNMENT, 1), then you are corrupting some data.

Also, you should be call glReadPixels before swapping buffers.

Maybe you are missing the “-1” in:
glReadPixels(x, viewport[3] - y, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);
Try:
glReadPixels(x, viewport[3] - y - 1, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, pixel);