hi,
i am trying to read the depth value from the z buffer using following code in my display function.However, it returns me some wierd values,most of the times 1.
As a sort of check, I used gluProject to project realworld (0.0,0.0,0.0) on window and get its depth value.Now when I am using window x and y coordinate obtained from above function in my glReadPixels function to get the depth value, its returning me 1.
Can anyone help me out in this? Do i need to make some initializations? I am using visual C++ 6.0. Following is my code.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0f,0.0f,100.0f,0.0f,0.0f,40.0f,0.0f,1.0f,0.0f );
glTranslatef(offset_x,offset_y,offset_z);
glRotatef(pitch, 1.0f, 0.0f, 0.0f);
glRotatef(yaw, 0.0f, 1.0f, 0.0f);
glLightf(GL_LIGHT0 , GL_SPOT_EXPONENT, 40.0f);
glLightfv(GL_LIGHT0 , GL_AMBIENT , light_ambient);
GLdouble modelView[16];
GLdouble projection[16];
GLint viewport[4];
//real world coordinates
GLdouble sx, sy, sz;
//window coordinates
GLdouble wx,wy,wz;
glGetDoublev (GL_MODELVIEW_MATRIX, modelView);
glGetDoublev (GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);
sx=0.0f;sy=0.0f;sz=0.0f;
// a variable to hold depth value from glReadpixels
GLfloat pixel ;
gluProject(sx, sy, sz, modelView, projection, viewport, &wx, &wy, &wz);
glReadPixels((int)wx,(int)wy,1,1,GL_DEPTH_COMPONENT,GL_FLOAT , &pixel);
cout<<sx<<","<<sy<<","<<sz<<endl;
cout<<wx<<","<<wy<<","<<wz<<endl;
cout<<pixel<<endl;
NOw, ideally wz and pixel shuold be the same…but this is not the case.Plz help me.Thanks a lot