Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

ParticleSystem.cpp

Go to the documentation of this file.
00001 
00006 #include "ParticleSystem.h"
00007 #include <iostream>
00008 using namespace std;
00009 using namespace GraphicsLib;
00010 using namespace ParticleSystems;
00011 
00012 
00013 ParticleSystem::ParticleSystem(int ident, Emitter *emit, DataStructure *dataStruc, Solver *s, int num, int var) {
00014         identify = ident;
00015         alive = true;
00016         paused = false;
00017         position = new Point3(0,0,0);
00018         em = emit;
00019         data = dataStruc;
00020         numInitParticles = num;
00021         varNewParticles = var;
00022         addSolver(s);
00023         
00024         //set up the initial system
00025         this->createNewParticles(numInitParticles);
00026         //set the storage capacity of the dead particles
00027         data->storageCapacity = var;
00028 }
00029 
00030 ParticleSystem::~ParticleSystem() {
00031         delete em;
00032         delete data;
00033 }
00034 
00038 void ParticleSystem::die() {
00039         alive = false;
00040         killParticles();
00041 }
00042 
00043 void ParticleSystem::restart() {
00044         alive = true;
00045 }
00046 
00047 void ParticleSystem::pause() {
00048         paused ^= true;
00049 }
00050 
00051 /*
00052  * Returns if the system is still alive
00053  */
00054 bool ParticleSystem::stillAlive() {
00055         return alive;
00056 }
00057 
00058 /*
00059  * Creates a new particle
00060  */
00061 void ParticleSystem::newParticle() {
00062         //tell the emitter to emit new particles
00063     //cout << "in new Partilce" << endl;
00064         //check if dead Particles are available
00065         if(data->hasDead()) {
00066         //if(data->getDead() !=NULL)
00067         #ifdef __PERFORMANCE__
00068         cout << "ParticleSystem: rebirth" << endl;
00069         #endif
00070         em->reBirth(data->getDead());
00071         } else {
00072                 data->addParticles(em->emittParticles());
00073         #ifdef __PERFORMANCE__
00074         cout << "ParticleSystem: adding" << endl;
00075         #endif
00076         }
00077 }
00078 
00079 /*
00080  * Kill the particles
00081  */
00082 void ParticleSystem::killParticles() {
00083         data->killParticles();
00084 }
00085 
00086 /*
00087 * Evolves the particle system adding new particles accoring to the birthrate
00088 */
00089 void ParticleSystem::evolve() {
00090         //evolve and a new Particles
00091         if(!paused && alive) {
00092                 //Random value for the new emition
00093                 int var = RandomPosNum(varNewParticles);
00094                 #ifdef __PARTICLE_GENERATION__
00095                 std::cout << var << std::endl;
00096                 #endif
00097                 for(int i = 0; i < var; i++) {
00098                         this->newParticle();
00099                 }
00100                 //evolve the existing Particles
00101                 data->evolve();
00102         }
00103 
00104 }
00105 
00106 void ParticleSystem::createNewParticles(int num) {
00107         for(int i = 0; i < num; i++) {
00108                 this->newParticle();
00109         }
00110 }
00111 /*
00112 * Add a new solver
00113 */
00114 void ParticleSystem::addSolver(Solver *s) {
00115         sList.insert(sList.begin(), s);
00116 
00117 }
00118 
00119 void ParticleSystem::draw() {
00120         //if type is IMAGE do the texture binding before
00121         //not once per particle
00122     if(em->type == Emitter::IMAGE) {
00123         glGenTextures(1, &((em->texture).texID));
00124             glBindTexture(GL_TEXTURE_2D, (em->texture).texID);
00125             glTexImage2D(GL_TEXTURE_2D, 0, 3, (em->texture).width, (em->texture).height, 0, GL_RGB,
00126                                         GL_UNSIGNED_BYTE, (em->texture).imageData);
00127             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
00128             glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
00129 
00130             glDisable(GL_LIGHTING);
00131             glEnable(GL_TEXTURE_2D);
00132             glBlendFunc(GL_SRC_ALPHA,GL_ONE);
00133             glEnable(GL_BLEND);
00134         data->draw();
00135         glDisable(GL_TEXTURE_2D);
00136             glDisable(GL_BLEND);
00137     } else {
00138                 //just draw with DEPTH_TEST enabled
00139                 //if we draw image we need it switched off
00140                 glEnable(GL_LIGHTING);
00141         glEnable(GL_DEPTH_TEST);
00142         data->draw();
00143         glDisable(GL_DEPTH_TEST);
00144     }
00145 }
00146 
00147 
00148 
00149 

Generated on Thu Mar 24 11:05:02 2005 for ParticleSystem by  doxygen 1.4.1