Mouse -> Ray

065f0635a4c94d685583c20132a4559d
0
Ed_Mack 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];
    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

4 Replies

Please log in or register to post a reply.

065f0635a4c94d685583c20132a4559d
0
Ed_Mack 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)

22b3033832c5c699c856814b0cf80cb1
0
bladder 101 Dec 30, 2004 at 02:49

sure:

clicky

065f0635a4c94d685583c20132a4559d
0
Ed_Mack 101 Dec 30, 2004 at 10:12

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

F4b1973b39579ee46019b272a5ae9dd4
0
jebus 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.