PDA

View Full Version : How can we project the 2D coordinates to the 3D coordinates?



Ehsan Kamrani
09-22-2005, 01:41 AM
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-

09-22-2005, 02:33 AM
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:

http://www.lighthouse3d.com/opengl/picking/

;)

memfr0b
09-22-2005, 02:36 AM
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.

RigidBody
09-22-2005, 03:05 AM
the depth value can be obtained with

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

Ehsan Kamrani
09-22-2005, 04:00 AM
Thanks :) ;)