Recognize that additional information is required to βnot designβ two-dimensional mouse coordinates in the 3D world. The two-dimensional position on the screen corresponds to an infinite number of points along the line, from the near plane to the far plane in 3D. This means that to get a 3D point, you also need to specify a depth value.
The gluUnProject function is a convenient way to do this and provides a parameter for this winZ . 0.0 will find a 3D point in the near plane, and 1.0 will find it in the background.
gluUnProject(winX, winY, winZ, model, proj, view, objX, objY, objZ);
Note: If gluUnProject not available, you can easily find the same with matrices only)
When a mouse is used to interact with 3D objects, this depth value is usually determined by fetching the depth from the buffer or intersecting the beam with the geometry of the scene or primitive (for example, a sphere or rectangle). If the goal is to interact with points, you can use a sphere around each point. If you just want the 3D point to be somewhere out there, you could determine the depth value (maybe 0.0 on the near plane, or 0.5 halfway between the near and far plane).
source share