Solved

Detect Mouse over Object (OpenGL)

Posted on 2004-03-24
4
4,751 Views
Last Modified: 2013-12-26
Ok. How do I detect a mouse over an object? Please be easy...
0
Comment
Question by:rossryan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:trylle
ID: 10675929
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).
0
 

Author Comment

by:rossryan
ID: 10693534
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?
0
 
LVL 1

Expert Comment

by:Tuomas
ID: 10693917
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
0
 
LVL 1

Accepted Solution

by:
trylle earned 500 total points
ID: 10700786
Just call the select buffer code with cursor coordinates (from e.g. GetCursorPos) after (or before, for that matter) your rendercode instead of only when you receive a mouse click event.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is RenderMan: RenderMan is a not any particular piece of software. RenderMan is an industry standard, defining set of rules that any rendering software should use, to be RenderMan-compliant. Pixar's RenderMan is a flagship implementation of …
Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question