View Full Version : How can we project the 2D coordinates to the 3D coordinates?
09-22-2005, 02: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?
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:
09-22-2005, 03: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.
09-22-2005, 04:05 AM
the depth value can be obtained with
glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, ...);
09-22-2005, 05:00 AM
Thanks :) ;)
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.