mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 19:29:47 +02:00
Merge pull request #701 from Ventrella/particles
added life stages to particles for effects to change over time
This commit is contained in:
commit
0bc0f3ca7a
4 changed files with 257 additions and 108 deletions
|
@ -3546,6 +3546,7 @@ void Application::updateParticleSystem(float deltaTime) {
|
||||||
|
|
||||||
ParticleSystem::ParticleAttributes attributes;
|
ParticleSystem::ParticleAttributes attributes;
|
||||||
|
|
||||||
|
attributes.radius = 0.01f;
|
||||||
attributes.gravity = 0.0f + 0.05f * sinf( t * 0.52f );
|
attributes.gravity = 0.0f + 0.05f * sinf( t * 0.52f );
|
||||||
attributes.airFriction = 2.5 + 2.0f * sinf( t * 0.32f );
|
attributes.airFriction = 2.5 + 2.0f * sinf( t * 0.32f );
|
||||||
attributes.jitter = 0.05f + 0.05f * sinf( t * 0.42f );
|
attributes.jitter = 0.05f + 0.05f * sinf( t * 0.42f );
|
||||||
|
@ -3562,7 +3563,7 @@ void Application::updateParticleSystem(float deltaTime) {
|
||||||
attributes.gravity = 0.0f;
|
attributes.gravity = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
_particleSystem.setParticleAttributesForEmitter(_coolDemoParticleEmitter, attributes);
|
_particleSystem.setParticleAttributes(_coolDemoParticleEmitter, attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
_particleSystem.setUpDirection(glm::vec3(0.0f, 1.0f, 0.0f));
|
_particleSystem.setUpDirection(glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
|
|
@ -29,6 +29,9 @@ Hand::Hand(Avatar* owningAvatar) :
|
||||||
// initialize all finger particle emitters with an invalid id as default
|
// initialize all finger particle emitters with an invalid id as default
|
||||||
for (int f = 0; f< NUM_FINGERS_PER_HAND; f ++ ) {
|
for (int f = 0; f< NUM_FINGERS_PER_HAND; f ++ ) {
|
||||||
_fingerParticleEmitter[f] = -1;
|
_fingerParticleEmitter[f] = -1;
|
||||||
|
|
||||||
|
//glm::vec4 color(1.0f, 0.6f, 0.0f, 0.5f);
|
||||||
|
//_particleSystem.setEmitterBaseParticle(f, true, 0.012f, color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -176,10 +179,43 @@ void Hand::setLeapHands(const std::vector<glm::vec3>& handPositions,
|
||||||
void Hand::updateFingerParticles(float deltaTime) {
|
void Hand::updateFingerParticles(float deltaTime) {
|
||||||
|
|
||||||
if (!_particleSystemInitialized) {
|
if (!_particleSystemInitialized) {
|
||||||
|
|
||||||
for ( int f = 0; f< NUM_FINGERS_PER_HAND; f ++ ) {
|
for ( int f = 0; f< NUM_FINGERS_PER_HAND; f ++ ) {
|
||||||
|
|
||||||
_fingerParticleEmitter[f] = _particleSystem.addEmitter();
|
_fingerParticleEmitter[f] = _particleSystem.addEmitter();
|
||||||
_particleSystem.setShowingEmitter(_fingerParticleEmitter[f], true);
|
|
||||||
|
ParticleSystem::ParticleAttributes attributes;
|
||||||
|
|
||||||
|
// set attributes for each life stage of the particle:
|
||||||
|
attributes.radius = 0.0f;
|
||||||
|
attributes.color = glm::vec4( 1.0f, 1.0f, 0.5f, 0.5f);
|
||||||
|
attributes.gravity = 0.0f;
|
||||||
|
attributes.airFriction = 0.0f;
|
||||||
|
attributes.jitter = 0.002f;
|
||||||
|
attributes.emitterAttraction = 0.0f;
|
||||||
|
attributes.tornadoForce = 0.0f;
|
||||||
|
attributes.neighborAttraction = 0.0f;
|
||||||
|
attributes.neighborRepulsion = 0.0f;
|
||||||
|
attributes.bounce = 1.0f;
|
||||||
|
attributes.usingCollisionSphere = false;
|
||||||
|
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 0, attributes);
|
||||||
|
|
||||||
|
attributes.radius = 0.01f;
|
||||||
|
attributes.jitter = 0.0f;
|
||||||
|
attributes.gravity = -0.005f;
|
||||||
|
attributes.color = glm::vec4( 1.0f, 0.2f, 0.0f, 0.4f);
|
||||||
|
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 1, attributes);
|
||||||
|
|
||||||
|
attributes.radius = 0.01f;
|
||||||
|
attributes.gravity = 0.0f;
|
||||||
|
attributes.color = glm::vec4( 0.0f, 0.0f, 0.0f, 0.2f);
|
||||||
|
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 2, attributes);
|
||||||
|
|
||||||
|
attributes.radius = 0.02f;
|
||||||
|
attributes.color = glm::vec4( 0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
_particleSystem.setParticleAttributes(_fingerParticleEmitter[f], 3, attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
_particleSystemInitialized = true;
|
_particleSystemInitialized = true;
|
||||||
} else {
|
} else {
|
||||||
// update the particles
|
// update the particles
|
||||||
|
@ -193,25 +229,25 @@ void Hand::updateFingerParticles(float deltaTime) {
|
||||||
|
|
||||||
glm::vec3 particleEmitterPosition = leapPositionToWorldPosition(_fingerTips[f]);
|
glm::vec3 particleEmitterPosition = leapPositionToWorldPosition(_fingerTips[f]);
|
||||||
|
|
||||||
// this aspect is still being designed....
|
glm::vec3 fingerDirection = particleEmitterPosition - leapPositionToWorldPosition(_fingerRoots[f]);
|
||||||
|
float fingerLength = glm::length(fingerDirection);
|
||||||
|
|
||||||
glm::vec3 tilt = glm::vec3
|
if (fingerLength > 0.0f) {
|
||||||
(
|
fingerDirection /= fingerLength;
|
||||||
30.0f * sinf( t * 0.55f ),
|
} else {
|
||||||
0.0f,
|
fingerDirection = IDENTITY_UP;
|
||||||
30.0f * cosf( t * 0.75f )
|
}
|
||||||
);
|
|
||||||
|
|
||||||
glm::quat particleEmitterRotation = glm::quat(glm::radians(tilt));
|
glm::quat particleEmitterRotation = rotationBetween(IDENTITY_UP, fingerDirection);
|
||||||
|
|
||||||
_particleSystem.setEmitterPosition(_fingerParticleEmitter[0], particleEmitterPosition);
|
_particleSystem.setEmitterPosition(_fingerParticleEmitter[f], particleEmitterPosition);
|
||||||
_particleSystem.setEmitterRotation(_fingerParticleEmitter[0], particleEmitterRotation);
|
_particleSystem.setEmitterRotation(_fingerParticleEmitter[f], particleEmitterRotation);
|
||||||
|
|
||||||
float radius = 0.005f;
|
float radius = 0.005f;
|
||||||
glm::vec4 color(1.0f, 0.6f, 0.0f, 0.5f);
|
glm::vec4 color(1.0f, 0.6f, 0.0f, 0.5f);
|
||||||
glm::vec3 velocity(0.0f, 0.005f, 0.0f);
|
glm::vec3 velocity = fingerDirection * 0.002f;
|
||||||
float lifespan = 0.3f;
|
float lifespan = 1.0f;
|
||||||
_particleSystem.emitParticlesNow(_fingerParticleEmitter[0], 1, radius, color, velocity, lifespan);
|
_particleSystem.emitParticlesNow(_fingerParticleEmitter[f], 1, radius, color, velocity, lifespan);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,12 @@
|
||||||
#include "ParticleSystem.h"
|
#include "ParticleSystem.h"
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
|
||||||
|
const float DEFAULT_PARTICLE_RADIUS = 0.01f;
|
||||||
const float DEFAULT_PARTICLE_BOUNCE = 1.0f;
|
const float DEFAULT_PARTICLE_BOUNCE = 1.0f;
|
||||||
const float DEFAULT_PARTICLE_AIR_FRICTION = 2.0f;
|
const float DEFAULT_PARTICLE_AIR_FRICTION = 2.0f;
|
||||||
|
|
||||||
ParticleSystem::ParticleSystem() {
|
ParticleSystem::ParticleSystem() {
|
||||||
|
|
||||||
_timer = 0.0f;
|
|
||||||
_numEmitters = 0;
|
_numEmitters = 0;
|
||||||
_numParticles = 0;
|
_numParticles = 0;
|
||||||
_upDirection = glm::vec3(0.0f, 1.0f, 0.0f); // default
|
_upDirection = glm::vec3(0.0f, 1.0f, 0.0f); // default
|
||||||
|
@ -27,18 +27,31 @@ ParticleSystem::ParticleSystem() {
|
||||||
_emitter[e].right = IDENTITY_RIGHT;
|
_emitter[e].right = IDENTITY_RIGHT;
|
||||||
_emitter[e].up = IDENTITY_UP;
|
_emitter[e].up = IDENTITY_UP;
|
||||||
_emitter[e].front = IDENTITY_FRONT;
|
_emitter[e].front = IDENTITY_FRONT;
|
||||||
_emitter[e].showingEmitter = false;
|
_emitter[e].visible = false;
|
||||||
_emitter[e].particleAttributes.bounce = DEFAULT_PARTICLE_BOUNCE;
|
_emitter[e].baseParticle.alive = false;
|
||||||
_emitter[e].particleAttributes.airFriction = DEFAULT_PARTICLE_AIR_FRICTION;
|
_emitter[e].baseParticle.age = 0.0f;
|
||||||
_emitter[e].particleAttributes.gravity = 0.0f;
|
_emitter[e].baseParticle.lifespan = 0.0f;
|
||||||
_emitter[e].particleAttributes.jitter = 0.0f;
|
_emitter[e].baseParticle.radius = 0.0f;
|
||||||
_emitter[e].particleAttributes.emitterAttraction = 0.0f;
|
_emitter[e].baseParticle.emitterIndex = 0;
|
||||||
_emitter[e].particleAttributes.tornadoForce = 0.0f;
|
_emitter[e].baseParticle.position = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
_emitter[e].particleAttributes.neighborAttraction = 0.0f;
|
_emitter[e].baseParticle.velocity = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
_emitter[e].particleAttributes.neighborRepulsion = 0.0f;
|
|
||||||
_emitter[e].particleAttributes.collisionSphereRadius = 0.0f;
|
|
||||||
_emitter[e].particleAttributes.collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f);
|
for (int s = 0; s<NUM_PARTICLE_LIFE_STAGES; s++) {
|
||||||
_emitter[e].particleAttributes.usingCollisionSphere = false;
|
_emitter[e].particleAttributes[s].radius = DEFAULT_PARTICLE_RADIUS;
|
||||||
|
_emitter[e].particleAttributes[s].color = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
_emitter[e].particleAttributes[s].bounce = DEFAULT_PARTICLE_BOUNCE;
|
||||||
|
_emitter[e].particleAttributes[s].airFriction = DEFAULT_PARTICLE_AIR_FRICTION;
|
||||||
|
_emitter[e].particleAttributes[s].gravity = 0.0f;
|
||||||
|
_emitter[e].particleAttributes[s].jitter = 0.0f;
|
||||||
|
_emitter[e].particleAttributes[s].emitterAttraction = 0.0f;
|
||||||
|
_emitter[e].particleAttributes[s].tornadoForce = 0.0f;
|
||||||
|
_emitter[e].particleAttributes[s].neighborAttraction = 0.0f;
|
||||||
|
_emitter[e].particleAttributes[s].neighborRepulsion = 0.0f;
|
||||||
|
_emitter[e].particleAttributes[s].collisionSphereRadius = 0.0f;
|
||||||
|
_emitter[e].particleAttributes[s].collisionSpherePosition = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
|
_emitter[e].particleAttributes[s].usingCollisionSphere = false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
for (unsigned int p = 0; p < MAX_PARTICLES; p++) {
|
for (unsigned int p = 0; p < MAX_PARTICLES; p++) {
|
||||||
|
@ -52,7 +65,6 @@ ParticleSystem::ParticleSystem() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int ParticleSystem::addEmitter() {
|
int ParticleSystem::addEmitter() {
|
||||||
|
|
||||||
_numEmitters ++;
|
_numEmitters ++;
|
||||||
|
@ -75,11 +87,16 @@ void ParticleSystem::simulate(float deltaTime) {
|
||||||
// update particles
|
// update particles
|
||||||
for (unsigned int p = 0; p < _numParticles; p++) {
|
for (unsigned int p = 0; p < _numParticles; p++) {
|
||||||
if (_particle[p].alive) {
|
if (_particle[p].alive) {
|
||||||
|
if (_particle[p].age > _particle[p].lifespan) {
|
||||||
|
killParticle(p);
|
||||||
|
} else {
|
||||||
updateParticle(p, deltaTime);
|
updateParticle(p, deltaTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ParticleSystem::updateEmitter(int e, float deltaTime) {
|
void ParticleSystem::updateEmitter(int e, float deltaTime) {
|
||||||
|
|
||||||
_emitter[e].front = _emitter[e].rotation * IDENTITY_FRONT;
|
_emitter[e].front = _emitter[e].rotation * IDENTITY_FRONT;
|
||||||
|
@ -106,9 +123,11 @@ void ParticleSystem::createParticle(int e, glm::vec3 position, glm::vec3 velocit
|
||||||
_particle[p].age = 0.0f;
|
_particle[p].age = 0.0f;
|
||||||
_particle[p].position = position;
|
_particle[p].position = position;
|
||||||
_particle[p].velocity = velocity;
|
_particle[p].velocity = velocity;
|
||||||
_particle[p].radius = radius;
|
|
||||||
_particle[p].color = color;
|
_particle[p].color = color;
|
||||||
|
|
||||||
|
_particle[p].radius = _emitter[e].particleAttributes[0].radius;
|
||||||
|
_particle[p].color = _emitter[e].particleAttributes[0].color;
|
||||||
|
|
||||||
_numParticles ++;
|
_numParticles ++;
|
||||||
|
|
||||||
assert(_numParticles <= MAX_PARTICLES);
|
assert(_numParticles <= MAX_PARTICLES);
|
||||||
|
@ -146,45 +165,72 @@ void ParticleSystem::setOrangeBlueColorPalette() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void ParticleSystem::setParticleAttributesForEmitter(int emitterIndex, ParticleAttributes attributes) {
|
void ParticleSystem::setParticleAttributes(int emitterIndex, ParticleAttributes attributes) {
|
||||||
|
|
||||||
_emitter[emitterIndex].particleAttributes.bounce = attributes.bounce;
|
for (int lifeStage = 0; lifeStage < NUM_PARTICLE_LIFE_STAGES; lifeStage ++ ) {
|
||||||
_emitter[emitterIndex].particleAttributes.gravity = attributes.gravity;
|
setParticleAttributes(emitterIndex, lifeStage, attributes);
|
||||||
_emitter[emitterIndex].particleAttributes.airFriction = attributes.airFriction;
|
}
|
||||||
_emitter[emitterIndex].particleAttributes.jitter = attributes.jitter;
|
|
||||||
_emitter[emitterIndex].particleAttributes.emitterAttraction = attributes.emitterAttraction;
|
|
||||||
_emitter[emitterIndex].particleAttributes.tornadoForce = attributes.tornadoForce;
|
|
||||||
_emitter[emitterIndex].particleAttributes.neighborAttraction = attributes.neighborAttraction;
|
|
||||||
_emitter[emitterIndex].particleAttributes.neighborRepulsion = attributes.neighborRepulsion;
|
|
||||||
_emitter[emitterIndex].particleAttributes.usingCollisionSphere = attributes.usingCollisionSphere;
|
|
||||||
_emitter[emitterIndex].particleAttributes.collisionSpherePosition = attributes.collisionSpherePosition;
|
|
||||||
_emitter[emitterIndex].particleAttributes.collisionSphereRadius = attributes.collisionSphereRadius;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ParticleSystem::setParticleAttributes(int emitterIndex, int lifeStage, ParticleAttributes attributes) {
|
||||||
|
|
||||||
|
ParticleAttributes * a = &_emitter[emitterIndex].particleAttributes[lifeStage];
|
||||||
|
|
||||||
|
a->radius = attributes.radius;
|
||||||
|
a->color = attributes.color;
|
||||||
|
a->bounce = attributes.bounce;
|
||||||
|
a->gravity = attributes.gravity;
|
||||||
|
a->airFriction = attributes.airFriction;
|
||||||
|
a->jitter = attributes.jitter;
|
||||||
|
a->emitterAttraction = attributes.emitterAttraction;
|
||||||
|
a->tornadoForce = attributes.tornadoForce;
|
||||||
|
a->neighborAttraction = attributes.neighborAttraction;
|
||||||
|
a->neighborRepulsion = attributes.neighborRepulsion;
|
||||||
|
a->usingCollisionSphere = attributes.usingCollisionSphere;
|
||||||
|
a->collisionSpherePosition = attributes.collisionSpherePosition;
|
||||||
|
a->collisionSphereRadius = attributes.collisionSphereRadius;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ParticleSystem::updateParticle(int p, float deltaTime) {
|
void ParticleSystem::updateParticle(int p, float deltaTime) {
|
||||||
|
|
||||||
_particle[p].age += deltaTime;
|
assert(_particle[p].age <= _particle[p].lifespan);
|
||||||
|
|
||||||
if (_particle[p].age > _particle[p].lifespan) {
|
float ageFraction = _particle[p].age / _particle[p].lifespan;
|
||||||
killParticle(p);
|
|
||||||
|
int lifeStage = (int)( ageFraction * (NUM_PARTICLE_LIFE_STAGES-1) );
|
||||||
|
|
||||||
|
float lifeStageFraction = ageFraction * ( NUM_PARTICLE_LIFE_STAGES - 1 ) - lifeStage;
|
||||||
|
|
||||||
|
/*
|
||||||
|
if ( p == 0 ) {
|
||||||
|
printf( "lifespan = %f ageFraction = %f lifeStage = %d lifeStageFraction = %f\n", _particle[p].lifespan, ageFraction, lifeStage, lifeStageFraction );
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
_particle[p].radius
|
||||||
|
= _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage ].radius * (1.0f - lifeStageFraction)
|
||||||
|
+ _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage+1].radius * lifeStageFraction;
|
||||||
|
|
||||||
|
_particle[p].color
|
||||||
|
= _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage ].color * (1.0f - lifeStageFraction)
|
||||||
|
+ _emitter[_particle[p].emitterIndex].particleAttributes[lifeStage+1].color * lifeStageFraction;
|
||||||
|
|
||||||
Emitter myEmitter = _emitter[_particle[p].emitterIndex];
|
Emitter myEmitter = _emitter[_particle[p].emitterIndex];
|
||||||
|
|
||||||
// apply random jitter
|
// apply random jitter
|
||||||
|
float j = myEmitter.particleAttributes[lifeStage].jitter;
|
||||||
_particle[p].velocity +=
|
_particle[p].velocity +=
|
||||||
glm::vec3
|
glm::vec3
|
||||||
(
|
(
|
||||||
-myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat(),
|
-j * ONE_HALF + j * randFloat(),
|
||||||
-myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat(),
|
-j * ONE_HALF + j * randFloat(),
|
||||||
-myEmitter.particleAttributes.jitter * ONE_HALF + myEmitter.particleAttributes.jitter * randFloat()
|
-j * ONE_HALF + j * randFloat()
|
||||||
) * deltaTime;
|
) * deltaTime;
|
||||||
|
|
||||||
// apply attraction to home position
|
// apply attraction to home position
|
||||||
glm::vec3 vectorToHome = myEmitter.position - _particle[p].position;
|
glm::vec3 vectorToHome = myEmitter.position - _particle[p].position;
|
||||||
_particle[p].velocity += vectorToHome * myEmitter.particleAttributes.emitterAttraction * deltaTime;
|
_particle[p].velocity += vectorToHome * myEmitter.particleAttributes[lifeStage].emitterAttraction * deltaTime;
|
||||||
|
|
||||||
// apply neighbor attraction
|
// apply neighbor attraction
|
||||||
int neighbor = p + 1;
|
int neighbor = p + 1;
|
||||||
|
@ -195,20 +241,20 @@ void ParticleSystem::updateParticle(int p, float deltaTime) {
|
||||||
if ( _particle[neighbor].emitterIndex == _particle[p].emitterIndex) {
|
if ( _particle[neighbor].emitterIndex == _particle[p].emitterIndex) {
|
||||||
glm::vec3 vectorToNeighbor = _particle[p].position - _particle[neighbor].position;
|
glm::vec3 vectorToNeighbor = _particle[p].position - _particle[neighbor].position;
|
||||||
|
|
||||||
_particle[p].velocity -= vectorToNeighbor * myEmitter.particleAttributes.neighborAttraction * deltaTime;
|
_particle[p].velocity -= vectorToNeighbor * myEmitter.particleAttributes[lifeStage].neighborAttraction * deltaTime;
|
||||||
|
|
||||||
float distanceToNeighbor = glm::length(vectorToNeighbor);
|
float distanceToNeighbor = glm::length(vectorToNeighbor);
|
||||||
if (distanceToNeighbor > 0.0f) {
|
if (distanceToNeighbor > 0.0f) {
|
||||||
_particle[neighbor].velocity += (vectorToNeighbor / ( 1.0f + distanceToNeighbor * distanceToNeighbor)) * myEmitter.particleAttributes.neighborRepulsion * deltaTime;
|
_particle[neighbor].velocity += (vectorToNeighbor / ( 1.0f + distanceToNeighbor * distanceToNeighbor)) * myEmitter.particleAttributes[lifeStage].neighborRepulsion * deltaTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply tornado force
|
// apply tornado force
|
||||||
glm::vec3 tornadoDirection = glm::cross(vectorToHome, myEmitter.up);
|
glm::vec3 tornadoDirection = glm::cross(vectorToHome, myEmitter.up);
|
||||||
_particle[p].velocity += tornadoDirection * myEmitter.particleAttributes.tornadoForce * deltaTime;
|
_particle[p].velocity += tornadoDirection * myEmitter.particleAttributes[lifeStage].tornadoForce * deltaTime;
|
||||||
|
|
||||||
// apply air friction
|
// apply air friction
|
||||||
float drag = 1.0 - myEmitter.particleAttributes.airFriction * deltaTime;
|
float drag = 1.0 - myEmitter.particleAttributes[lifeStage].airFriction * deltaTime;
|
||||||
if (drag < 0.0f) {
|
if (drag < 0.0f) {
|
||||||
_particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f);
|
_particle[p].velocity = glm::vec3(0.0f, 0.0f, 0.0f);
|
||||||
} else {
|
} else {
|
||||||
|
@ -216,7 +262,7 @@ void ParticleSystem::updateParticle(int p, float deltaTime) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// apply gravity
|
// apply gravity
|
||||||
_particle[p].velocity -= _upDirection * myEmitter.particleAttributes.gravity * deltaTime;
|
_particle[p].velocity -= _upDirection * myEmitter.particleAttributes[lifeStage].gravity * deltaTime;
|
||||||
|
|
||||||
// update position by velocity
|
// update position by velocity
|
||||||
_particle[p].position += _particle[p].velocity;
|
_particle[p].position += _particle[p].velocity;
|
||||||
|
@ -226,36 +272,66 @@ void ParticleSystem::updateParticle(int p, float deltaTime) {
|
||||||
_particle[p].position.y = _particle[p].radius;
|
_particle[p].position.y = _particle[p].radius;
|
||||||
|
|
||||||
if (_particle[p].velocity.y < 0.0f) {
|
if (_particle[p].velocity.y < 0.0f) {
|
||||||
_particle[p].velocity.y *= -myEmitter.particleAttributes.bounce;
|
_particle[p].velocity.y *= -myEmitter.particleAttributes[lifeStage].bounce;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// collision with sphere
|
// collision with sphere
|
||||||
if (myEmitter.particleAttributes.usingCollisionSphere) {
|
if (myEmitter.particleAttributes[lifeStage].usingCollisionSphere) {
|
||||||
glm::vec3 vectorToSphereCenter = myEmitter.particleAttributes.collisionSpherePosition - _particle[p].position;
|
glm::vec3 vectorToSphereCenter = myEmitter.particleAttributes[lifeStage].collisionSpherePosition - _particle[p].position;
|
||||||
float distanceToSphereCenter = glm::length(vectorToSphereCenter);
|
float distanceToSphereCenter = glm::length(vectorToSphereCenter);
|
||||||
float combinedRadius = myEmitter.particleAttributes.collisionSphereRadius + _particle[p].radius;
|
float combinedRadius = myEmitter.particleAttributes[lifeStage].collisionSphereRadius + _particle[p].radius;
|
||||||
if (distanceToSphereCenter < combinedRadius) {
|
if (distanceToSphereCenter < combinedRadius) {
|
||||||
|
|
||||||
if (distanceToSphereCenter > 0.0f){
|
if (distanceToSphereCenter > 0.0f){
|
||||||
glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter;
|
glm::vec3 directionToSphereCenter = vectorToSphereCenter / distanceToSphereCenter;
|
||||||
_particle[p].position = myEmitter.particleAttributes.collisionSpherePosition - directionToSphereCenter * combinedRadius;
|
_particle[p].position = myEmitter.particleAttributes[lifeStage].collisionSpherePosition - directionToSphereCenter * combinedRadius;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// do this at the end...
|
||||||
|
_particle[p].age += deltaTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ParticleSystem::setCollisionSphere(int e, glm::vec3 position, float radius) {
|
void ParticleSystem::setCollisionSphere(int e, glm::vec3 position, float radius) {
|
||||||
_emitter[e].particleAttributes.usingCollisionSphere = true;
|
|
||||||
_emitter[e].particleAttributes.collisionSpherePosition = position;
|
int lifeStage = 0;
|
||||||
_emitter[e].particleAttributes.collisionSphereRadius = radius;
|
|
||||||
|
_emitter[e].particleAttributes[lifeStage].usingCollisionSphere = true;
|
||||||
|
_emitter[e].particleAttributes[lifeStage].collisionSpherePosition = position;
|
||||||
|
_emitter[e].particleAttributes[lifeStage].collisionSphereRadius = radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ParticleSystem::setEmitterBaseParticle(int emitterIndex, bool showing ) {
|
||||||
|
|
||||||
|
_emitter[emitterIndex].baseParticle.alive = true;
|
||||||
|
_emitter[emitterIndex].baseParticle.emitterIndex = emitterIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ParticleSystem::setEmitterBaseParticle(int emitterIndex, bool showing, float radius, glm::vec4 color ) {
|
||||||
|
|
||||||
|
_emitter[emitterIndex].baseParticle.alive = true;
|
||||||
|
_emitter[emitterIndex].baseParticle.emitterIndex = emitterIndex;
|
||||||
|
_emitter[emitterIndex].baseParticle.radius = radius;
|
||||||
|
_emitter[emitterIndex].baseParticle.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void ParticleSystem::render() {
|
void ParticleSystem::render() {
|
||||||
|
|
||||||
// render the emitters
|
// render the emitters
|
||||||
for (unsigned int e = 0; e < _numEmitters; e++) {
|
for (int e = 0; e < _numEmitters; e++) {
|
||||||
if (_emitter[e].showingEmitter) {
|
|
||||||
|
if (_emitter[e].baseParticle.alive) {
|
||||||
|
glColor4f(_emitter[e].baseParticle.color.r, _emitter[e].baseParticle.color.g, _emitter[e].baseParticle.color.b, _emitter[e].baseParticle.color.a );
|
||||||
|
glPushMatrix();
|
||||||
|
glTranslatef(_emitter[e].position.x, _emitter[e].position.y, _emitter[e].position.z);
|
||||||
|
glutSolidSphere(_emitter[e].baseParticle.radius, 6, 6);
|
||||||
|
glPopMatrix();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_emitter[e].visible) {
|
||||||
renderEmitter(e, 0.2f);
|
renderEmitter(e, 0.2f);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -271,19 +347,49 @@ void ParticleSystem::render() {
|
||||||
void ParticleSystem::renderParticle(int p) {
|
void ParticleSystem::renderParticle(int p) {
|
||||||
|
|
||||||
glColor4f(_particle[p].color.r, _particle[p].color.g, _particle[p].color.b, _particle[p].color.a );
|
glColor4f(_particle[p].color.r, _particle[p].color.g, _particle[p].color.b, _particle[p].color.a );
|
||||||
|
|
||||||
|
if (USE_BILLBOARD_RENDERING) {
|
||||||
|
glm::vec3 cameraPosition = Application::getInstance()->getCamera()->getPosition();
|
||||||
|
glm::vec3 viewVector = _particle[p].position - cameraPosition;
|
||||||
|
float distance = glm::length(viewVector);
|
||||||
|
|
||||||
|
if (distance >= 0.0f) {
|
||||||
|
viewVector /= distance;
|
||||||
|
glm::vec3 up = glm::vec3(viewVector.y, viewVector.z, viewVector.x);
|
||||||
|
glm::vec3 right = glm::vec3(viewVector.z, viewVector.x, viewVector.y);
|
||||||
|
|
||||||
|
glm::vec3 p0 = _particle[p].position - right * _particle[p].radius - up * _particle[p].radius;
|
||||||
|
glm::vec3 p1 = _particle[p].position + right * _particle[p].radius - up * _particle[p].radius;
|
||||||
|
glm::vec3 p2 = _particle[p].position + right * _particle[p].radius + up * _particle[p].radius;
|
||||||
|
glm::vec3 p3 = _particle[p].position - right * _particle[p].radius + up * _particle[p].radius;
|
||||||
|
|
||||||
|
glBegin(GL_TRIANGLES);
|
||||||
|
glVertex3f(p0.x, p0.y, p0.z);
|
||||||
|
glVertex3f(p1.x, p1.y, p1.z);
|
||||||
|
glVertex3f(p2.x, p2.y, p2.z);
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
glBegin(GL_TRIANGLES);
|
||||||
|
glVertex3f(p0.x, p0.y, p0.z);
|
||||||
|
glVertex3f(p2.x, p2.y, p2.z);
|
||||||
|
glVertex3f(p3.x, p3.y, p3.z);
|
||||||
|
glEnd();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
glPushMatrix();
|
glPushMatrix();
|
||||||
glTranslatef(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
glTranslatef(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
||||||
glutSolidSphere(_particle[p].radius, 6, 6);
|
glutSolidSphere(_particle[p].radius, 6, 6);
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
|
||||||
// render velocity lines
|
if (SHOW_VELOCITY_TAILS) {
|
||||||
glColor4f( _particle[p].color.x, _particle[p].color.y, _particle[p].color.z, 0.5f);
|
glColor4f( _particle[p].color.x, _particle[p].color.y, _particle[p].color.z, 0.5f);
|
||||||
glm::vec3 end = _particle[p].position - _particle[p].velocity * 2.0f;
|
glm::vec3 end = _particle[p].position - _particle[p].velocity * 2.0f;
|
||||||
glBegin(GL_LINES);
|
glBegin(GL_LINES);
|
||||||
glVertex3f(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
glVertex3f(_particle[p].position.x, _particle[p].position.y, _particle[p].position.z);
|
||||||
glVertex3f(end.x, end.y, end.z);
|
glVertex3f(end.x, end.y, end.z);
|
||||||
|
|
||||||
glEnd();
|
glEnd();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,16 @@
|
||||||
|
|
||||||
const int MAX_PARTICLES = 5000;
|
const int MAX_PARTICLES = 5000;
|
||||||
const int MAX_EMITTERS = 20;
|
const int MAX_EMITTERS = 20;
|
||||||
|
const int NUM_PARTICLE_LIFE_STAGES = 4;
|
||||||
|
const bool USE_BILLBOARD_RENDERING = false;
|
||||||
|
const bool SHOW_VELOCITY_TAILS = false;
|
||||||
|
|
||||||
class ParticleSystem {
|
class ParticleSystem {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
struct ParticleAttributes {
|
struct ParticleAttributes {
|
||||||
|
float radius;
|
||||||
|
glm::vec4 color;
|
||||||
float bounce;
|
float bounce;
|
||||||
float gravity;
|
float gravity;
|
||||||
float airFriction;
|
float airFriction;
|
||||||
|
@ -34,31 +39,23 @@ public:
|
||||||
ParticleSystem();
|
ParticleSystem();
|
||||||
|
|
||||||
int addEmitter(); // add (create) an emitter and get its unique id
|
int addEmitter(); // add (create) an emitter and get its unique id
|
||||||
void emitParticlesNow(int e, int numParticles, float radius, glm::vec4 color, glm::vec3 velocity, float lifespan);
|
void emitParticlesNow(int emitterIndex, int numParticles, float radius, glm::vec4 color, glm::vec3 velocity, float lifespan);
|
||||||
void simulate(float deltaTime);
|
void simulate(float deltaTime);
|
||||||
void render();
|
void render();
|
||||||
|
void setEmitterBaseParticle(int emitterIndex, bool showing );
|
||||||
|
void setEmitterBaseParticle(int emitterIndex, bool showing, float radius, glm::vec4 color );
|
||||||
|
|
||||||
void setParticleAttributesForEmitter(int emitterIndex, ParticleAttributes attributes);
|
|
||||||
void setOrangeBlueColorPalette(); // apply a nice preset color palette to the particles
|
void setOrangeBlueColorPalette(); // apply a nice preset color palette to the particles
|
||||||
void setUpDirection(glm::vec3 upDirection) {_upDirection = upDirection;} // tell particle system which direction is up
|
void setUpDirection(glm::vec3 upDirection) {_upDirection = upDirection;} // tell particle system which direction is up
|
||||||
|
void setParticleAttributes(int emitterIndex, ParticleAttributes attributes);
|
||||||
void setCollisionSphere(int emitterIndex, glm::vec3 position, float radius); // specify a sphere for the particles to collide with
|
void setParticleAttributes(int emitterIndex, int lifeStage, ParticleAttributes attributes);
|
||||||
void setEmitterPosition(int emitterIndex, glm::vec3 position) { _emitter[emitterIndex].position = position; } // set position of emitter
|
void setCollisionSphere (int emitterIndex, glm::vec3 position, float radius); // specify a sphere for the particles to collide with
|
||||||
void setEmitterRotation(int emitterIndex, glm::quat rotation) { _emitter[emitterIndex].rotation = rotation; } // set rotation of emitter
|
void setEmitterPosition (int emitterIndex, glm::vec3 position) { _emitter[emitterIndex].position = position; } // set position of emitter
|
||||||
void setShowingEmitter (int emitterIndex, bool showing ) { _emitter[emitterIndex].showingEmitter = showing; } // set its visibiity
|
void setEmitterRotation (int emitterIndex, glm::quat rotation) { _emitter[emitterIndex].rotation = rotation; } // set rotation of emitter
|
||||||
|
void setShowingEmitter (int emitterIndex, bool showing ) { _emitter[emitterIndex].visible = showing; } // set its visibiity
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
struct Emitter {
|
|
||||||
glm::vec3 position;
|
|
||||||
glm::quat rotation;
|
|
||||||
glm::vec3 right;
|
|
||||||
glm::vec3 up;
|
|
||||||
glm::vec3 front;
|
|
||||||
bool showingEmitter;
|
|
||||||
ParticleAttributes particleAttributes;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Particle {
|
struct Particle {
|
||||||
bool alive; // is the particle active?
|
bool alive; // is the particle active?
|
||||||
glm::vec3 position; // position
|
glm::vec3 position; // position
|
||||||
|
@ -70,8 +67,18 @@ private:
|
||||||
int emitterIndex; // which emitter created this particle?
|
int emitterIndex; // which emitter created this particle?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct Emitter {
|
||||||
|
glm::vec3 position;
|
||||||
|
glm::quat rotation;
|
||||||
|
glm::vec3 right; // derived from rotation
|
||||||
|
glm::vec3 up; // derived from rotation
|
||||||
|
glm::vec3 front; // derived from rotation
|
||||||
|
bool visible;
|
||||||
|
Particle baseParticle; // a non-physical particle at the emitter position
|
||||||
|
ParticleAttributes particleAttributes[NUM_PARTICLE_LIFE_STAGES]; // the attributes of particles emitted from this emitter
|
||||||
|
};
|
||||||
|
|
||||||
glm::vec3 _upDirection;
|
glm::vec3 _upDirection;
|
||||||
float _timer;
|
|
||||||
Emitter _emitter[MAX_EMITTERS];
|
Emitter _emitter[MAX_EMITTERS];
|
||||||
Particle _particle[MAX_PARTICLES];
|
Particle _particle[MAX_PARTICLES];
|
||||||
int _numParticles;
|
int _numParticles;
|
||||||
|
@ -81,7 +88,6 @@ private:
|
||||||
void updateEmitter(int e, float deltaTime);
|
void updateEmitter(int e, float deltaTime);
|
||||||
void updateParticle(int index, float deltaTime);
|
void updateParticle(int index, float deltaTime);
|
||||||
void createParticle(int e, glm::vec3 position, glm::vec3 velocity, float radius, glm::vec4 color, float lifespan);
|
void createParticle(int e, glm::vec3 position, glm::vec3 velocity, float radius, glm::vec4 color, float lifespan);
|
||||||
//void runSpecialEffectsTest(int e, float deltaTime); // for debugging and artistic exploration
|
|
||||||
void killParticle(int p);
|
void killParticle(int p);
|
||||||
void renderEmitter(int emitterIndex, float size);
|
void renderEmitter(int emitterIndex, float size);
|
||||||
void renderParticle(int p);
|
void renderParticle(int p);
|
||||||
|
|
Loading…
Reference in a new issue