Selecting Objects?

Started by
3 comments, last by Zakwayda 18 years, 7 months ago
I am working on an interactive chess board and am hung up on how to select the different pieces. Does anyone know how I should go about doing this. I haven't been able to find anything even telling me where to start. Any help would be much appreciated.
Advertisement
You might want to start with gluUnproject().
Typically, you have a representation of your scene that's not just graphical, but physical. You then project a ray through space in the direction the user clicked, and see what object the ray intersects.

If you want to do it with OpenGL, you can use GL picking (ick!) or render the scene with each object being a different color (turn off lighting and texturing), and read back the color that the clicked pixel has.
enum Bool { True, False, FileNotFound };
its called picking...

google for it

see http://www.lighthouse3d.com/opengl/picking/ (random example from google)

Just to be a little more specific, there are a couple of ways to do picking in OpenGL. The above link discusses picking in OpenGL selection mode. With this method you render your scene twice, once for display and once for picking. Before rendering for picking, you set up a special pick matrix which restricts your view frustum to a small volume that more or less corresponds to a ray through the mouse position. OpenGL can then be set up to inform you whenever a rendered object intersects this frustum; such objects can be considered picked.

Another method is geometrical, and as suggested earlier can be implemented using gluUnProject(). The standard method is to unproject two points. Each is constructed from the mouse x and y (adjusted to be in window coordinates); the z values are chosen to correspond with the depth range (usually [0,1]). These two points form a line segment in world space that can be used to pick objects. As OpenGL provides no collision detection support, determining which objects the segment (ray) intersects is up to you.

This topic is closed to new replies.

Advertisement