PDA

View Full Version : Catch if mouse click on some object



jirkamelich
11-10-2002, 09:11 AM
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.

mm_freak
11-10-2002, 11:08 AM
You need to use the selection mode of OpenGL. I don't know how to program it (never used), sorry.

tinak
11-10-2002, 01:00 PM
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[loop*4+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

jirkamelich
11-11-2002, 08:46 AM
Thank you very much, but I have one more question about it, what kind of variable mv is ?

tinak
11-11-2002, 03:31 PM
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

jirkamelich
11-12-2002, 07:22 AM
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).]