PDA

View Full Version : 3D Picking problem....



04-09-2003, 11:20 AM
I am using the following code:

int
picking(void (*f)(), float x, float y)
{
GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);

// designate buffer for hits
GLuint nameBuffer[BUFSIZE];
glSelectBuffer(BUFSIZE, nameBuffer);
glRenderMode(GL_SELECT);

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

glGetIntegerv(GL_VIEWPORT,viewport);
gluPickMatrix(x,viewport[3]-y,5,5,viewport);
gluPerspective (45., 1., 1., 200.);
glMatrixMode(GL_MODELVIEW);

// draw what we will be picking
(*f)();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();

// restoring the original projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();

// returning to normal rendering mode
unsigned int hits = glRenderMode(GL_RENDER);

cout << hits << endl;

// if there are hits process them
if (hits != 0)
{
GLuint names, *ptr, minZ,*ptrNames, numberOfNames;

ptr = (GLuint *) nameBuffer;
minZ = 0xffffffff;
for (unsigned int i = 0; i < hits; i++)
{
names = *ptr;
ptr++;
if (*ptr < minZ)
{
numberOfNames = names;
minZ = *ptr;
ptrNames = ptr+2;
}
ptr += names+2;
}

return *ptrNames;
}
return -1;
}

and pass it this function:

static void
drawPickable()
{
if (objectCount > 0)
{
// clear the name stack
glInitNames();

for (int i = 0; i < objectCount; i++)
{
if (dWasher->placed(i))
{
glPushName(dWasher->placedGetId(i));
dWasher->displayOn(i);
glPopName();
}

// it may have been deleted, so check
else if (p[i] != NULL)
{
glPushName(i);
p[i]->display();
glPopName();
}
}
}
}

I use the drawPickable() function in my display routine as well, and all objects show up like they are supposed to. The problem however, is when I try to pick objects, I only can pick objects if they are in the 'else if' case using p[i]->display which displays all the objects defined in a vector in my main file. The amusing thing is that the call dWasher->displayOn(i) calls the p[i]->display on another vector (defined in exactly the same way) in the scope of my header file. My question is, why is it that if I can see it, I cannot pick it?