I started messing with the NeHe lesson 06, which draws a spinning cube.
I wrote some code to print the value from Z buffer in the middle of viewport:
/* This is C, NOT C++! */ int viewport; GLfloat depth = 0.0f; /* Query window size */ glGetIntegerv( GL_VIEWPORT, viewport ); /* Read 1x1 area from z buffer at centre of viewport */ glReadPixels( viewport / 2, viewport / 2, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth ); printf( "%f ", depth );
The value printed is not the “real” distance to the spinning cube, it is something between 0.96 and 0.98. The cube is about 4.5 units away.
The problem is: How to get the distance to the cube from Z buffer in world coordinates?
I mean a Z coordinate similar to the value (winZ) gluProject returns:
GLint gluProject( ..., GLdouble* winZ );
Am I clear?