PDA

View Full Version : Troubles in reading depth buffer. Please help me...



newname
03-15-2001, 06:26 PM
Trouble. I want to obtain the depth information of the current view. But the result is not what I want. I use the following codes:

glPixelTransferf(GL_DEPTH_SCALE, 0.1f);
glPixelTransferf(GL_DEPTH_BIAS, 0.5f);
glReadPixels(0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT, GL_FLOAT, depthResults);

The result value I got at any pixel is either 0.5 or 0.6, where 0.6 means the pixel is on an object and 0.5 means empty. But nothing between [0.5, 0.6]! There is a ball in the context.

Why the depth values on the object are all the same? It should be distributed in [0, 1]!
How can I do?

Thanks for any responses.


I now make a very simple one for testing. It fails in my computer. Can anyone try the following codes:

[Please debug into the function copyDepthToColor() to see the depth values (only 0.5 and 0.6). Also you can view them by opening the saved file 512x512x4.dat, in which 4 Bytes per pixel in float type.]

//////////////// program begins /////////////
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>

GLUquadricObj *quadric;
int stenSize;
int winWidth, winHeight;
GLfloat *depthSave = NULL;

void drawSphere(void)
{
glPushMatrix();
glTranslatef(0,0,-2);
gluSphere(quadric, 1, 30, 20);
glPopMatrix();
}

void resizeBuffers(void)
{
if(depthSave != NULL)
free(depthSave);
depthSave = malloc(winWidth * winHeight * sizeof(GLfloat));
}

void SaveMemData(int w, int h, int eachsize, void *ptr)
{
FILE *file;
char str[256];
sprintf(str, "%dx%dx%d.dat",w,h,eachsize);
file=fopen(str,"wb");
if(file==0)
return;
fwrite(ptr, w*h*eachsize,1,file);
fclose(file);
}

void copyDepthToColor()
{
glPixelTransferf(GL_DEPTH_SCALE, 0.1f);
glPixelTransferf(GL_DEPTH_BIAS, 0.5f);

glReadPixels(0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT, GL_FLOAT,
depthSave);

SaveMemData(winWidth, winHeight, sizeof(GLfloat), depthSave);
}


void init(void)
{
glMatrixMode(GL_PROJECTION);
glFrustum(-.33, .33, -.33, .33, .5, 40);

glMatrixMode(GL_MODELVIEW);
gluLookAt(0, 0, 7, 0, 0, 0, 0, 1, 0);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glEnable(GL_NORMALIZE);

quadric = gluNewQuadric();
}

void redrawNoCSG(void)
{
glDisable(GL_STENCIL_TEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
// glCullFace(GL_BACK);
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
drawSphere();
copyDepthToColor();
glPopMatrix();

// copyInterest();
glutSwapBuffers();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);
//glFrustum(0,0,width,height,0.1,20); //add
winWidth = width;
winHeight = height;
resizeBuffers();
glutPostRedisplay();
}

int main(int argc, char **argv)
{
glutInitWindowSize(512,512);
glutInit(&argc, argv);
glutInitDisplayString("double depth");
glutInitDisplayMode(GLUT_DOUBLE|GLUT_STENCIL|GLUT_ DEPTH|GLUT_RGBA);
(void)glutCreateWindow("depth buffer testing");
glutDisplayFunc(redrawNoCSG);
glutReshapeFunc(reshape);

glGetIntegerv(GL_STENCIL_BITS, &stenSize);
printf("%d bits of stencil available in this visual\n", stenSize);

init();
glutMainLoop();

return 0;
}
//////////////////////////////////////////////////////////