I am using gluUnProject to get a 3d coordinate from the mouse position, well what else are you going to use it for. And to sum it up it doesn’t work! I use glReadPixels to get the depth. I think this works, I get values ranging from 0-1 (that should be correct) with numbers closer to 0 being nearer. I also think I am seeing the affects of the depth buffer not being linear becuase when I click a far away object I gvet a value of about 0.951 if I click somewhere a lot further away it only goes up to about 0.952 etc. so I think this is working. I pass this to gluUnproject with the mouse x and y coordinates (the y is inverted at the start) What I get back are coordinates that must be wrong, for example I click at the origion and it will return -10,-10,-11 (when rounded to int). The numbers don’t seem to change correctly along the axis ( for example I test the origion and tehn text 10 units to the right along the x-axis and it isn’t changed by anything like 10 units. Here is my code:
GLfloat pixel; GLdouble obx,oby,obz; GLint viewport[4]; GLdouble mvmatrix[16]; GLdouble projmatrix[16]; glGetIntegerv (GL_VIEWPORT,viewport); glGetDoublev (GL_MODELVIEW_MATRIX,mvmatrix); glGetDoublev (GL_PROJECTION_MATRIX,projmatrix); y = viewport[3] - y; glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&pixel); gluUnProject(x,y,pixel,mvmatrix,projmatrix,viewport,&obx,&oby,&obz);
I have also been haveing problems with the selection buffer in that it always gives the wrong results in my MFC level editor, this might have something to do with it(well probably not). But I can’t find anything wrong with the code.