Thanks to Bob.
Actually my program is rather complex. But I now make a very simple one for testing. It still fails in my computer. Can you 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, wheachsize,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
", stenSize);
init();
glutMainLoop();
return 0;
}
//////////////////////////////////////////////////////////
[This message has been edited by newname (edited 03-15-2001).]
[This message has been edited by newname (edited 03-15-2001).]