adolar
06-10-2004, 11:54 AM
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 ??
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 ??