Top Banner
Visualization of smoke using particle systems VERONICA GINMAN & KIM MALMROS Bachelor’s essay at CSC Supervisor: Petter Ögren Examiner: Mårten Björkman TRITA apr 2013-12
31

Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Jul 22, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Visualization of smoke using particle systems

VERONICA GINMAN & KIM MALMROS

Bachelor’s essay at CSCSupervisor: Petter Ögren

Examiner: Mårten Björkman

TRITA apr 2013-12

Page 2: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds
Page 3: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

AbstractVisualization is used everywhere in today’s society, frombig screen pictures and video games to commercials andartistic images. The desire to be able to create realisticlooking and behaving phenomena has long existed and to-day’s technology allows us to come close to realistic results.Particle systems is a technique that has long been used tocreate many visualisations for all of these purposes. In thispaper we will describe this simple method of creating realis-tic visualizations. We will look at the results of the simplestof particle systems and compare them to those of the moreadvanced.

Page 4: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Referat

Visualisering används idag inom många områden så somfilmer, datorspel, reklam och även för konstnärliga bilder.Viljan att kunna skapa fenomen som ser verklighetstrognaut och som beter sig på ett realistiskt sätt, har länge fun-nits. Idag har teknologin kommit så pass långt att det nugår att skapa visualiseringar som kommer riktigt nära verk-ligheten. Partikelsystem är en teknik som länge har använtsför att skapa visualiseringar inom alla dessa områden. I denhär rapporten kommer vi beskriva denna enkla metod somanvänds för att skapa verklighetstrogna visualiseringar. Vikommer att analysera resultaten av ett enkelt partikelsy-stem och jämföra dessa med resultat från mer avanceradepartikelsystem.

Page 5: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Statement of collaboration

Veronica: Research, programming, all sections of report, proof reading.Kim: Research, section 2.4.1 and 5.1, proof reading.

Page 6: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Contents

1 Introduction 11.1 Problem statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Background 32.1 History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Real-time vs. pre-rendering . . . . . . . . . . . . . . . . . . . . . . . 32.3 Smoke visualization . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.4 Related work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.4.1 Particle systems and fluid dynamics . . . . . . . . . . . . . . 42.4.2 Particle systems and NURBS surfaces . . . . . . . . . . . . . 6

3 Approach 93.1 The particle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93.2 The particle system . . . . . . . . . . . . . . . . . . . . . . . . . . . 103.3 The main class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

4 Results 11

5 Conclusions 155.1 Improvements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

Bibliography 17

Appendices 18Appendix A . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18

PVector.hpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18PVector.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18Particle.hpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19Particle.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19ParticleSystem.hpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21ParticleSystem.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22System.cpp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

Page 7: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Chapter 1

Introduction

Particle systems is a technique used to model objects that are difficult to model withclassical surface-based modelling. Objects that are dynamic, change over time orthat are fuzzy, fluid or in other ways non-static may be modelled in a more realisticway using particle systems. Particle systems creates a volume of particles withindividual properties rather than a surface with a texture. With this approach it ispossible to change each individual particle’s appearance over time and in this waycreating visualizations that in a realistic way show fire, smoke, water, explosions andwith some alterations even hair and fur. This technique has many useful applicationsand one of these is in special effects. Whether it is big explosions, dust, fires or largecrowds of people, special effects are essential in many films, animated or motioncapture. Films featuring massive explosions and destroyed buildings may want tohave these effects simulated rather than having to do them for real. In this reportwe will be focusing on the modelling of smoke using particle systems. Smoke is innature made up of small smoke particles that move independently from each other,however, since the particles will have mostly the same forces acting on them theywill behave similarly.

1.1 Problem statementThe goal is to create realistic looking and behaving smoke using a particle system.

1

Page 8: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds
Page 9: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Chapter 2

Background

2.1 History

Particle systems have been used to model many different kinds of flock behavioursfrom birds and fish to water, fire and smoke and with some alterations even hair,cloth and fur. In essence particle systems are best applied to phenomena that canbe seen as a volume of tiny objects that interact mainly with their neighbours.

Particle systems are said to have been used in video games as early as in the1960’s[3] to simulate explosions using 2D pixel clouds. In the early 1980’s however,William T. Reeves used particle systems to model fire engulfing a planet in the filmStar Trek II: Wrath of Khan[5], the result of this can be seen in 2.1. Reeves isconsidered to be the inventor of particle systems and he produced a paper on in it1983 explaining how it was done[5]. Reeves continued to develop this idea furtherand has since contributed more to the area of particle systems. Development hascontinued and today most game engines, such as the popular Unreal engine[1],contains a particle system.

2.2 Real-time vs. pre-rendering

The technique of using particle systems is used frequently within the visualizationfield. However, using particle systems when rendering in real-time, such as withmany computer games, differs from that of pre-rendering as is the case when used infilms. Rendering in real-time will require more of the users computer and thereforeparticle systems used in games with real-time rendering may not include as manyparticles as when used with pre-rendering. The number of particles, as well as thenumber of vertices each polygon that makes up these particles has, can greatlyaffect performance and appearance. Since real-time rendering in general is muchmore complicated, computer graphics used in games can not keep up with that of thespecial effects seen in films. In films, especially those featuring real people, specialeffects are often required to be flawless so that the viewer can not tell the differencefrom the real footage and the computer animated visualizations. According to Lutz

3

Page 10: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

CHAPTER 2. BACKGROUND

Figure 2.1. Planet being engulfed by fire in the film Wrath of Khan[5].

Latta[3] real-time implementations of particle systems could handle up to 10 000particles in 2004 however Latta propose a full GPU implementation that can handleup to 1 million particles[3].

2.3 Smoke visualizationVisualization of smoke could be described differently depending on the source ofthe smoke. Smoke emerging from a burning building or a bonfire will behave in away similar to that of clouds or fog because of the large quantities of smoke beingcreated, example in image 2.3. Smoke coming from an explosion or a collapsingbuilding will behave in way that can be described as a huge powerful cloud rapidlyexpanding from the source of explosion and then changing into the appearance offog, example in image 2.2. Furthermore, smoke coming from a cigarette or a candlewill slowly rise, following the flow of air only, and because of the small amounts ofsmoke created it can appear more translucent and fine. Example of incense smokein image 2.4 and a realistic visualization of small scale smoke as seen in image 2.5based on the work of Park et al.[4] described more in section 2.4.2. This small scalesmoke is also probably the most difficult to visualize due to the fact that it requiresa great touch of realism achieved by the use of air flow simulation, more on this insection 2.4.1.

2.4 Related work

2.4.1 Particle systems and fluid dynamics

Smoke particles behaviour are similar to that of light gases. The smoke particlesare light and small and when emitted from a still source such as a candle will have

4

Page 11: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

2.4. RELATED WORK

Figure 2.2. Large scale smoke from explosion, Photo by Charles Levy, Title Atomicbombing of Nagasaki on August 9

no velocity. What causes the behaviour of the particles are mainly the surroundingforces. Unlike birds or fish smoke particles will have no affect on their own movementbut are solely dependent on outside forces pushing and pulling them in differentdirections. Smoke is in general drawn upwards due to the particles light weightnot being heavily affected by gravity and also because smoke tend to be warm andwarm air rises. Because the light weight of the particles they will follow the airstream and thus rise upwards. Smoke, like fluids, will also follow surfaces wheninteracting with objects. To model such behaviour computational fluid dynamicsare often used. Commonly used equations for solving CFD are the Navier-Stokesand Euler equations. Both equations describe how velocities and mass change andassuming viscosity to be zero removing heat conduction[2] from the Navier-Stokesgives the Euler equation. Stam et al.[6] applied the equation into an implementationthat gives stable solutions meaning that even for small time steps the simulationwill not give absurd results.

5

Page 12: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

CHAPTER 2. BACKGROUND

Figure 2.3. Smoke from a bonfire, Photo by Tomas Sienicki, 2005. Smoke frombonfire.

2.4.2 Particle systems and NURBS surfacesPark et al. [4]present an interesting way to visualize small scale smoke such as smokecoming from candles, incense and cigarettes. Particles are used to create streaklinesfrom which they create NURBS surfaces. Opacity maps are then calculated usingthe density of the smoke and its surface contours characteristics and then appliedto the surfaces. As can be seen in 2.5 their approach create very realistic lookingsmoke that captures the behavioural characteristics of small scale smoke and at alow computational cost.

6

Page 13: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

2.4. RELATED WORK

Figure 2.4. Small scale smoke from incense,Source:http://en.wikipedia.org/wiki/Image:Incenselonghua.jpg

Figure 2.5. Result and work approach from Park et al. [4]

7

Page 14: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds
Page 15: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Chapter 3

Approach

Our implementation for creating realistic looking smoke is done in C++ and withthe use of OpenGL. It consists of two parts and a main function to tie them together.The parts are the particle and the particle system. The main function is in a thirdpart called system, in this function there is a infinite while-loop. Each iteration ofthis loop will be referred to as a cycle in the rest of the report.

3.1 The particle

The particle class is the class that contains all the information for the individualparticle. Each particle has colour, size, shape, lifespan, location, acceleration andvelocity associated with it. All of these parameters can be tweaked to improve theappearance and behaviour of the particles. Colour is described by RGBA valueswhich, by using a blend function in OpenGL, means that our particles can becometransparent. RGBA values are red, green and blue values with the A representingthe alpha value. The alpha value ranges from 0, which is completely transparent orinvisible, to 1 which is completely opaque. The colour parameter is currently set togo from opaque, to completely transparent. This is done by letting the alpha valueof each particle depend on the lifespan which means that the particle becomes moretransparent with each cycle. The lifespan of the particles are initiated at a pre-setvalue and then decreased slightly each cycle. The lifespan represents how long aparticle will live and when it reaches zero the particle is marked as dead.

To make the colour appear more realistic it will appear less translucent if severalparticles are drawn on top of each other. To make one single particle’s colour dependon other crossing particles OpenGL provides a useful blending mechanism that weused which allows the colour from each object in the same location to be added tothe final drawing colour.

All the particles are drawn from a starting position which we call the point ofemission or the emitter. Each particle receive a randomized emission point by theparticle system that is dependent on the emitter given to each particle system as aparameter when a new system is initialized.

9

Page 16: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

CHAPTER 3. APPROACH

To make the particles move the location is updated each cycle. The movement israndomized to make the particles behave more realistic than if they were to move instraight lines. The particles will move upwards in general but can fluctuate slightlydownwards.

The shape of the particles are circular OpenGL polygons with 10 vertices each.

3.2 The particle systemThe particle system class is the class where new particles are added to a system.The particle system takes one parameter, a vector, that is the emission point for theparticle system. The particle system is a collection of many particles and containsa vector to keep track of them. A particle can be added to the vector using theaddParticle function. A new particle is added every cycle in our system, to create acontinuous flow of particles. Each particle’s run function is called from the system,when a particle is dead it is removed from the vector of particles and its run functionwill not be called. If the particle vector is empty the system is dead.

3.3 The main classThe main class contains all the information and code necessary for drawing. It isfrom the main class that everything runs and where the particle system is initiated.In each cycle of the while loop a new particle is added to the system and theparticle systems run function is called. Then everything is drawn using OpenGL.If the particle system is dead, the while-loop is broken.

10

Page 17: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Chapter 4

Results

The images below show the output visualizations from our particle system withdifferent values for the particle size and the number of particles emitted each cycle.The simulation was originally done as grey particles on white background but welater did the same simulations as light particles on black background because thatseemed to be the convention. All screenshots were taken after the first emittedparticles had died so that particles of all ages are represented in the visualizations.

11

Page 18: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

CHAPTER 4. RESULTS

Figure 4.1. Results from our implementation. 1. Size 0.02 emitted particles percycle 1 2. Size 0.01 emitted particles per cycle 4 3. Size 0.1 emitted particles percycle 1 4. Size 0.05 emitted particles per cycle 1 5. Size 0.05 emitted particles percycle 2 6. Size 0.02 emitted particles per cycle 2 7. Size 0.005 emitted particles percycle 10.

Figure 4.2. Results when using dark particles on white background

12

Page 19: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Figure 4.3. Cropped image of 4 in 4.1

Figure 4.4. Cropped image of 2.3

13

Page 20: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds
Page 21: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Chapter 5

Conclusions

The visual result of our implemented system can not be considered realisticallylooking or behaving. However, the first thing people would relate our visualizationto would probably be smoke coming from a fire. In visualization the appearance ofan object and how it is perceived by the viewer is the most important thing. Realisticaccuracy is less important but may of course help in creating visualizations thatshould appear realistic. Looking at 4.3 the texture appear quite realistic comparedto 4.4, which is a real photo of smoke.

Comparing our different outputs with each other, the result of having largeparticles images 3, 4, 5 in 4.1 can appear more realistic while zoomed in in termsof texture as seen in 4.4. The small particles look too scattered though this mightbe improved by adding more particles. However, visualizing 10 particles per cycle,such as in 7 in 4.1, and with each particle having a lifespan of 3 000 cycles, resultsin rendering 30 000 particles which is more work than preferred on an ordinary PC.In addition, each particle was made up of 10 vertices making it 300 000 vertices intotal, lowering the amount of vertices could have a positive effect on performance.

5.1 Improvements

Possible improvements would be to apply fluid dynamics so that individual particlesinteract with each other. Stam [6] outlines a computationally inexpensive methodof applying fluid dynamics to simulate smoke which a particle system can be builton top of which would improve the realism.

The shape and colour of the particles can also be improved. While smoke par-ticles do not come in a particular shape in the real world, randomizing between afew irregular shapes might improve appearance. This might be especially good forthose particles that are outside the main collection of particles. By applying dif-ferent textures to the particles we might also make the appearance more realistic.Using a texture that itself has a fuzzy edge and a fading colour towards the edgescould soften the look making edges of particles less apparent.

Offloading some of the operations on the GPU could lead to performance im-

15

Page 22: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

CHAPTER 5. CONCLUSIONS

provements when the number of particles become larger. Since the particles inour current implementation are independent of each other it should be possible toimprove performance by parallelizing their position and transparency updating.

16

Page 23: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

Bibliography

[1] Particle system reference. http://udn.epicgames.com/ThreeParticleSystemReference.html.

[2] John D. Anderson. Computational Fluid Dynamics, The Basics With Applica-tions. Mcgraw-Hill Higher Education, 1995.

[3] Lutz Latta. Building a million-particle system. http://www.gamasutra.com/view/feature/130535/building_a_millionparticle_system.php?page=1, July2004.

[4] Jinho Park, Yeongho Seol, Frederic Cordier, and Junyong Noh. A smoke visu-alization model for capturing surface-like features. 00(0), 2010.

[5] William T. Reeves. Particle systems a technique for modeling a class of fuzzyobjects. 17(3), July 1983.

[6] Jos Stam. Real-time fluid dynamics for games. http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/GDC03.pdf.

17

Page 24: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

BIBLIOGRAPHY

Appendix A

PVector.hpp

#ifndef _PVector#define _PVectorclass PVector {public:

double x;double y;double z;PVector();PVector(double x_, double y_, double z_);void add(const PVector & v);PVector operator=(const PVector & other);

};#endif

PVector.cpp

#include "PVector.hpp"

PVector::PVector(){x = 0;y = 0;z = 0;

};PVector::PVector(double x_, double y_, double z_){

x = x_;y = y_;z = z_;

};

void PVector::add(const PVector & v){x = x + v.x;y = y + v.y;z = z + v.z;

};

PVector PVector::operator=(const PVector & other){x = other.x;y = other.y;z = other.z;return *this;

18

Page 25: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

APPENDIX A

};

Particle.hpp

#ifndef _Particle#define _Particle#include "PVector.hpp"

class Particle{private:

PVector location;PVector velocity;PVector acceleration;float lifespan;

public:static const double size;static const double _pi;static const double angle[10];static const double _cos[10];static const double _sin[10];Particle();Particle(PVector l);void run();void update();void display();bool isDead();double random(double upper, double lower);

};

#endif

Particle.cpp

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<X11/Xlib.h>#include<GL/glx.h>#include<GL/glu.h>#include<cmath>#include "Particle.hpp"

extern GLuint smokeTexture;

19

Page 26: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

BIBLIOGRAPHY

const double Particle::size = 0.02;const double Particle::_pi = 3.141592;const double Particle::angle[10] ={2*_pi*1/10., 2*_pi*2/10., 2*_pi*3/10.,2*_pi*4/10., 2*_pi*5/10., 2*_pi*6/10., 2*_pi*7/10., 2*_pi*8/10., 2*_pi*9/10., 2*_pi};

const double Particle::_cos[10] = {size*cos(angle[0]),size*cos(angle[1]), size*cos(angle[2]), size*cos(angle[3]),size*cos(angle[4]), size*cos(angle[5]), size*cos(angle[6]),size*cos(angle[7]), size*cos(angle[8]), size*cos(angle[9])};

const double Particle::_sin[10] = {size*sin(angle[0]),size*sin(angle[1]), size*sin(angle[2]), size*sin(angle[3]),size*sin(angle[4]), size*sin(angle[5]), size*sin(angle[6]),size*sin(angle[7]), size*sin(angle[8]), size*sin(angle[9])};

Particle::Particle(){}

Particle::Particle(PVector l){location = l;velocity = PVector(0.0,0.001,0);lifespan = 255;

}

void Particle::run(){update();display();

}

double Particle::random(double upper, double lower){double range = (upper-lower);double random_double = lower+(range*((double)rand())/(RAND_MAX));return random_double;

}void Particle::update(){

double x_acc = 0.000034*random(-1,1);double y_acc = 0.00001*random(-1,1);double z_acc = 0.0;//0.0001*random();acceleration = PVector(x_acc,y_acc,z_acc);velocity.add(acceleration);location.add(velocity);lifespan -= 0.2;

}void Particle::display(){

20

Page 27: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

APPENDIX A

double inty = 1.2 - lifespan/255.;glColor4f(0.9,0.9,0.9,inty);//, inty, inty);

//float delta_theta = 0.01;//double _pi = 3.141592;//glBindTexture(GL_TEXTURE_2D, smokeTexture);glBegin( GL_POLYGON ); // OR GL_LINE_LOOPfor(std::size_t i = 0; i<10;++i)

{glVertex3f(location.x + _cos[i], location.y + _sin[i], 0.);

}glEnd();glFlush();

}

bool Particle::isDead(){if(lifespan < 0.0) {

return true;}else{

return false;}

}

ParticleSystem.hpp

#ifndef _ParticleSystem#define _ParticleSystem

#include<vector>#include"PVector.hpp"#include "Particle.hpp"

class ParticleSystem{private:

std::vector<Particle> particles;PVector emitter;

public:ParticleSystem(){};ParticleSystem(PVector l);void addParticle();PVector random();void run();bool isEmpty();

21

Page 28: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

BIBLIOGRAPHY

};

#endif

ParticleSystem.cpp

#include "ParticleSystem.hpp"#include<iostream>#include<cmath>#include<stdlib.h>

ParticleSystem::ParticleSystem(PVector l){emitter = l;

}

void ParticleSystem::addParticle(){

PVector tmp = random();tmp.add(emitter);Particle p(tmp);particles.push_back(p);

}

void ParticleSystem::run(){std::vector<Particle>::iterator it;

for(it = particles.begin(); it < particles.end();){if(!(*it).isDead()){

(*it).run();++it;

}else{

it = particles.erase(it);}

}}

PVector ParticleSystem::random(){

double lower = -0.02;double upper = 0.02;double range = upper-lower;double rand_x = lower+(range*((double)rand())/(RAND_MAX));double rand_y = lower+(range*((double)rand())/(RAND_MAX));

22

Page 29: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

APPENDIX A

return PVector(rand_x,rand_y,0);}

bool ParticleSystem::isEmpty(){if(particles.size() == 0)

return true;else

return false;}

System.cpp

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<X11/Xlib.h>#include<GL/glx.h>#include<GL/glu.h>#include<png.h>#include "Particle.hpp"#include "PVector.hpp"#include "ParticleSystem.hpp"

Display *dpy;Window root;GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };XVisualInfo *vi;Colormap cmap;XSetWindowAttributes swa;Window win;GLXContext glc;XWindowAttributes gwa;XEvent xev;

int main(int argc, char *argv[]) {

PVector emission_location(0,0,0);ParticleSystem ps(emission_location);dpy = XOpenDisplay(NULL);

if(dpy == NULL) {printf("\n\tcannot connect to X server\n\n");exit(0);

23

Page 30: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

BIBLIOGRAPHY

}

root = DefaultRootWindow(dpy);

vi = glXChooseVisual(dpy, 0, att);

if(vi == NULL) {printf("\n\tno appropriate visual found\n\n");exit(0);

}else {

printf("\n\tvisual %p selected\n", (void *)vi->visualid);}

cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);

swa.colormap = cmap;

win = XCreateWindow(dpy, root, 0, 0, 600, 600, 0, vi->depth,InputOutput, vi->visual, CWColormap | CWEventMask, &swa);

XMapWindow(dpy, win);XStoreName(dpy, win, "Smoke");

glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);glXMakeCurrent(dpy, win, glc);

glEnable(GL_BLEND);glDisable(GL_DEPTH_TEST);glBlendFunc (GL_SRC_ALPHA,GL_SRC_ALPHA);XGetWindowAttributes(dpy, win, &gwa);glViewport(0, 0, gwa.width, gwa.height);glClearColor(1.0, 1.0, 1.0, 1.0);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);while(1) {

ps.addParticle();glClearColor(1.0, 1.0, 1.0, 1.0);glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);ps.run();glXSwapBuffers(dpy, win);if(ps.isEmpty())

break;

24

Page 31: Visualizationofsmokeusingparticlesystems676008/FULLTEXT01.pdf · Chapter 2 Background 2.1 History Particlesystemshavebeenusedtomodelmanydifferentkindsofflockbehaviours from birds

APPENDIX A

} /* this closes while(1) { */} /* this is the } which closes int main(int argc, char *argv[]) { */

25