Optimized ParticleSystem. Still can't see particles though D:

This commit is contained in:
Stiffly
2014-05-30 22:19:03 +02:00
parent dbebd5a6e9
commit 28c3cf5e16
4 changed files with 101 additions and 116 deletions
+1
View File
@@ -14,6 +14,7 @@ namespace Components
std::vector<Color> ColorSpectrum; std::vector<Color> ColorSpectrum;
std::vector<glm::vec3> ScaleSpectrum; std::vector<glm::vec3> ScaleSpectrum;
double LifeTime; double LifeTime;
double SpawnTime;
std::vector<glm::vec3> VelocitySpectrum; std::vector<glm::vec3> VelocitySpectrum;
std::vector<float> AngularVelocitySpectrum; std::vector<float> AngularVelocitySpectrum;
std::vector<glm::vec3> OrientationSpectrum; //Keep? std::vector<glm::vec3> OrientationSpectrum; //Keep?
+46 -46
View File
@@ -745,29 +745,29 @@ EntityID GameWorld::CreateTank(int playerID)
} }
CommitEntity(wheel); CommitEntity(wheel);
auto entity = CreateEntity(tank); // auto entity = CreateEntity(tank);
auto transformComponent = AddComponent<Components::Transform>(entity); // auto transformComponent = AddComponent<Components::Transform>(entity);
transformComponent->Position = glm::vec3(2, -1.7, 2.0); // transformComponent->Position = glm::vec3(2, -1.7, 2.0);
transformComponent->Scale = glm::vec3(3, 3, 3); // transformComponent->Scale = glm::vec3(3, 3, 3);
transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0)); // transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0));
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity); // auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
emitterComponent->SpawnCount = 2; // emitterComponent->SpawnCount = 2;
emitterComponent->SpawnFrequency = 0.005; // emitterComponent->SpawnFrequency = 0.005;
emitterComponent->SpreadAngle = glm::pi<float>(); // emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false; // emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5; // emitterComponent->LifeTime = 0.5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100); // //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); // emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
CommitEntity(entity); // CommitEntity(entity);
//
auto particleEntity = CreateEntity(entity); // auto particleEntity = CreateEntity(entity);
auto TEMP = AddComponent<Components::Transform>(particleEntity); // auto TEMP = AddComponent<Components::Transform>(particleEntity);
TEMP->Scale = glm::vec3(0); // TEMP->Scale = glm::vec3(0);
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity); // auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; // spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
emitterComponent->ParticleTemplate = particleEntity; // emitterComponent->ParticleTemplate = particleEntity;
//
CommitEntity(particleEntity); // CommitEntity(particleEntity);
} }
{ {
@@ -830,29 +830,29 @@ EntityID GameWorld::CreateTank(int playerID)
CommitEntity(wheel); CommitEntity(wheel);
#pragma endregion #pragma endregion
auto entity = CreateEntity(tank); // auto entity = CreateEntity(tank);
auto transformComponent = AddComponent<Components::Transform>(entity); // auto transformComponent = AddComponent<Components::Transform>(entity);
transformComponent->Position = glm::vec3(-2, -1.7, 2.0); // transformComponent->Position = glm::vec3(-2, -1.7, 2.0);
transformComponent->Scale = glm::vec3(3, 3, 3); // transformComponent->Scale = glm::vec3(3, 3, 3);
transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0)); // transformComponent->Orientation = glm::angleAxis(glm::pi<float>() / 2, glm::vec3(1, 0, 0));
auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity); // auto emitterComponent = AddComponent<Components::ParticleEmitter>(entity);
emitterComponent->SpawnCount = 2; // emitterComponent->SpawnCount = 2;
emitterComponent->SpawnFrequency = 0.005; // emitterComponent->SpawnFrequency = 0.005;
emitterComponent->SpreadAngle = glm::pi<float>(); // emitterComponent->SpreadAngle = glm::pi<float>();
emitterComponent->UseGoalVelocity = false; // emitterComponent->UseGoalVelocity = false;
emitterComponent->LifeTime = 0.5; // emitterComponent->LifeTime = 0.5;
//emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100); // //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi<float>() / 100);
emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); // emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05));
CommitEntity(entity); // CommitEntity(entity);
//
auto particleEntity = CreateEntity(entity); // auto particleEntity = CreateEntity(entity);
auto TEMP = AddComponent<Components::Transform>(particleEntity); // auto TEMP = AddComponent<Components::Transform>(particleEntity);
TEMP->Scale = glm::vec3(0); // TEMP->Scale = glm::vec3(0);
auto spriteComponent = AddComponent<Components::Sprite>(particleEntity); // auto spriteComponent = AddComponent<Components::Sprite>(particleEntity);
spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; // spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png";
emitterComponent->ParticleTemplate = particleEntity; // emitterComponent->ParticleTemplate = particleEntity;
//
CommitEntity(particleEntity); // CommitEntity(particleEntity);
} }
CommitEntity(tank); CommitEntity(tank);
+53 -60
View File
@@ -52,64 +52,59 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID
SpawnParticles(entity); SpawnParticles(entity);
emitterComponent->TimeSinceLastSpawn = 0; emitterComponent->TimeSinceLastSpawn = 0;
} }
}
std::list<ParticleData>::iterator it; auto particleComponent = m_World->GetComponent<Components::Particle>(entity);
for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) if(particleComponent)
{
EntityID particleID = entity;
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID);
double timeLived = glfwGetTime() - particleComponent->SpawnTime;
if(timeLived > particleComponent->LifeTime)
{ {
EntityID particleID = (it)->ParticleID; m_World->RemoveEntity(particleID);
auto transformComponent = m_World->GetComponent<Components::Transform>(particleID); }
auto particleComponent = m_World->GetComponent<Components::Particle>(particleID); else
{
// FIX: calculate once
float timeProgress = timeLived / particleComponent->LifeTime;
// ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color);
// Scale interpolation
if(particleComponent->ScaleSpectrum.size() > 1)
VectorInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale);
// Velocity interpolation
if(particleComponent->VelocitySpectrum.size() > 1)
VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity);
double timeLived = glfwGetTime() - it->SpawnTime; /*// Angular velocity interpolation
if(timeLived > particleComponent->LifeTime) if (particleComponent->AngularVelocitySpectrum.size() != 0)
{ {
m_World->RemoveEntity(particleID); if(particleComponent->AngularVelocitySpectrum.size() > 1)
it = m_ParticleEmitter[entity].erase(it); {
ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity);
transformComponent->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
}
else
{
transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, it->Orientation);
//it->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
}
} }
else
//Angular velocity interpolation
if(particleComponent->OrientationSpectrum.size() > 1)
{ {
// FIX: calculate once VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation);
float timeProgress = timeLived / particleComponent->LifeTime; glm::vec3 v1 = (particleComponent->OrientationSpectrum[0]);
// ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); glm::vec3 v2 = (it->Orientation);
// Scale interpolation glm::vec3 v3 = glm::normalize(glm::cross(v1,v2));
if(particleComponent->ScaleSpectrum.size() > 1) float angle = glm::acos(glm::dot(v1, v2) / (glm::length(v1) * glm::length(v2)));
VectorInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale);
// Velocity interpolation
if(particleComponent->VelocitySpectrum.size() > 1)
VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity);
// Angular velocity interpolation
if (particleComponent->AngularVelocitySpectrum.size() != 0)
{
if(particleComponent->AngularVelocitySpectrum.size() > 1)
{
ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity);
transformComponent->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
}
else
{
transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, it->Orientation);
//it->Orientation = glm::angleAxis(it->AngularVelocity, it->Orientation);
}
}
//Angular velocity interpolation
if(particleComponent->OrientationSpectrum.size() > 1)
{
VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation);
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)));
transformComponent->Orientation = glm::angleAxis(angle, v3); transformComponent->Orientation = glm::angleAxis(angle, v3);
} }*/
transformComponent->Position += transformComponent->Velocity * (float)dt;
transformComponent->Position += transformComponent->Velocity * (float)dt;
it++;
}
} }
} }
} }
@@ -173,15 +168,13 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID)
particle->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum; particle->AngularVelocitySpectrum = eComponent->AngularVelocitySpectrum;
ParticleData data; particle->SpawnTime = glfwGetTime();
data.ParticleID = ent; // if (particle->AngularVelocitySpectrum.size() != 0)
data.SpawnTime = glfwGetTime(); // data.AngularVelocity = particle->AngularVelocitySpectrum[0];
if (particle->AngularVelocitySpectrum.size() != 0) // if (particle->OrientationSpectrum.size() != 0)
data.AngularVelocity = particle->AngularVelocitySpectrum[0]; // data.Orientation = particle->OrientationSpectrum[0];
if (particle->OrientationSpectrum.size() != 0) // else data.Orientation = eOrientation * glm::vec3(0,0,-1);
data.Orientation = particle->OrientationSpectrum[0]; //m_ParticleEmitter[emitterID].push_back(data);
else data.Orientation = eOrientation * glm::vec3(0,0,-1);
m_ParticleEmitter[emitterID].push_back(data);
} }
} }
+1 -10
View File
@@ -16,15 +16,6 @@
namespace Systems namespace Systems
{ {
struct ParticleData
{
EntityID ParticleID;
double SpawnTime;
float AngularVelocity;
glm::vec3 Orientation;
Color color;
};
class ParticleSystem : public System class ParticleSystem : public System
{ {
public: public:
@@ -49,7 +40,7 @@ private:
//void ColorInterpolation(double timeProgress, std::vector<Color> spectrum, Color &color); //void ColorInterpolation(double timeProgress, std::vector<Color> spectrum, Color &color);
void ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha); void ScalarInterpolation(double timeProgress, std::vector<float> spectrum, float &alpha);
void Billboard(); void Billboard();
std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter; //std::map<EntityID, std::list<ParticleData>> m_ParticleEmitter;
std::map<EntityID, double> m_TimeSinceLastSpawn; std::map<EntityID, double> m_TimeSinceLastSpawn;
std::map<EntityID, double> m_ExplosionEmitters; std::map<EntityID, double> m_ExplosionEmitters;
std::shared_ptr<Systems::TransformSystem> m_TransformSystem; std::shared_ptr<Systems::TransformSystem> m_TransformSystem;