The Accumulation Buffer (The Framebuffer) (OpenGL Programming) Part 2

Motion Blur

Similar methods can be used to simulate motion blur, as shown in Plate 7 and Figure 10-2. Suppose your scene has some stationary and some moving objects in it, and you want to make a motion-blurred image extending over a small interval of time. Set up the accumulation buffer in the same way, but instead of jittering the images spatially, jitter them temporally. The entire scene can be made successively dimmer by calling


as the scene is drawn into the accumulation buffer, where decayFactor is a number from 0.0 to 1.0. Smaller numbers for decayFactor cause the object to appear to be moving faster. You can transfer the completed scene, with the object’s current position and a ‚Äúvapor trail" of previous positions, from the accumulation buffer to the standard color buffer with


The image looks correct even if the items move at different speeds or if some of them are accelerated. As before, the more jitter points (temporal, in this case) you use, the better the final image, at least up to the point where you begin to lose resolution because of the finite precision in the accumulation buffer. You can combine motion blur with antialiasing by jittering in both the spatial and temporal domains, but you pay for higher quality with longer rendering times.

 Motion-Blurred Object

Figure 10-2 Motion-Blurred Object

Depth of Field

A photograph made with a camera is in perfect focus only for items lying in a single plane a certain distance from the film. The farther an item is from this plane, the more out of focus it is. The depth of field for a camera is a region about the plane of perfect focus where items are out of focus by a small enough amount.

Under normal conditions, everything you draw with OpenGL is in focus (unless your monitor is bad; in which case, everything is out of focus). The accumulation buffer can be used to approximate what you would see in a photograph, where items are increasingly blurred as their distance from the plane of perfect focus increases. It isn’t an exact simulation of the effects produced in a camera, but the result looks similar to what a camera would produce.

To achieve this result, draw the scene repeatedly using calls with different argument values to glFrustum(). Choose the arguments so that the position of the viewpoint varies slightly around its true position and so that each frustum shares a common rectangle that lies in the plane of perfect focus, as shown in Figure 10-3. The results of all the renderings should be averaged in the usual way using the accumulation buffer.

Plate 10 shows an image of five teapots drawn using the depth-of-field effect. The gold teapot (second from the left) is in focus, and the other teapots get progressively blurrier, depending on their distance from the focal plane (gold teapot). The code used to draw this image is shown in Example 10-7 (which assumes that accPerspective() and accFrustum() are defined as described in Example 10-4). The scene is drawn eight times, each with a slightly jittered viewing volume, by calling accPerspective(). As you recall, with scene antialiasing, the fifth and sixth parameters jitter the viewing volumes in the and y-directions. For the depth-of-field effect, however, you want to jitter the volume while holding it stationary at the focal plane. The focal plane is the depth value defined by the ninth (last) parameter to accPerspective(), which is z = 5.0 in this example. The amount of blur is determined by multiplying the x and y jitter values (seventh and eighth parameters of accPerspective()) by a constant. Determining the constant is not a science; experiment with values until the depth of field is as pronounced as you want. (Note that in Example 10-7, the fifth and sixth parameters to accPerspective() are set to 0.0, so scene antialiasing is turned off.)

Example 10-7 Depth-of-Field Effect: dof.c

Depth-of-Field Effect: dof.c




Jittered Viewing Volume for Depth-of-Field Effects

Figure 10-3 Jittered Viewing Volume for Depth-of-Field Effects










Soft Shadows

To accumulate soft shadows resulting from multiple light sources, render the shadows with one light turned on at a time, and accumulate them together. This can be combined with spatial jittering to antialias the scene at the same time.


If you need to take 9 or 16 samples to antialias an image, you might think that the best choice of points is an equally spaced grid across the pixel. Surprisingly, this is not necessarily true. In fact, sometimes it’s a good idea to take points that lie in adjacent pixels. You might want a uniform distribution or a normalized distribution, clustering toward the center of the pixel. In addition, Table 10-5 shows a few sets of reasonable jittering values to be used for some selected sample counts. Most of the examples in this table are uniformly distributed in the pixel, and all lie within the pixel.




(0.25, 0.75), {0.75, 0.25)


(0.5033922635, 0.8317967229), (0.7806016275, 0.2504380877), (0.2261828938, 0.4131553612)


(0.375, 0.25), (0.125, 0.75), (0.875, 0.25), (0.625, 0.75)


(0.5, 0.5), (0.3, 0.1), (0.7, 0.9), (0.9, 0.3), (0.1, 0.7)


(0.4646464646, 0.4646464646), (0.1313131313, 0.7979797979), (0.5353535353, 0.8686868686), (0.8686868686, 0.5353535353), (0.7979797979, 0.1313131313), (0.2020202020, 0.2020202020)


(0.5625, 0.4375), (0.0625, 0.9375), (0.3125, 0.6875), (0.6875, 0.8125), (0.8125, 0.1875), (0.9375, 0.5625), (0.4375, 0.0625), (0.1875, 0.3125)


(0.5, 0.5), (0.1666666666, 0.9444444444), (0.5, 0.1666666666), (0.5, 0.8333333333), (0.1666666666, 0.2777777777), (0.8333333333, 0.3888888888), (0.1666666666, 0.6111111111), (0.8333333333, 0.7222222222), (0.8333333333, 0.0555555555)

Table 10-5 Sample Jittering Values




(0.4166666666, 0.625), {0.9166666666, 0.875), {0.25, 0.375), (0.4166666666, 0.125), {0.75, 0.125), (0.0833333333, 0.125), (0.75, 0.625), {0.25, 0.875), (0.5833333333, 0.375), {0.9166666666, 0.375), {0.0833333333, 0.625), (0.583333333, 0.875)


{0.375, 0.4375), {0.625, 0.0625), {0.875, 0.1875), (0.125, 0.0625), {0.375, 0.6875), {0.875, 0.4375), {0.625, 0.5625), (0.375, 0.9375), (0.625, 0.3125), (0.125, 0.5625), (0.125, 0.8125), {0.375, 0.1875), {0.875, 0.9375), {0.875, 0.6875), {0.125, 0.3125), {0.625, 0.8125)

Table 10-5 Sample Jittering Values

Next post:

Previous post: