PDA

View Full Version : Picking big problem!!!



remedios79
12-18-2002, 01:49 PM
Hi!

I have a big problem. I'm trying to pick a sphere in a 3D environment.

In my scene I have a wing, some other stuff (an isosurface) and my sphere.

This is my code

My display function call this function

PaintIso(GLenum mode)
{
setupMatrices();
iso->setCamera(from,at,up);
iso->setOrientation(P, V*R);
iso->Render();
if (SNmode == NAVIGATION_MODE)
glColor4f(RedS, GreenS, BlueS, .5);
else
glColor4f(0., 1., 0., .5);
if (mode == GL_SELECT)
glPushName(1);
glutSolidSphere(SphereRay, 10, 10);
}


This is the function that get the picked objects.
I call this one inside my mouseFunction

void PickObject(int x, int y);
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];

glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(BUFSIZE, selectBuf);
(void) glRenderMode(GL_SELECT);
glInitNames();

glMatrixMode(GL_PROJECTION);
glPushMatrix();
gluPickMatrix((GLdouble) x, (GLdouble) (viewport[3] - y), 25., 25., viewport);

PaintIso(GL_SELECT);

glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glFlush();

hits = glRenderMode(GL_RENDER);
if (hits != 0)
{
GLint i, j, numberOfNames;
GLuint names, *ptr, minZ,*ptrNames;

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

ptr += names+2;
}
if (numberOfNames > 0) {
printf ("You picked the sphere ");
if (SNmode == NAVIGATION_MODE)
SNmode = SPHERE_MODE;
else
SNmode = NAVIGATION_MODE;
ptr = ptrNames;
for (j = 0; j < numberOfNames; j++,ptr++) {
printf ("%d ", *ptr);
}
}
else
printf("You didn't pick the sphere!");
printf ("\n");
}

glutPostRedisplay();
}

It seems very easy to use, but of course, it doen't work.

If I push a name for the wing I obtain that clicking everywhere in the viewport I'm selecting the wing, even if it's not in the clipping area!!!

I know there's something pretty wrong, but I don't know what...

Please, help me!

Thank you very much,
Remedios

stef007g
12-19-2002, 08:17 AM
May be you can try this:
call glPushName(0) just after glInitNames() and use glLoadName(1) instead of glPushName(1) in PaintIso(GLenum mode).
That's the way I'm doing picking (http://nehe.gamedev.net/) and it's work.
Hope this help,
Stef.

remedios79
12-19-2002, 04:01 PM
Thank you for the advice, now I'm using glPushName, and I figured out that my viewport was redimensionated automatically (because I'm using GLUI) and now it works, thanks!