PDA

View Full Version : glReadPixels



hof267
07-12-2012, 05:56 AM
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??

hof267
07-12-2012, 06:05 AM
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....

Dan Bartlett
07-12-2012, 07:52 AM
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);

V-man
07-13-2012, 09:55 AM
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.

Ludde
07-13-2012, 12:05 PM
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);