projecting a ray from mouse position

Hello all
I’m sure this is been done already in some tutorial if so please let me know.

I’m trying to project a ray from the camera to wherever the mouse points to.

any pointers will be appreciated.

thank you…


what do you mean? An african or a european ray?

if you think about it, then the projection of a ray through the optical centre and the mouse’s coordinates is just the mouse coordinates. In other words, what you’re asking is actually a trivial problem. (Think about it… the projection of any point on the retina plane is just the coordinates of that point!! there IS no depth to change its 2d position).

do you mean, perhaps, constructing a ray through a point? or something??


What I ment was constructing a ray from the camera position to where the mouse points to, the problem is that I’m not sure how to calculate the vector in relation to the nearZ plane of the fustrum.

I tried to caculate the angle of projection like this:

projScale = (viewRight-viewLeft) / tan (hViewAngle );

so I can set a point on the nearZ plane to later be transformed by the camera forward vector.
p = (x,y,0) * projScale;
mouseVec = camera.forwardVec * p;

So I’m not trying to project a point along the forward vector, but rather through the mouse position.

How do I get (X,Y) coordinates normalized to the nearZ plane of the frustum?

use gluUnProject with winz=0, that gives you the mouse position on the near Clipping plane in GL coordinates

DaViper Thank you so much.
It works just fine!!