Advertisement Jump to content
Sign in to follow this  

Geting wrong coordinates with gluUnProject

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

If you intended to correct an error in the post then please contact us.

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;
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.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!