How can i take DEPTH value with glReadPixels?

glReadPixels로 찾은 깊이값을 수정?

I want to take depth_valuew with glReadPixels.
I exhaust some days about this.
As you see below, I coded program.
First, i draw rectangle with z_value=-1.
and i clicked with mouse_pointer in rectangle.
So print its pixel_value_depth.
I think this print -2.
But …
92559604281399047000000000000000000000000000000000000000000000.000000
these string of numbers was printed.
What’s the problem?
How Can i fix it…?
Give me correct code.!!!


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

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_FLAT);
glDepthRange(0.0, 1.0);
}

void drawRects()
{
glPushMatrix();
glTranslatef(0,0, -7);
glBegin(GL_QUADS);
glColor3f(1.0, 0.0, 1.0);
glVertex3i(0, 2, -2);
glVertex3i(0, 7, -2);
glVertex3i(5, 7, -2);
glVertex3i(5, 2, -2);
glEnd();
glPopMatrix();
}

void PickObject(int button, int state, int x, int y)
{
int m_iView[4];
glGetIntegerv(GL_VIEWPORT, m_iView);

GLdouble depth;
glReadPixels (x, m_iView[3]-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &depth);
printf("

depth=%f

", depth);
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawRects();
glFlush();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0.0, 8.0, 0.0, 8.0, -0.5, 2.5);
gluPerspective( 90, 1, 0.1, 10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (200, 200);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(PickObject);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

You should expect a number between 0.0 and 1.0, because you will get the depth value after projection, not the original depth value you set in the glVertex calls. You can use the gluUnProject function to reverse the projection.

As for your specific problem, try changing GL_FLOAT to GL_DOUBLE in your glReadPixels call. You are reading a float value into a double variable.