Problem with "Picking"

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

{
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 ("

");
}

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

I’m sorry, I forget to explain:

the first function is called PaintIso(), and It’s called inside the second function, and inside the DisplayFunction.

Thank you again
Remedios