0
101 Dec 27, 2004 at 12:16

Hi, in my little program I’m trying to transform the mouse position into a ray, and then find the ray’s intersection with a plane at 0,0,0 (normal 0, 1, 0). And of course, it doesn’t work…

I’ve tried using gluUnproject, but the position it gave always seemed varying degrees off. After that, I tried doing it manually like so:

(Unoptimised, that’s for once it works :P)

    GLint viewport[4];
glGetIntegerv (GL_VIEWPORT, viewport);

/* Some constants */
float near = 0.1, far = 4000, ratio = (float)viewport[2] / (float)viewport[3];

InputMouseInfo i ( InputSystem::GetS().GetMouseInfo() );

float tangent ( tanf(fov * 0.5) );

/* Normalised mouse coordinates in view space */
float dx ( (2 * (float)i.x / (float)viewport[2] - 1) / ratio );
float dy ( 1 - 2 * (float)i.y / (float)viewport[3] );

vector3 n (dx * tangent * near, dy * tangent * near, near);
vector3 f (dx * tangent * far, dy * tangent * far, far);

/* Get the modelview matrix, convert it to matrix44 and find inverse */
GLdouble glModMtx[16];
glGetDoublev (GL_MODELVIEW_MATRIX, glModMtx);
matrix44 modMtx (vector4 ((float)glModMtx[0], (float)glModMtx[1], (float)glModMtx[2], (float)glModMtx[3]),
vector4 ((float)glModMtx[4], (float)glModMtx[5], (float)glModMtx[6], (float)glModMtx[7]),
vector4 ((float)glModMtx[8], (float)glModMtx[9], (float)glModMtx[10], (float)glModMtx[11]),
vector4 ((float)glModMtx[12], (float)glModMtx[13], (float)glModMtx[14], (float)glModMtx[15]));

matrix44 invModMtx ( InvertMatrix44 (modMtx) );

n = invModMtx * n;
f = invModMtx * f;

/* Find point when y = 0 */
vector3 dir ((n - f).normalize());
float units (n.y / -dir.y);
vector3 strike (n + dir * units);


Can anyone give me some hints/tips as to where I’m going wrong?
Thanks
Ed Mack

#### 4 Replies

0
101 Dec 30, 2004 at 00:48

Can anyone point me in the direction of any sort of openGL mouse->ray implimentation? It’d be greatly appreciated. (I’m beginning to think this should have been in the Graphics forum, move it if you feel so)

0
101 Dec 30, 2004 at 02:49

sure:

0
101 Dec 30, 2004 at 10:12

Thank you! Now why didn’t google find that :)

0
101 Jan 04, 2005 at 17:41

i use gluUnproject and it works fine. note that you should call it twice: once with z=0 and once with z=1. this gives you all the components to create your ray.