How To: Draw Motion-Blur/Trails using the Accumulation Buffer in OpenGL

I read that this is the slow/old-school/borderline-retarded way of going about adding motion blur to your scenes, but I haven’t started learning GLSL yet, so for the time being at least, this works :D

OpenGL Accumulation Buffer Motion-Blur/Trails

The trick is that you have to perform the following actions in this specific order in your drawing function for it to work:

  1. Clear the colour buffer (i.e. the screen) and the depth buffer (really, the colour buffer doesn’t need to be cleared because we’re over-writing it later, but the depth buffer definitely does!).
  2. Copy the accumulation buffer over the top of the colour buffer. (i.e. the contents of the colour buffer gets overwritten with the contents of the accumulation buffer).
  3. Clear the accumulation buffer.
  4. Draw your geometry.
  5. Copy the colour buffer onto the accumulation buffer. (i.e. the contents of the accumulation buffer gets overwritten with the contents of the colour buffer).

The relevant section of code from my drawScene() function is:

Note: See that </star> tag at the end of the above code snippet? That doesn’t exist. It’s an artifact produced by WordPress in combination with the WP-Syntax plugin and is somehow related (I think) to escaping HTML entities and auto-completion of mismatching XHTML. Don’t believe me? Click the title of the post or the read more link below and find the same section of code in the full listing – for some reason it doesn’t appear there… Odd, huh?

Update: Uploaded a screen capture of the program running to YouTube so you can see it in action without having to compile it yourself:

Full source-code available after the jump for those interested…

Phew! I know the example code with just some simple GL_POINTS is a bit dull, but I’m sure you can do something more interesting with the technique – and if you do, I’d love to see the results :)
Flattr this

3 thoughts on “How To: Draw Motion-Blur/Trails using the Accumulation Buffer in OpenGL”

  1. It’s not working for me:

    1. Hmm, that’s odd – you’ll have to debug whether you’re getting an accumulation buffer at all.

      Try getting rid of the four lines saying: SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 16); etc. and see if that helps… Otherwise I’m not sure – it works fine for me.

      1. No I figured out why it wasn’t working, the problem was that I didn’t acquire the Accumulation buffer
        I’m using glut not SDL, and so I’d to acquire it manually :D
        Thank you :D

Leave a Reply

Your email address will not be published.