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;

event.GetPosition(&x,&y);

winx = (GLdouble) x;

winy = (GLdouble) 250-y; // window height - y

// get the z-value

glReadPixels(x,y,1,1,GL_DEPTH_COMPONENT,GL_FLOAT,depth);

//z = (float) *depth; // right???

winz = (GLdouble) z;

// here we should get the object coordinates from the screen coordinates

glMatrixMode(GL_MODELVIEW);

glGetDoublev(GL_MODELVIEW_MATRIX, modelviewMatrix);

glMatrixMode(GL_PROJECTION);

glGetDoublev(GL_PROJECTION_MATRIX , projectionMatrix);

glGetIntegerv(GL_VIEWPORT,viewport);

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

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

/grodslukaren