canot read depth buffer

The following program gives a value of (some random number) for the red point, but 0.0000 for the z-value of the point.
How do I get a return of z != 0.0000, given that the point is located at 50, 50, 50?
What is wrong with brg?

// depthtest.c
// compile as gcc depthtest.c -lglut

#include<stdio.h>
#include <GL/glut.h>

void display(){
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, 200, 0, 200, -100, 100);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glEnable(GL_DEPTH_TEST);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glColor3f(1.0, 0.0, 0.0);
  glPointSize(5);
  glBegin(GL_POINTS);
  glVertex3i(50, 50, 50);
  glEnd();
  glutSwapBuffers();
  float z; 
  glReadPixels(50, 50, 0, 0, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
  GLubyte bgr;
  glReadPixels(50, 50, 0, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, &bgr);
  printf("z = %f; bgr = %u
", z, bgr);
  
}

int main(int argc, char* argv[]){
  glutInit(&argc,argv);
  glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  glutInitWindowSize(200, 200);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("depth test");
    glutDisplayFunc(display);
  glutMainLoop();
  return 0;
}

I see a couple of problems: First of all, you’re not reading any pixels, since you specify 0 for the width and height of the region you read. If you want to read 1 pixel, pass 1 for the 3rd and 4th argument of glReadPixels().

Also, you would get a random value for the color at least the first time because you’re using double buffering, and call glReadPixels() after glutSwapBuffers(). So you read from the new back buffer after swapping, which you have never drawn to yet on the first call to display(). If you want to read from the buffer you were just drawing to, move the glReadPixels() calls before glutSwapBuffers().

Changing the 0 to 1 helps. I misinterpreted the glReadPixels() definition to be (target) + width & (target) + height.
Leaving the glutSwapBuffers() where it is, I get z = 0.25194 and bgr = 0. Since the glOrtho depth is 200, 200 * 0.25194 = 50.388. 50.388 ~= my point location. I would say hallelujah, except that we are still in Lent.
Moving the glutSwapBuffers() to after the printf statement has no affect on the value of z or bgr. I am still missing a piece of the BGR puzzle.

[QUOTE=reto.koradi;1258885]I see a couple of problems: First of all, you’re not reading any pixels, since you specify 0 for the width and height of the region you read. If you want to read 1 pixel, pass 1 for the 3rd and 4th argument of glReadPixels().

Also, you would get a random value for the color at least the first time because you’re using double buffering, and call glReadPixels() after glutSwapBuffers(). So you read from the new back buffer after swapping, which you have never drawn to yet on the first call to display(). If you want to read from the buffer you were just drawing to, move the glReadPixels() calls before glutSwapBuffers().[/QUOTE]

Sorry, I missed one other problem: You’re only passing in 1 byte for reading the BGR value, which is 3 bytes. That one byte will be populated with the B value, which is indeed 0, since you were drawing red. The remaining two bytes would be written beyond the end of the memory you pass to glReadPixels(), with possibly bad consequences. Try this fixed version:


void display(){
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glOrtho(0, 200, 0, 200, -100, 100);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glEnable(GL_DEPTH_TEST);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glColor3f(1.0, 0.0, 0.0);
  glPointSize(5);
  glBegin(GL_POINTS);
  glVertex3i(50, 50, 50);
  glEnd();
  float z = 0.0f; 
  glReadPixels(50, 50, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);
  GLubyte bgr[3] = {0};
  glReadPixels(50, 50, 1, 1, GL_BGR_EXT, GL_UNSIGNED_BYTE, bgr);
  printf("z = %f; bgr = %u/%u/%u
", z, bgr[0], bgr[1], bgr[2]);
  glutSwapBuffers();
}

That works. Problems solved. Thanx.

FYI for anyone else reading this thread:
given

 glMatrixMode(GL_PROJECTION);
  glOrtho(xmin, xmax, ymin, ymax, zmin, zmax);
  glMatrixMode(GL_MODELVIEW);
  glBegin ...
  ;;;
  glEnd();
  glReadPixels(ix, iy, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z);

the z value returned is 0.0 if the pixel (ix, iy) is located at zmax, and 1.0 if the pixel is located at zmin.
My earlier analysis of z was incorrect.