Jump to content
  • Advertisement
Sign in to follow this  
X Abstract X

Converting Screen to 3D World Coords

This topic is 3270 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to write a method to convert from screen coordinates to 3D world coordinates but I don't know where to even begin. I'm working in Java with JOGL and my 3D math knowledge is very minimal. I don't know if it makes a difference but, in my game I'm using a 3rd person camera with the ability to rotate 360 degrees about the y-axis and 90 degrees about the x-axis.

Share this post

Link to post
Share on other sites
You can use the gluUnProject function. Every point on the screen corresponds to a line in the 3D world, going from the camera in the direction of that pixel. When using gluUnProject you specify the screen coordinates as well as a Z-coordinate which is the distance to the 3D point you want.
If you have drawn your scene already, and want to find the 3D-coordinate of what is visible at a certain pixel on the screen, you can use glReadPixels with GL_DEPTH_COMPONENT to obtain the distance to pass to gluUnProject.
You also need the matrices to pass to gluUnProject, which must match the camera-matrices you have set up to transform your 3D world, as well as the viewport. These can be obtained using glGet (glGetDoublev and glGetIntegerv).

GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLint viewport[4];

glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
glGetIntegerv(GL_VIEWPORT, viewport);

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!