Toying With Graphics:
Depth-Of-Field
Overview
My program has a whole host of tiny features, which allow the user to play with the relatively simple environment but the meat of the project has to do with its depth effect.
OpenGL renders everything in perfect focus, near or far, as if the camera had an infinitely small pinhole aperture. In real life this isnšt possible, even with our own eyes. So when we see perfect, crisp images of objects both near and far, we automatically deem the image as bogus or fake.
Along with texture mapping, lighting and perspective viewing, add a depth of field can add another dimension (as it were) of reality.
How To
In order to achieve this effect, we must generate an image such that an object at the focal distance remains relatively unchanged while everything else is blurred as if out of focus. As in a camera, we need to make the blurring more pronounced the further the object/surface is from our specified focal point.
My approach was to render the image several times by offsetting the camera a small jitter distance from the true camera position. I simply moved the camera in a circular pattern in a plane perpendicular to the camerašs gaze vector. Rendering 8 times is probably the most economical but I recommend 16 renderings to get a pretty good image. I use 50 renderings for my best images (as seen below) but remember, the higher quality the more processor time youšll need. Which brings us to this projects performance bottleneck: The Accumulator.
The Accumulator
Since we need to render multiple times, we need to save a rendering before we render the next one. Enter the accumulator, a buffer similar to the drawing buffer we draw to by default except it has higher precision floating point numbers. The accumulator also has some special features than make multiple renderings easier to manage.
Quick Reference
These are some useful (and sometimes necessary) function calls and constants you may want to use:
glutInitDisplayMode( GLUT_RGB |
GLUT_DOUBLE | GLUT_ACCUM );
glAccum( GL_LOAD, [mult_factor] );
GL_LOAD
GL_ACCUM
GL_RETURN
Screen Shots
Sphere in focus
Focus set for near infinity
Donut in focus: I changed the texture mode to help illustrate just how ŗout-of-focus˛ the other objects are.
Notice the far end of the donut is sharper than the near end.
Far objects in focus. Note how fuzzy the nearer objects are especially with the checker board texture.
Near infinite focal depth with white fog.
I wrote in a number of different fogs. I like the green because it reminds me of the sky when a tornado warning is in effect.
Still To Come
Hopefully Išll be able to figure out how to add an executable for Wintel and OS X.
Resources
http://www.sgi.com/software/opengl/advanced98/notes/notes.html
OpenGL Programming Guide 3rd ed. Mason Woo et al.
ISBN: 0-201-60458-2