glReadPixels with GL_DEPTH_COMPONENT returns 0

Hi guys,

I’m trying to use glReadPixels with the GL_DEPTH_COMPONENT, but I can’t seem to get it return something else than 0. I enabled the depth buffer, check for an error (there is none), the zfar and znear look fine to me…

My code is highly inspired from unproject.c from the red book, so without more comments:

void display(void)
{
glEnable (GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

gluLookAt (0.0, 0.0, zcampos, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

glColor3f(0,1,1);
glutWireCube(1.0);
glBegin(GL_QUADS);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(1.0, 0.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();

glFlush();
}

/* Change these values for a different transformation */
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
500.0);
gluPerspective(45.0,(GLfloat) w/(GLfloat) h, znear, zfar);
}

void mouse(int button, int state, int x, int y)
{
glEnable (GL_DEPTH_TEST);
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; /* OpenGL y coordinate position /
GLdouble wx, wy, wz; /
returned world x, y, z coords */
GLfloat realz;

switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);

/* note viewport[3] is height of window in pixels */
realy = viewport[3] - (GLint) y - 1;
printf ("Coordinates at cursor are (%4d, %4d)
", x, realy);

        glReadPixels(x, realy, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &realz);
        GLint err = glGetError();
        if (err != GL_NO_ERROR) {
        	printf("there was an ERROR

");
}

        printf("z value is for (%d, %d): %f

", x, realy, realz); // always 0

        gluUnProject ((GLdouble) x, (GLdouble) realy, realz,
                       mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
        printf ("World coords at z=%f are (%f, %f, %f)

",
realz, wx, wy, wz);
}
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
exit(0);
break;
default:
break;
}
}

/*

  • Open window, register input callback functions
    /
    int main(int argc, char
    * argv)
    {
    glClearColor(0.0,0.0,0.0,0.0);
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (200, 200);
    glEnable (GL_DEPTH_TEST);
    glutCreateWindow (argv[0]);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc (keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
    return 0;
    }

Hrmm, looks good to me. I would try doing a glDrawPixels immediately before and then reading it back as a simple test.

Good thinking. I added:

float zBuffer[window_w][window_h];
int i,j;
for (i=0; i<window_w; i++) {
for (j=0; j<window_h; j++) {
zBuffer[i][j] = 10.0f;
}
}

glDrawPixels(window_w, window_h, GL_DEPTH_COMPONENT, GL_FLOAT, zBuffer);

No change, glReadPixels still returns 0

Good thinking. I added:

float zBuffer[window_w][window_h];
int i,j;
for (i=0; i<window_w; i++) {
for (j=0; j<window_h; j++) {
zBuffer[i][j] = 10.0f;
}
}

glDrawPixels(window_w, window_h, GL_DEPTH_COMPONENT, GL_FLOAT, zBuffer);

No change, glReadPixels still returns 0

Good thinking. I added:

float zBuffer[window_w][window_h];
int i,j;
for (i=0; i<window_w; i++) {
for (j=0; j<window_h; j++) {
zBuffer[i][j] = 10.0f;
}
}

glDrawPixels(window_w, window_h, GL_DEPTH_COMPONENT, GL_FLOAT, zBuffer);

No change, glReadPixels still returns 0

Good thinking. I added:

float zBuffer[window_w][window_h];
int i,j;
for (i=0; i<window_w; i++) {
for (j=0; j<window_h; j++) {
zBuffer[i][j] = 10.0f;
}
}

glDrawPixels(window_w, window_h, GL_DEPTH_COMPONENT, GL_FLOAT, zBuffer);

No change, glReadPixels still returns 0

Sorry about all the posting, the site said it had an error so I reposted… anyways, I did not find out what the problem was, but starting over just worked. If I find out what went was wrong I’ll post it here.