From a13e9d32ec791a83f9bfb35b0d56ff0336cc1000 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 29 Apr 2014 11:51:57 +0200 Subject: [PATCH] Generalized interpolation functions. (orientation spectrum not working properly (quaterions...)) --- src/Components/Particle.h | 3 +- src/Components/ParticleEmitter.h | 3 +- src/GameWorld.cpp | 2 +- src/Systems/ParticleSystem.cpp | 93 +++++++++++++++++--------------- src/Systems/ParticleSystem.h | 9 ++-- 5 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/Components/Particle.h b/src/Components/Particle.h index a4725b3..d08b1a7 100644 --- a/src/Components/Particle.h +++ b/src/Components/Particle.h @@ -12,10 +12,11 @@ namespace Components struct Particle : Component { std::vector ColorSpectrum; - std::vector ScaleSpectrum; + std::vector ScaleSpectrum; double LifeTime; std::vector VelocitySpectrum; std::vector AngularVelocitySpectrum; + std::vector OrientationSpectrum; //Keep? virtual Particle* Clone() const override { return new Particle(*this); } }; diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 6ddd302..ae66fca 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -25,11 +25,12 @@ struct ParticleEmitter : Component float SpawnFrequency; int SpawnCount; std::vector ColorSpectrum; - std::vector ScaleSpectrum; + std::vector ScaleSpectrum; float SpreadAngle; double LifeTime; std::vector VelocitySpectrum; std::vector AngularVelocitySpectrum; + std::vector OrientationSpectrum; //Keep? virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index f63c86a..7d25c46 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -103,7 +103,7 @@ void GameWorld::Initialize() emitter->LifeTime = 4; emitter->SpawnCount = 1; emitter->SpreadAngle = glm::pi()/20; - emitter->SpawnFrequency = 0.008; + emitter->SpawnFrequency = 1.008; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 2fd1cfe..06b2397 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -46,17 +46,34 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID } else { - //FIX: calculate once + // FIX: calculate once float timeProgress = timeLived / particleComponent->LifeTime; - //ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); + // ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); + // Scale interpolation if(particleComponent->ScaleSpectrum.size() > 1) - ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + VectorInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + // Velocity interpolation if(particleComponent->VelocitySpectrum.size() > 1) - VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); + VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); + // Angular velocity interpolation if(particleComponent->AngularVelocitySpectrum.size() > 1) - AngularVelocityInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); + ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); + //Angular velocity interpolation + if(particleComponent->OrientationSpectrum.size() > 1) + VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation); - transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, glm::vec3(0, 0, 1)); +// glm::vec3 v1 = particleComponent->OrientationSpectrum[0]; +// glm::vec3 v2 = it->Orientation; +// glm::vec3 v3 = glm::normalize(glm::cross(v1,v2)); +// float angle = glm::acos(glm::dot(v1, v2) / glm::length(v1) * glm::length(v2)); +// float s = sin(angle / 2); +// transformComponent->Orientation.x = v3.x * s; +// transformComponent->Orientation.y = v3.y * s; +// transformComponent->Orientation.z = v3.z * s; +// transformComponent->Orientation.w = glm::cos(angle/2); + + //float alpha = it->AngularVelocity * dt; + //transformComponent->Orientation = transformComponent->Orientation * it->Orientation; transformComponent->Position += transformComponent->Velocity * (float)dt; it++; @@ -89,7 +106,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) particleTransform->Position = emitterTransform->Position; particleTransform->Scale = glm::vec3(1, 1, 1); - //The emitter's orientation as "start value" times the default direction for quaternion. Times the speed, and then rotate on x and y axis with the randomized spread angle. + //The emitter's orientation as "start value" times the default direction for emitter. Times the speed, and then rotate on x and y axis with the randomized spread angle. float spreadAngle = emitterComponent->SpreadAngle; particleTransform->Velocity = emitterOrientation * glm::vec3(0, 0, -1) * speed * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(1, 0, 0))) * @@ -100,12 +117,13 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = emitterComponent->LifeTime; - particle->ScaleSpectrum.push_back(1); //TEMP - particle->ScaleSpectrum.push_back(1); //TEMP + particle->ScaleSpectrum.push_back(glm::vec3(1)); //TEMP + //particle->ScaleSpectrum.push_back(glm::vec3(1,4,1)); //TEMP particle->VelocitySpectrum.push_back(particleTransform->Velocity); //TEMP particle->VelocitySpectrum.push_back(testVel); //TEMP - //particle->AngularVelocitySpectrum.push_back(0.f); - particle->AngularVelocitySpectrum.push_back(-glm::pi()/10); +// particle->AngularVelocitySpectrum.push_back(0.f); +// particle->AngularVelocitySpectrum.push_back(-glm::pi()); + particle->OrientationSpectrum = particle->VelocitySpectrum; // Color startColor = {.4f, .45f, .2f}; // particle->ColorSpectrum.push_back(startColor); @@ -117,7 +135,8 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); - data.AngularVelocity = particle->AngularVelocitySpectrum[0]; + //data.AngularVelocity = particle->AngularVelocitySpectrum[0]; + data.Orientation = particle->OrientationSpectrum[0]; m_ParticleEmitter[emitterID].push_back(data); } @@ -129,47 +148,37 @@ float Systems::ParticleSystem::RandomizeAngle(float spreadAngle) return ((float)rand() / ((float)RAND_MAX + 1) * spreadAngle) - spreadAngle/2; } -//Interpolates the scale of the particle -void Systems::ParticleSystem::ScaleInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &s) -{ - float dScale = glm::abs(spectrum[0] - spectrum[1]); - if(spectrum[0] > spectrum[1]) - dScale *= -1; - s = glm::vec3(spectrum[0] + dScale * timeProgress); -} - //Interpolates the velocity of the particle -void Systems::ParticleSystem::VelocityInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &v) +void Systems::ParticleSystem::VectorInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &v) { - float dVelocity = glm::abs(spectrum[0].x - spectrum[1].x); + float dAxisValue = glm::abs(spectrum[0].x - spectrum[1].x); if(spectrum[0].x > spectrum[1].x) - dVelocity *= -1; - v.x = spectrum[0].x + dVelocity * timeProgress; - dVelocity = glm::abs(spectrum[0].y - spectrum[1].y); + dAxisValue *= -1; + v.x = spectrum[0].x + dAxisValue * timeProgress; + dAxisValue = glm::abs(spectrum[0].y - spectrum[1].y); if (spectrum[0].y > spectrum[1].y) - dVelocity *= -1; - v.y = spectrum[0].y + dVelocity * timeProgress; - dVelocity = glm::abs(spectrum[0].z - spectrum[1].z); + dAxisValue *= -1; + v.y = spectrum[0].y + dAxisValue * timeProgress; + dAxisValue = glm::abs(spectrum[0].z - spectrum[1].z); if(spectrum[0].z > spectrum[1].z) - dVelocity *= -1; - v.z = spectrum[0].z + dVelocity * timeProgress; + dAxisValue *= -1; + v.z = spectrum[0].z + dAxisValue * timeProgress; } -void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector spectrum, Color &c) -{ - float dColor = glm::abs(spectrum[0].r - spectrum[1].r); - c.r = spectrum[0].r + dColor * timeProgress; - dColor = glm::abs(spectrum[0].g - spectrum[1].g); - c.g = spectrum[0].g + dColor * timeProgress; - dColor = glm::abs(spectrum[0].b - spectrum[1].b); - c.b = spectrum[0].b + dColor * timeProgress; -} +// void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector spectrum, Color &c) +// { +// float dColor = glm::abs(spectrum[0].r - spectrum[1].r); +// c.r = spectrum[0].r + dColor * timeProgress; +// dColor = glm::abs(spectrum[0].g - spectrum[1].g); +// c.g = spectrum[0].g + dColor * timeProgress; +// dColor = glm::abs(spectrum[0].b - spectrum[1].b); +// c.b = spectrum[0].b + dColor * timeProgress; +// } -void Systems::ParticleSystem::AngularVelocityInterpolation(double timeProgress, std::vector spectrum, float &alpha) +void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vector spectrum, float &alpha) { float dAlpha = glm::abs(spectrum[0] - spectrum[1]); if(spectrum[0] > spectrum[1]) dAlpha *= -1; alpha = spectrum[0] + dAlpha * timeProgress; - } \ No newline at end of file diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 9e10b99..4dff51e 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -21,6 +21,7 @@ namespace Systems double SpawnTime; float Scale; float AngularVelocity; + glm::vec3 Orientation; Color color; }; @@ -34,10 +35,10 @@ public: private: void SpawnParticles(EntityID emitterID); float RandomizeAngle(float spreadAngle); - void ScaleInterpolation(double timeProgress, std::vector scaleSpectrum, glm::vec3 &scale); - void VelocityInterpolation(double timeProgress, std::vector velocitySpectrum, glm::vec3 &velocity); - void ColorInterpolation(double timeProgress, std::vector colorSpectrum, Color &color); - void AngularVelocityInterpolation(double timeProgress, std::vector spectrum, float &angularVelocity); + //void ScaleInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &scale); + void VectorInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &velocity); + //void ColorInterpolation(double timeProgress, std::vector spectrum, Color &color); + void ScalarInterpolation(double timeProgress, std::vector spectrum, float &alpha); void Billboard(); std::map> m_ParticleEmitter; std::map m_TimeSinceLastSpawn;