exact color with glReadPixels ?

Hi !

I´m trying to emulate the picking function by drawing the objects in different red colors in the back buffer.
Afterwards the pixel color under the mouse cursor is read in order to determine which object has been selected.

The code looks like this

  
  ...
  GLubyte pixels[1]={0};                              // storage for 1 Pixel red color

  glPushAttrib(GL_ALL_ATTRIB_BITS); 

  // disable environment to avoid color change of pixels 
  glBindTexture(GL_TEXTURE_2D, 0); 
  glDisable(GL_DITHER);
  glDisable(GL_DEPTH_TEST);
  glDisable(GL_BLEND);
  glDisable(GL_TEXTURE_2D);
  glDisable(GL_COLOR_MATERIAL);
  glDisable(GL_LIGHTING);                  
  
  ...
  // draw objects 
  for (i=1; i<=20; i++) {
    glColor3ub(i*10,0,0); // allows 255:10=25 different red objects in back buffer                                          
    glCallList(MYLIST);
  }
  ...

  glPixelStorei(GL_UNPACK_ALIGNMENT,1);                            // needed ??
  glReadBuffer(GL_BACK);
  glReadPixels(MouseX,lpRect->bottom-MouseY-1, 1,1,GL_RED,GL_UNSIGNED_BYTE,pixels);

The above code seems to work BUT the pixel color which is returned is not the value I have set at the beginning.
There is a difference of +/- 2 e.g.
red pixel result is : 8, 16, 25, 41… (for the first 4 objects)
and should be : 10, 20, 30, 40…

Neither floating point nor Glubyte works correctly.
Is there a way to obtain the EXACT pixel color which was set in the drawing procedure ??

This isn’t an answer to your question, sorry.
If you’re doing this just for fun that’s fine, but you realize you can juse use GL_SELECT on mouse move rather than rendering the scene in different shades of red.

Your method of selection is going to be slower because a fragment passes through the entire pipeline rather than just ending at ‘fragment ownership’ as is done using the GL_SELECT rendering method.

Originally posted by Aeluned:
…Your method of selection is going to be slower because a fragment passes through the entire pipeline rather than just ending at ‘fragment ownership’ as is done using the GL_SELECT rendering method.
Mmmh, I though GL selection was done in software ??? Not sure which one is faster. Can any body clarify this ?

Originally posted by adolar:
There is a difference of +/- 2 e.g.
red pixel result is : 8, 16, 25, 41… (for the first 4 objects)
and should be : 10, 20, 30, 40…

You’re probably rendering to a 16 bit color buffer, where you have only 5 bits of precision for the red color channel.

If you want full GLubyte precision, you must switch to a 32 bpp display mode.

First I used OpenGLs Picking and GL_SELECTION but my program is a kind of rotating chess board.
That´s why the Z order changes allways depending on the view angle.
If I use GL_SELECTION I must take care of the depth values which means I have to sort them with every rotation.
Using color values is much easier and it is still fast enough - chess players are patient…

Thanks to Zeckensack for his hint. But even in 32 bit mode the values are the same.
I guess that glPixelStorei(GL_UNPACK_ALIGNMENT,1) reserves 1 Byte for each color per Pixel.
Thus 8 Bit for the red component.

@ Zeckensack : trotzdem danke…

All you have to do is set your modelview matrix before doing the GL_SELECT render.
your vertices will end up at the proper z values.

you can read the z values back from the feedback buffer and select the nearest piece that way.

You shouldn’t have to worry about sorting anything.