Detect what the user clicks on

I am drawing a bunch of spheres with:


void DrawPoint(Point P, double rad, int Color)
{
	glPushMatrix();
	        glColor3f(1,1,1); //white
		glTranslatef(P.getX(), P.getY(), P.getZ());
		glutSolidSphere(rad, 20, 20);
	glPopMatrix();
}

I am storing these in a display list.

Once the spheres are displayed and i am rotating and zooming and all that good stuff (in the glut main loop), I would like the user to click on one of them and report which one he clicked on. Is this possible?

Thanks!

David

Yes, but you need to use the selection mechanism, and you will have to change the hit target name between spheres.

You will get a list of hits in the buffer which you will have to determine which is closest.

Colors and readback may be simpler.

http://www.opengl.org/resources/faq/technical/selection.htm

A better solution would be to to render each sphere with another color. And read the pixel with glReadPixel from the backbuffer.

Thanks for the replies guys. I found a super easy way to do it though. There is a function set called ZPR (Zoom Pan Rotate - http://www.nigels.com/glt/gltzpr/)..)

  1. it is great for what I am doing (plotting stuff that i want to look at, nothing like gaming haha)

  2. all you have to do to select an object in a display list is:


int main()
{
//...other setup stuff...
zprSelectionFunc(SelecFunc);
zprPickFunc(pick);
//...more stuff...
}


void pick(GLint name)
{
   printf("Pick: %d
", name);
   fflush(stdout);
}


void SelecFunc()
{
	DrawSpheres();
	glCallList(TestList);
}

This should be global


GLuint TestList;


void DrawSpheres()
{
	TestList = glGenLists(1);
	glNewList(TestList,GL_COMPILE);
	   DrawSphere(0,0,0,1,0,0,5);
	   DrawSphere(1,0,0,0,1,0,6);
	   DrawSphere(0,1,0,0,0,1,7);
   glEndList();

}


void DrawSphere(double x, double y, double z, double r, double g, double b, GLuint nam)
{
    glPushMatrix();  
		glPushName(nam);
         glColor3f(r,g,b);
         glTranslatef(x,y,z);
		 glutSolidSphere(1, 20, 20);
      glPopName();
    glPopMatrix();
}

Hope this helps others! By the way, why is something to the effect of ZPR not built right into glut?? It seems to be SUPER useful!

-Dave

Well, that ZPR is just a wrapper for the selection mode. See dorbies reply.

Please note that this feature is very useful for small scenes but can get very slow for bigger ones.

CatDog