Picking color

Dear everyone

I have some question for you. You know, we can know the color on pixel using glReadpixel().
My implementation is following.

OnRButtonDown(UINT nFlags, CPoint point){
unsigned char rgb[3];
glReadPixels(point.x, m_vViewport[3] - point.y - 1, 1, 1, GL_RGB, GL_UNSIGNED_BYTE, rgb);

char ss[50];
sprintf(ss,"%d %d %d",(int)rgb[2],(int)rgb[1],(int)rgb[0]);

I draw some objects on the screen.
But the result always shows 204 204 204
Could you please explain this?
Thank you


At the beginnig I wanted to thank you . Recently I was thinking about color picking too but something was wrong and I couldn’t figure out what was it. When I looked to your code I noticed that you have something like “m_vViewport[3] - point.y” in glReadPixels() and I thought “What the hell is that?!”. So I got back to my program and I tried to subtract mouse position Y from screen height and now every thing works JUST FINE! I am not sure why is that so. Meaby I just didn’t know it but is OpenGL taking (0,0) as left-bottom corner of the screen? Thanx once again.

OK, getting back to your code. Hmm… I am afraid that a problem is somewhere else in the code. My own was similiar. Are you using double buffering? Did you check if your result isn’t your clear color?

I can send you a piece of my code, if you wish. Just send me an e-mail.

See you,


204 is CC in hex, which is what some compiler uses for uninitialized values in debug mode (to help debugging). Try initialize your array to something else and see if it actually get the values 204 or if it keeps its old values. If the old values remains, then I can only think of one thing; you don’t have a rendering context at the time you’re reading the color.

Orzech, yes, OpenGL has the origin in the lower left corner.

Originally posted by Bob:
Orzech, yes, OpenGL has the origin in the lower left corner.

Good to know that.