gluUnProject

I have an MFC_OpenGL Application where I have used gluUnProject() function in it… & the code goes like this:

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

I have problem in glGetDoublev(GL_MODELVIEW_MATRIX, modelview), here I am not able to get model matrix, when i debug, i get the garbage values for all the 16 values in the MODEL MATRIX…

So plzzzzzz help me in this…

How are your projection and modelview variables defined?

Are your matrices pointers to floats? If they are doubles, then I would think you are getting garbage into the matrices before you try and retrieve them, possibly with a glLoadMatrix or glMultMatrix.

I have used the same code in different MFC Appln where it’s working fine only when I maximum the window (if the window is restored back, i am not able to get the same value of the mouse pointer in a 3D space), but it’s not working in my present MFC appln, is this anything to do with setting up or intializing the viewport!!!

Thank U

How are your projection and modelview variables defined?

variables are defined as:
GLint viewport[4];
GLdouble modelview[16];
GLdouble projection[16];
GLfloat winX, winY, winZ;
GLdouble posX, posY, posZ;

glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
glGetDoublev( GL_PROJECTION_MATRIX, projection );
glGetIntegerv( GL_VIEWPORT, viewport );

winX = (float)x;
winY = (float)viewport[3] - (float)y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );

gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);

return Vector(posX, posY, posZ);

Please make sure first that your OpenGL render context is valid before calling all OpenGL commands.

Please make sure then that your glPushMatrix and glPopMatrix are used as a pair.