LWJGL: Getting mouse's 3D location

I have a scene set up in a 3D viewport. The game is 2.5D, so for the most part, the game will be composed of sprites.
Until I started using glRotated(0.15, 1, 0, 0); to rotate the entire view of the game, I had no problem getting the world location of the mouse. Now, obviously, the input methods I use for Mouse’s X and Mouse’s Y on the world need updating, or I need new methods.

So my question is, is there any method to getting the physical X, Y, and Z location of the mouse, taking in the input of the mouse’s X and Y? If OpenGL doesn’t support the mouse’s location with drawn objects or anything like that, could anyone help me with the math?

I gladly thank anyone who takes the time to read this. :slight_smile:


int x = Mouse.getX()

Update every frame

Thanks for the effort… but I’m asking for the Mouse’s X, Y, and Z coordinates in the 3D space, not the 2D window.

  1. The mouse is a 2D interface, not a 3D one.
  2. You can do by

int x = Mouse.getX()
int y = Mouse.getY()
// there is no z function

I think what he/she means is that a X/Y position from the mouse on the screen can be many positions in 3D space - are you looking to project it onto an object? If so, you want to look up generating rays into the screen based on your current projection matrix and collide those with the shapes within your scene.

Cheers,

Kev

Sorry for the misunderstanding

Exactly this. Hrmm. So I assume OpenGL Utilities don’t really support anythinwg like this, so I’ll have to create it myself.
I’m thinking about how I would create rays. Perhaps I should read up on it. It doesn’t seem like the easiest task, especially taking into account Field of View, and rotation at 15 Degrees.

Typically you simply read the value of the depth buffer at the 2D coordinate of your wish.

That depth value is then unprojected through the projection view, and you end up with 3D coordinates in modelview space.


	static IntBuffer viewport = BufferUtils.createIntBuffer(16);
	static FloatBuffer modelview = BufferUtils.createFloatBuffer(16);
	static FloatBuffer projection = BufferUtils.createFloatBuffer(16);
	static FloatBuffer winZ = BufferUtils.createFloatBuffer(20);
	static FloatBuffer position = BufferUtils.createFloatBuffer(3);

	static public Vector3f getMousePositionIn3dCoords(int mouseX, int mouseY)
	{

		viewport.clear();
		modelview.clear();
		projection.clear();
		winZ.clear();;
		position.clear();
		float winX, winY;


		GL11.glGetFloat( GL11.GL_MODELVIEW_MATRIX, modelview );
		GL11.glGetFloat( GL11.GL_PROJECTION_MATRIX, projection );
		GL11.glGetInteger( GL11.GL_VIEWPORT, viewport );

		winX = (float)mouseX;
		winY = /* (float)viewport.get(3) -  */  //Uncomment this if you invert Y
			(float)mouseY;

		GL11.glReadPixels(mouseX, (int)winY, 1, 1, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, winZ);

		float zz = winZ.get();

		GLU.gluUnProject(winX, winY, zz, modelview, projection, viewport, position);



		Vector3f v = new Vector3f (position.get(0),position.get(1),position.get(2));


		return v ;
	}



I won’ try to explain what is happening, since I myself forgot exactly how all this stuff works after writing it. In short is what Riven said.

Note that you have to use this method after your 3d rendering is done, otherwise the depth buffer will be empty and will not yield the result you’re expecting.

The best way is to do a ray intersection test against the world. That way you can avoid doing an expensive readback from the GPU.