   About gluUnproject and object space coords
  I am trying to get the global coordinates of the point of a mouse click.I do this by first using glReadPixels, get the z value and then pass mouselocation.x,y,z to gluUnproject (this method is suggested by official FAQ at opengl.org):glReadPixels((GLint)mouseLocation.x, (GLint)mouseLocation.y, 1, 1, GL_DEPTH_COMPONENT, GL_DOUBLE, &z);glGetDoublev(GL_MODELVIEW_MATRIX, m);glGetDoublev(GL_PROJECTION_MATRIX, p);glGetIntegerv(GL_VIEWPORT, v);gluUnproject(mouseLocation.x, mouseLocation.y, z, m, p, v, &objSpaceCoords[0], &objSpaceCoords[1], &objSpaceCoords[2]);I initially assumed that the object space coordinates returned by gluUnproject was the same as global coordinates. But it seems that it's not. If it is the same, there's something wrong. If it's not the same, what is object space coordinates and how can I convert it to global coordinates, where (0.0, 0.0, 0.0) is in the middle of the screen?I confirmed that mouseLocation is valid (and correct). The base (0, 0) is at the lower left corner, same as OpenGL format. (GLint) typecast properly trims decimal numbers (so 600.0 would become 600). z value turns out to be always 3.0 but I don't think that's the problem. The returned objSpaceCoords are much smaller than what I expect, but seems to be in the right format (0.0, 0.0 is the middle of screen, 0.4, 0.4 is the upper right corner, etc). Also, if I zoom in and such,objSpaceCoords remains he same. I want transformations to reflect it.Thank you.[Edited by - hyn on July 16, 2004 2:46:36 PM]
  >>z value turns out to be always 3.0 << this is worrying in a 3d scene.what u should do is log the z value of the cursors position (from readpixels DEPTH_COMPONENT) to the console each frame and move the cursor around , u should see it changing   ;  The value doesn't change. It seems to be always 3.0.The part that most bothers me is that even though im passing my modelview and projection matrix, my transofmartions aren't taken into consideration in claculating the objSpaceCoords.   ;  Sorry if this is stupid but are you sure that you've drawn your scene before you get the z value.  I was trying to get gluUnproject working on my game and i never did in the end i wrote the whole thing myself.  Which isn't that hard to do.   ;  Yes, my scene most certainly is drawn when the mouse is clicked.I have a timer calling my drawScene method at around 60 fps, and when the mouse is clicked, I read the data off a pixel at that specific time. If you don't mind, how did you go about writing the fuction yourself?   ;  >>The value doesn't change. It seems to be always 3.0.<<not good, if there are objects on the screen but the value always remains at 3.0 then somethings screwed, solve this first. why u cant receieve their depth values (perhaps depthwritemask = 0 or depthtest is not enabled or cleardepth = 0 etc)>>The part that most bothers me is that even though im passing my modelview and projection matrix, my transofmartions aren't taken into consideration in claculating the objSpaceCoords.<<im not to sure what u mean usingglGetDoublev(GL_MODELVIEW_MATRIX, m);glGetDoublev(GL_PROJECTION_MATRIX, p);is enuf this gets the current matrix values that opengl is using   ;  about the transformation not being taken into consideration i had the same problem when i tried to do it.  Here what i use you'll have to get a matrix class and a vector class.MouseX and MouseY must be between -1 and 1.  It's not optimized or anything but it does the job.  I can't remember where i got the matrix class from sorry. CVector is a 3 component vector (x,y,z) and CVector4 is a 4 component vector (x,y,z,w).// Converts the passed position to a position on the terrainCVector GetPositionOnTerrain(double MouseX,double MouseY){glLoadIdentity();glPopMatrix();glPushMatrix();GLint viewport[4];glGetIntegerv(GL_VIEWPORT, viewport);int PixelX = int((MouseX + 1.0) * 0.5 * (viewport[2]-1));int PixelY = int((MouseY + 1.0) * 0.5 * (viewport[3]-1));float ZPos;glReadPixels(PixelX, PixelY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &ZPos);if(ZPos == 1.00000)return NULL;MATRIX4X4 modelview;MATRIX4X4 projection;MATRIX4X4 ModelViewProjection;glGetDoublev( GL_MODELVIEW_MATRIX, modelview );glGetDoublev( GL_PROJECTION_MATRIX, projection );ModelViewProjection = projection*modelview;ModelViewProjection.Invert();CVector4 Test(MouseX,MouseY,(ZPos*2)-1,1);Test = ModelViewProjection*Test;Test.x /= Test.w;Test.y /= Test.w;Test.z /= Test.w;return CVector(Test.x,Test.y,Test.z);}   