tags:

views:

457

answers:

3

I want to find out which facet is under the mouse cursor in an OpenGL application. Using the selection buffer seems quite inaccurate to me. Are there other solutions?

+1  A: 

A common trick is to paint each facet in a different color (simply 0x000001 to n) render offscreen and get the color under the mouse point.

Martin Beckett
Be careful of dithering, blending, fog, lightning... basically anything that can remotely affect the colour of your polygons if you really want to use this technique. One option is drastically reducing number of colors and making them sufficiently different.
artificialidiot
+3  A: 

Do it manually, using ray intersection. Use your camera matrix (if not explicit, you can query it from OpenGL), use it to generate a picking ray in world space, and see whether it intersects your particular face or not. Sort intersections on depth (t), and you have the closest face under the cursor.

Depending on your data structure (hierarchy, etc) and size this could be either really slow or really efficient.

Paul-Jan
How this is different than using selection buffer of a very small size?
artificialidiot
Thanks. I think I'm going with this. It's more accurate than using selection buffer, can easily be ported to DirectX and although my scene is small enough to use the coloring technique using a picking ray seems like the only way if the scene is large (i.e. using a scene graph and/or BVH).
mdm
@artificalidiot - it's API-independent, and allows you to completely separate rendering and picking (which can be nice). However, if you use OpenGL constructs like point_size and line_width and want the picking to be visually correct, using the selection buffer can be more convenient.
Paul-Jan
A: 

Also take a look at this section of the OpenGL FAQ.

aib