Skip to main content
GameDev.net gamedev.net
🔒 Locked

How do you convert the mouse 2d screen coordinates to 3d coordinates?

Started by carmellose Jan 23, 2008 at 5:25 AM 2 replies 9.2k views
Original Post
carmellose
carmellose
hello, i would like to know how to convert the mouse 2d screen coordinates to 3d coordinates ... without using GLuUnprojet(). I know the z coordinate canot be known since it is lost in 2D. However, what i'm trying to do is to backprojet the 2D point and get its z value from the intersection with a 3d sphere. what i did is what i'd thought it would be ok to do, but i'm not sure. Could you math/GL gurus tell me if it is ok ? many thanks : 1 - i have a 2 mouse point, say, x_mouse = (x,y). 2 - i convert the point to 3D by setting X_mouse = (x,y,z,1), with an arbitrary z. 3 - i "backproject" the point using the inverse perspective projection matrix (defined here at the bottom http://glprogramming.com/red/appendixf.html) 4- i get a point which is X_mouse_3D = (a,b,c,d,e), which i normalized by thelast coordinate : X_mouse_3D_normalized = (a/e,b/e,c/e,d/e,1) 5 - i get the 'real' z coordinate of X_mouse_3D_normalized by replacing the coordinates of X_mouse_3D_normalized in the equation of a sphere i know is it clear ...? is this the way i should do it ? thanks in advance ;)
Brother Bob
Brother Bob
Looks like you're missing a few steps. The procedure is the following.

  1. Expand to 3D homogenous coordinate. That is make a vector (x, y, z, 1). See below for Z-value.

  2. Map the X- and Y-value into normalized device coordinates, that is, from [0, viewport size] to the range [-1, 1].

  3. Multiply by the inverse of the projection matrix.

  4. Multiply by the inverse of the modelview matrix.

  5. Perform perspective division.

  6. The result after perspective division is world space coordinates. Skip the multiplication by the modelview matrix and you get the result in eye space instead.


The Z-value is necessary for mapping one 3D-space to another. Choose -1 for a point on the near clip plane and 1 for a value on the far clip plane. By unprojecting a point on both near and far clip planes, you can connect them by a line if you don't have a particular depth for unprojection.

I should also point out that, regarding Z, a value of 0 does not necessarily mean halfway between the clip planes. For perspective division, the values of Z are highly non-uniformly distributed between the clip planes.
SiCrane
SiCrane
You can also try grabbing the depth value from the depth buffer using the mouse coordinates.
carmellose
carmellose
ok, thanks very much for the explanations !

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.