PDA

View Full Version : Simple Scene Picking



David Doria
11-09-2008, 08:12 AM
I've seen code (namely: http://jerome.jouvie.free.fr/OpenGl/Tutorials/Tutorial27.php) that has zillions (ok, 50) lines for picking objects.

The main part seems to be saving the projection matrix, setting the pick matrix to a very small window, and then resetting the projection matrix.

If I have a very simply scene - maybe 10 objects - this all seems unnecessary. I thought I could just pick with the same view that I am displaying on the screen. I tried to do a very much simpler version:




void Pick(GLdouble x, GLdouble y)
{
GLuint buffer[1024];
const int bufferSize = sizeof(buffer)/sizeof(GLuint);

GLint hits;

GLint min = -1;
GLuint minZ = -1;

glSelectBuffer(bufferSize,buffer);
glRenderMode(GL_SELECT);
glInitNames();
display();
hits = glRenderMode(GL_RENDER);

/* Determine the nearest hit */

int i=0, j=0;
if (hits)
{
for (i=0, j=0; i<hits; i++)
{
if (buffer[j+1]<minZ)
{
/* If name stack is empty, return -1 */
/* If name stack is not empty, return top-most name */

if (buffer[j]==0)
min = -1;
else
min = buffer[j+2+buffer[j]];

minZ = buffer[j+1];
}

j += buffer[j] + 3;
}
}

cout << "Name: " << min << endl;

}


but it doesn't work - the picked values are all wrong.

Is there any reason this wouldn't work?

Dave