Solved

Detect Mouse over Object (OpenGL)

Posted on 2004-03-24
4
4,696 Views
Last Modified: 2013-12-26
Ok. How do I detect a mouse over an object? Please be easy...
0
Comment
Question by:rossryan
  • 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now