Geting wrong coordinates with gluUnProject


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.

I’m not sure I understand what you are trying to say.
All that gluUnproject does is convert screen space coordinates to object space. It doesn’t matter if you have some rotation applied to the modelview matrix.
If you take the result of gluUnproject and feed them back to gluProject, you would get the original screen coordinates.

OK I try to explain.

For example I draw a coordinate system using GL_LINES:


I rotated it glRotatef(90,0,1,0) and X coordinate is 300 according to gluUnProject(), and it should be 0 because there are no translations. 300 because I call glOrtho(-300aspect,300aspect,-300,300,-300,300). And when I switch on isometric view the (0,0,0) is not in the centre of coordinate system (it is but gluUnProject shows different). I hope now You understand my problem.
So please help me, its really important for me.

Thanks in advance!!!

Sorry for double posting. But maybe somebody can write a simple program for me. Using winapi, opengl (without glut) and glOrtho view, drawing a coordinate system (GL_LINES) in centre of the screen, and showing x,y,z coordinates in status bar with gluUnProject. I try to compare it with my program and maybe I figure out what is wrong. Maybe Visual Studio is messing up something. I use VS 2008 Pro and I write for Win Vista. I would be really grateful for any help! Tanks in advance!