| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_PARTICLE |
|---|
| 17 | |
|---|
| 18 | #include "particle_system.h" |
|---|
| 19 | |
|---|
| 20 | #include "particle_emitter.h" |
|---|
| 21 | #include "particle_engine.h" |
|---|
| 22 | #include "compiler.h" |
|---|
| 23 | #include "material.h" |
|---|
| 24 | |
|---|
| 25 | using namespace std; |
|---|
| 26 | |
|---|
| 27 | /** |
|---|
| 28 | \brief standard constructor |
|---|
| 29 | \param count the Count of particles in the System |
|---|
| 30 | \param type The Type of the ParticleSystem |
|---|
| 31 | |
|---|
| 32 | \todo this constructor is not jet implemented - do it |
|---|
| 33 | */ |
|---|
| 34 | ParticleSystem::ParticleSystem (unsigned int maxCount, PARTICLE_TYPE type) |
|---|
| 35 | { |
|---|
| 36 | this->setClassName ("ParticleSystem"); |
|---|
| 37 | |
|---|
| 38 | this->maxCount = maxCount; |
|---|
| 39 | this->count = 0; |
|---|
| 40 | this->particleType = type; |
|---|
| 41 | this->particles = NULL; |
|---|
| 42 | this->setConserve(.8); |
|---|
| 43 | this->setLifeSpan(.1); |
|---|
| 44 | this->setInheritSpeed(0); |
|---|
| 45 | this->glID = NULL; |
|---|
| 46 | this->setRadius(1.0, 1.0, 0.0); |
|---|
| 47 | this->setType(PARTICLE_SPRITE, 1); |
|---|
| 48 | ParticleEngine::getInstance()->addSystem(this); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | \brief standard deconstructor |
|---|
| 54 | |
|---|
| 55 | */ |
|---|
| 56 | ParticleSystem::~ParticleSystem() |
|---|
| 57 | { |
|---|
| 58 | // delete what has to be deleted here |
|---|
| 59 | ParticleEngine::getInstance()->removeSystem(this); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | void ParticleSystem::setType(PARTICLE_TYPE particleType, int count) |
|---|
| 64 | { |
|---|
| 65 | this->particleType = particleType; |
|---|
| 66 | this->dialectCount = count; |
|---|
| 67 | if (glID != NULL) |
|---|
| 68 | delete glID; |
|---|
| 69 | |
|---|
| 70 | glID = new GLuint[count]; |
|---|
| 71 | for (int i = 0; i< count; i++) |
|---|
| 72 | glID[i] = 0; |
|---|
| 73 | |
|---|
| 74 | glID[0] = glGenLists(count); |
|---|
| 75 | |
|---|
| 76 | material = new Material("transperencyMap"); |
|---|
| 77 | material->setDiffuseMap("pictures/radialTransparency.jpg"); |
|---|
| 78 | |
|---|
| 79 | glNewList(glID[0], GL_COMPILE); |
|---|
| 80 | glBegin(GL_TRIANGLE_STRIP); |
|---|
| 81 | glTexCoord2f(1, 1); |
|---|
| 82 | glVertex3f(0.0, 1.0, 1.0); |
|---|
| 83 | glTexCoord2f(1, 0); |
|---|
| 84 | glVertex3f(0.0, 1.0, 0.0); |
|---|
| 85 | glTexCoord2f(0, 1); |
|---|
| 86 | glVertex3f(0.0, 0.0, 1.0); |
|---|
| 87 | glTexCoord2f(0, 0); |
|---|
| 88 | glVertex3f(0.0, 0.0, 0.0); |
|---|
| 89 | glEnd(); |
|---|
| 90 | glEndList(); |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | // setting properties |
|---|
| 97 | void ParticleSystem::setMaterial(Material* material) |
|---|
| 98 | { |
|---|
| 99 | this->material = material; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | /** |
|---|
| 105 | \brief how much of the speed from the ParticleEmitter should flow onto the ParticleSystem |
|---|
| 106 | \param value a Value between zero and one |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | if you want to change the value of this variable during emission time (to make it more dynamic) |
|---|
| 110 | you may want to use the animation class |
|---|
| 111 | */ |
|---|
| 112 | void ParticleSystem::setInheritSpeed(float value) |
|---|
| 113 | { |
|---|
| 114 | if (unlikely(value > 1.0)) |
|---|
| 115 | this->inheritSpeed = 1; |
|---|
| 116 | else if (unlikely(value < 0.0)) |
|---|
| 117 | this->inheritSpeed = 0; |
|---|
| 118 | else |
|---|
| 119 | this->inheritSpeed = value; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | void ParticleSystem::setLifeSpan(float lifeSpan, float randomLifeSpan) |
|---|
| 124 | { |
|---|
| 125 | this->lifeSpan = lifeSpan; |
|---|
| 126 | this->randomLifeSpan = randomLifeSpan; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | void ParticleSystem::setRadius(float startRadius, float endRadius, float randomStartRadius, float randomEndRadius) |
|---|
| 130 | { |
|---|
| 131 | this->startRadius = startRadius; |
|---|
| 132 | this->endRadius = endRadius; |
|---|
| 133 | this->randomStartRadius = randomStartRadius; |
|---|
| 134 | this->randomEndRadius = randomEndRadius; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | void ParticleSystem::setConserve(float conserve) |
|---|
| 138 | { |
|---|
| 139 | if (conserve > 1.0) |
|---|
| 140 | this->conserve = 1.0; |
|---|
| 141 | else if (conserve < 0.0) |
|---|
| 142 | this->conserve = 0.0; |
|---|
| 143 | else |
|---|
| 144 | this->conserve = conserve; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | void ParticleSystem::tick(float dt) |
|---|
| 150 | { |
|---|
| 151 | Particle* tickPart = particles; // the particle to Tick |
|---|
| 152 | Particle* prevPart = NULL; // |
|---|
| 153 | while (likely(tickPart != NULL)) |
|---|
| 154 | { |
|---|
| 155 | |
|---|
| 156 | tickPart->position = tickPart->position + tickPart->velocity; |
|---|
| 157 | tickPart->radius += tickPart->radiusIt * dt; |
|---|
| 158 | |
|---|
| 159 | // many more to come |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | |
|---|
| 163 | if (this->conserve < 1.0) |
|---|
| 164 | tickPart->velocity = tickPart->velocity * this->conserve; |
|---|
| 165 | // find out if we have to delete tickPart |
|---|
| 166 | if ((tickPart->timeToLive -= dt) <= 0) |
|---|
| 167 | { |
|---|
| 168 | // remove the particle from the list |
|---|
| 169 | if (likely(prevPart != NULL)) |
|---|
| 170 | { |
|---|
| 171 | prevPart->next = tickPart->next; |
|---|
| 172 | delete tickPart; |
|---|
| 173 | tickPart = prevPart->next; |
|---|
| 174 | } |
|---|
| 175 | else |
|---|
| 176 | { |
|---|
| 177 | prevPart = NULL; |
|---|
| 178 | this->particles = tickPart->next; |
|---|
| 179 | delete tickPart; |
|---|
| 180 | tickPart = this->particles; |
|---|
| 181 | } |
|---|
| 182 | --this->count; |
|---|
| 183 | } |
|---|
| 184 | else |
|---|
| 185 | { |
|---|
| 186 | prevPart = tickPart; |
|---|
| 187 | tickPart = tickPart->next; |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | void ParticleSystem::draw(void) |
|---|
| 193 | { |
|---|
| 194 | // material->select(); |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | glMatrixMode(GL_MODELVIEW); |
|---|
| 198 | |
|---|
| 199 | Particle* drawPart = particles; |
|---|
| 200 | if (likely(drawPart != NULL)) |
|---|
| 201 | { |
|---|
| 202 | glBegin(GL_POINTS); |
|---|
| 203 | while (likely(drawPart != NULL)) |
|---|
| 204 | { |
|---|
| 205 | // draw in DOT mode |
|---|
| 206 | glPushMatrix(); |
|---|
| 207 | glTranslatef(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
|---|
| 208 | glScalef(drawPart->radius, drawPart->radius, drawPart->radius); |
|---|
| 209 | glCallList(*this->glID); |
|---|
| 210 | |
|---|
| 211 | // glVertex3f(drawPart->position.x, drawPart->position.y, drawPart->position.z); |
|---|
| 212 | drawPart = drawPart->next; |
|---|
| 213 | glPopMatrix(); |
|---|
| 214 | } |
|---|
| 215 | glEnd(); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | |
|---|
| 220 | void ParticleSystem::addParticle(Vector position, Vector velocity, unsigned int data) |
|---|
| 221 | { |
|---|
| 222 | if (this->count <= this->maxCount) |
|---|
| 223 | { |
|---|
| 224 | // if it is the first Particle |
|---|
| 225 | if (unlikely(particles == NULL)) |
|---|
| 226 | { |
|---|
| 227 | this->particles = new Particle; |
|---|
| 228 | this->particles->next = NULL; |
|---|
| 229 | } |
|---|
| 230 | // filling the List from the beginning |
|---|
| 231 | else |
|---|
| 232 | { |
|---|
| 233 | Particle* tmpPart = new Particle; |
|---|
| 234 | tmpPart->next = this->particles; |
|---|
| 235 | this->particles = tmpPart; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | particles->timeToLive = this->lifeSpan + (float)(random()/RAND_MAX)* this->randomLifeSpan; |
|---|
| 239 | particles->position = position; |
|---|
| 240 | particles->velocity = velocity; |
|---|
| 241 | // particle->rotation = ; //! \todo rotation is once again something to be done. |
|---|
| 242 | particles->mass = this->initialMass + (random()/RAND_MAX -.5)* this->randomInitialMass; |
|---|
| 243 | particles->radius = this->startRadius + (random()/RAND_MAX-.5)*this->randomStartRadius; |
|---|
| 244 | |
|---|
| 245 | particles->radiusIt = (this->endRadius + (random()/RAND_MAX-.5)*this->randomEndRadius - particles->radius) / particles->timeToLive; |
|---|
| 246 | |
|---|
| 247 | ++this->count; |
|---|
| 248 | } |
|---|
| 249 | else |
|---|
| 250 | PRINTF(4)("maximum count of particles reached not adding any more\n"); |
|---|
| 251 | } |
|---|
| 252 | |
|---|