PDA

View Full Version : Detecting mouse position on object



overTaker
03-13-2014, 03:56 AM
I'm trying to implement detection of mouse position on a model - for clicking or highlighting models, when mouse is over them, with possibility of reading the world coordinates of cursor. I've got it set up and working using color picking, but I found out that glReadPixels function is terribly slow, even when reading one pixel with no format conversions.
Is there any way, that could be used when mouse moves around the scene, and would not slow the application down?

Steven Katic
03-17-2014, 01:54 AM
This is a common problem that occurs as the number of objects in the scene increases. Not so much because glReadpixels becomes the bottleneck, but because the searching technique used to find the object that was hit with the mouse can become a major bottleneck.

RE: glReadPixels: this tends to get blamed (unfairly I think) as being the bottle neck when picking performance degrades.As the number of objects and the number of vertices that make up each object increases, performance in general can tend to degrade(naturally).

Metrics: You can test this at a subjective level(usablity of your app: which you have done already, hence you post) or you could use the hard numbers of timing processes to find the specific bottleneck(s)(Or learn how to do it) if you prefer.Timing your processes may give a more accurate indication of the causes of performance degradation and help you avoid making faulty assumptions. Color picking with glReadPixels is generally ok for upto a few dozen of low/meduim poly objects.But it very much depends.

So, there are lots of options. It's a matter of doing some research to find those options, thier pros and cons, experimenting with some of the options attractive to you, then choosing to use your prefered option.
A common alternative to color picking and other picking techniques utilizing glReadPixels: is use of scene intersection queries(casting rays). AABB and BSP are two strategies and thier hybrid variations are what you may look into. They primarily rely on the concept of casting rays into the 3d scene in order to more rapidly identify the hit object. The more time critical your app is (i.e relies on interactive response time like in an FPS with lots of dynamic objects ) the more likely you may need to look at these. If time is not so critical you could get away with other search techniques (e.g. 3d modelling software: use of hashtables, binary search trees, and/or user selectable subobject hierachies on which to search like: Models mode, submodel meshes mode, submesh triangles/points mode etc)'

So if we assume your color picking design has no serious flaws & works well for a few dozen objects, you may have to start looking at some of these more "serious" techniques often required for handing tons of data an/or meeting highly interactive requirements. So some terms to search about are: AABB, BSP(binary space partioning), ray casting(alternative to glReadPixels ), picking, even ray casting on gpu((an advanced alternative that has been a topic of research), Collision detection in 2D/3D games (is a generally related area).

If you have (slooowww) performance problems with just a few objects that are low poly, there may be a problem with your design.
If you expect to immediatedly pick objects as your mouse simply hovers over them, as opposed to just picking them with a mouse click(then searching which one was hit), it may further exacerbate interactive performance problems.

Have you tried hit testing and searching for hit objects only when you left mouse click on an object. Is it still slow? how many objects in the scene are we talking about?