View Full Version : gluUnProject() - glReadPixels()

07-03-2001, 11:11 AM
I get really strange values when I do my projection with gluUnProject.

One is supposed to put the screen coordinates as in-parameter to gluUnProject. Is it so easy that I can take the x/y values from the mouseevent I receive with a mouse click and use them as screen coordinates, what do I do with the z-coordinate?? Iīve read that one should use glReadPixels. But I donīt manage that...

some code:
GLdouble modelviewMatrix[16];
GLdouble projectionMatrix[16];
GLint viewport[4];
GLdouble winx,winy,winz;
GLdouble objx, objy, objz;
GLvoid* depth = 0; // is this right???

int x,y;
float z;
z = 0;

winx = (GLdouble) x;
winy = (GLdouble) 250-y; // window height - y
// get the z-value
glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,d epth);
//z = (float) *depth; // right????
winz = (GLdouble) z;

// here we should get the object coordinates from the screen coordinates
glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);
glGetDoublev(GL_PROJECTION_MATRIX , projectionMatrix);

gluUnProject(winx,winy,winz,modelviewMatrix,projec tionMatrix,viewport,&objx,&objy,&objz);

// the object coordinates are now stored in objx/y/z


07-03-2001, 01:36 PM
Not sure but try

float depth;

I think that should be enough
the other option is

float depth[0];
glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,d epth);

You must define the size of the pointer, I don't think you did that, but I never looked too hard.