glReadPixels()

Hello. I’ve searched the forum and saw great examples on the usage of glReadPixels(), but everytime I check the values returned in color[3] they are always 204, 204, 204. Anyone knows why? Sorry if this question is dumb.

Thanks!

You’re doing something wrong. That’s all I can say with the details you provided.

Now, explain what you do. Preferably with code.

In my OnLButtonDown function, I have

COrthoApp m_orthodontics3dapp = (COrthoApp)AfxGetApp();

GLubyte color[3];

glReadPixels(point.x,point.y,1,1,GL_RGB,GL_UNSIGNED_BYTE,color);

That’s all. I have a break point after glReadPixels and when I check it, it says 204 204 204. Rather I think it’s more like not changed by glReadPixels at all. Perhaps I’m not too familiar with VC programming. Please help?

That was not very helpful.
Do you have an OpenGL context current when reading? (Because 204 == 0xCC which is what some copilers use to tag uninitialzed data in debug mode. So maybe the OpenGL call did nothing to your local variable.)
You are aware the point coordinates need to be in OpenGL screen coordinates (origin bottom left)?
What’s your glReadBuffer?
What did you expect?

Can yu give the complete code?

I’m sorry if I have irritated some of you, I’m a super newbie in this. I only have an extra glReadBuffer(GL_BACK) (since I use double buffering) before the glreadpixels line. I think the glreadpixels is not updating the color[3]. The device context should be correct, since it gets to breakpoint only in that child window and the coordinates are not windows coordinates (they are ok when I check the variables in debug). Does the fact that I’m using MFC with main frame/child windows matter?

BTW I can’t give the full code because that’s all to that function (OnLButtonDown), and the rest of the files are in like megabytes. I had to take over somebody’s job, that’s why I’m so confused…

If you’re doing the read in the mouse down, you probably want to be reading from the front buffer since the back buffer that you drew to would already have been swapped to the front.

Also, as has already been noted, you probably aren’t reading at the point you think you are reading from… Windows coordinates (such as you get for the mouse position) read from upper left as 0,0 with positive Y going down, OpenGL coordinates (such as you want to pass to glReadPixels) read from the lower left as 0,0 with positive Y going up.

I was not irritated, just straight.

Again one reason for the function to not touch the color variable can be that you might not have an OpenGL context current at the time you do the call. Don’t mix that term with device context (HDC).

To check that:

  • Put a breakpoint on all wglMakeCurrent calls you find in the code and check if there is one != NULL when you’re reaching the glReadPixels call.
  • You can also add a wglGetCurrentContext and wglGetCurrentDC right before the glReadPixels and check if they return valid handles and the ones you expected.
  • Add some simple drawing commands like
    glDrawBuffer(GL_FRONT);
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    glFinish();
    at that point and check if it works (=> red window).
  • Check if the color variable is really written by initializing it to some fancy number. (Could also be that you actually read some grey pixel from somewhere.)
  • For the experienced, switch to disassembly and check if you get through the OpenGL32.dll into the ICD.