Finding 2D point on screen from 3D perspective

Well, the title explains what I need to know :slight_smile:
So if I have a basic lwjgl/opengl 3D world with some entities which can be some NPC’s or other game stuff. And lets say I want to make floating names, test if object is seen by camera etc. I need to know exact 2D point coordinates when I have 3D point location data.

This is how I did 3D picking (needs to be “reversed”):
Please tell me if there’s a better way.


public static Vector3f getMousePosition(int mouseX, int mouseY) {
		viewport = BufferUtils.createIntBuffer(16);
		modelview = BufferUtils.createFloatBuffer(16);
		projection = BufferUtils.createFloatBuffer(16);
		winZ = BufferUtils.createFloatBuffer(1);
		position = BufferUtils.createFloatBuffer(3);
		GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelview);
		GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);
		GL11.glGetInteger(GL11.GL_VIEWPORT, viewport);
		GL11.glReadPixels(mouseX, mouseY, 1, 1, GL11.GL_DEPTH_COMPONENT,
				GL11.GL_FLOAT, winZ);

		if (winZ.get(0) == 1) {
			return null;
		}
		GLU.gluUnProject(mouseX, mouseY, winZ.get(), modelview, projection,
				viewport, position);
		
		return new Vector3f(position.get(0), position.get(1), position.get(2));
	}

So I was thinking…
By using this (found on stackoverflow):

screen_coordinates = projection_matrix * modelview_matrix * world_coordinates

Could I somehow get position this way? I need help :slight_smile:

There’s an infinite set of points that all map to a given screen position and they form a line. Taking the point on the near clipping plane and any ‘forward’ from that (say the far clipping plane) will give you two points on the line in the direction into the scene. Cast the ray thorough the scene to check stuff. There should be plenty of reference code available to do this.

For floating name tags:

  1. Calculate the world space position of the name tag from the object’s position.
  2. Manually do the matrix transformations on the CPU.
  3. You now have a (normalized) screen coordinate and a depth for the name tag.
  4. Draw text centered at those screen coordinates at the calculated depth with both the modelview and projection matrices set to identity (our coordinates are already transformed).