Generalized interpolation functions. (orientation spectrum not working properly (quaterions...))

This commit is contained in:
Stiffly
2014-04-29 11:51:57 +02:00
parent 0feb983277
commit a13e9d32ec
5 changed files with 61 additions and 49 deletions
+2 -1
View File
@@ -12,10 +12,11 @@ namespace Components
struct Particle : Component
{
std::vector<Color> ColorSpectrum;
std::vector<float> ScaleSpectrum;
std::vector<glm::vec3> ScaleSpectrum;
double LifeTime;
std::vector<glm::vec3> VelocitySpectrum;
std::vector<float> AngularVelocitySpectrum;
std::vector<glm::vec3> OrientationSpectrum; //Keep?
virtual Particle* Clone() const override { return new Particle(*this); }
};
+2 -1
View File
@@ -25,11 +25,12 @@ struct ParticleEmitter : Component
float SpawnFrequency;
int SpawnCount;
std::vector<Color> ColorSpectrum;
std::vector<float> ScaleSpectrum;
std::vector<glm::vec3> ScaleSpectrum;
float SpreadAngle;
double LifeTime;
std::vector<glm::vec3> VelocitySpectrum;
std::vector<float> AngularVelocitySpectrum;
std::vector<glm::vec3> OrientationSpectrum; //Keep?
virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); }
+1 -1
View File
@@ -103,7 +103,7 @@ void GameWorld::Initialize()
emitter->LifeTime = 4;
emitter->SpawnCount = 1;
emitter->SpreadAngle = glm::pi<float>()/20;
emitter->SpawnFrequency = 0.008;
emitter->SpawnFrequency = 1.008;
auto model = AddComponent<Components::Model>(ent, "Model");
model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj";
+51 -42
View File
@@ -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<Components::Particle>(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<float>()/10);
// particle->AngularVelocitySpectrum.push_back(0.f);
// particle->AngularVelocitySpectrum.push_back(-glm::pi<float>());
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<float> 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<glm::vec3> spectrum, glm::vec3 &v)
void Systems::ParticleSystem::VectorInterpolation(double timeProgress, std::vector<glm::vec3> 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<Color> 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<Color> 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<float> spectrum, float &alpha)
void Systems::ParticleSystem::ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha)
{
float dAlpha = glm::abs(spectrum[0] - spectrum[1]);
if(spectrum[0] > spectrum[1])
dAlpha *= -1;
alpha = spectrum[0] + dAlpha * timeProgress;
}
+5 -4
View File
@@ -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<float> scaleSpectrum, glm::vec3 &scale);
void VelocityInterpolation(double timeProgress, std::vector<glm::vec3> velocitySpectrum, glm::vec3 &velocity);
void ColorInterpolation(double timeProgress, std::vector<Color> colorSpectrum, Color &color);
void AngularVelocityInterpolation(double timeProgress, std::vector<float> spectrum, float &angularVelocity);
//void ScaleInterpolation(double timeProgress, std::vector<float> spectrum, glm::vec3 &scale);
void VectorInterpolation(double timeProgress, std::vector<glm::vec3> spectrum, glm::vec3 &velocity);
//void ColorInterpolation(double timeProgress, std::vector<Color> spectrum, Color &color);
void ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha);
void Billboard();
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
std::map<EntityID, double> m_TimeSinceLastSpawn;