PDA

View Full Version : Mouse -> Ray


Ed Mack
12-27-2004, 05:16 AM
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];
float fov = DegToRad(45);

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

Ed Mack
12-29-2004, 05:48 PM
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)

bladder
12-29-2004, 07:49 PM
sure:

clicky (http://www.mt-wudan.com/wu_tut.php?t=gl_mouseray)

Ed Mack
12-30-2004, 03:12 AM
Thank you! Now why didn't google find that :)

jebus
01-04-2005, 10:41 AM
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.