Showing the changes to interface/src/ParticleSystem.cpp (1 of 4 files). Show all
Unified · Split
| @@ -31,6 +31,7 @@ ParticleSystem::ParticleSystem() { · 30 unmodified lines | ||
| 31 | 31 | _emitter[emitterIndex].particleResolution = DEFAULT_PARTICLE_SPHERE_RESOLUTION; |
| 32 | 32 | _emitter[emitterIndex].particleLifespan = DEFAULT_PARTICLE_LIFESPAN; |
| 33 | 33 | _emitter[emitterIndex].showingBaseParticle = false; |
| 34 | + _emitter[emitterIndex].emitReserve = 0.0; | |
| 34 | 35 | _emitter[emitterIndex].thrust = 0.0f; |
| 35 | 36 | _emitter[emitterIndex].rate = 0.0f; |
| 36 | 37 | _emitter[emitterIndex].currentParticle = 0; |
| @@ -80,6 +81,24 @@ int ParticleSystem::addEmitter() { · 43 unmodified lines | ||
| 80 | 81 | |
| 81 | 82 | void ParticleSystem::simulate(float deltaTime) { |
| 82 | 83 | |
| 84 | + // emit particles | |
| 85 | + for (int e = 0; e < _numEmitters; e++) { | |
| 86 | + | |
| 87 | + assert( e >= 0 ); | |
| 88 | + assert( e <= MAX_EMITTERS ); | |
| 89 | + assert( _emitter[e].rate >= 0 ); | |
| 90 | + | |
| 91 | + _emitter[e].emitReserve += _emitter[e].rate * deltaTime; | |
| 92 | + | |
| 93 | + int numParticlesEmittedThisTime = (int)_emitter[e].emitReserve; | |
| 94 | + | |
| 95 | + _emitter[e].emitReserve -= numParticlesEmittedThisTime; | |
| 96 | + | |
| 97 | + for (int p = 0; p < numParticlesEmittedThisTime; p++) { | |
| 98 | + createParticle(e); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 83 | 102 | // update particles |
| 84 | 103 | for (unsigned int p = 0; p < _numParticles; p++) { |
| 85 | 104 | if (_particle[p].alive) { |
| @@ -92,17 +111,6 @@ void ParticleSystem::simulate(float deltaTime) { · 6 unmodified lines | ||
| 92 | 111 | } |
| 93 | 112 | } |
| 94 | 113 | |
| 95 | −void ParticleSystem::emitNow(int e) { | |
| 96 | − | |
| 97 | − assert( e >= 0 ); | |
| 98 | − assert( e <= MAX_EMITTERS ); | |
| 99 | − assert( _emitter[e].rate >= 0 ); | |
| 100 | − | |
| 101 | − for (int p = 0; p < _emitter[e].rate; p++) { | |
| 102 | − createParticle(e); | |
| 103 | − } | |
| 104 | −} | |
| 105 | − | |
| 106 | 114 | void ParticleSystem::createParticle(int e) { |
| 107 | 115 | |
| 108 | 116 | for (unsigned int p = 0; p < MAX_PARTICLES; p++) { |
| @@ -115,11 +123,12 @@ void ParticleSystem::createParticle(int e) { · 6 unmodified lines | ||
| 115 | 123 | _particle[p].position = _emitter[e].position; |
| 116 | 124 | _particle[p].radius = _emitter[e].particleAttributes[0].radius; |
| 117 | 125 | _particle[p].color = _emitter[e].particleAttributes[0].color; |
| 126 | + _particle[p].previousParticle = -1; | |
| 118 | 127 | |
| 119 | − if (( _particle[_emitter[e].currentParticle].alive) && (_particle[_emitter[e].currentParticle].emitterIndex == e )) { | |
| 120 | − _particle[p].previousParticle = _emitter[e].currentParticle; | |
| 121 | − } else { | |
| 122 | − _particle[p].previousParticle = -1; | |
| 128 | + if ( _particle[_emitter[e].currentParticle].alive) { | |
| 129 | + if (_particle[_emitter[e].currentParticle].emitterIndex == e ) { | |
| 130 | + _particle[p].previousParticle = _emitter[e].currentParticle; | |
| 131 | + } | |
| 123 | 132 | } |
| 124 | 133 | |
| 125 | 134 | _emitter[e].currentParticle = p; |
| @@ -128,7 +137,7 @@ void ParticleSystem::createParticle(int e) { · 2 unmodified lines | ||
| 128 | 137 | |
| 129 | 138 | assert(_numParticles <= MAX_PARTICLES); |
| 130 | 139 | |
| 131 | − return; | |
| 140 | + break; | |
| 132 | 141 | } |
| 133 | 142 | } |
| 134 | 143 | } |
| @@ -139,9 +148,15 @@ void ParticleSystem::killParticle(int p) { · 4 unmodified lines | ||
| 139 | 148 | assert( p < MAX_PARTICLES); |
| 140 | 149 | assert( _numParticles > 0); |
| 141 | 150 | |
| 142 | − _particle[p].alive = false; | |
| 151 | + _particle[p].alive = false; | |
| 152 | + _particle[p].previousParticle = -1; | |
| 153 | + _particle[p].position = _emitter[_particle[p].emitterIndex].position; | |
| 154 | + _particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f); | |
| 155 | + _particle[p].age = 0.0f; | |
| 156 | + _particle[p].emitterIndex = -1; | |
| 157 | + | |
| 143 | 158 | _numParticles --; |
| 144 | −} | |
| 159 | + } | |
| 145 | 160 | |
| 146 | 161 | |
| 147 | 162 | void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleAttributes attributes) { |
| @@ -274,12 +289,16 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 126 unmodified lines | ||
| 274 | 289 | |
| 275 | 290 | void ParticleSystem::killAllParticles() { |
| 276 | 291 | |
| 292 | + for (int e = 0; e < _numEmitters; e++) { | |
| 293 | + _emitter[e].currentParticle = -1; | |
| 294 | + _emitter[e].emitReserve = 0.0f; | |
| 295 | + } | |
| 296 | + | |
| 277 | 297 | for (unsigned int p = 0; p < _numParticles; p++) { |
| 278 | 298 | killParticle(p); |
| 279 | 299 | } |
| 280 | 300 | } |
| 281 | 301 | |
| 282 | − | |
| 283 | 302 | void ParticleSystem::render() { |
| 284 | 303 | |
| 285 | 304 | // render the emitters |
| @@ -340,29 +359,24 @@ void ParticleSystem::renderParticle(int p) { · 54 unmodified lines | ||
| 340 | 359 | glEnd(); |
| 341 | 360 | } |
| 342 | 361 | } else if (_emitter[_particle[p].emitterIndex].particleRenderStyle == PARTICLE_RENDER_STYLE_SPHERE) { |
| 362 | + | |
| 343 | 363 | glPushMatrix(); |
| 344 | 364 | glTranslatef(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z); |
| 345 | 365 | glutSolidSphere(_particle[p].radius, _emitter[_particle[p].emitterIndex].particleResolution, _emitter[_particle[p].emitterIndex].particleResolution); |
| 346 | 366 | glPopMatrix(); |
| 347 | 367 | |
| 348 | − if (SHOW_VELOCITY_TAILS) { | |
| 349 | − glColor4f( _particle[p].color.x, _particle[p].color.y, _particle[p].color.z, 0.5f); | |
| 350 | − glm::vec3 end = _particle[p].position - _particle[p].velocity * 2.0f; | |
| 351 | − glBegin(GL_LINES); | |
| 352 | − glVertex3f(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z); | |
| 353 | − glVertex3f(end.x, end.y, end.z); | |
| 354 | − glEnd(); | |
| 355 | − } | |
| 356 | 368 | } else if (_emitter[_particle[p].emitterIndex].particleRenderStyle == PARTICLE_RENDER_STYLE_RIBBON) { |
| 357 | 369 | |
| 358 | 370 | if (_particle[p].previousParticle != -1) { |
| 359 | − if (_particle[_particle[p].previousParticle].alive) { | |
| 371 | + if ((_particle[p].alive) | |
| 372 | + && (_particle[_particle[p].previousParticle].alive) | |
| 373 | + && (_particle[_particle[p].previousParticle].emitterIndex == _particle[p].emitterIndex )) { | |
| 360 | 374 | |
| 361 | 375 | int pp = _particle[p].previousParticle; |
| 362 | 376 | |
| 363 | 377 | glm::vec3 v = _particle[p].position - _particle[pp].position; |
| 364 | 378 | float distance = glm::length(v); |
| 365 | − | |
| 379 | + | |
| 366 | 380 | if (distance > 0.0f) { |
| 367 | 381 | |
| 368 | 382 | v /= distance; |
| @@ -415,7 +429,7 @@ void ParticleSystem::renderParticle(int p) { · 46 unmodified lines | ||
| 415 | 429 | glVertex3f(ppDown.x, ppDown.y, ppDown.z ); |
| 416 | 430 | |
| 417 | 431 | glEnd(); |
| 418 | − } | |
| 432 | + } | |
| 419 | 433 | } |
| 420 | 434 | } |
| 421 | 435 | } |