#### Archived

This topic is now archived and is closed to further replies.

# gluUnProject returns viewport co-ordinates instead of world

This topic is 5376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to determine the 3D world coordinates of a point that is selected by the mouse. I am using gluUnProject and was going to get the equation of the line and test for intersection. However whenever I use gluUnProject it just returns x and y values of the mouse position in 2D viewport coordinates (i.e. 512, 384 as centre of screen in 1024, 768 mode). I am using the following procedure when the mouse is clicked:
void MouseClick (GLdouble x, GLdouble y, GLdouble z, GLdouble &wx, GLdouble &wy, GLdouble &wz)
{

GLint viewport[4]; //array to hold viewport

GLdouble mvmatrix[16],projmatrix[16];
GLfloat depth;

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);

gluUnProject(x, y, z, mvmatrix, projmatrix, viewport, &wx, &wy, &wz);

}

As my cursor is always centred (crosshair stays central) I am entering the x and y values of the mouse coordinates as the centre of the screen. I have tried altering the value of the depth between 0 and 1 and they all give the same results. I dont know if it is something to do with my matrices as I swap between Ortho and projection to display text. Any help would be greatly appreciated. Thanks, Richard

##### Share on other sites
This always happens, I try for ages, eventually decide I need help, post a message and within 5 minutes solve it myself.

I was calling the procedure from the WM_LBUTTONDOWN and it wasn''t using the transformation matrices and rotation matrices I had applied to my world. When I called it from my display routine after the transformations it worked fine.

Now to link it to a mouse press.