As i know, OpenGL projects the 3D coordinates to the 2D coordinates. But in some games-As an example, war games-, how have the programmers project the 2D coordinates to the 3D coordinates? mouse clicks only return two values indicating the x and y coordinates. So how can we find the z coordinate to select the objects?Are there any useful functions to project the 2D coordinates to the 3D coordinates?

-Ehsan-

You can use the ‘gluUnproject’ command to retrieve the 3D coords for the position of the mouse.

There is a nice tutorial on picking and selecting objects here:

gluUnproject does this if you know the pixel’s depth value.

Another common approach is to build a ray from the camera’s position and the selected pixel, and check for intersections with the relevant objects. This approach doesn’t involve OpenGL though.

the depth value can be obtained with

glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, …);

Thanks