View Full Version : gluUnProject

05-24-2002, 06:14 AM

I have a couple questions conserning gluUnProject().

1. What the heck is the winZ coordinate? And how do I get it?
2. Where should I grap the modelview and projection matrices?
3. Where can I find tutorials on how to deal with gluUnProject()?

Thanks for your help,


05-24-2002, 06:32 AM

The Z co-ordinate usually comes from a picking render or some kind of hit test. In order to correctly unproject a point, you need to know its distance in the scene.

As for getting model and projection matrices, see below. The method below takes mouse co-ordinates (from windows) and unprojects them. The `vertex' parameter contains the x, y mouse co-ordinates and the z co-ordinate given by the picking render.

// Unproject the given mouse point back into the scene.

void CSelectionBuffer :: UnProjectMouse ( CVertex3D& Point, CVertex3D& Out )

// Finally, we need to reverse transform the values we have back into the world to get correct world x, y, z.

GLdouble Projection [16], Model [16];
GLint Viewport [4];

// Now get the matrices and viewport.

glGetDoublev ( GL_PROJECTION_MATRIX, Projection );
glGetDoublev ( GL_MODELVIEW_MATRIX, Model );
glGetIntegerv( GL_VIEWPORT, Viewport );

// We will need to invert mouse y (because its top left whereas our window is bottom left origin).

double Invert_m_y = static_cast<double> ( Viewport [3] ) - static_cast<double> ( Point.v [1] );


// Inverse transform the given point - note its important that the viewing transforms are setup as they were rendered.

gluUnProject ( static_cast<double> ( Point.v [0] ), // Mouse x.
Invert_m_y, // Inverted mouse y.
0.0, // The z depth result we got from selection.
Model, // Model view matrix.
Projection, // Projection matrix.
Viewport, // Viewport co-ordinates.
&h1.x, // Final result x.
&h1.y, // Final result y.
&h1.z ); // Final result z.

// Copy results across ( from double to float ).

Out.v [0] = static_cast<float> ( h1.x );
Out.v [1] = static_cast<float> ( h1.y );
Out.v [2] = static_cast<float> ( h1.z );

[This message has been edited by Robbo (edited 05-24-2002).]

05-24-2002, 07:00 AM

it works, at least somehow. If I rotate my model so it is rotated 90 the coordinates I get back are everything but not right!


05-24-2002, 07:20 AM
Well, some source-code would be nice - we need to see how you get your z and what your screen x, y are. Also, how you unproject. Don't forget that your transform (model) matrix must be the same when you unproject as it was when you rendered the scene in the first place. ie:

glPushMatrix ()
....ApplyMyCameraTransform ()
....DrawMyStuff ()
glPopMatrix ()

.. then, before you unproject,

ApplyMyCameraTransform ()
....Do My unprojection


05-24-2002, 07:28 AM
Hm, sorry about my panic, but it was my confused mind which made me go berserk and mess up my code.
It works now, thanks for your help!!