Fire Special FX
by Seumas McNally

Pixel buffer "burn" or "fire" effects are fairly straightforward. In a nutshell they are a mutated box filter applied every frame to a bitmap with a ramping palette proceeding from black at 0 to some desired "hot" color at 255. For each pixel (skipping a few towards each edge), add the values of the pixel itself, plus the pixels to the right, to the left, and below, then divide by 4, subtract a fade-out constant, and write the value back into the buffer. Notice that the pixel above is ignored, which causes the blurring to propogate upwards, creating an effect that looks like fire or steam. Here is some C sample code (untested and probably incomplete):

Assumptions: Data is unsigned char pointer to bitmap, Width and Height are dimensions, and Pitch is bytes between rows (often just Width). DONTBURN is number of edge pixels to skip. BURNFADE is speed to fade out buffer.

int pixel;
unsigned char *line; //Pointer to start of current line.
for(y = DONTBURN; y < Height - DONTBURN; y++)
{
   line = Data + y * Pitch;
   for(x = DONTBURN; x < Width - DONTBURN; x++)
   {
      pixel = ((line[x] + line[x - 1] + line[x + 1] +
              line[x + Pitch]) >>2) - BURNFADE;
      line[x] = (pixel < 0 ? 0 : pixel); //Don't write < 0.
   }
}

This is highly simplified, and doesn't include handy optimizations such as using 32-bit integer or 64-bit FPU reads to quickly skip over burning large chunks of the buffer that are all 0. That's left as an exercise for the performance programmer. :)

April 13th Update:

Here are a couple of other interesting things that Particle Fire does to achieve its effects. The "sparklies" in the darker flame are produced by simply iterating over each line of the buffer, selecting one random X pixel along that line, testing it to see if it is darker than a certian threshold, and if it is, it brightening it by writing a slightly lighter color into the buffer. The wall of flame along the bottom or top is produced by simply setting pixels in the furthest top or bottom burnable line of the buffer each frame. Two algorithms are used, a mostly random one where each pixel can change up or down by a random amount each frame, and a more ordered 1-dimensional Perlin Noise functions that produces a much "smoother" flame.

Copyright 1998-1999 by Seumas McNally.
No reproduction may be made without the author's written consent.

Courtesy Of Longbow Digital Artists

Discuss this article in the forums


Date this article was posted to GameDev.net: 9/14/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
Fire

© 1999-2011 Gamedev.net. All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!