# Geting wrong coordinates with gluUnProject

## Recommended Posts

Hello!!! I have a problem with gluUnProject function. The code:
GLint viewport[4];
GLdouble projection[16];
GLdouble modelview[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY,posZ,posZ1,posZ2;

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetDoublev(GL_MODELVIEW_MATRIX, modelview);

POINT mouse;
GetCursorPos(&mouse);
ScreenToClient(hwnd, &mouse);

winX = (float)mouse.x;
winY = (float)mouse.y;
winY = (float)viewport[3] - winY;

glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
gluUnProject(winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

The problem is, that the coordinates of axes that are on the plane of monitor (for example X and Y) are good, but the coordinate of perpendicular axis (f.e Z)is not 0 but fRange or -fRange. fRange in my code, is the clipping plane from glOrtho. When I Rotate the view for example Y rotation -90 or 90 degrees, the coordinate of perpendicular to monitor plane axis X is fRange/-fRange not 0. When I use an isometric view the (0,0,0) point is not in the centre of the coordinate system but somewhere far away. I solve the problem for Z coordinate:
gluUnProject(winX, winY, 0, modelview, projection, viewport, &posX, &posY, &posZ1);
gluUnProject(winX, winY, 1, modelview, projection, viewport, &posX, &posY, &posZ2);

And when I have Z1 and Z2 I can calculate the proper Z value. But what about rotations when my X or Y axis become a Z axis(perpendicular to monitor plane) then X and Y coordinates are not proper but fRange/-fRange (they are good only in front and back view). I don't have any idea what is wrong. Please help me. Thanks in advance!!! PS. Sorry for my bad English, I hope this post is understandable.

.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628395
• Total Posts
2982435

• 10
• 9
• 19
• 24
• 10