Project, source and executable for Windows XP using glut32.
Project builds with MSVC version 7; might be possible to port
to other versions and other platforms, but I haven't done so.
Please excuse the fragmented code; this is slashed out of a
separate test projects of mine.
Download zip file (about a megabyte)
Your OpenGL implementation needs to support GENERATE_MIPMAP
and TEXTURE_LOD_BIAS to work correctly. Else you'll probably
get asserts, although you can probably take these out and get
"something" on the screen.
This code uses only vanilla OpenGL; it does not require any
fancy vendor-specific extensions, nor the ARB_fragment_shader
extension. That's basically the positive: the negative is that
it looks awful (hey, it's using vanilla OGL and is easy to
understand; you can't have it all in the same project).
Using GENERATE_MIPMAP, TEXTURE_LOD_BIAS and CopyTexSubImage to
render depth of field in two passes. First, render the scene
smaller to generate the "blurred" image, and put a function of
distance-from-camera in the alpha channel. Second, apply this
texture in DECAL mode using projective texturing that maps the
screen projection onto geometry; this is done with TexGen in
This technique is very dependent on the quality of your MIP
map generation, and the size of the blur texture you choose to
To run the sample: in a command-line window, change to the
directory where the .exe lives, and start it with dof.exe.
Move left/right and front/back with arrow keys. Toggle blurring
with the "b" key. Quit with Escape. WARNING: You'll need to be
in 32-bit mode for this to work correctly.
If you start with "-w" it will run in windowed mode.
There are alternate ways of doing depth blur. For example, you
can treat the entire screen as a texture, either using multiple
render targets, or using rectangular textures. You can read the
depth buffer and invert to figure out depth position, and do a
sampling based on that (ATI has a nice ps2.0-based demo doing
this for the Radeon 9700).
Send fan mail to your favourite community volounteer, and send
hate mail to whatever environmental polluter or green house gas
emitter stokes your ire.