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

Screen to 3D World Coords (again)

This topic is 3174 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 can't seem to figure out how to convert from screen coordinates to 3D world coordinates. I'm working in JOGL and this is my code currently. It simply does not work. I would really appreciate if someone could help.
public Vector screenToWorld(int screenX, int screenY) {
		double world[] = new double[3]; //store world coordinates here
		int viewport[] = new int[4];
		double modelview[] = new double[16];
		double projection[] = new double[16];
		
		gl.glGetIntegerv(GL.GL_VIEWPORT, viewport, 0);
		gl.glGetDoublev(GL.GL_MODELVIEW_MATRIX, modelview, 0);
		gl.glGetDoublev(GL.GL_PROJECTION_MATRIX, projection, 0);
		
		glu.gluUnProject(screenX, screenY, gl.GL_DEPTH_COMPONENT, modelview, 0, projection, 0, viewport, 0, world, 0);
		
		return(new Vector((float)world[0], (float)world[1], (float)world[2]));
}

Share this post


Link to post
Share on other sites
Advertisement
You are missing a step. GL_DEPTH_COMPONENT does not mean the depth itself, but is a flag for glReadPixels to obtain the depth, which can be different at every pixel.
You should be able to get the depth as follows:

float[] depth = new float[1];
gl.glReadPixels(screenX, screenY, 1, 1, gl.GL_DEPTH_COMPONENT, GL_FLOAT, depth);


Then you pass depth[0] as the z-component to gluUnProject.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!