Displaying A Depth Image - Help!!

OK, I read the value from the depth buffer into a float called bob.

I then define a GLubyte called grey and do

grey = bob * 255;

Then I write “grey” to the buffer and display the buffer (as below) but I always get white.

glDrawPixels(imagewidth, imageheight, GL_RGB, GL_UNSIGNED_BYTE, Image);

Anyone know why? Ive tried playing about with different data types but to no avail

you have copied the depth val to r, g and b channels? Or rather is bob xy3 in size, i.e. a r, g and b channel or simpler still use a struct with r, g, b glubytes in it. malloc x*y of these. OpenGL still takes an array of these for drawpixels.

Gavin , yep Ive done all that. Im copying the grey value to the each of the r,g,b values.

What depth-ranges do you get in the float bob? Try capturing the lowest anf highest values you get when reading the depth buffer.

Remember, if you use a perspecive projection, the read-back depth value is not linearly scaled. So if you have a lot of ‘very far’ values, they are all quite close to 1 --> a white image.



Yes Ive experimented with different settings for the near and far planes. I think the problem is something to do with the variable types. Like how do you convert a float to a GLubyte? I think data is being lost.

Why is this so hard to do? Youd think there would be some examples somewhere that show how to do this effectively.

Sorry for going on but Ive spent ages trying to do something that you’d expect to take 5 minutes. And yes ive searched previous postings and still no joy.

thats life eh?


Don’t know if this will help but I found that I was getting A LOT of z buffer values VERY close to 1. That’s even for pixels where I knew the object was quite close to the ‘camera’. Turned out to be exactly how it should be due to the non-linear way in which the z buffer information is stored.

This might be of some use: http://www.opengl.org/discussion_boards/ubb/Forum2/HTML/009293.html

I used this to back calculate all the z values and know everything works fine.

EDIT: For quick reference this is taken from the above link:
for Near Clip = 10 and Far Clip = 10000,
zval =0.9 range = 99.1
zval =0.99 range = 909
zval = 0.999 range = 5002

So a ‘pixel’ at range 100 even with the far clip plane at 10000 would look white.

[This message has been edited by thelamberto (edited 07-30-2002).]

Hmm, doesn’t this work:

GLubyte Grey;
GLfloat Bob;

Bob=val from depthbuffer
Bob*=256; (so now STILL a float)

Ciao and tell me if this solves it,


So if you alter the rgb values manually you get different colours? Or still white


thanks. Im going to play about with the values for the near and far planes.


No Ive laready tried that doesn’t work although it may be because the near and far planes are spaced too far apart.

Will post when i get it to work.

OK. Got it to work. Use the following code to calculate pixel depth (thanks mate!).

dcp = FarClip - NearClip
scp = FarClip + NearClip
a = -1.0 * ( FarClip * NearClip / dcp )
b = scp/ ( 2.0 * dcp ) + 0.5

depth = a / ( zval - b )

What I did was find the max and minimum depths first. I used this as a range. So a pixel with a minimum depth was set to white (ie. 255) and a pixel with a maxmimum depth was set to black (ie. 0). Inbetween were set to shades of grey.

One other thing. When getting maximum pixel depth check for zval == 1 (ie. value from the depth buffer). If zval == 1 then ignore it as regards calcuating maximum depth. As said before this can screw things up considerably.