How to Read & Display the Depth Buffer's Contents

… Er …

I’m trying to do some real-time CSG and I need to check out my depth buffer (visually) to be sure I’m on the right track; so here’s what I did:

First I declare an array to hold the anticipated depth buffer information:
GLfloat *Depth_Data = (GLfloat *) malloc(sizeof(GLfloat) * width * height);

Then I disable writes to the color buffer,
glColorMask(GL_FALSE, …);

Next I draw a sphere (to the depth buffer)
glutSolidSphere();

I Subsequently read data from the depth buffer to the array Depth_Data and enable writes to the color buffer:
glReadPixels(0, 0, width, height, GL_DEPTH_COMPONENT, GL_FLOAT, Depth_Data);
glColorMask(GL_TRUE, …);

Lastly I (attempt to) Draw the depth information to the color buffer as follows:
glDrawPixels(width, height, GL_LUMINANCE, GL_FLOAT, Depth_Data);

… but I get nothing?!

So then what am I doing wrong?


P.S:
I made the check:
printf("OpenGL version: %s

", glGetString(GL_VERSION));

… and got “1.3.0” which is kidda cool except that the call

GLint TexUnit_Count;

glGetIntegerv(GL_MAX_TEXTURE_UNITS_ARB, &TexUnit_Count);

generates an error … (something like) undeclared identifier/variable …

Methinks perhaps my header file is “pointing” to the wrong object files or something. I only recently had my graphics card upgraded to a GeForce 3 Ti200 from a RIVA or something …

H E L P !!!

You have to make sure that your raster positio is at the lower-left corner of the screen.
Try this :

/* Get the current matrix mode and raster position */
GLfloat raster_pos[4];
GLint matrix_mode;
glGetIntegerv(GL_MATRIX_MODE, &matrix_mode);
glGetFloatv(GL_CURRENT_RASTER_POSITION, raster_pos);

/* Set the projection and modelview matrices to identity */
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();

/* Set the raster position at the lower-left corner*/
glRasterPos2i(-1, -1);

/* Draw pixels */
glDrawPixels(width, height, GL_LUMINANCE, GL_FLOAT, Depth_Data);

/* Restore matrices and raster position */
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(matrix_mode);
glRasterPos4fv(raster_pos);