Jump to content
• Advertisement

Public Group

# Converting Screen to 3D World Coords

This topic is 3352 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

##### Share on other sites
Advertisement
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).
Example:
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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
17
3. 3
4. 4
5. 5
• Advertisement

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633735
• Total Posts
3013594
×

## Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!