OpenGL Programming/Motion Blur



= Introduction =

Apart from color, depth and stencil buffers, most graphics cards also provide a so-called accumulation buffer. These are essentially color buffers with a precision 16 bits or more per color. However, it is not possible to draw directly into the accumulation buffer. Instead, there are only a few operations that work on the entire accumulation buffer at once.


 * : Copy the contents of the normal color buffer, multiplied by, to the accumulation buffer.
 * : Copy the contents of the accumulation buffer, multiplied by, to the color buffer.
 * : Add the contents of the normal color buffer, multiplied by, to the accumulation buffer.
 * : Add  to all the pixels in the accumulation buffer.
 * : Multiply all the pixels in the accumulation buffer by.

In all cases,  is a float. These functions make it easy to calculate the (weighted) sum or average of a number of frames. In this first tutorial, we will see how we can use this to easily add motion blur to almost any OpenGL program.

= Motion blur =

Motion blur happens when objects move so fast (with respect to the camera), or the exposure time of a camera is so long, that during the exposure, some objects have moved more than half a pixel, and therefore appear smeared out. Motion blur is normally not desirable, but it is sometimes hard to avoid with real cameras, because the exposure time depends (for a large part) on the amount of light in the scene. The less light there is, the longer the exposure time, and the stronger the effect of motion blur is. This is one of the reasons that video shot outdoors with a camcorder "feels" different from video shot indoors with the same camera.

= Simulating motion blur =

It is actually very easy to create motion blur effects with the accumulation buffer. The usual way an OpenGL application works is this:

Where the function  takes care of updating the coordinates of moving objects in the scene, and/or of the movement of the camera itself. After the scene is drawn, it is shown on screen with, and optionally you wait for a while to limit the framerate. To add motion blur, you can draw the scene multiple times with smaller timesteps, and only show the average of those:

The best effect is achieved by accumulating a large number of frames, but you will be quickly limited by the power of your GPU. It is best to make the number of frames you accumulate configurable, or otherwise try to automatically determine how many frames you can render within one full.

= Exercises =


 * Add motion blur to any of the previous tutorials. The more interesting ones are the mini-portal and glescraft, where you can walk around the scene.
 * If you are adding post-processing effects to your scene, should you do this before or after applying motion blur?
 * In case your GPU doesn't provide an accumulation buffer, could you recreate glAccum functionality by rendering to textures?