History

added color modulation

Jeffrey Ventrella committed 13 years ago

Commit 37ff8ae1 parent f83ee1e4 files · +340 −249Browse files

Showing the changes to interface/src/ParticleSystem.cpp (1 of 4 files). Show all

Unified · Split

interface/src/ParticleSystem.cpp+80−15
@@ -20,6 +20,7 @@ const float DEFAULT_EMITTER_RENDER_LENGTH = 0.2f; · 19 unmodified lines
2020
2121 ParticleSystem::ParticleSystem() {
2222
23+ _timer = 0.0f;
2324 _numEmitters = 0;
2425 _numParticles = 0;
2526 _upDirection = glm::vec3(0.0f, 1.0f, 0.0f); // default
@@ -44,6 +45,9 @@ ParticleSystem::ParticleSystem() { · 18 unmodified lines
4445
4546 ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage];
4647
48+ setParticleAttributesToDefault(a);
49+
50+ /*
4751 a->radius = DEFAULT_PARTICLE_RADIUS;
4852 a->color = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
4953 a->bounce = DEFAULT_PARTICLE_BOUNCE;
@@ -60,6 +64,9 @@ ParticleSystem::ParticleSystem() { · 10 unmodified lines
6064 a->collisionPlaneNormal = _upDirection;
6165 a->collisionPlanePosition = glm::vec3(0.0f, 0.0f, 0.0f);
6266 a->usingCollisionPlane = false;
67+ a->modulationAmplitude = 0.0f;
68+ a->modulationStyle = COLOR_MODULATION_STYLE_NULL;
69+ */
6370 }
6471 };
6572
@@ -87,6 +94,8 @@ int ParticleSystem::addEmitter() { · 21 unmodified lines
8794
8895 void ParticleSystem::simulate(float deltaTime) {
8996
97+ _timer += deltaTime;
98+
9099 // emit particles
91100 for (int e = 0; e < _numEmitters; e++) {
92101
@@ -173,6 +182,31 @@ void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleAttributes · 80 unmodified lines
173182 }
174183 }
175184
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+
176210 void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleLifeStage lifeStage, ParticleAttributes attributes) {
177211
178212 ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage];
@@ -193,32 +227,34 @@ void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleLifeStage l · 14 unmodified lines
193227 a->usingCollisionPlane = attributes.usingCollisionPlane;
194228 a->collisionPlanePosition = attributes.collisionPlanePosition;
195229 a->collisionPlaneNormal = attributes.collisionPlaneNormal;
230+ a->modulationAmplitude = attributes.modulationAmplitude;
231+ a->modulationRate = attributes.modulationRate;
232+ a->modulationStyle = attributes.modulationStyle;
196233 }
197234
198235
236+
199237 void ParticleSystem::updateParticle(int p, float deltaTime) {
200238
201− if (_emitter[_particle[p].emitterIndex].particleLifespan > 0.0) {
239+ Emitter myEmitter = _emitter[_particle[p].emitterIndex];
202240
203− Emitter myEmitter = _emitter[_particle[p].emitterIndex];
241+ assert(_particle[p].age <= myEmitter.particleLifespan);
204242
205− assert(_particle[p].age <= myEmitter.particleLifespan);
206−
207− float ageFraction = _particle[p].age / myEmitter.particleLifespan;
208−
209− int lifeStage = (int)( ageFraction * (NUM_PARTICLE_LIFE_STAGES-1) );
243+ float ageFraction = 0.0f;
244+ int lifeStage = 0;
245+ float lifeStageFraction = 0.0f;
210246
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;
212252
213−
253+ // adjust radius
214254 _particle[p].radius
215255 = myEmitter.particleAttributes[lifeStage ].radius * (1.0f - lifeStageFraction)
216256 + myEmitter.particleAttributes[lifeStage+1].radius * lifeStageFraction;
217−
218− _particle[p].color
219− = myEmitter.particleAttributes[lifeStage ].color * (1.0f - lifeStageFraction)
220− + myEmitter.particleAttributes[lifeStage+1].color * lifeStageFraction;
221−
257+
222258 // apply random jitter
223259 float j = myEmitter.particleAttributes[lifeStage].jitter;
224260 _particle[p].velocity +=
@@ -293,8 +329,37 @@ void ParticleSystem::updateParticle(int p, float deltaTime) { · 68 unmodified lines
293329 }
294330 }
295331 }
296− }
332+ }
297333
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+
298363 // do this at the end...
299364 _particle[p].age += deltaTime;
300365 }