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
00025 this->createNewParticles(numInitParticles);
00026
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
00053
00054 bool ParticleSystem::stillAlive() {
00055 return alive;
00056 }
00057
00058
00059
00060
00061 void ParticleSystem::newParticle() {
00062
00063
00064
00065 if(data->hasDead()) {
00066
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
00081
00082 void ParticleSystem::killParticles() {
00083 data->killParticles();
00084 }
00085
00086
00087
00088
00089 void ParticleSystem::evolve() {
00090
00091 if(!paused && alive) {
00092
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
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
00113
00114 void ParticleSystem::addSolver(Solver *s) {
00115 sList.insert(sList.begin(), s);
00116
00117 }
00118
00119 void ParticleSystem::draw() {
00120
00121
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
00139
00140 glEnable(GL_LIGHTING);
00141 glEnable(GL_DEPTH_TEST);
00142 data->draw();
00143 glDisable(GL_DEPTH_TEST);
00144 }
00145 }
00146
00147
00148
00149