Not logged in, Join Here! or Log In Below:  
News Articles Search    

Submitted by Olivier Bucher, posted on October 29, 2001

Image Description, by Olivier Bucher

I always wondered what a depth buffer could look. I had a good idea but i thought that actually see one could be cool. So here is my little 2 cents to the IOTD afficionados :).

The engine being developped under OpenGL, i used the glReadPixels to extract the raw depth buffer data then some resampling are needed to get RGB values. I used the devil image lib ( to save the final data. The pic where white visually means far array is the actual one, the other one was cool enough to be showed too ;)

The image is taken from my current personnal game project Djidjoo ( Except that the landscape geometry can be modified in real time with the mouse, the engine has no exciting feature. It's a very simple/basic engine using very well known OpenGL techs (Vertex Array + strips, multitexturing, lighting), 3ds ( and md3 files are used for objects/characters. Some state variables can be enabled/disabled/configured through a console.

I only hope to release the final game one day :).


Image of the Day Gallery


Message Center / Reader Comments: ( To Participate in the Discussion, Join the Community )
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
The Digital Bean

October 29, 2001, 12:15 PM



October 29, 2001, 12:21 PM

looks kinda foggy :)


October 29, 2001, 12:44 PM

How fast is your performance using glReadPixels() ?

It looks like you could do some neat fog effects using this if its not too slow.

David Olsson

October 29, 2001, 12:59 PM

Linear fog...


October 29, 2001, 01:17 PM

I have rendered the depthbuffer in OpenGL too, but not with such a beautiful result. Here is a link to a discussion on the message board on how to do it in OpenGL.

About the speed. It runs about 2 fps on my computer.
On my computer it takes longer writing the pixels to the frame buffer than reading them from the depthbuffer.


October 29, 2001, 01:19 PM

Technically, you can do this exact effect using linear fog. No need to actually read the depth buffer. All you really need to do is set the near fog value to the minz value, and the far fog value to the maxz value, then set the fog to linear. After that, you can render everything without textures or lighting, just with the color set to black (or white, if near things are white), and the opposite color for the fog value. This would basically generate this exact picture.



October 29, 2001, 01:31 PM

"How fast is your performance using glReadPixels() ?"

Well don't even think about using glReadPixels for real-time effect....
When i took these screenshots the FPS was 32 instead of 60 !!

"Linear fog..."
Nop, glFogf(GL_FOG_MODE, GL_EXP);

"I have rendered the depthbuffer in OpenGL too, but not with such a beautiful result"
Thanks, if you liked it i'm happy with it ;)


October 29, 2001, 01:33 PM

Before reading the text I honestly thought the first picture was some kind of volumetric fog demo. Pretty neat. :)


October 29, 2001, 01:36 PM

There are better ways to access the depth buffer. Check out any of the oGL depth shadow examples. Looks cool though, I went through this back when I was putting depth shadows into my engine. Turned out that projecting a depth texture onto the screen and rendering to texture was a whole lot faster than reading the depth buffer(duh).

I like the screens on your site.


October 29, 2001, 01:54 PM

nice terrain engine :)


October 29, 2001, 02:45 PM

Heck, you could simply set the vertex color = Z value/MaxDepth... that would give you the first picture, and vertex color = 1-(z value/maxdepth) would give you the second picture... and it would run MUCH faster.



October 29, 2001, 02:55 PM

Yeah, I am aware of this. But using this technique, you could be much more selective on where the fog should be placed.


October 29, 2001, 03:03 PM

A nice way to present your terrain engine with a twist! : )


October 29, 2001, 05:54 PM


The first shot looks like a sandstorm somewhere in the Sahara or something (except for the color ofcourse ;))

Timothy Barnes

October 29, 2001, 06:28 PM

That's cool. I've always been quite curious as well. At least I got my question answered. Keep up the good work.


October 29, 2001, 07:10 PM

From Ready4Dis :
"Heck, you could simply set the vertex color = Z value/MaxDepth... that would give you the first picture, and vertex color = 1-(z value/maxdepth) would give you the second picture... and it would run MUCH faster. "

Err, Nop. Because depth values are clamped from 0 to 1. Sometimes min and max depth values have very similar. Dividing these kind of values may give a full black image (if MaxDepth is not clamped, 500.0f for ex.) or a full white (if MaxDepth is clamped). You can trust me i've tested both cases ;)


October 29, 2001, 09:57 PM

The basic idea behind Ready4Dis's post is correct. I've done it before with a vertex shader. You just have to do a little more work to scale the values.

The vertex shader runs extremely fast, much much faster than glReadPixels.


October 29, 2001, 10:13 PM

That "inverted" for looks really cool- sortof like wacky nightvision or something. Good luck making your game!


October 29, 2001, 10:13 PM

GVENoM's way is also more portable, as well as being MUCH faster. I tried doing the same thing ages ago using Direct3D - FPS went from 240fps downto 14fps when locking the depth buffer and reading from it. Using projective texturing, whilst not achieving the full 16-bits per pixel accuracy of the glReadPixels method, yields much faster results and is portable to DirectX too. You can get about 11-bits of depth accuracy using render to texture on a standard TNT/TNT2/GeForce/GeForce2/Radeon class card.


October 29, 2001, 11:48 PM

Don't talk about devIL or you'll bring the mighty doomwiz down among us :)

Great IOTD - something different :)

Rasmus Christian Kaae

October 30, 2001, 01:55 AM

glColor4f(1/z, 1/z, 1/z, 1/z);


October 30, 2001, 03:22 AM

glColor4f(1/z, 1/z, 1/z, 1/z);

As you know, (1/z) / (1/z) = 1 so that would be a no-op. Storing reciprocal depth in the color or alpha channels can be used for some cool things however. There was a paper in the GDC 2001 proceedings on this by Boyd and some other Microsoft fellow. It's even nicer for software rendering because reciprocal z is linear in screenspace.


October 30, 2001, 04:14 AM

Nice shoot, let us know when you release a demo ! :)


Rag on a Stick

October 30, 2001, 05:24 AM

You've been reading my mind. I was thinking about how to do some sort of sonar thing for a submarine, and I was toying with the idea of rendering the depth buffer. Didn't have the time to have a loop at what it looks like though, so thank you :)


October 30, 2001, 08:41 AM

Dude.. it doesn't matter what they GET clamped to internally. If you set it up to render the z buffer from 1.0 -> 100.0, you now set 100.0 as your MaxZ. So anything < 1.0 won't be displayed and anything > 1.0 will be divided by 100.0... giving you a value (color value) between 0.0 and 1.0. The farther it is, the closer to 1.0 it will get... or you can inverse this by doing 1 - (z/maxz).


Sonic Silicon

October 30, 2001, 08:56 AM

Hmm, I wonder how well this output would work if put into a Random Dot Stereogram (a.k.a. Magic/Hidden Eye pictures.) I remember Bullfrog's Magic Carpet supprting that. A OpenGL wrapper of it would be interesting (if indusive of headaches.)

Rasmus Christian Kaae

October 30, 2001, 10:13 AM

I used 1/z for my softwarerenderer for A) faster sorting and B) to be able to use "incremental" zbuffer-clearing.


October 30, 2001, 11:15 AM

Too late!


October 30, 2001, 11:41 AM

Ok, may be i have misunderstood something or mis-coded it :) Thanks for the help.

This thread contains 29 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.