Showing the changes to interface/src/ParticleSystem.cpp (1 of 4 files). Show all
Unified · Split
| @@ -20,6 +20,7 @@ const float DEFAULT_EMITTER_RENDER_LENGTH = 0.2f; · 19 unmodified lines | |||
| 20 | 20 | ||
| 21 | ParticleSystem::ParticleSystem() { | 21 | ParticleSystem::ParticleSystem() { |
| 22 | 22 | ||
| 23 | _timer = 0.0f; | ||
| 23 | _numEmitters = 0; | 24 | _numEmitters = 0; |
| 24 | _numParticles = 0; | 25 | _numParticles = 0; |
| 25 | _upDirection = glm::vec3(0.0f, 1.0f, 0.0f); // default | 26 | _upDirection = glm::vec3(0.0f, 1.0f, 0.0f); // default |
| @@ -44,6 +45,9 @@ ParticleSystem::ParticleSystem() { · 18 unmodified lines | |||
| 44 | 45 | ||
| 45 | ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage]; | 46 | ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage]; |
| 46 | 47 | ||
| 48 | setParticleAttributesToDefault(a); | ||
| 49 | |||
| 50 | /* | ||
| 47 | a->radius = DEFAULT_PARTICLE_RADIUS; | 51 | a->radius = DEFAULT_PARTICLE_RADIUS; |
| 48 | a->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f); | 52 | a->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f); |
| 49 | a->bounce = DEFAULT_PARTICLE_BOUNCE; | 53 | a->bounce = DEFAULT_PARTICLE_BOUNCE; |
| @@ -60,6 +64,9 @@ ParticleSystem::ParticleSystem() { · 10 unmodified lines | |||
| 60 | a->collisionPlaneNormal = _upDirection; | 64 | a->collisionPlaneNormal = _upDirection; |
| 61 | a->collisionPlanePosition = glm::vec3(0.0f, 0.0f, 0.0f); | 65 | a->collisionPlanePosition = glm::vec3(0.0f, 0.0f, 0.0f); |
| 62 | a->usingCollisionPlane = false; | 66 | a->usingCollisionPlane = false; |
| 67 | a->modulationAmplitude = 0.0f; | ||
| 68 | a->modulationStyle = COLOR_MODULATION_STYLE_NULL; | ||
| 69 | */ | ||
| 63 | } | 70 | } |
| 64 | }; | 71 | }; |
| 65 | 72 | ||
| @@ -87,6 +94,8 @@ int ParticleSystem::addEmitter() { · 21 unmodified lines | |||
| 87 | 94 | ||
| 88 | void ParticleSystem::simulate(float deltaTime) { | 95 | void ParticleSystem::simulate(float deltaTime) { |
| 89 | 96 | ||
| 97 | _timer += deltaTime; | ||
| 98 | |||
| 90 | // emit particles | 99 | // emit particles |
| 91 | for (int e = 0; e < _numEmitters; e++) { | 100 | for (int e = 0; e < _numEmitters; e++) { |
| 92 | 101 | ||
| @@ -173,6 +182,31 @@ void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleAttributes · 80 unmodified lines | |||
| 173 | } | 182 | } |
| 174 | } | 183 | } |
| 175 | 184 | ||
| 185 | void ParticleSystem::setParticleAttributesToDefault(ParticleAttributes * a) { | ||
| 186 | |||
| 187 | a->radius = DEFAULT_PARTICLE_RADIUS; | ||
| 188 | a->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f); | ||
| 189 | a->bounce = DEFAULT_PARTICLE_BOUNCE; | ||
| 190 | a->airFriction = DEFAULT_PARTICLE_AIR_FRICTION; | ||
| 191 | a->gravity = 0.0f; | ||
| 192 | a->jitter = 0.0f; | ||
| 193 | a->emitterAttraction = 0.0f; | ||
| 194 | a->tornadoForce = 0.0f; | ||
| 195 | a->neighborAttraction = 0.0f; | ||
| 196 | a->neighborRepulsion = 0.0f; | ||
| 197 | a->collisionSphereRadius = 0.0f; | ||
| 198 | a->collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f); | ||
| 199 | a->usingCollisionSphere = false; | ||
| 200 | a->collisionPlaneNormal = _upDirection; | ||
| 201 | a->collisionPlanePosition = glm::vec3(0.0f, 0.0f, 0.0f); | ||
| 202 | a->usingCollisionPlane = false; | ||
| 203 | a->modulationAmplitude = 0.0f; | ||
| 204 | a->modulationRate = 0.0; | ||
| 205 | a->modulationStyle = COLOR_MODULATION_STYLE_NULL; | ||
| 206 | |||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 176 | void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleLifeStage lifeStage, ParticleAttributes attributes) { | 210 | void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleLifeStage lifeStage, ParticleAttributes attributes) { |
| 177 | 211 | ||
| 178 | ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage]; | 212 | ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage]; |
| @@ -193,32 +227,34 @@ void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleLifeStage l · 14 unmodified lines | |||
| 193 | a->usingCollisionPlane = attributes.usingCollisionPlane; | 227 | a->usingCollisionPlane = attributes.usingCollisionPlane; |
| 194 | a->collisionPlanePosition = attributes.collisionPlanePosition; | 228 | a->collisionPlanePosition = attributes.collisionPlanePosition; |
| 195 | a->collisionPlaneNormal = attributes.collisionPlaneNormal; | 229 | a->collisionPlaneNormal = attributes.collisionPlaneNormal; |
| 230 | a->modulationAmplitude = attributes.modulationAmplitude; | ||
| 231 | a->modulationRate = attributes.modulationRate; | ||
| 232 | a->modulationStyle = attributes.modulationStyle; | ||
| 196 | } | 233 | } |
| 197 | 234 | ||
| 198 | 235 | ||
| 236 | |||
| 199 | void ParticleSystem::updateParticle(int p, float deltaTime) { | 237 | void ParticleSystem::updateParticle(int p, float deltaTime) { |
| 200 | 238 | ||
| 201 | if (_emitter[_particle[p].emitterIndex].particleLifespan > 0.0) { | 239 | Emitter myEmitter = _emitter[_particle[p].emitterIndex]; |
| 202 | 240 | ||
| 203 | Emitter myEmitter = _emitter[_particle[p].emitterIndex]; | 241 | assert(_particle[p].age <= myEmitter.particleLifespan); |
| 204 | 242 | ||
| 205 | assert(_particle[p].age <= myEmitter.particleLifespan); | 243 | float ageFraction = 0.0f; |
| 206 | 244 | int lifeStage = 0; | |
| 207 | float ageFraction = _particle[p].age / myEmitter.particleLifespan; | 245 | float lifeStageFraction = 0.0f; |
| 208 | |||
| 209 | int lifeStage = (int)( ageFraction * (NUM_PARTICLE_LIFE_STAGES-1) ); | ||
| 210 | 246 | ||
| 211 | float lifeStageFraction = ageFraction * ( NUM_PARTICLE_LIFE_STAGES - 1 ) - lifeStage; | 247 | if (_emitter[_particle[p].emitterIndex].particleLifespan > 0.0) { |
| 248 | |||
| 249 | ageFraction = _particle[p].age / myEmitter.particleLifespan; | ||
| 250 | lifeStage = (int)( ageFraction * (NUM_PARTICLE_LIFE_STAGES-1) ); | ||
| 251 | lifeStageFraction = ageFraction * ( NUM_PARTICLE_LIFE_STAGES - 1 ) - lifeStage; | ||
| 212 | 252 | ||
| 213 | 253 | // adjust radius | |
| 214 | _particle[p].radius | 254 | _particle[p].radius |
| 215 | = myEmitter.particleAttributes[lifeStage ].radius * (1.0f - lifeStageFraction) | 255 | = myEmitter.particleAttributes[lifeStage ].radius * (1.0f - lifeStageFraction) |
| 216 | + myEmitter.particleAttributes[lifeStage+1].radius * lifeStageFraction; | 256 | + myEmitter.particleAttributes[lifeStage+1].radius * lifeStageFraction; |
| 217 | 257 | ||
| 218 | _particle[p].color | ||
| 219 | = myEmitter.particleAttributes[lifeStage ].color * (1.0f - lifeStageFraction) | ||
| 220 | + myEmitter.particleAttributes[lifeStage+1].color * lifeStageFraction; | ||
| 221 | |||
| 222 | // apply random jitter | 258 | // apply random jitter |
| 223 | float j = myEmitter.particleAttributes[lifeStage].jitter; | 259 | float j = myEmitter.particleAttributes[lifeStage].jitter; |
| 224 | _particle[p].velocity += | 260 | _particle[p].velocity += |
| @@ -293,8 +329,37 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 68 unmodified lines | |||
| 293 | } | 329 | } |
| 294 | } | 330 | } |
| 295 | } | 331 | } |
| 296 | } | 332 | } |
| 297 | 333 | ||
| 334 | // adjust color | ||
| 335 | _particle[p].color | ||
| 336 | = myEmitter.particleAttributes[lifeStage ].color * (1.0f - lifeStageFraction) | ||
| 337 | + myEmitter.particleAttributes[lifeStage+1].color * lifeStageFraction; | ||
| 338 | |||
| 339 | // apply color modulation | ||
| 340 | if (myEmitter.particleAttributes[lifeStage ].modulationAmplitude > 0.0f) { | ||
| 341 | float modulation = 0.0f; | ||
| 342 | float radian = _timer * myEmitter.particleAttributes[lifeStage ].modulationRate * PI_TIMES_TWO; | ||
| 343 | if (myEmitter.particleAttributes[lifeStage ].modulationStyle == COLOR_MODULATION_STYLE_LIGHNTESS_PULSE) { | ||
| 344 | if ( sinf(radian) > 0.0f ) { | ||
| 345 | modulation = myEmitter.particleAttributes[lifeStage].modulationAmplitude; | ||
| 346 | } | ||
| 347 | } else if (myEmitter.particleAttributes[lifeStage].modulationStyle == COLOR_MODULATION_STYLE_LIGHTNESS_WAVE) { | ||
| 348 | float a = myEmitter.particleAttributes[lifeStage].modulationAmplitude; | ||
| 349 | modulation = a * ONE_HALF + sinf(radian) * a * ONE_HALF; | ||
| 350 | } | ||
| 351 | |||
| 352 | _particle[p].color.r += modulation; | ||
| 353 | _particle[p].color.g += modulation; | ||
| 354 | _particle[p].color.b += modulation; | ||
| 355 | _particle[p].color.a += modulation; | ||
| 356 | |||
| 357 | if (_particle[p].color.r > 1.0f) {_particle[p].color.r = 1.0f;} | ||
| 358 | if (_particle[p].color.g > 1.0f) {_particle[p].color.g = 1.0f;} | ||
| 359 | if (_particle[p].color.b > 1.0f) {_particle[p].color.b = 1.0f;} | ||
| 360 | if (_particle[p].color.a > 1.0f) {_particle[p].color.a = 1.0f;} | ||
| 361 | } | ||
| 362 | |||
| 298 | // do this at the end... | 363 | // do this at the end... |
| 299 | _particle[p].age += deltaTime; | 364 | _particle[p].age += deltaTime; |
| 300 | } | 365 | } |