Showing the changes to interface/src/ParticleSystem.cpp (1 of 3 files). Show all
Unified · Split
| @@ -27,6 +27,7 @@ ParticleSystem::ParticleSystem() { · 26 unmodified lines | ||
| 27 | 27 | for (unsigned int emitterIndex = 0; emitterIndex < MAX_EMITTERS; emitterIndex++) { |
| 28 | 28 | |
| 29 | 29 | Emitter * e = &_emitter[emitterIndex]; |
| 30 | + e->active = false; | |
| 30 | 31 | e->position = glm::vec3(0.0f, 0.0f, 0.0f); |
| 31 | 32 | e->previousPosition = glm::vec3(0.0f, 0.0f, 0.0f); |
| 32 | 33 | e->direction = glm::vec3(0.0f, 1.0f, 0.0f); |
| @@ -72,25 +73,16 @@ void ParticleSystem::simulate(float deltaTime) { · 39 unmodified lines | ||
| 72 | 73 | |
| 73 | 74 | _timer += deltaTime; |
| 74 | 75 | |
| 75 | − // emit particles | |
| 76 | − for (int e = 0; e < _numEmitters; e++) { | |
| 77 | − | |
| 78 | − assert(e >= 0); | |
| 79 | − assert(e <= MAX_EMITTERS); | |
| 80 | − assert(_emitter[e].rate >= 0); | |
| 76 | + // update emitters | |
| 77 | + for (int emitterIndex = 0; emitterIndex < _numEmitters; emitterIndex++) { | |
| 78 | + assert(emitterIndex <= MAX_EMITTERS); | |
| 81 | 79 | |
| 82 | − _emitter[e].emitReserve += _emitter[e].rate * deltaTime; | |
| 83 | − _emitter[e].numParticlesEmittedThisTime = (int)_emitter[e].emitReserve; | |
| 84 | − _emitter[e].emitReserve -= _emitter[e].numParticlesEmittedThisTime; | |
| 85 | − | |
| 86 | − for (int p = 0; p < _emitter[e].numParticlesEmittedThisTime; p++) { | |
| 87 | − float timeFraction = (float)p / (float)_emitter[e].numParticlesEmittedThisTime; | |
| 88 | − createParticle(e, timeFraction); | |
| 80 | + if (_emitter[emitterIndex].active) { | |
| 81 | + updateEmitter(emitterIndex, deltaTime); | |
| 89 | 82 | } |
| 90 | 83 | } |
| 91 | − | |
| 92 | − // update particles | |
| 93 | 84 | |
| 85 | + // update particles | |
| 94 | 86 | for (int p = 0; p < MAX_PARTICLES; p++) { |
| 95 | 87 | if (_particle[p].alive) { |
| 96 | 88 | if (_particle[p].age > _emitter[_particle[p].emitterIndex].particleLifespan) { |
| @@ -102,6 +94,20 @@ void ParticleSystem::simulate(float deltaTime) { · 5 unmodified lines | ||
| 102 | 94 | } |
| 103 | 95 | } |
| 104 | 96 | |
| 97 | + | |
| 98 | +void ParticleSystem::updateEmitter(int emitterIndex, float deltaTime) { | |
| 99 | + | |
| 100 | + _emitter[emitterIndex].emitReserve += _emitter[emitterIndex].rate * deltaTime; | |
| 101 | + _emitter[emitterIndex].numParticlesEmittedThisTime = (int)_emitter[emitterIndex].emitReserve; | |
| 102 | + _emitter[emitterIndex].emitReserve -= _emitter[emitterIndex].numParticlesEmittedThisTime; | |
| 103 | + | |
| 104 | + for (int p = 0; p < _emitter[emitterIndex].numParticlesEmittedThisTime; p++) { | |
| 105 | + float timeFraction = (float)p / (float)_emitter[emitterIndex].numParticlesEmittedThisTime; | |
| 106 | + createParticle(emitterIndex, timeFraction); | |
| 107 | + } | |
| 108 | +} | |
| 109 | + | |
| 110 | + | |
| 105 | 111 | void ParticleSystem::createParticle(int e, float timeFraction) { |
| 106 | 112 | |
| 107 | 113 | for (unsigned int p = 0; p < MAX_PARTICLES; p++) { |
| @@ -212,7 +218,6 @@ void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleLifeStage l · 104 unmodified lines | ||
| 212 | 218 | } |
| 213 | 219 | |
| 214 | 220 | |
| 215 | − | |
| 216 | 221 | void ParticleSystem::updateParticle(int p, float deltaTime) { |
| 217 | 222 | |
| 218 | 223 | Emitter myEmitter = _emitter[_particle[p].emitterIndex]; |
| @@ -363,14 +368,16 @@ void ParticleSystem::killAllParticles() { · 144 unmodified lines | ||
| 363 | 368 | void ParticleSystem::render() { |
| 364 | 369 | |
| 365 | 370 | // render the emitters |
| 366 | − for (int e = 0; e < _numEmitters; e++) { | |
| 367 | − | |
| 368 | − if (_emitter[e].showingBaseParticle) { | |
| 369 | − glColor4f(_particle[0].color.r, _particle[0].color.g, _particle[0].color.b, _particle[0].color.a); | |
| 370 | − glPushMatrix(); | |
| 371 | − glTranslatef(_emitter[e].position.x, _emitter[e].position.y, _emitter[e].position.z); | |
| 372 | − glutSolidSphere(_particle[0].radius, _emitter[e].particleResolution, _emitter[e].particleResolution); | |
| 373 | − glPopMatrix(); | |
| 371 | + for (int e = 0; e < MAX_EMITTERS; e++) { | |
| 372 | + | |
| 373 | + if (_emitter[e].active) { | |
| 374 | + if (_emitter[e].showingBaseParticle) { | |
| 375 | + glColor4f(_particle[0].color.r, _particle[0].color.g, _particle[0].color.b, _particle[0].color.a); | |
| 376 | + glPushMatrix(); | |
| 377 | + glTranslatef(_emitter[e].position.x, _emitter[e].position.y, _emitter[e].position.z); | |
| 378 | + glutSolidSphere(_particle[0].radius, _emitter[e].particleResolution, _emitter[e].particleResolution); | |
| 379 | + glPopMatrix(); | |
| 380 | + } | |
| 374 | 381 | } |
| 375 | 382 | |
| 376 | 383 | if (_emitter[e].visible) { |