gluUnProject

I have a problem.I want to change mouse coordinate to gl coordinate using below function.But it doesn’t work.
What’s going wrong??
And Is there any better way to do this?

Here is the code:

GLdouble modelMatrix[16];
GLdouble projMatrix[16];
GLint viewport[4];

glGetDoublev(GL_MODELVIEW_MATRIX ,modelMatrix);
glGetDoublev(GL_PROJECTION_MATRIX ,projMatrix);
glGetIntegerv(GL_VIEWPORT,viewport);

GLdouble winX,winY;
winX = mousePoint.x;
winY = viewport[3] - MousePoint.y;

GLfloat winZ ;
glReadPixels(winX,winY,1,1,GL_DEPTH_COMPONENT ,GL_FLOAT, &winZ);

GLdouble glX,glY,glZ;
int value =gluUnProject(winX,winY,winZ,modelMatrix,projMatrix,viewport,
&glX,&glY,&glZ); //sometimes value is false,why?

Hi !

What do you mean with sometimes value is false ? are the values a total messup or what ?

Do you check fo errors ?

You should change:
winY = viewport[3] - MousePoint.y;
to:
winY = (viewport[3]-1) - MousePoint.y;

But I don’t think that is the problem.

Mikael

Hi!
Thanks for reply.

int value =gluUnProject(winX,winY,winZ,modelMatrix,projMatrix,viewport,
&glX,&glY,&glZ); //sometimes value is false,why?

It was a mistake.“value” never was false.

But I found that glReadPixels() often fills winZ with 1.0 and sometimes fills winZ with the correct value.
What’s the problem?