How To: Create a Simple Fireworks Effect in OpenGL and SDL

I’m teaching the programming stream for Diploma in I.T. – Multimedia at the moment, so we’re going to be making some simple games and things over the next couple of months, but to start them off we’re doing a little bit of work on particle effects because they’re simple and fun – you just write a single class which describes the properties and behavior of your particle, and then scale it up by adding a large number of particles to get some pretty nice effects.

So instead of doing a particle fountain, I thought some fireworks might be kinda neat… Take a swiz at the video below and see what you think!

Full C++ source-code available after the break for those of a curious nature =D

Continue reading How To: Create a Simple Fireworks Effect in OpenGL and SDL

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:

    // Clear the draw and depth buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    // Take the contents of the current accumulation buffer and copy it to the colour buffer with each pixel multiplied by a factor
    // i.e. we clear the screen, draw the last frame again (which we saved in the accumulation buffer), then draw our stuff at its new location on top of that
    glAccum(GL_RETURN, 0.95f);
 
    // Clear the accumulation buffer (don't worry, we re-grab the screen into the accumulation buffer after drawing our current frame!)
    glClear(GL_ACCUM_BUFFER_BIT);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
 
    // Translate everything into the screen (z-axis) by -windowDepth
    glTranslatef(0.0f, 0.0f, -windowDepth);
 
    // Apply oscillating rotation to the matrix around the Z-axis.
    // Multiplying by 100 is just a fudge factor to increase the total rotation amount
    glRotatef(cos(deg2rad(Star::rotationAmount)) * 100, 0.0f, 0.0f, 1.0f);
 
    vector<star>::iterator starIter; // This needs to be a standard iterator not a constant iterator so we can modify what's being pointed to!!
 
    // Do the actual drawing...
    for (starIter = stars.begin(); starIter != stars.end(); starIter++)
    {
        // Change the point size to the stars size property
        glPointSize(starIter->getStarSize());
 
        glBegin(GL_POINTS); // This needs to be inside the loop so the PointSize change (above) takes effect
 
            // Set the colour
            glColor3f(starIter->getRedComponent(), starIter->getGreenComponent(), starIter->getBlueComponent());
 
            // Draw the point
            glVertex3f(starIter->getX(), starIter->getY(), starIter->getZ());
 
        glEnd();
 
        // Move the star closer (or reset it if it's too close) ready for the next frame
        starIter->moveStar();
    }
 
    // Swap the buffers so we can see what we've drawn without -watching- it being drawn
    SDL_GL_SwapBuffers();</star>

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…

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