Skip to main content
GameDev.net gamedev.net
🔒 Locked

Small issue reading depth buffer

Started by _Seth_ Oct 31, 2004 at 3:37 PM 7 replies 750+ views
Original Post
_Seth_
_Seth_
Hi guys, I'm trying to read the depth buffer and place that information in an array like this: GLfloat *depthBuff = new float[RESX*RESY]; glReadPixels(0,0, RESX,RESY, GL_DEPTH_COMPONENT, GL_FLOAT, depthBuff ); If I process that array and place a GL_POINT vertex to every hit the ReadPixel has recorded, then I found back the image I read (I use gluUnProject). The problem is, when I zoom out the camera, I find out that ReadPixel has detected "valid" pixels where there were none. It always happenx at the very bounderies of the viewing frustum and on the near plane and far plane. I decided to write a file with the depthBuff array information and found out that while most of the depth value of the pixels are correctly recorded as 0 or 1 (non existant), a few ones are given values where there were nothing in view. I'm using glClearDepth(1.0f); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); and I have made my near and far plane very close to each other to get the most of depth precision. Is that a normal behavior? What would you suggest? Thanks jeff
mikeman
mikeman
First of all, only pixels with depth=1.0 are "nonexistant". Everything less is "valid".

As far as I can understand, you're saying you get depth<1.0 in pixels where nothing is rendered. It would help if you provide some of these values to check.
_Seth_
_Seth_
Sure.

This image is rendered with the information contained into the depthbuff array with the camera unchanged.

http://www.jflevesque.com/image.jpg

On this image, I have zoomed out and moved the camera a little. Noticed the black pixels that follow the original viewing volume. (it's a bit hard to tell with just that image though)

http://www.jflevesque.com/image2.jpg

and here's the depthbuff array dump in this format:
pixel x, pixel y, depth recorded

the image is 300X300.

http://www.jflevesque.com/reference.zip

Jeff
mikeman
mikeman
It's really confusing, because the dump shows various different z values, but the first image is just a quad facing straight to the camera. All the zvalues should be the same.

Could you describe more precisely all the steps you follow to render the scene? I don't really know what you're trying to do.
_Seth_
_Seth_
It's pretty simple.

Camera init
glMatrixMode   ( GL_PROJECTION );  // Select The Projection Matrix    glLoadIdentity ( );                // Reset The Projection Matrix	gluPerspective(45.0f, 1.0f, 1.0f, 7.0f);	glGetFloatv(GL_PROJECTION_MATRIX, lightprojection);			glMatrixMode   ( GL_MODELVIEW );	glLoadIdentity();	gluLookAt(lightPosition.x, lightPosition.y, lightPosition.z,				0.0f, 0.0f, -4.0f,				0.0f, 1.0f, 0.0f);	glGetFloatv(GL_MODELVIEW_MATRIX, lightprojection);


void Display(void){	GLfloat *depthBuff = new GLfloat[RESX*RESY];		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);			glViewport(0, 0, RESX, RESY);	//Draw the scene	glColor3f(1,0,0);	glBegin(GL_QUADS);		glVertex3f(-0.6,-0.6,-3.0);		glVertex3f(0.6,-0.6,-3.0);		glVertex3f(0.6,0.6,-3.0);		glVertex3f(-0.6,0.6,-3.0);	glEnd();	glReadBuffer(GL_BACK);	glPixelStorei(GL_PACK_ALIGNMENT, 1);	glReadPixels(0,0, RESX,RESY, GL_DEPTH_COMPONENT, GL_FLOAT, depthBuff  );	for(int y=0;y < RESY;y++)	 	 for(int x=0;x< RESX;x++)		 		 reference << x <<" " << y << " " << depthBuff[x + ((RESY) * y)] << std::endl;	glFinish();	glutSwapBuffers();	glutPostRedisplay();        delete[] depthBuff;}


Like you said, it gives a weird depth buffer reading and I wonder why?!

Jeff

[Edited by - _Seth_ on November 1, 2004 11:33:44 AM]
_Seth_
_Seth_

Could anyone post a link to a source that works fine and get the right values from the depth buffer? I'll compare it with my code and maybe find what's wrong.

Thank you
Jeff
Enigma
Enigma
Quote:
Original post by mikeman
Your code is fine.


Well, apart from the huge memory leak in Display. std::vector is your friend.

Enigma
_Seth_
_Seth_
Yeah, I noticed the memory leak!

A few people tried it out too and it works fine for them. But I still get weird values. I have an ATI Radeon 9800 pro... could it be the card? I have the lastest drivers...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.