PDA

View Full Version : glReadPixels/gluUnProject problem



ATolba
05-25-2011, 06:59 AM
Hi,

I am new to OpenGL, and I am having this annoying problem for a while. I hope someone can help me. I am drawing some objects in my scene successfully, and trying to show in status bar of my application the object coord while moving mouse over it.
My code look like this (screenX and screenY are mouse position):
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realY;
glGetIntegerv(GL_VIEWPORT,viewport);
glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX,projmatrix);
realY = viewport[3] - screenY;
GLfloat winZ;
glReadPixels(screenX, realY, 1,1,GL_DEPTH_COMPONENT,GL_FLOAT,&winZ);
return gluUnProject((GLdouble)screenX,(GLdouble)realY, (GLdouble)winZ, mvmatrix,projmatrix,viewport, &wx,&wy,&wz);

I am using glFrustum with near plane= 15.0 and far=1000.0

This is working perfectly when my mouse is over the object, but once it is over an empty area in the screen, it returns strange values: winZ is always 0.67 and wz,wy, and wz are odd.

Thanks in advance,
Abeer

The Little Body
05-25-2011, 08:34 AM
You certainly use something like this for to clear the screen before to draw the object :

glClearDepth(0.67);
//glClearColor(red,green,blue,alpha);
...
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BIT)

So, the value of winZ is always 0.67 except where you have draw your object (where the Z value is set to anothers values)