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 | for (unsigned int emitterIndex = 0; emitterIndex < MAX_EMITTERS; emitterIndex++) { | 27 | for (unsigned int emitterIndex = 0; emitterIndex < MAX_EMITTERS; emitterIndex++) { |
| 28 | 28 | ||
| 29 | Emitter * e = &_emitter[emitterIndex]; | 29 | Emitter * e = &_emitter[emitterIndex]; |
| 30 | e->active = false; | ||
| 30 | e->position = glm::vec3(0.0f, 0.0f, 0.0f); | 31 | e->position = glm::vec3(0.0f, 0.0f, 0.0f); |
| 31 | e->previousPosition = glm::vec3(0.0f, 0.0f, 0.0f); | 32 | e->previousPosition = glm::vec3(0.0f, 0.0f, 0.0f); |
| 32 | e->direction = glm::vec3(0.0f, 1.0f, 0.0f); | 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 | _timer += deltaTime; | 74 | _timer += deltaTime; |
| 74 | 75 | ||
| 75 | // emit particles | 76 | // update emitters |
| 76 | for (int e = 0; e < _numEmitters; e++) { | 77 | for (int emitterIndex = 0; emitterIndex < _numEmitters; emitterIndex++) { |
| 77 | 78 | assert(emitterIndex <= MAX_EMITTERS); | |
| 78 | assert(e >= 0); | ||
| 79 | assert(e <= MAX_EMITTERS); | ||
| 80 | assert(_emitter[e].rate >= 0); | ||
| 81 | 79 | ||
| 82 | _emitter[e].emitReserve += _emitter[e].rate * deltaTime; | 80 | if (_emitter[emitterIndex].active) { |
| 83 | _emitter[e].numParticlesEmittedThisTime = (int)_emitter[e].emitReserve; | 81 | updateEmitter(emitterIndex, deltaTime); |
| 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); | ||
| 89 | } | 82 | } |
| 90 | } | 83 | } |
| 91 | |||
| 92 | // update particles | ||
| 93 | 84 | ||
| 85 | // update particles | ||
| 94 | for (int p = 0; p < MAX_PARTICLES; p++) { | 86 | for (int p = 0; p < MAX_PARTICLES; p++) { |
| 95 | if (_particle[p].alive) { | 87 | if (_particle[p].alive) { |
| 96 | if (_particle[p].age > _emitter[_particle[p].emitterIndex].particleLifespan) { | 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 | void ParticleSystem::createParticle(int e, float timeFraction) { | 111 | void ParticleSystem::createParticle(int e, float timeFraction) { |
| 106 | 112 | ||
| 107 | for (unsigned int p = 0; p < MAX_PARTICLES; p++) { | 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 | void ParticleSystem::updateParticle(int p, float deltaTime) { | 221 | void ParticleSystem::updateParticle(int p, float deltaTime) { |
| 217 | 222 | ||
| 218 | Emitter myEmitter = _emitter[_particle[p].emitterIndex]; | 223 | Emitter myEmitter = _emitter[_particle[p].emitterIndex]; |
| @@ -363,14 +368,16 @@ void ParticleSystem::killAllParticles() { · 144 unmodified lines | |||
| 363 | void ParticleSystem::render() { | 368 | void ParticleSystem::render() { |
| 364 | 369 | ||
| 365 | // render the emitters | 370 | // render the emitters |
| 366 | for (int e = 0; e < _numEmitters; e++) { | 371 | for (int e = 0; e < MAX_EMITTERS; e++) { |
| 367 | 372 | ||
| 368 | if (_emitter[e].showingBaseParticle) { | 373 | if (_emitter[e].active) { |
| 369 | glColor4f(_particle[0].color.r, _particle[0].color.g, _particle[0].color.b, _particle[0].color.a); | 374 | if (_emitter[e].showingBaseParticle) { |
| 370 | glPushMatrix(); | 375 | glColor4f(_particle[0].color.r, _particle[0].color.g, _particle[0].color.b, _particle[0].color.a); |
| 371 | glTranslatef(_emitter[e].position.x, _emitter[e].position.y, _emitter[e].position.z); | 376 | glPushMatrix(); |
| 372 | glutSolidSphere(_particle[0].radius, _emitter[e].particleResolution, _emitter[e].particleResolution); | 377 | glTranslatef(_emitter[e].position.x, _emitter[e].position.y, _emitter[e].position.z); |
| 373 | glPopMatrix(); | 378 | glutSolidSphere(_particle[0].radius, _emitter[e].particleResolution, _emitter[e].particleResolution); |
| 379 | glPopMatrix(); | ||
| 380 | } | ||
| 374 | } | 381 | } |
| 375 | 382 | ||
| 376 | if (_emitter[e].visible) { | 383 | if (_emitter[e].visible) { |