Academic Tutorials

English | French | Portugese | German | Italian
Home Advertise Payments Recommended Websites Interview Questions FAQs
News Source Codes E-Books Downloads Jobs Web Hosting

Graphics Introduction
Graphics Printmaking
Graphics Photography
Graphics for Web
Computer graphics
Computer graphics II
Graphics C++, SDL
Graphics QuickCG
Graphics Light and Color
Graphics Color Model
Graphics Image
Graphics 2D Drawing
Graphics Flood Fill
Graphics Clipping
Graphics Fractals
Graphics Sierpinski
Graphics Julia
Graphics Fire Effect
Graphics Tunnel Effect
graphics Raycasting
Graphics Raycaster
Graphics Floor & Ceiling
Graphics Sprites
Graphics Filtering
Graphics Fourier Trans
Graphics FT on Images
Graphics DC Component
Graphics Texture Gen..
Graphics Random Noise
Graphics Clouds

HTML Tutorials
HTML Tutorial
XHTML Tutorial
CSS Tutorial
TCP/IP Tutorial
CSS 1.0
CSS 2.0
XML Tutorials
XML Tutorial
XSL Tutorial
XSLT Tutorial
DTD Tutorial
Schema Tutorial
XForms Tutorial
XSL-FO Tutorial
XML DOM Tutorial
XLink Tutorial
XQuery Tutorial
XPath Tutorial
XPointer Tutorial
RDF Tutorial
SOAP Tutorial
WSDL Tutorial
RSS Tutorial
WAP Tutorial
Web Services Tutorial
Browser Scripting
JavaScript Tutorial
VBScript Tutorial
DHTML Tutorial
HTML DOM Tutorial
WMLScript Tutorial
E4X Tutorial
Server Scripting
ASP Tutorial
PERL Tutorial
SQL Tutorial
ADO Tutorial
Apple Script
PL/SQL Tutorial
SQL Server
.NET (dotnet)
.Net Mobile
C# : C Sharp
SVG Tutorial
Flash Tutorial
Media Tutorial
SMIL Tutorial
Photoshop Tutorial
Gimp Tutorial
Gnuplot Programming
GIF Animation Tutorial
Scientific Visualization Tutorial
Web Building
Web Browsers
Web Hosting
W3C Tutorial
Web Building
Web Quality
Web Semantic
Web Careers
Weblogic Tutorial
Web Site Hosting
Domain Name
Java Tutorials
Java Tutorial
JSP Tutorial
Servlets Tutorial
Struts Tutorial
EJB Tutorial
JMS Tutorial
JMX Tutorial
Programming Langauges
C Tutorial
C++ Tutorial
Visual Basic Tutorial
Data Structures Using C
Assembly Language
Forth Programming
Lisp Programming
Data Warehousing
CGI Programming
Emacs Tutorial
Soft Skills
Communication Skills
Time Management
Project Management
Team Work
Leadership Skills
Corporate Communication
Negotiation Skills
Database Tutorials
Operating System
Software Testing
SAP Module
Business Warehousing
SAP Basis
Material Management
Sales & Distribution
Human Resource
Customer Relationship Management
Production and Planning
Networking Programming
Corba Tutorial
Networking Tutorial
Microsoft Office
Microsoft Word
Microsoft Outlook
Microsoft PowerPoint
Microsoft Publisher
Microsoft Excel
Microsoft Front Page
Microsoft InfoPath
Microsoft Access
Financial Accounting
Managerial Accounting
Network Sites

Fire Effect

Previoushome Next

Fire Effect



This is a very short tutorial, about the very classic Fire Effect, and creating a fire color palette for it. Maybe this article will be extended later with fire made by particles or other better looking fires.

The Classic Fire Effect

This fire effect has been used quite often for java demos, oldskool demos, etc... It's very easy to program, and gives very neat looking fire.

First you have to create a palette, typically of 256 colors, but it can be any other amount as well here because we emulate it. With the HSLtoRGB function it's easy to create a good palette, select a hue range from red to yellow, keep saturation at 255, and brightness from 0 to 255 so that more red colors become darker as well. The palette is created to be used with the drawBuffer function, so the RGB colors are stored in a single integer value in BGR order.

To contain the fire, you need a buffer (called fire[][] here) with the same size as the screen. In a graphics mode that uses a palette, this buffer can be the screen itself, but here we use RGB mode and the palette is emulated. The buffer starts with every pixel at 0. Then at the bottom row, give a random value to each pixel, and keep changing the random values every frame. Each frame, calculate each row of pixels based on the two rows below it: the value of each pixel, becomes the sum of the 3 pixels below it (one directly below it, one to the left of this one, and one to the right of this one), and one pixel directly two rows below it. Then divide the sum through a value slightly larger than 4, so that the fire dies out as it rises. The larger the value you divide it through, the lower the flames can rise. For example, if you use *16, /65, which is the same as dividing through 4.0625, the flames will rise higher than if you use *4, /17, which is the same as dividing through 4.25. If you divide it though 4, the fire keeps rising forever, and if you divide it though 5, it dies out way to fast.

This image shows which pixels are included in the calculation: the pixel with the cross is the current pixel, and the 4 green pixels are the ones used to calculate the value of the current pixel.

Do this from top to bottom, if you do the calculations in the wrong order, some pixels will already be recalculated while other pixels that depend on them still have to be calculated. Each new frame may depend only on the values of the previous frame. The top of the screen has y-coordinate 0, and the bottom has y-coordinate h-1.

On the sides of the screen, the pixels have no neighbors to the left or right anymore, and to overcome this, the effect can be made circular so that the rightmost pixel has the leftmost pixel as it's neighbor and vica versa. For this, modulo division through the width of the screen can be used on the x-coordinate of the neighbors.

The version of the effect here isn't very fast at high resolutions, but there's still set a maximum number of frames per second by using the waitFrame function, so that it won't run too fast on faster computers in the future.

Because we have a fire buffer, we can use the drawBuffer function instead of drawing each pixel separately, which is much faster. The buffer that gets drawn isn't the same as the fire array though, because that one has values from 0 to 256, which gives the wrong colors. Instead we create a new buffer and store the RGB color gotten from the palette in it, and then draw this buffer (which also has the name "buffer" here).

Put the following code inside the the main.cpp file. The comments in the code will explain a bit more:

#include <cmath>
#include <string>
#include <vector>
#include <iostream>

#include "quickcg.h"
using namespace QuickCG;

//define the width and height of the screen and the buffers
const int screenWidth = 640;
const int screenHeight = 128;

Uint32 fire[screenWidth][screenHeight];  //this buffer will contain the fire
Uint32 buffer[screenWidth][screenHeight];  //this is the buffer to be drawn to the screen
Uint32 palette[256]; //this will contain the color palette

int main(int argc, char *argv[])
  //set up the screen
  screen(screenWidth, screenHeight, 0, "fire");
  ColorRGB color; //used during palette generation
  float time = getTime(), oldTime; //the time of this and the previous frame, for timing
  //make sure the fire buffer is zero in the beginning
  for(int x = 0; x < w; x++)
  for(int y = 0; y < h; y++)
  fire[x][y] = 0;
  //generate the palette
  for(int x = 0; x < 256; x++)
    //HSLtoRGB is used to generate colors:
    //Hue goes from 0 to 85: red to yellow
    //Saturation is always the maximum: 255
    //Lightness is 0..255 for x=0..128, and 255 for x=128..255
    color = HSLtoRGB(ColorHSL(x / 3, 255, std::min(255, x * 2)));
    //set the palette to the calculated RGB value
    palette[x] = RGBtoINT(color);
  //start the loop (one frame per loop)
    //timing: set to maximum 50 milliseconds per frame = 20 frames per second
    oldTime = time;
    waitFrame(oldTime, 0.05);
    time = getTime();
    //randomize the bottom row of the fire buffer
    for(int x = 0; x < w; x++) fire[x][h - 1] = abs(32768 + rand()) % 256;
    //do the fire calculations for every pixel, from top to bottom
    for(int y = 0; y < h - 1; y++)
    for(int x = 0; x < w; x++)
      fire[x][y] =
        ((fire[(x - 1 + w) % w][(y + 1) % h]
        + fire[(x) % w][(y + 1) % h]
        + fire[(x + 1) % w][(y + 1) % h]
        + fire[(x) % w][(y + 2) % h])
        * 32) / 129;

    //set the drawing buffer to the fire buffer, using the palette colors
    for(int x = 0; x < w; x++)
    for(int y = 0; y < h; y++)
      buffer[x][y] = palette[fire[x][y]];
    //draw the buffer and redraw the screen

If you run it, first everything is black, and the flames rise two pixels every frame until they reached their maximum height. Then they'll still keep animating because the bottom is randomized every frame:

If you don't randomize the bottom of the fire buffer every frame but only at the beginning, the flames won't be animated either, and once they have risen to their maximum height, they'll stay still forever:

By changing parameters, you can tweak the fire, for example this is what I got for changing the calculations to:

fire[x][y]= ((fire[(x - 1) % w][(y + 1) % h]
            + fire[(x) % w][(y + 2) % h]
            + fire[(x + 1) % w][(y + 1) % h]
            + fire[(x) % w][(y + 3) % h])
            * 64) / 257;

Instead of randomizing the bottom row of pixels you can also randomize the edges of objects, for example to make flaming text, ...
© Lode Vandevenne. Reproduced with permission.

Be the first one to comment on this page.

  Graphics eBooks
More Links » »
 Graphics FAQs
More Links » »
 Graphics Interview Questions
More Links » »
 Graphics Articles
More Links » »
 Graphics News
More Links » »
 Graphics Jobs
More Links » »

Share And Enjoy:These icons link to social bookmarking sites where readers can share and discover new web pages.
  • blinkbits
  • BlinkList
  • blogmarks
  • co.mments
  • connotea
  • digg
  • Fark
  • feedmelinks
  • Furl
  • LinkaGoGo
  • Ma.gnolia
  • NewsVine
  • Netvouz
  • RawSugar
  • Reddit
  • scuttle
  • Shadows
  • Simpy
  • Smarking
  • Spurl
  • TailRank
  • Wists
  • YahooMyWeb

Previoushome Next

Keywords: Fire Effect, Graphics, Graphics, Graphics tutorial, Graphics tutorial pdf, history of Graphics, learn Graphics

HTML Quizzes
CSS Quiz
CSS 1.0 Quiz
CSS 2.0 Quiz
XML Quizzes
XML Quiz
XSL Quiz
DTD Quiz
Schema Quiz
XForms Quiz
XLink Quiz
XQuery Quiz
XPath Quiz
XPointer Quiz
RDF Quiz
RSS Quiz
WAP Quiz
Web Services Quiz
Browser Scripting Quizzes
JavaScript Quiz
VBScript Quiz
WMLScript Quiz
E4X Quiz
Server Scripting Quizzes
ASP Quiz
SQL Quiz
ADO Quiz
CVS Quiz
Python Quiz
Apple Script Quiz
SQL Server Quiz
PHP Quiz
.NET (dotnet) Quizzes
Microsoft.Net Quiz
ASP.Net Quiz
.Net Mobile Quiz
C# : C Sharp Quiz
VC++ Quiz
Multimedia Quizzes
SVG Quiz
Flash Quiz
Media Quiz
Photoshop Quiz
Gimp Quiz
Matlab Quiz
Gnuplot Programming Quiz
GIF Animation Quiz
Scientific Visualization Quiz
Graphics Quiz
Web Building Quizzes
Web Browsers Quiz
Web Hosting Quiz
W3C Quiz
Web Building Quiz
Web Quality Quiz
Web Semantic Quiz
Web Careers Quiz
Weblogic Quiz
SEO Quiz
Web Site Hosting Quiz
Domain Name Quiz
Java Quizzes
Java Quiz
JSP Quiz
Servlets Quiz
Struts Quiz
EJB Quiz
JMS Quiz
JMX Quiz
Eclipse Quiz
J2ME Quiz
Programming Langauges Quizzes
C Quiz
C++ Quiz
Visual Basic Quiz
Data Structures Using C Quiz
Cobol Quiz
Assembly Language Quiz
Mainframe Quiz
Forth Programming Quiz
Lisp Programming Quiz
Pascal Quiz
Delphi Quiz
Fortran Quiz
OOPs Quiz
Data Warehousing Quiz
CGI Programming Quiz
Emacs Quiz
Gnome Quiz
ILU Quiz
Soft Skills Quizzes
Communication Skills Quiz
Time Management Quiz
Project Management Quiz
Team Work Quiz
Leadership Skills Quiz
Corporate Communication Quiz
Negotiation Skills Quiz
Database Quizzes
Oracle Quiz
MySQL Quiz
Operating System Quizzes
BSD Quiz
Symbian Quiz
Unix Quiz
Internet Quiz
IP-Masquerading Quiz
IPC Quiz
Software Testing Quizzes
Testing Quiz
Firewalls Quiz
SAP Module Quizzes
ERP Quiz
Business Warehousing Quiz
SAP Basis Quiz
Material Management Quiz
Sales & Distribution Quiz
Human Resource Quiz
Netweaver Quiz
Customer Relationship Management Quiz
Production and Planning Quiz
Networking Programming Quizzes
Corba Quiz
Networking Quiz
Microsoft Office Quizzes
Microsoft Word Quiz
Microsoft Outlook Quiz
Microsoft PowerPoint Quiz
Microsoft Publisher Quiz
Microsoft Excel Quiz
Microsoft Front Page Quiz
Microsoft InfoPath Quiz
Microsoft Access Quiz
Accounting Quizzes
Financial Accounting Quiz
Managerial Accounting Quiz
Testimonials | Contact Us | Link to Us | Site Map
Copyright ? 2008. Academic All rights reserved Privacy Policies | About Us
Our Portals : Academic Tutorials | Best eBooksworld | Beyond Stats | City Details | Interview Questions | Discussions World | Excellent Mobiles | Free Bangalore | Give Me The Code | Gog Logo | Indian Free Ads | Jobs Assist | New Interview Questions | One Stop FAQs | One Stop GATE | One Stop GRE | One Stop IAS | One Stop MBA | One Stop SAP | One Stop Testing | Webhosting in India | Dedicated Server in India | Sirf Dosti | Source Codes World | Tasty Food | Tech Archive | Testing Interview Questions | Tests World | The Galz | Top Masala | Vyom | Vyom eBooks | Vyom International | Vyom Links | Vyoms | Vyom World | Important Websites
Copyright ? 2003-2023 Vyom Technosoft Pvt. Ltd., All Rights Reserved.