View Full Version : Catch if mouse click on some object
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.
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.
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:
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)
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);
hits = glRenderMode(GL_RENDER);
if (hits > 0)
choose = buffer;
depth = buffer;
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];
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.
11-11-2002, 08:46 AM
Thank you very much, but I have one more question about it, what kind of variable mv is ?
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...
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).]
Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.