PDA

View Full Version : why the z-buffer value is always 1 when i use glReadPixels() to read the z-buffer?



xixitomato
02-19-2004, 06:55 PM
in openinventor ,i use the callback node to read the z-buffer value and want to get the z coordinate in the real world. I use the glReadPixels() to read the z-buffer, but all the depth
value i reading is 1. I do not know why i can not get it. my programme
is following.


//project the elbowCenter to the screen
elbowCenter.setValue(1.0,-3.0,0.0);
SbViewVolume myViewVolume=myCamera->getViewVolume();
SbVec3f pointScreen;
int x,y;
myViewVolume. projectToScreen(SbVec3f(elbowCenter[0], elbowCenter[1],
elbowCenter[2]),pointScreen);
x=pointScreen[0]*size[0];
y=pointScreen[1]*size[1];

glEnable (GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

//get the real world coordinate --posz
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 = x;
winY = y;
glReadPixels( x, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ
);
gluUnProject( winX, winY, winZ, modelview, projection, viewport,
&posX, &posY, &posZ);

Relic
02-20-2004, 02:57 AM
That's the depth clear value. Make sure you pick at the correct place!
Windows coordinates are top left orign, OpenGL pixel coordinates are bottom left.
y needs to be inverted if you get it from Windows. Use this formula:
y = windowClientHeight - 1 - yMousePosition;

Example: You clicked on the bottom line in a 640*480 window.
windowClientHeight == 480, yMouse is 479, y == 0.
glReadPixels from there. Voila!