Link to home
Start Free TrialLog in
Avatar of rossryan
rossryan

asked on

Detect Mouse over Object (OpenGL)

Ok. How do I detect a mouse over an object? Please be easy...
Avatar of trylle
trylle

You can do this by casting a ray from where the mouse coordinates on the screen and project it into your scene, usually with the gluUnProject function (then checking if it intersects any of the objects bounding boxes, spheres, etc., then searching through the triangles, etc.).

OR you can use the OpenGL selection buffer which is much easier to use.

The basic idea with the OpenGL selection buffer is that you do your rendering as you usually do it. Then you have a second cycle (in your case this would happen every frame) where you initialize a select buffer, set the GL_SELECT render mode, set up view transformations and a pick matrix, and draw your objects again. When you go back to the GL_RENDER render mode, the hits are returned in the select buffer. You can get multiple hits, because there may be objects stacked on top of each other in the view.

There is an example and more in-depth explanation of it here (http://www.helsinki.fi/~tksuoran/selection.html).
Avatar of rossryan

ASKER

Hmm. Have a select buffer, thought that you needed to click on an object for that to work...

So you're saying that if it (the mouse cursor) floats (not clicks) over an object, that will show up in the select buffer?
Here is a link to a tutorial, which shows how to select an object with left mousebutton:

http://www.ultimategameprogramming.com/zips/Gl_GlutSelection.ZIP

Here are other simple tutorials from the same page:

http://www.ultimategameprogramming.com/Tutorial.php?category=OpenGL&page=1
ASKER CERTIFIED SOLUTION
Avatar of trylle
trylle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial