How to use OpenGL Utilities (Where do I find gluUnProject()) ?

I need to work out the world coordinates given screen coordinates.


All the advice out there is to use gluUnProject - the recipe is like this:


          #include <GL/gl.h>
          #include  <GL/glu.h>
          #include <GL/glut.h>

            glGetIntegerv (GL_VIEWPORT, viewport);
            glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
            glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
/  note viewport[3] is height of window in pixels  */
            realy = viewport[3] - (GLint) y - 1;
            printf ("Coordinates at cursor are (%4d, %4d)\n",
               x, realy);
            gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
               mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
            printf ("World coords at z=0.0 are (%f, %f, %f)\n",
               wx, wy, wz);
            gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0,
               mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
            printf ("World coords at z=1.0 are (%f, %f, %f)\n",   
               wx, wy, wz);


However - I can't work out where to find (for linking) and how to locate (for #include) glu.h and in particular gluUnProject.


How should this be done?

Accepted Answer

I don't have a definite answer to your question, but as this forum is pretty low-traffic, I'll go ahead and reply.


I don't know if GLU is available on whatever platform you're targeting here, but I wouldn't be surprised if it wasn't. Wikipedia says GLU isn't implemented in OpenGL ES, and I think GLU is somewhat antiquated in general.


In any case, GLKit might have what you need. On this page:


https://developer.apple.com/reference/glkit/1664423-glkit_math_utilities


Is an 'unproject' function. I didn't compare it to the GLU function, but it seems likely (or at least possible) that it performs the same function. Of course you could also implement the functionality yourself, although that would require digging into the math a bit.


Also, what platform/OS are you targeting, and what version of OpenGL are you using? (I know that's just example code you posted, but it looks like it's using outdated OpenGL features.)

Thanks so much!


You've given me the first clue I've been able to find, and the glkit function has the right signture at least 🙂


I'm targetting an iPad. You're right - the code is outdated because it comes from an old answer about how to get world coords from screen coords. Hopefully the princple still apples 😮

How to use OpenGL Utilities (Where do I find gluUnProject()) ?
 
 
Q