Catch if mouse click on some object

I would like to catch when mouse click on some object in scene, does somebody know how to do it - I would like to get name of the object. I tried tutorial on nehe.gametutorials.net but it doesn’t work.

You need to use the selection mode of OpenGL. I don’t know how to program it (never used), sorry.

I had trouble with this recently but then recalled the tutorial at nehe.gamedev.net once I incorporated the code into my program it worked like a charm.

First thing to remember is to include something like the following for each object you wish to track clicking for before drawing the object. The command is only used when selection picking is switched on:

glLoadName(ObjID);

Now, in your mouseclick routine call something as follows to execute the selection routine and return the object id clicked on:

ObjID = FindObjectClickedOn(x,y)

Then finally the code itself to click on the object which I almost practically took line by line from the nehe tutorial and reworked to fit my specific coding. But here is the code I used anyway but it is probably best of you start with nehe’s code.

int FindTileClickedOn(GLfloat x,GLfloat y)
{
GLuint buffer[512]={0};
GLint viewport[4]={0};
GLint choose;
GLint hits;
GLint depth;

glGetIntegerv(GL_VIEWPORT,viewport);
glSelectBuffer(512,buffer);
(void) glRenderMode(GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix((GLdouble)x,(GLdouble) (viewport[3]-y),0.1f,0.1f,viewport);
if (mv.OP == ‘O’) gluOrtho2D(mv.s.x0,mv.s.w,mv.s.y0,mv.s.h);
if (mv.OP == ‘P’) gluPerspective(mv.angle,mv.s.w/mv.s.h,mv.vnear,mv.vfar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
DrawScene();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
hits = glRenderMode(GL_RENDER);
if (hits > 0)
{
choose = buffer[3];
depth = buffer[1];
for (int loop=1;loop<hits;loop++)
{
//Check next cycle of buffer blocks for a better match.
if ( buffer[loop4+1] < GLuint (depth) )
{
choose = buffer[loop
4+3];
depth = buffer[loop*4+1];
}
}
return choose;
}
else
{
return -1;
}
}

Changing the 0.1f’s in the gluPickMatrix command limits the area around the mousepointer that is checked.

Hope that helps you in some way.

Tina

Thank you very much, but I have one more question about it, what kind of variable mv is ?

Ah, sorry, mv is a variable I use to signify my viewport structure which holds the various view settings…

If you look up the commands for glOrtho and gluPerspective you should see what values they need to be…

Tina

OK, but I need to declare this variable, otherwise I cann’t compile the project. Or caqn you just send me declaration of mv ?

[This message has been edited by jirkamelich (edited 11-14-2002).]