From 5e402af093dc3f122e57d03c069125b92a911823 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Thu, 17 Apr 2014 00:08:27 +0200 Subject: [PATCH 01/27] Added ParticleSystem skeleton plus basic temporary implementation. --- src/GameWorld.cpp | 2 +- src/Systems/ParticleSystem.cpp | 47 +++++++++++++++++++ src/Systems/ParticleSystem.h | 33 +++++++++++++ vs11/Returngeance/Returngeance.vcxproj | 2 + .../Returngeance/Returngeance.vcxproj.filters | 38 ++++----------- 5 files changed, 91 insertions(+), 31 deletions(-) create mode 100644 src/Systems/ParticleSystem.cpp create mode 100644 src/Systems/ParticleSystem.h diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 7a5a543..371dd2e 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -98,7 +98,7 @@ void GameWorld::AddSystems() //AddSystem("LevelGenerationSystem"); AddSystem("InputSystem"); //AddSystem("CollisionSystem"); - ////AddSystem("ParticleSystem"); + //AddSystem("ParticleSystem"); //AddSystem("PlayerSystem"); AddSystem("FreeSteeringSystem"); AddSystem("SoundSystem"); diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp new file mode 100644 index 0000000..3df99a0 --- /dev/null +++ b/src/Systems/ParticleSystem.cpp @@ -0,0 +1,47 @@ +#include "ParticleSystem.h" +#include "PrecompiledHeader.h" + +#include "World.h" +void Systems::ParticleSystem::Update(double dt) +{ + +} + +void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) +{ + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if(!transformComponent) + return; + + transformComponent->Position.y --; + + timeLived[entity] += dt; + + auto emitterComponent = m_World->GetComponent(entity, "ParticleEmitter"); + + + +} + +void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) +{ + cf->Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); +} + +void Systems::ParticleSystem::SpawnParticles() +{ + for(int i = 0; i < 100; i++) + { + auto particle = m_World->CreateEntity(); + auto transform = m_World->AddComponent(particle, "Transform"); + transform->Position = glm::vec3(0); + particles.push_back(particle); + } +} + +void Systems::ParticleSystem::Draw(double dt) +{ + +} + + diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h new file mode 100644 index 0000000..4211d89 --- /dev/null +++ b/src/Systems/ParticleSystem.h @@ -0,0 +1,33 @@ +#ifndef ParticleSystem_h__ +#define ParticleSystem_h__ + +#include "System.h" +#include "Components/Transform.h" +#include "Components/ParticleEmitter.h" + + +namespace Systems +{ + + +class ParticleSystem : public System +{ +public: + ParticleSystem(World* world); + void RegisterComponents(ComponentFactory* cf) override; + + void Update(double dt) override; + void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + +private: + void SpawnParticles(); + + + +}; + + +} + + +#endif // ParticleSystem_h__ \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 4726981..40cd9be 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -109,6 +109,7 @@ + @@ -151,6 +152,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 26bcf35..185a9c4 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -50,6 +50,9 @@ + + Particle System\Systems + @@ -157,36 +160,6 @@ Rendering - - Physics\Components - - - Physics\Components - - - Physics\Components - - - Physics\Components - - - Physics\Components - - - Physics\Components - - - Physics\Components - - - Physics\Components - - - Physics\Components - - - Physics\Components - Physics\Components @@ -242,6 +215,11 @@ Audio + + + + Particle System\Systems + From 55221cca12a45b97ba1d127a21a7454dbe73a540 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 22 Apr 2014 16:52:14 +0200 Subject: [PATCH 02/27] Implemented Spawn and Remove function for particles. Also added a Particle component. --- src/Components/Particle.h | 24 ++++++++++ src/Components/ParticleEmitter.h | 11 ++--- src/GameWorld.h | 1 + src/Systems/ParticleSystem.cpp | 47 +++++++++++++++---- src/Systems/ParticleSystem.h | 13 +++-- vs11/Returngeance/Returngeance.vcxproj | 1 + .../Returngeance/Returngeance.vcxproj.filters | 3 ++ 7 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 src/Components/Particle.h diff --git a/src/Components/Particle.h b/src/Components/Particle.h new file mode 100644 index 0000000..71ac940 --- /dev/null +++ b/src/Components/Particle.h @@ -0,0 +1,24 @@ +#ifndef Components_Particle_h__ +#define Components_Particle_h__ + +#include "System.h" +#include "Component.h" +#include "Components/Transform.h" +#include "Components/ParticleEmitter.h" +#include "Color.h" +#include + +namespace Components +{ + + struct Particle : Component + { + std::vector ColorSpectrum; + std::vector ScaleSpectrum; + double LifeTime; + std::vector VelocitySpectrum; + std::vector AngularVelocitySpectrum; + }; + +} +#endif // !Components_Particle_h__ \ No newline at end of file diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 48248a8..9f5e20f 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -1,10 +1,7 @@ #ifndef Components_ParticleEmitter_h__ #define Components_ParticleEmitter_h__ -#include "System.h" #include "Component.h" -#include "Components/Transform.h" -#include "Components/ParticleEmitter.h" #include "Color.h" #include @@ -13,15 +10,15 @@ namespace Components struct ParticleEmitter : Component { - int ParticleTemplate; + EntityID ParticleTemplate; float SpawnFrequency; int SpawnCount; std::vector ColorSpectrum; std::vector ScaleSpectrum; float SpreadAngle; - float LifeTime; - std::vector VelocitySpectrum; - std::vector AngularVelocitySpectrum; + double LifeTime; + std::vector VelocitySpectrum; + std::vector AngularVelocitySpectrum; }; } diff --git a/src/GameWorld.h b/src/GameWorld.h index 264ec4d..4f74358 100755 --- a/src/GameWorld.h +++ b/src/GameWorld.h @@ -20,6 +20,7 @@ #include "Components/Input.h" #include "Components/Model.h" #include "Components/ParticleEmitter.h" +#include "Components/Particle.h" #include "Components/PointLight.h" #include "Components/SoundEmitter.h" #include "Components/Sprite.h" diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index bc23511..d742e5d 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -3,9 +3,10 @@ #include "World.h" + void Systems::ParticleSystem::Update(double dt) { - + m_TimeSinceLastSpawn += dt; } void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -13,26 +14,56 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID auto transformComponent = m_World->GetComponent(entity, "Transform"); if(!transformComponent) return; - - - auto emitterComponent = m_World->GetComponent(entity, "ParticleEmitter"); + if(emitterComponent) + { + if(m_TimeSinceLastSpawn > emitterComponent->SpawnFrequency) + { + SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); + m_TimeSinceLastSpawn = 0; + } + + std::list::iterator it; + for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();) + { + it->TimeLived += dt; + auto particleComponent = m_World->GetComponent(it->ParticleID, "Particle"); + if(it->TimeLived > particleComponent->LifeTime) + { + m_ParticleEmitter[entity].erase(it); + break; + } + else + { + it++; + } + + } + } } void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) { cf->Register("ParticleEmitter", []() { return new Components::ParticleEmitter(); }); + cf->Register("Particle", []() { return new Components::Particle(); }); } -void Systems::ParticleSystem::SpawnParticles() +void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle) { - for(int i = 0; i < 100; i++) + std::list particles; + for(int i = 0; i < spawnCount; i++) { - auto particle = m_World->CreateEntity(); - auto transform = m_World->AddComponent(particle, "Transform"); + auto ent = m_World->CreateEntity(); + auto transform = m_World->AddComponent(ent, "Transform"); transform->Position = glm::vec3(0); + auto particle = m_World->AddComponent(ent, "Particle"); + particle->LifeTime = 4; + ParticleData data; + data.ParticleID = ent; + data.TimeLived = 0; + m_ParticleEmitter[emitterID].push_back(data); } } diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 35dfee1..8414a96 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -4,11 +4,17 @@ #include "System.h" #include "Components/Transform.h" #include "Components/ParticleEmitter.h" +#include "Components/Particle.h" namespace Systems { + struct ParticleData + { + EntityID ParticleID; + double TimeLived; + }; class ParticleSystem : public System { @@ -21,12 +27,13 @@ public: void UpdateEntity(double dt, EntityID entity, EntityID parent) override; void Draw(double dt); private: - void SpawnParticles(); + void SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle); + std::map> m_ParticleEmitter; + double m_TimeSinceLastSpawn; }; - } -#endif // ParticleSystem_h__ \ No newline at end of file +#endif // !ParticleSystem_h__ \ No newline at end of file diff --git a/vs11/Returngeance/Returngeance.vcxproj b/vs11/Returngeance/Returngeance.vcxproj index 40cd9be..3cfd619 100755 --- a/vs11/Returngeance/Returngeance.vcxproj +++ b/vs11/Returngeance/Returngeance.vcxproj @@ -127,6 +127,7 @@ + diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 185a9c4..35f6ce6 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -220,6 +220,9 @@ Particle System\Systems + + Particle System\Components + From 78d4dbf8c2deb99ac1b6bd880fb5370d2b066588 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 22 Apr 2014 17:59:55 +0200 Subject: [PATCH 03/27] Added scale and color interpolation --- src/Systems/ParticleSystem.cpp | 32 +++++++++++++++++++++++++++++--- src/Systems/ParticleSystem.h | 6 +++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index d742e5d..8488d3d 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -27,9 +27,10 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID std::list::iterator it; for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();) { - it->TimeLived += dt; auto particleComponent = m_World->GetComponent(it->ParticleID, "Particle"); - if(it->TimeLived > particleComponent->LifeTime) + + int timeLived = glfwGetTime() - it->SpawnTime; + if(timeLived > particleComponent->LifeTime) { m_ParticleEmitter[entity].erase(it); break; @@ -39,7 +40,20 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID it++; } + // Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time. + // How big fraction the color is multiplied with + float timeProgress = timeLived / particleComponent->LifeTime; + // The difference between the start and end value + float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); + it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress; + deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g); + it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress; + deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); + it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress; + //Interpolates the scale for the particle + float deltaScale = glm::abs(particleComponent->ScaleSpectrum[0] - particleComponent->ScaleSpectrum[1]); + it->Scale = particleComponent->ScaleSpectrum[0] + deltaScale * timeProgress; } } } @@ -56,13 +70,25 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun for(int i = 0; i < spawnCount; i++) { auto ent = m_World->CreateEntity(); + auto transform = m_World->AddComponent(ent, "Transform"); transform->Position = glm::vec3(0); + auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = 4; + Color startColor = {.4f, .45f, .2f}; + particle->ColorSpectrum.push_back(startColor); + Color endColor = {0.f, 45.f, 23.f}; + particle->ColorSpectrum.push_back(endColor); + particle->ScaleSpectrum.push_back(1); + particle->ScaleSpectrum.push_back(30); + ParticleData data; data.ParticleID = ent; - data.TimeLived = 0; + data.SpawnTime = glfwGetTime(); + data.color = particle->ColorSpectrum[0]; + data.Scale = particle->ScaleSpectrum[0]; + m_ParticleEmitter[emitterID].push_back(data); } } diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 8414a96..84674e6 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -5,6 +5,8 @@ #include "Components/Transform.h" #include "Components/ParticleEmitter.h" #include "Components/Particle.h" +#include "Color.h" +#include namespace Systems @@ -13,7 +15,9 @@ namespace Systems struct ParticleData { EntityID ParticleID; - double TimeLived; + double SpawnTime; + float Scale; + Color color; }; class ParticleSystem : public System From bdac6fa9f955934306e3e089248546863ef665fb Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 22 Apr 2014 18:58:41 +0200 Subject: [PATCH 04/27] Added velocity interpolation --- src/GameWorld.cpp | 13 +++++++++++++ src/Systems/ParticleSystem.cpp | 24 ++++++++++++++++++++---- src/Systems/ParticleSystem.h | 4 ++-- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 5d471b8..455c105 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -90,6 +90,19 @@ void GameWorld::Initialize() emitter->Loop = true; GetSystem("SoundSystem")->PlaySound(emitter); }*/ + +// { +// // Particle emitter +// auto ent = CreateEntity(); +// auto transform = AddComponent(ent, "Transform"); +// transform->Position = glm::vec3(0); +// auto emitter = AddComponent(ent, "ParticleEmitter"); +// emitter->LifeTime = 4; +// emitter->SpawnCount = 3; +// emitter->SpreadAngle = 35; +// emitter->SpawnFrequency = 0.3; +// //emitter-> +// } } void GameWorld::Update(double dt) diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 8488d3d..d379ba9 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -4,6 +4,11 @@ #include "World.h" +Systems::ParticleSystem::ParticleSystem(World *m_World) : System(m_World) +{ + m_TimeSinceLastSpawn = 0; +} + void Systems::ParticleSystem::Update(double dt) { m_TimeSinceLastSpawn += dt; @@ -23,13 +28,14 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); m_TimeSinceLastSpawn = 0; } - + std::cout<::iterator it; for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();) { auto particleComponent = m_World->GetComponent(it->ParticleID, "Particle"); - int timeLived = glfwGetTime() - it->SpawnTime; + double timeLived = glfwGetTime() - it->SpawnTime; + std::cout< particleComponent->LifeTime) { m_ParticleEmitter[entity].erase(it); @@ -51,9 +57,17 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress; - //Interpolates the scale for the particle + //Interpolates the scale of the particle float deltaScale = glm::abs(particleComponent->ScaleSpectrum[0] - particleComponent->ScaleSpectrum[1]); it->Scale = particleComponent->ScaleSpectrum[0] + deltaScale * timeProgress; + + //Interpolates the velocity of the particle + float deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].x - particleComponent->VelocitySpectrum[1].x); + it->Velocity.x = particleComponent->VelocitySpectrum[0].x + deltaVelocity * timeProgress; + deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].y - particleComponent->VelocitySpectrum[1].y); + it->Velocity.y = particleComponent->VelocitySpectrum[0].y + deltaVelocity * timeProgress; + deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].z - particleComponent->VelocitySpectrum[1].z); + it->Velocity.z = particleComponent->VelocitySpectrum[0].z + deltaVelocity * timeProgress; } } } @@ -75,13 +89,15 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun transform->Position = glm::vec3(0); auto particle = m_World->AddComponent(ent, "Particle"); - particle->LifeTime = 4; + particle->LifeTime = 4000; Color startColor = {.4f, .45f, .2f}; particle->ColorSpectrum.push_back(startColor); Color endColor = {0.f, 45.f, 23.f}; particle->ColorSpectrum.push_back(endColor); particle->ScaleSpectrum.push_back(1); particle->ScaleSpectrum.push_back(30); + particle->VelocitySpectrum.push_back(glm::vec3(0, -.2, 0)); + particle->VelocitySpectrum.push_back(glm::vec3(0, -3, 0)); ParticleData data; data.ParticleID = ent; diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 84674e6..3c24e79 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -18,13 +18,13 @@ namespace Systems double SpawnTime; float Scale; Color color; + glm::vec3 Velocity; }; class ParticleSystem : public System { public: - ParticleSystem(World* world) - :System(world) { }; + ParticleSystem(World* world); void RegisterComponents(ComponentFactory* cf) override; void Update(double dt) override; From 100f8f5be4aed93d8c1cb3172065109ceb221446 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Wed, 23 Apr 2014 21:33:43 +0200 Subject: [PATCH 05/27] Bug fix: checked for particles not created --- src/GameWorld.cpp | 24 ++++++++++++------------ src/Systems/ParticleSystem.cpp | 3 +++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 455c105..1743d6d 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -91,18 +91,18 @@ void GameWorld::Initialize() GetSystem("SoundSystem")->PlaySound(emitter); }*/ -// { -// // Particle emitter -// auto ent = CreateEntity(); -// auto transform = AddComponent(ent, "Transform"); -// transform->Position = glm::vec3(0); -// auto emitter = AddComponent(ent, "ParticleEmitter"); -// emitter->LifeTime = 4; -// emitter->SpawnCount = 3; -// emitter->SpreadAngle = 35; -// emitter->SpawnFrequency = 0.3; -// //emitter-> -// } + { + // Particle emitter + auto ent = CreateEntity(); + auto transform = AddComponent(ent, "Transform"); + transform->Position = glm::vec3(0); + auto emitter = AddComponent(ent, "ParticleEmitter"); + emitter->LifeTime = 4; + emitter->SpawnCount = 3; + emitter->SpreadAngle = 35; + emitter->SpawnFrequency = 0.3; + //emitter-> + } } void GameWorld::Update(double dt) diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index d379ba9..048832d 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -28,11 +28,14 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); m_TimeSinceLastSpawn = 0; } + std::cout<::iterator it; for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();) { auto particleComponent = m_World->GetComponent(it->ParticleID, "Particle"); + if(!particleComponent) + break; double timeLived = glfwGetTime() - it->SpawnTime; std::cout< Date: Wed, 23 Apr 2014 22:46:19 +0200 Subject: [PATCH 06/27] Bug fix: fixed time to correct resolution, seconds. --- src/GameWorld.cpp | 6 +++--- src/Systems/ParticleSystem.cpp | 26 ++++++++++++++------------ src/Systems/ParticleSystem.h | 1 - 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 1743d6d..242d0bc 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -49,7 +49,7 @@ void GameWorld::Initialize() model->ModelFile = "Models/Placeholders/tank/Chassi.obj"; } - for(int i = 0; i < 83; i++) + for(int i = 0; i < 2; i++) { auto light = CreateEntity(); auto transform = AddComponent(light, "Transform"); @@ -67,7 +67,7 @@ void GameWorld::Initialize() model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; } - for(int i = 0; i < 500; i++) + for(int i = 0; i < 3; i++) { auto ball = CreateEntity(); auto transform = AddComponent(ball, "Transform"); @@ -100,7 +100,7 @@ void GameWorld::Initialize() emitter->LifeTime = 4; emitter->SpawnCount = 3; emitter->SpreadAngle = 35; - emitter->SpawnFrequency = 0.3; + emitter->SpawnFrequency = 0.01; //emitter-> } } diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 048832d..d2ccc8c 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -25,23 +25,25 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID { if(m_TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { - SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); - m_TimeSinceLastSpawn = 0; + if(m_ParticleEmitter[entity].size() < 100) + { + SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); + m_TimeSinceLastSpawn = 0; + } + std::cout<<"Particle count: "<::iterator it; - for(it = m_ParticleEmitter[entity].begin(); it == m_ParticleEmitter[entity].end();) + for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { auto particleComponent = m_World->GetComponent(it->ParticleID, "Particle"); - if(!particleComponent) - break; + double timeLived = glfwGetTime() - it->SpawnTime; - std::cout< particleComponent->LifeTime) { m_ParticleEmitter[entity].erase(it); + std::cout<<"Removed dead particle..."<LifeTime; // The difference between the start and end value float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); @@ -70,7 +72,7 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].y - particleComponent->VelocitySpectrum[1].y); it->Velocity.y = particleComponent->VelocitySpectrum[0].y + deltaVelocity * timeProgress; deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].z - particleComponent->VelocitySpectrum[1].z); - it->Velocity.z = particleComponent->VelocitySpectrum[0].z + deltaVelocity * timeProgress; + it->Velocity.z = particleComponent->VelocitySpectrum[0].z + deltaVelocity * timeProgress;*/ } } } @@ -83,7 +85,6 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle) { - std::list particles; for(int i = 0; i < spawnCount; i++) { auto ent = m_World->CreateEntity(); @@ -92,7 +93,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun transform->Position = glm::vec3(0); auto particle = m_World->AddComponent(ent, "Particle"); - particle->LifeTime = 4000; + particle->LifeTime = 4; Color startColor = {.4f, .45f, .2f}; particle->ColorSpectrum.push_back(startColor); Color endColor = {0.f, 45.f, 23.f}; @@ -110,6 +111,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun m_ParticleEmitter[emitterID].push_back(data); } + std::cout<<"Added "< - namespace Systems { From 5997ee232054f1ee99eb506120888a44d4dd5393 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Thu, 24 Apr 2014 00:00:29 +0200 Subject: [PATCH 07/27] Added models and temporary "fly everywhere" direction to particles --- src/Components/Particle.h | 2 -- src/GameWorld.cpp | 4 ++-- src/Systems/ParticleSystem.cpp | 40 ++++++++++++++++++++++------------ src/Systems/ParticleSystem.h | 2 ++ 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Components/Particle.h b/src/Components/Particle.h index 71ac940..b169ee2 100644 --- a/src/Components/Particle.h +++ b/src/Components/Particle.h @@ -3,8 +3,6 @@ #include "System.h" #include "Component.h" -#include "Components/Transform.h" -#include "Components/ParticleEmitter.h" #include "Color.h" #include diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 242d0bc..d11cce1 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -98,9 +98,9 @@ void GameWorld::Initialize() transform->Position = glm::vec3(0); auto emitter = AddComponent(ent, "ParticleEmitter"); emitter->LifeTime = 4; - emitter->SpawnCount = 3; + emitter->SpawnCount = 1; emitter->SpreadAngle = 35; - emitter->SpawnFrequency = 0.01; + emitter->SpawnFrequency = 0.05; //emitter-> } } diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index d2ccc8c..e62218d 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -25,31 +25,37 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID { if(m_TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { - if(m_ParticleEmitter[entity].size() < 100) + //if(m_ParticleEmitter[entity].size() < 100) // TEMP { SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); m_TimeSinceLastSpawn = 0; } - std::cout<<"Particle count: "<::iterator it; - for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) + for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end(); it++) { - auto particleComponent = m_World->GetComponent(it->ParticleID, "Particle"); - - + EntityID particleID = it->ParticleID; + auto particleComponent = m_World->GetComponent(particleID, "Particle"); double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) { m_ParticleEmitter[entity].erase(it); - std::cout<<"Removed dead particle..."<GetComponent(particleID, "Transform"); + float speed = 10 * dt; + transformComponent->Position.x += it->Direction.x * speed; + transformComponent->Position.y += it->Direction.y * speed; + transformComponent->Position.z += it->Direction.z * speed; + // Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time. /*// How big fraction the color is multiplied with @@ -90,24 +96,30 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun auto ent = m_World->CreateEntity(); auto transform = m_World->AddComponent(ent, "Transform"); - transform->Position = glm::vec3(0); + transform->Position = glm::vec3(0, 20, 0); + transform->Scale = glm::vec3(1, 1, 0); auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = 4; - Color startColor = {.4f, .45f, .2f}; + /*Color startColor = {.4f, .45f, .2f}; particle->ColorSpectrum.push_back(startColor); Color endColor = {0.f, 45.f, 23.f}; particle->ColorSpectrum.push_back(endColor); particle->ScaleSpectrum.push_back(1); particle->ScaleSpectrum.push_back(30); particle->VelocitySpectrum.push_back(glm::vec3(0, -.2, 0)); - particle->VelocitySpectrum.push_back(glm::vec3(0, -3, 0)); + particle->VelocitySpectrum.push_back(glm::vec3(0, -3, 0));*/ + + auto model = m_World->AddComponent(ent, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); - data.color = particle->ColorSpectrum[0]; - data.Scale = particle->ScaleSpectrum[0]; +// data.color = particle->ColorSpectrum[0]; +// data.Scale = particle->ScaleSpectrum[0]; + data.Direction = glm::vec3(((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1); + data.Direction = glm::normalize(data.Direction); m_ParticleEmitter[emitterID].push_back(data); } diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 53bf08e..9b70082 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -5,6 +5,7 @@ #include "Components/Transform.h" #include "Components/ParticleEmitter.h" #include "Components/Particle.h" +#include "Components/Model.h" #include "Color.h" #include @@ -18,6 +19,7 @@ namespace Systems float Scale; Color color; glm::vec3 Velocity; + glm::vec3 Direction; }; class ParticleSystem : public System From b07605594d748c6feff89de1e1f4feac625086f3 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Thu, 24 Apr 2014 00:44:26 +0200 Subject: [PATCH 08/27] Fixed loop, can't miss particles. (tried to add physics to particles) --- src/Systems/ParticleSystem.cpp | 30 +++++++++++++++++++++--------- src/Systems/ParticleSystem.h | 2 ++ 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index e62218d..7cdec83 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -34,21 +34,24 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID } std::list::iterator it; - for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end(); it++) + for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { EntityID particleID = it->ParticleID; auto particleComponent = m_World->GetComponent(particleID, "Particle"); double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) { + + m_World->RemoveEntity(particleID); m_ParticleEmitter[entity].erase(it); //std::cout<<"Removed dead particle..."<GetComponent(particleID, "Transform"); float speed = 10 * dt; @@ -113,6 +116,14 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun auto model = m_World->AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; + /*auto physics = m_World->AddComponent(ent, "Physics"); + physics->Mass = 1; + + auto physicShape = m_World->AddComponent(ent, "Box"); + physicShape->Width = 0.5; + physicShape->Height = 0.5; + physicShape->Depth = 0.5;*/ + ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); @@ -123,12 +134,13 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun m_ParticleEmitter[emitterID].push_back(data); } - std::cout<<"Added "< From 166a9f48f1e9d3a2d7c2db5fe575272f362d18ba Mon Sep 17 00:00:00 2001 From: Stiffly Date: Fri, 25 Apr 2014 00:24:13 +0200 Subject: [PATCH 09/27] Added support for multiple emitters. (ParticleEmitter component now friend class to ParticleSystem) --- src/Components/ParticleEmitter.h | 7 +++++++ src/GameWorld.cpp | 27 ++++++++++++++++----------- src/Systems/ParticleSystem.cpp | 26 +++++++++++++------------- src/Systems/ParticleSystem.h | 4 ++-- 4 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 9f5e20f..46ad8fb 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -5,11 +5,15 @@ #include "Color.h" #include +namespace Systems { class ParticleSystem; } + namespace Components { struct ParticleEmitter : Component { + friend class Systems::ParticleSystem; + EntityID ParticleTemplate; float SpawnFrequency; int SpawnCount; @@ -19,6 +23,9 @@ struct ParticleEmitter : Component double LifeTime; std::vector VelocitySpectrum; std::vector AngularVelocitySpectrum; + +private: + double TimeSinceLastSpawn; }; } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index d11cce1..be62e0a 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -77,7 +77,7 @@ void GameWorld::Initialize() auto model = AddComponent(ball, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Sphere.obj"; auto sphere = AddComponent(ball, "Sphere"); - sphere->Radius = 0.5; + sphere->Radius = 0.05; auto physics = AddComponent(ball, "Physics"); physics->Mass = 1; } @@ -92,16 +92,21 @@ void GameWorld::Initialize() }*/ { - // Particle emitter - auto ent = CreateEntity(); - auto transform = AddComponent(ent, "Transform"); - transform->Position = glm::vec3(0); - auto emitter = AddComponent(ent, "ParticleEmitter"); - emitter->LifeTime = 4; - emitter->SpawnCount = 1; - emitter->SpreadAngle = 35; - emitter->SpawnFrequency = 0.05; - //emitter-> + for(int i = 0; i < 4 ; i++) + { + // Particle emitter + auto ent = CreateEntity(); + auto transform = AddComponent(ent, "Transform"); + transform->Position = glm::vec3(i * 10, 20, 0); + auto emitter = AddComponent(ent, "ParticleEmitter"); + emitter->LifeTime = 2; + emitter->SpawnCount = 1; + emitter->SpreadAngle = 35; + emitter->SpawnFrequency = 2; + auto model = AddComponent(ent, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + //emitter-> + } } } diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 7cdec83..22075bc 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -6,12 +6,12 @@ Systems::ParticleSystem::ParticleSystem(World *m_World) : System(m_World) { - m_TimeSinceLastSpawn = 0; + } void Systems::ParticleSystem::Update(double dt) { - m_TimeSinceLastSpawn += dt; + } void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID parent) @@ -23,14 +23,13 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID auto emitterComponent = m_World->GetComponent(entity, "ParticleEmitter"); if(emitterComponent) { - if(m_TimeSinceLastSpawn > emitterComponent->SpawnFrequency) + emitterComponent->TimeSinceLastSpawn += dt; + + auto transformComponent = m_World->GetComponent(entity, "Transform"); + if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { - //if(m_ParticleEmitter[entity].size() < 100) // TEMP - { - SpawnParticles(entity, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); - m_TimeSinceLastSpawn = 0; - } - //std::cout<<"Particle count: "<Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); + emitterComponent->TimeSinceLastSpawn = 0; } std::list::iterator it; @@ -54,7 +53,7 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID } auto transformComponent = m_World->GetComponent(particleID, "Transform"); - float speed = 10 * dt; + float speed = 20 * dt; transformComponent->Position.x += it->Direction.x * speed; transformComponent->Position.y += it->Direction.y * speed; transformComponent->Position.z += it->Direction.z * speed; @@ -92,14 +91,16 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) cf->Register("Particle", []() { return new Components::Particle(); }); } -void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCount, float spreadAngle) +void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle) { for(int i = 0; i < spawnCount; i++) { auto ent = m_World->CreateEntity(); auto transform = m_World->AddComponent(ent, "Transform"); - transform->Position = glm::vec3(0, 20, 0); + transform->Position.x = pos.x; + transform->Position.y = pos.y; + transform->Position.z = pos.z; transform->Scale = glm::vec3(1, 1, 0); auto particle = m_World->AddComponent(ent, "Particle"); @@ -134,7 +135,6 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, float spawnCoun m_ParticleEmitter[emitterID].push_back(data); } - //std::cout<<"Added "<> m_ParticleEmitter; - double m_TimeSinceLastSpawn; + std::map m_TimeSinceLastSpawn; }; From 956e7cab7bf8b9b10926bec4be5151d2bf3bb5bc Mon Sep 17 00:00:00 2001 From: Stiffly Date: Fri, 25 Apr 2014 04:32:15 +0200 Subject: [PATCH 10/27] BUG FIX: Iterator not increased incorrectly -> No longer a particle stuck in emitter. --- src/GameWorld.cpp | 10 ++++---- src/Systems/ParticleSystem.cpp | 43 ++++++++++++++++++---------------- src/Systems/ParticleSystem.h | 4 ++-- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index be62e0a..17d1a0f 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -49,7 +49,7 @@ void GameWorld::Initialize() model->ModelFile = "Models/Placeholders/tank/Chassi.obj"; } - for(int i = 0; i < 2; i++) + /*for(int i = 0; i < 2; i++) { auto light = CreateEntity(); auto transform = AddComponent(light, "Transform"); @@ -65,7 +65,7 @@ void GameWorld::Initialize() auto model = AddComponent(light, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; - } + }*/ for(int i = 0; i < 3; i++) { @@ -92,7 +92,7 @@ void GameWorld::Initialize() }*/ { - for(int i = 0; i < 4 ; i++) + for(int i = 0; i < 1 ; i++) { // Particle emitter auto ent = CreateEntity(); @@ -100,9 +100,9 @@ void GameWorld::Initialize() transform->Position = glm::vec3(i * 10, 20, 0); auto emitter = AddComponent(ent, "ParticleEmitter"); emitter->LifeTime = 2; - emitter->SpawnCount = 1; + emitter->SpawnCount = 3; emitter->SpreadAngle = 35; - emitter->SpawnFrequency = 2; + emitter->SpawnFrequency = 0.2; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; //emitter-> diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 22075bc..30fb6c1 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -28,14 +28,21 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID auto transformComponent = m_World->GetComponent(entity, "Transform"); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { - SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle); + SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle, emitterComponent->LifeTime); emitterComponent->TimeSinceLastSpawn = 0; } - + std::cout<<"Number of particles in list for emitter "<::iterator it; for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { - EntityID particleID = it->ParticleID; + EntityID particleID = (it)->ParticleID; + auto transformComponent = m_World->GetComponent(particleID, "Transform"); + float speed = 20 * dt; + //ERROR: Direction seems to be 0 for first particle in the list... + transformComponent->Position.x += it->Direction.x * speed; + transformComponent->Position.y += it->Direction.y * speed; + transformComponent->Position.z += it->Direction.z * speed; + auto particleComponent = m_World->GetComponent(particleID, "Particle"); double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) @@ -43,8 +50,6 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID m_World->RemoveEntity(particleID); m_ParticleEmitter[entity].erase(it); - //std::cout<<"Removed dead particle..."<GetComponent(particleID, "Transform"); - float speed = 20 * dt; - transformComponent->Position.x += it->Direction.x * speed; - transformComponent->Position.y += it->Direction.y * speed; - transformComponent->Position.z += it->Direction.z * speed; + // Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time. @@ -91,7 +92,7 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) cf->Register("Particle", []() { return new Components::Particle(); }); } -void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle) +void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime) { for(int i = 0; i < spawnCount; i++) { @@ -104,7 +105,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, transform->Scale = glm::vec3(1, 1, 0); auto particle = m_World->AddComponent(ent, "Particle"); - particle->LifeTime = 4; + particle->LifeTime = lifeTime; /*Color startColor = {.4f, .45f, .2f}; particle->ColorSpectrum.push_back(startColor); Color endColor = {0.f, 45.f, 23.f}; @@ -117,6 +118,15 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, auto model = m_World->AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; + auto light = m_World->AddComponent(ent, "PointLight"); + light->Specular = glm::vec3(0.1f, 0.1f, 0.1f); + light->Diffuse = glm::vec3(1.f, 1.f, 0.f); + light->constantAttenuation = 0.03f; + light->linearAttenuation = 0.00009f; + light->quadraticAttenuation = 0.07f; + light->spotExponent = 0.0f; + + /*auto physics = m_World->AddComponent(ent, "Physics"); physics->Mass = 1; @@ -130,17 +140,10 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, data.SpawnTime = glfwGetTime(); // data.color = particle->ColorSpectrum[0]; // data.Scale = particle->ScaleSpectrum[0]; + //Random between [-1,1] on every axis data.Direction = glm::vec3(((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1); data.Direction = glm::normalize(data.Direction); m_ParticleEmitter[emitterID].push_back(data); } } - - -// void Systems::ParticleSystem::Draw(double dt) -// { -// -// } - - diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 484a212..5b0ae2e 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -8,6 +8,7 @@ #include "Components/Model.h" #include "Components/Physics.h" #include "Components/Box.h" +#include "Components/PointLight.h" #include "Color.h" #include @@ -32,9 +33,8 @@ public: void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; - void Draw(double dt); private: - void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle); + void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime); std::map> m_ParticleEmitter; std::map m_TimeSinceLastSpawn; From 023f49b0ec1738611793f8a2ca70ef79785a8967 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Sun, 27 Apr 2014 00:17:21 +0200 Subject: [PATCH 11/27] Implemented support for spread angle for particle emitter. --- src/GameWorld.cpp | 18 ++++++------ src/Systems/ParticleSystem.cpp | 52 +++++++++++++++++++--------------- src/Systems/ParticleSystem.h | 6 ++-- 3 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 17d1a0f..3edaf44 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -40,14 +40,14 @@ void GameWorld::Initialize() physics->Mass = 10; } - { + /*{ auto TankTest = CreateEntity(); auto transform = AddComponent(TankTest, "Transform"); transform->Position = glm::vec3(1.5f, 0.7f, 5.f); auto model = AddComponent(TankTest, "Model"); model->ModelFile = "Models/Placeholders/tank/Chassi.obj"; - } + }*/ /*for(int i = 0; i < 2; i++) { @@ -67,7 +67,7 @@ void GameWorld::Initialize() model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; }*/ - for(int i = 0; i < 3; i++) + /*for(int i = 0; i < 3; i++) { auto ball = CreateEntity(); auto transform = AddComponent(ball, "Transform"); @@ -80,7 +80,7 @@ void GameWorld::Initialize() sphere->Radius = 0.05; auto physics = AddComponent(ball, "Physics"); physics->Mass = 1; - } + }*/ /*{ auto entity = CreateEntity(); @@ -97,15 +97,15 @@ void GameWorld::Initialize() // Particle emitter auto ent = CreateEntity(); auto transform = AddComponent(ent, "Transform"); + transform->Orientation = glm::angleAxis(glm::pi()/4, glm::vec3(0,1,0)); transform->Position = glm::vec3(i * 10, 20, 0); auto emitter = AddComponent(ent, "ParticleEmitter"); - emitter->LifeTime = 2; - emitter->SpawnCount = 3; - emitter->SpreadAngle = 35; - emitter->SpawnFrequency = 0.2; + emitter->LifeTime = 4; + emitter->SpawnCount = 4; + emitter->SpreadAngle = glm::pi()/6; + emitter->SpawnFrequency = 0.08; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; - //emitter-> } } } diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 30fb6c1..a5479f2 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -28,21 +28,18 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID auto transformComponent = m_World->GetComponent(entity, "Transform"); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { - SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle, emitterComponent->LifeTime); + SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle, emitterComponent->LifeTime, dt); emitterComponent->TimeSinceLastSpawn = 0; } - std::cout<<"Number of particles in list for emitter "<::iterator it; for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { EntityID particleID = (it)->ParticleID; auto transformComponent = m_World->GetComponent(particleID, "Transform"); - float speed = 20 * dt; - //ERROR: Direction seems to be 0 for first particle in the list... - transformComponent->Position.x += it->Direction.x * speed; - transformComponent->Position.y += it->Direction.y * speed; - transformComponent->Position.z += it->Direction.z * speed; + transformComponent->Position += transformComponent->Velocity; + auto particleComponent = m_World->GetComponent(particleID, "Particle"); double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) @@ -57,9 +54,6 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID it++; } - - - // Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time. /*// How big fraction the color is multiplied with float timeProgress = timeLived / particleComponent->LifeTime; @@ -92,20 +86,30 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) cf->Register("Particle", []() { return new Components::Particle(); }); } -void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime) +void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime, double dt) { + auto emitterTransform = m_World->GetComponent(emitterID, "Transform"); + glm::quat emitterOrientation = emitterTransform->Orientation; + float tempSpeed = 5 * dt; + glm::vec3 speed = glm::vec3(tempSpeed); for(int i = 0; i < spawnCount; i++) { auto ent = m_World->CreateEntity(); - auto transform = m_World->AddComponent(ent, "Transform"); - transform->Position.x = pos.x; - transform->Position.y = pos.y; - transform->Position.z = pos.z; - transform->Scale = glm::vec3(1, 1, 0); - + auto particleTransform = m_World->AddComponent(ent, "Transform"); + particleTransform->Position.x = pos.x; + particleTransform->Position.y = pos.y; + particleTransform->Position.z = pos.z; + 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. + particleTransform->Velocity = emitterOrientation * glm::vec3(0, 0, -1) * speed * + glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(1, 0, 0))) * + glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))); + + auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = lifeTime; + /*Color startColor = {.4f, .45f, .2f}; particle->ColorSpectrum.push_back(startColor); Color endColor = {0.f, 45.f, 23.f}; @@ -116,15 +120,15 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, particle->VelocitySpectrum.push_back(glm::vec3(0, -3, 0));*/ auto model = m_World->AddComponent(ent, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/Cube.obj"; + model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; - auto light = m_World->AddComponent(ent, "PointLight"); + /*auto light = m_World->AddComponent(ent, "PointLight"); light->Specular = glm::vec3(0.1f, 0.1f, 0.1f); light->Diffuse = glm::vec3(1.f, 1.f, 0.f); light->constantAttenuation = 0.03f; light->linearAttenuation = 0.00009f; light->quadraticAttenuation = 0.07f; - light->spotExponent = 0.0f; + light->spotExponent = 0.0f;*/ /*auto physics = m_World->AddComponent(ent, "Physics"); @@ -140,10 +144,12 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, data.SpawnTime = glfwGetTime(); // data.color = particle->ColorSpectrum[0]; // data.Scale = particle->ScaleSpectrum[0]; - //Random between [-1,1] on every axis - data.Direction = glm::vec3(((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1, ((double)rand() / ((double)RAND_MAX + 1) * 2) -1); - data.Direction = glm::normalize(data.Direction); m_ParticleEmitter[emitterID].push_back(data); } } + +float Systems::ParticleSystem::RandomizeAngle(float spreadAngle) +{ + return ((float)rand() / ((float)RAND_MAX + 1) * spreadAngle) - spreadAngle/2; +} \ No newline at end of file diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 5b0ae2e..059eb2f 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -21,8 +21,6 @@ namespace Systems double SpawnTime; float Scale; Color color; - glm::vec3 Velocity; - glm::vec3 Direction; }; class ParticleSystem : public System @@ -34,9 +32,11 @@ public: void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; private: - void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime); + void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime, double dt); std::map> m_ParticleEmitter; std::map m_TimeSinceLastSpawn; + + float RandomizeAngle(float spreadAngle); }; From 28ac8980f2fdc0edc4daa295dd324745353cd657 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Sun, 27 Apr 2014 03:20:14 +0200 Subject: [PATCH 12/27] tested and fixed scale and velocity interpolation. Fix: could not go from lesser value to larger. Added methods for interpolation. --- src/GameWorld.cpp | 4 +- src/Systems/ParticleSystem.cpp | 94 ++++++++++++++++++---------------- src/Systems/ParticleSystem.h | 3 +- 3 files changed, 53 insertions(+), 48 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 3edaf44..d4bba0f 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -97,12 +97,12 @@ void GameWorld::Initialize() // Particle emitter auto ent = CreateEntity(); auto transform = AddComponent(ent, "Transform"); - transform->Orientation = glm::angleAxis(glm::pi()/4, glm::vec3(0,1,0)); + transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); transform->Position = glm::vec3(i * 10, 20, 0); auto emitter = AddComponent(ent, "ParticleEmitter"); emitter->LifeTime = 4; emitter->SpawnCount = 4; - emitter->SpreadAngle = glm::pi()/6; + emitter->SpreadAngle = glm::pi()/4; emitter->SpawnFrequency = 0.08; 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 a5479f2..b833de5 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -37,10 +37,8 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID { EntityID particleID = (it)->ParticleID; auto transformComponent = m_World->GetComponent(particleID, "Transform"); - - transformComponent->Position += transformComponent->Velocity; - auto particleComponent = m_World->GetComponent(particleID, "Particle"); + double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) { @@ -54,28 +52,20 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID it++; } - // Interpolates the color for each color channel by the start and end value. Decides how much the color should be interpolated based on time. - /*// How big fraction the color is multiplied with float timeProgress = timeLived / particleComponent->LifeTime; // The difference between the start and end value - float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); + /*float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress; deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g); it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress; deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); - it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress; + it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress;*/ - //Interpolates the scale of the particle - float deltaScale = glm::abs(particleComponent->ScaleSpectrum[0] - particleComponent->ScaleSpectrum[1]); - it->Scale = particleComponent->ScaleSpectrum[0] + deltaScale * timeProgress; + + ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); - //Interpolates the velocity of the particle - float deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].x - particleComponent->VelocitySpectrum[1].x); - it->Velocity.x = particleComponent->VelocitySpectrum[0].x + deltaVelocity * timeProgress; - deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].y - particleComponent->VelocitySpectrum[1].y); - it->Velocity.y = particleComponent->VelocitySpectrum[0].y + deltaVelocity * timeProgress; - deltaVelocity = glm::abs(particleComponent->VelocitySpectrum[0].z - particleComponent->VelocitySpectrum[1].z); - it->Velocity.z = particleComponent->VelocitySpectrum[0].z + deltaVelocity * timeProgress;*/ + transformComponent->Position += transformComponent->Velocity; } } } @@ -90,8 +80,10 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, { auto emitterTransform = m_World->GetComponent(emitterID, "Transform"); glm::quat emitterOrientation = emitterTransform->Orientation; - float tempSpeed = 5 * dt; + + float tempSpeed = 4 * dt; glm::vec3 speed = glm::vec3(tempSpeed); + for(int i = 0; i < spawnCount; i++) { auto ent = m_World->CreateEntity(); @@ -104,41 +96,26 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, //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. particleTransform->Velocity = emitterOrientation * glm::vec3(0, 0, -1) * speed * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(1, 0, 0))) * - glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))); + glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) * + glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1))); + glm::vec3 testVel = glm::vec3(particleTransform->Velocity.x, -particleTransform->Velocity.y * 1.5, particleTransform->Velocity.z); //TEMP auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = lifeTime; - - /*Color startColor = {.4f, .45f, .2f}; - particle->ColorSpectrum.push_back(startColor); - Color endColor = {0.f, 45.f, 23.f}; - particle->ColorSpectrum.push_back(endColor); - particle->ScaleSpectrum.push_back(1); - particle->ScaleSpectrum.push_back(30); - particle->VelocitySpectrum.push_back(glm::vec3(0, -.2, 0)); - particle->VelocitySpectrum.push_back(glm::vec3(0, -3, 0));*/ + particle->ScaleSpectrum.push_back(4); //TEMP + particle->ScaleSpectrum.push_back(1); //TEMP + particle->VelocitySpectrum.push_back(particleTransform->Velocity); //TEMP + particle->VelocitySpectrum.push_back(testVel); //TEMP + +// Color startColor = {.4f, .45f, .2f}; +// particle->ColorSpectrum.push_back(startColor); +// Color endColor = {0.f, 45.f, 23.f}; +// particle->ColorSpectrum.push_back(endColor); auto model = m_World->AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; - /*auto light = m_World->AddComponent(ent, "PointLight"); - light->Specular = glm::vec3(0.1f, 0.1f, 0.1f); - light->Diffuse = glm::vec3(1.f, 1.f, 0.f); - light->constantAttenuation = 0.03f; - light->linearAttenuation = 0.00009f; - light->quadraticAttenuation = 0.07f; - light->spotExponent = 0.0f;*/ - - - /*auto physics = m_World->AddComponent(ent, "Physics"); - physics->Mass = 1; - - auto physicShape = m_World->AddComponent(ent, "Box"); - physicShape->Width = 0.5; - physicShape->Height = 0.5; - physicShape->Depth = 0.5;*/ - ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); @@ -149,7 +126,34 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, } } +//Randomizes between -spreadAngle/2 and spreadAngle/2 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 scaleSpectrum, glm::vec3 &scale) +{ + float deltaScale = glm::abs(scaleSpectrum[0] - scaleSpectrum[1]); + if(scaleSpectrum[0] > scaleSpectrum[1]) + deltaScale *= -1; + scale = glm::vec3(scaleSpectrum[0] + deltaScale * timeProgress); +} + +//Interpolates the velocity of the particle +void Systems::ParticleSystem::VelocityInterpolation(double timeProgress, std::vector velocitySpectrum, glm::vec3 &velocity) +{ + float deltaVelocity = glm::abs(velocitySpectrum[0].x - velocitySpectrum[1].x); + if(velocitySpectrum[0].x > velocitySpectrum[1].x) + deltaVelocity *= -1; + velocity.x = velocitySpectrum[0].x + deltaVelocity * timeProgress; + deltaVelocity = glm::abs(velocitySpectrum[0].y - velocitySpectrum[1].y); + if (velocitySpectrum[0].y > velocitySpectrum[1].y) + deltaVelocity *= -1; + velocity.y = velocitySpectrum[0].y + deltaVelocity * timeProgress; + deltaVelocity = glm::abs(velocitySpectrum[0].z - velocitySpectrum[1].z); + if(velocitySpectrum[0].z > velocitySpectrum[1].z) + deltaVelocity *= -1; + velocity.z = velocitySpectrum[0].z + deltaVelocity * timeProgress; } \ No newline at end of file diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 059eb2f..8940794 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -37,7 +37,8 @@ private: std::map m_TimeSinceLastSpawn; float RandomizeAngle(float spreadAngle); - + void ScaleInterpolation(double timeProgress, std::vector scaleSpectrum, glm::vec3 &scale); + void VelocityInterpolation(double timeProgress, std::vector velocitySpectrum, glm::vec3 &velocity); }; } From 04cafc53e227c0bf26f1b278c8be44d407569477 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Sun, 27 Apr 2014 07:48:16 +0200 Subject: [PATCH 13/27] Implemented entity cloning --- src/Component.h | 2 + src/Components/Box.h | 2 + src/Components/Camera.h | 2 + src/Components/DirectionalLight.h | 2 + src/Components/FreeSteering.h | 2 + src/Components/Input.h | 2 + src/Components/Model.h | 2 + src/Components/Particle.h | 2 + src/Components/ParticleEmitter.h | 2 + src/Components/Physics.h | 2 + src/Components/PointLight.h | 2 + src/Components/SoundEmitter.h | 2 + src/Components/Sphere.h | 2 + src/Components/Sprite.h | 2 + src/Components/Template.h | 7 ++- src/Components/Transform.h | 4 +- src/World.cpp | 55 ++++++++++++++++++- src/World.h | 21 ++++--- .../Returngeance/Returngeance.vcxproj.filters | 8 ++- 19 files changed, 107 insertions(+), 16 deletions(-) diff --git a/src/Component.h b/src/Component.h index 21d59f6..05e0628 100755 --- a/src/Component.h +++ b/src/Component.h @@ -7,6 +7,8 @@ struct Component { EntityID Entity; + + virtual Component* Clone() const = 0; }; class ComponentFactory : public Factory { }; diff --git a/src/Components/Box.h b/src/Components/Box.h index b9ddea7..3fe3687 100644 --- a/src/Components/Box.h +++ b/src/Components/Box.h @@ -14,6 +14,8 @@ struct Box : Component float Width; float Height; float Depth; + + virtual Box* Clone() const override { return new Box(*this); } }; } diff --git a/src/Components/Camera.h b/src/Components/Camera.h index 2cd379d..91e55b5 100755 --- a/src/Components/Camera.h +++ b/src/Components/Camera.h @@ -13,6 +13,8 @@ struct Camera : Component float FOV; float NearClip; float FarClip; + + virtual Camera* Clone() const override { return new Camera(*this); } }; } diff --git a/src/Components/DirectionalLight.h b/src/Components/DirectionalLight.h index ee2dd72..1d7de1e 100755 --- a/src/Components/DirectionalLight.h +++ b/src/Components/DirectionalLight.h @@ -13,6 +13,8 @@ struct DirectionalLight : Component float MaxRange; float SpecularIntensity; Color Color; + + virtual DirectionalLight* Clone() const override { return new DirectionalLight(*this); } }; } diff --git a/src/Components/FreeSteering.h b/src/Components/FreeSteering.h index a972eaf..ba40534 100755 --- a/src/Components/FreeSteering.h +++ b/src/Components/FreeSteering.h @@ -9,6 +9,8 @@ struct FreeSteering : Component { FreeSteering() : Speed(35) { } float Speed; + + virtual FreeSteering* Clone() const override { return new FreeSteering(*this); } }; } diff --git a/src/Components/Input.h b/src/Components/Input.h index b1c11b5..15cb286 100755 --- a/src/Components/Input.h +++ b/src/Components/Input.h @@ -18,6 +18,8 @@ struct Input : Component std::array LastMouseState; float dX, dY; float WheelDelta; + + virtual Input* Clone() const override { return new Input(*this); } }; } diff --git a/src/Components/Model.h b/src/Components/Model.h index ad3b4f3..cacfe17 100755 --- a/src/Components/Model.h +++ b/src/Components/Model.h @@ -16,6 +16,8 @@ struct Model : Component Color Color; bool Visible; bool ShadowCaster; + + virtual Model* Clone() const override { return new Model(*this); } }; } diff --git a/src/Components/Particle.h b/src/Components/Particle.h index b169ee2..dfa3075 100644 --- a/src/Components/Particle.h +++ b/src/Components/Particle.h @@ -16,6 +16,8 @@ namespace Components double LifeTime; std::vector VelocitySpectrum; std::vector AngularVelocitySpectrum; + + virtual Particle* Clone() const override { return new Particle(*this); } }; } diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 46ad8fb..5aafcdc 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -24,6 +24,8 @@ struct ParticleEmitter : Component std::vector VelocitySpectrum; std::vector AngularVelocitySpectrum; + virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); } + private: double TimeSinceLastSpawn; }; diff --git a/src/Components/Physics.h b/src/Components/Physics.h index 956ae7a..98a682f 100644 --- a/src/Components/Physics.h +++ b/src/Components/Physics.h @@ -12,6 +12,8 @@ struct Physics : Component : Mass(0.f) { } float Mass; + + virtual Physics* Clone() const override { return new Physics(*this); } }; } diff --git a/src/Components/PointLight.h b/src/Components/PointLight.h index a7a5d4a..248fec6 100755 --- a/src/Components/PointLight.h +++ b/src/Components/PointLight.h @@ -16,6 +16,8 @@ struct PointLight : Component float constantAttenuation, linearAttenuation, quadraticAttenuation; float spotExponent; Color color; + + virtual PointLight* Clone() const override { return new PointLight(*this); } }; } diff --git a/src/Components/SoundEmitter.h b/src/Components/SoundEmitter.h index b4c74ee..05c5f15 100755 --- a/src/Components/SoundEmitter.h +++ b/src/Components/SoundEmitter.h @@ -17,6 +17,8 @@ struct SoundEmitter : Component float Pitch; bool Loop; std::string Path; + + virtual SoundEmitter* Clone() const override { return new SoundEmitter(*this); } }; } diff --git a/src/Components/Sphere.h b/src/Components/Sphere.h index 2d089dd..f92e418 100644 --- a/src/Components/Sphere.h +++ b/src/Components/Sphere.h @@ -12,6 +12,8 @@ struct Sphere : Component : Radius(1.f){ } float Radius; + + virtual Sphere* Clone() const override { return new Sphere(*this); } }; } diff --git a/src/Components/Sprite.h b/src/Components/Sprite.h index f54d3d0..29c930f 100755 --- a/src/Components/Sprite.h +++ b/src/Components/Sprite.h @@ -13,6 +13,8 @@ struct Sprite : Component { std::string SpriteFile; Color Color; + + virtual Sprite* Clone() const override { return new Sprite(*this); } }; } diff --git a/src/Components/Template.h b/src/Components/Template.h index 4bcb25c..613622c 100755 --- a/src/Components/Template.h +++ b/src/Components/Template.h @@ -6,7 +6,12 @@ namespace Components { -struct Template : Component { }; +struct Template + : public Component +{ + virtual Template* Clone() const override { return nullptr; } +}; } + #endif // !Components_Template_h__ \ No newline at end of file diff --git a/src/Components/Transform.h b/src/Components/Transform.h index 2bfd58d..26b701e 100755 --- a/src/Components/Transform.h +++ b/src/Components/Transform.h @@ -6,7 +6,7 @@ namespace Components { -struct Transform : Component +struct Transform : public Component { Transform() : Scale(glm::vec3(1.f)) { } @@ -15,6 +15,8 @@ struct Transform : Component glm::quat Orientation; glm::vec3 Velocity; glm::vec3 Scale; + + virtual Transform* Clone() const override { return new Transform(*this); } }; } diff --git a/src/World.cpp b/src/World.cpp index 78b297d..ef9d236 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -93,6 +93,7 @@ void World::ProcessEntityRemovals() for (auto entity : m_EntitiesToRemove) { m_EntityParents.erase(entity); + m_EntityChildren.erase(entity); // Remove components for (auto pair : m_EntityComponents[entity]) { @@ -116,7 +117,8 @@ void World::ProcessEntityRemovals() EntityID World::CreateEntity(EntityID parent /*= 0*/) { EntityID newEntity = GenerateEntityID(); - m_EntityParents.insert(std::pair(newEntity, parent)); + m_EntityParents[newEntity] = parent; + m_EntityChildren[parent].push_back(newEntity); return newEntity; } @@ -148,7 +150,58 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string comp return AddComponent(entity, componentType); } +void World::AddComponent(EntityID entity, std::string componentType, std::shared_ptr component) +{ + component->Entity = entity; + m_ComponentsOfType[componentType].push_back(component); + m_EntityComponents[entity][componentType] = component; + for (auto pair : m_Systems) + { + auto system = pair.second; + system->OnComponentCreated(componentType, component); + } +} + void World::AddSystem(std::string systemType) { m_Systems[systemType] = std::shared_ptr(m_SystemFactory.Create(systemType)); } + +EntityID World::CloneEntity(EntityID entity, EntityID parent /* = 0 */) +{ + int clone = CreateEntity(parent); + + for (auto pair : m_EntityComponents[entity]) + { + auto type = pair.first; + auto component = std::shared_ptr(pair.second->Clone()); + if (component != nullptr) + { + AddComponent(clone, type, component); + } + } + + auto itChildren = m_EntityChildren.find(entity); + if (itChildren != m_EntityChildren.end()) + { + for (EntityID child : itChildren->second) + { + CloneEntity(child, clone); + } + } + + return clone; +} + +std::list World::GetEntityChildren(EntityID entity) +{ + auto it = m_EntityChildren.find(entity); + if (it == m_EntityChildren.end()) + { + return std::list(); + } + else + { + return it->second; + } +} diff --git a/src/World.h b/src/World.h index 9deeab9..d0c0692 100755 --- a/src/World.h +++ b/src/World.h @@ -34,6 +34,7 @@ public: std::shared_ptr GetSystem(std::string systemType); EntityID CreateEntity(EntityID parent = 0); + EntityID CloneEntity(EntityID entity, EntityID parent = 0); void RemoveEntity(EntityID entity); @@ -41,6 +42,7 @@ public: EntityID GetEntityParent(EntityID entity); EntityID GetEntityBaseParent(EntityID entity); + std::list GetEntityChildren(EntityID entity); template T GetProperty(EntityID entity, std::string property) @@ -83,13 +85,16 @@ protected: EntityID m_LastEntityID; std::stack m_RecycledEntityIDs; - // A bottom to top tree. A map of child entities to parent entities. - std::unordered_map m_EntityParents; - std::unordered_map> m_EntityProperties; + std::unordered_map m_EntityParents; // child -> parent + std::unordered_map> m_EntityChildren; // parent -> child + std::unordered_map> m_EntityProperties; std::unordered_map>> m_ComponentsOfType; std::unordered_map>> m_EntityComponents; + // Internal: Add a component to an entity + void AddComponent(EntityID entity, std::string componentType, std::shared_ptr component); + std::list m_EntitiesToRemove; void ProcessEntityRemovals(); @@ -121,14 +126,8 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp return nullptr; } - component->Entity = entity; - m_ComponentsOfType[componentType].push_back(component); - m_EntityComponents[entity][componentType] = component; - for (auto pair : m_Systems) - { - auto system = pair.second; - system->OnComponentCreated(componentType, component); - } + AddComponent(entity, componentType, component); + return component; } diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 35f6ce6..2e7f203 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -215,14 +215,18 @@ Audio - - Particle System\Systems Particle System\Components + + Physics\Components + + + Physics\Components + From 0b8a3230c308aed83c307ffcd15dfebe130e2a6e Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 28 Apr 2014 16:18:08 +0200 Subject: [PATCH 14/27] Fixed GetComponent bug which would create invalid components if the entity didn't have the requested component --- src/World.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/World.h b/src/World.h index d0c0692..8829933 100755 --- a/src/World.h +++ b/src/World.h @@ -135,7 +135,16 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp template T* World::GetComponent(EntityID entity, std::string componentType) { - return (T*)m_EntityComponents[entity][componentType].get(); + auto components = m_EntityComponents[entity]; + auto it = components.find(componentType); + if (it != components.end()) + { + return static_cast(it->second.get()); + } + else + { + return nullptr; + } } #endif // World_h__ \ No newline at end of file From 5e1355f3fe4c0aeb099c51cd1cf2883c398e6473 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Mon, 28 Apr 2014 04:00:53 +0200 Subject: [PATCH 15/27] Optimizations (cherry picked from commit 7e32946173f0151eb912a69cc7d234754e3f7ae8) --- src/Systems/RenderSystem.cpp | 7 ++++--- src/Systems/TransformSystem.cpp | 31 +++++++++++++++++++++++++++++++ src/Systems/TransformSystem.h | 3 ++- src/World.cpp | 9 +++------ 4 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index d8c3c19..9e2b426 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -23,10 +23,11 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa auto model = m_World->GetResourceManager()->Load("Model", modelComponent->ModelFile); if (model != nullptr) { - glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); + /*glm::vec3 position = m_TransformSystem->AbsolutePosition(entity); glm::quat orientation = m_TransformSystem->AbsoluteOrientation(entity); - glm::vec3 scale = m_TransformSystem->AbsoluteScale(entity); - m_Renderer->AddModelToDraw(model, position, orientation, scale, modelComponent->Visible, modelComponent->ShadowCaster); + glm::vec3 scale = m_TransformSystem->AbsoluteScale(entity);*/ + Components::Transform absoluteTransform = m_TransformSystem->AbsoluteTransform(entity); + m_Renderer->AddModelToDraw(model, absoluteTransform.Position, absoluteTransform.Orientation, absoluteTransform.Scale, modelComponent->Visible, modelComponent->ShadowCaster); } } diff --git a/src/Systems/TransformSystem.cpp b/src/Systems/TransformSystem.cpp index a23d92b..0d1767d 100755 --- a/src/Systems/TransformSystem.cpp +++ b/src/Systems/TransformSystem.cpp @@ -60,3 +60,34 @@ glm::vec3 Systems::TransformSystem::AbsoluteScale(EntityID entity) return absScale; } + +Components::Transform Systems::TransformSystem::AbsoluteTransform(EntityID entity) +{ + glm::vec3 absPosition; + glm::quat absOrientation; + glm::vec3 absScale(1); + + do + { + auto transform = m_World->GetComponent(entity, "Transform"); + entity = m_World->GetEntityParent(entity); + auto transform2 = m_World->GetComponent(entity, "Transform"); + + // Position + if (entity != 0) + absPosition += transform2->Orientation * transform->Position; + else + absPosition += transform->Position; + // Orientation + absOrientation = transform->Orientation * absOrientation; + // Scale + absScale *= transform->Scale; + } while (entity != 0); + + Components::Transform transform; + transform.Position = absPosition; + transform.Orientation = absOrientation; + transform.Scale = absScale; + + return transform; +} diff --git a/src/Systems/TransformSystem.h b/src/Systems/TransformSystem.h index 542000d..754a567 100755 --- a/src/Systems/TransformSystem.h +++ b/src/Systems/TransformSystem.h @@ -15,7 +15,8 @@ public: //void Update(double dt) override; //void UpdateEntity(double dt, EntityID entity, EntityID parent) override; - + + Components::Transform AbsoluteTransform(EntityID entity); glm::vec3 AbsolutePosition(EntityID entity); glm::quat AbsoluteOrientation(EntityID entity); glm::vec3 AbsoluteScale(EntityID entity); diff --git a/src/World.cpp b/src/World.cpp index ef9d236..e6a10b8 100755 --- a/src/World.cpp +++ b/src/World.cpp @@ -22,16 +22,13 @@ EntityID World::GenerateEntityID() void World::RecursiveUpdate(std::shared_ptr system, double dt, EntityID parentEntity) { - for (auto pair : m_EntityParents) + for (auto &pair : m_EntityParents) { EntityID child = pair.first; EntityID parent = pair.second; - if (parent == parentEntity) - { - system->UpdateEntity(dt, child, parent); - RecursiveUpdate(system, dt, child); - } + system->UpdateEntity(dt, child, parent); + //RecursiveUpdate(system, dt, child); } } From d917be0135d951745ff8b8a7119f9b11b2c1ca1f Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 28 Apr 2014 17:55:51 +0200 Subject: [PATCH 16/27] Default values for ParticleEmitter.h --- src/Components/ParticleEmitter.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 5aafcdc..afb5622 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -14,6 +14,13 @@ struct ParticleEmitter : Component { friend class Systems::ParticleSystem; + ParticleEmitter() + : SpawnFrequency(0) + , SpawnCount(0) + , SpreadAngle(0) + , LifeTime(0) + , TimeSinceLastSpawn(0) { } + EntityID ParticleTemplate; float SpawnFrequency; int SpawnCount; From ea4b7b005e160d441a2a4f9e6072f1b5e513c07a Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 28 Apr 2014 17:56:05 +0200 Subject: [PATCH 17/27] Now use the CloneEntity() function to spawn new particles. --- src/GameWorld.cpp | 7 ++++ src/Systems/ParticleSystem.cpp | 63 +++++++++++++++++----------------- src/Systems/ParticleSystem.h | 11 +++--- src/World.h | 7 ++++ 4 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index d4bba0f..8d75f10 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -106,6 +106,13 @@ void GameWorld::Initialize() emitter->SpawnFrequency = 0.08; auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + + auto particleEnt = CreateEntity(); + AddComponent(particleEnt, "Transform"); + model = AddComponent(particleEnt, "Model"); + model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + + emitter->ParticleTemplate = particleEnt; } } } diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index b833de5..e274e3f 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -28,7 +28,7 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID auto transformComponent = m_World->GetComponent(entity, "Transform"); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { - SpawnParticles(entity, transformComponent->Position, emitterComponent->SpawnCount, emitterComponent->SpreadAngle, emitterComponent->LifeTime, dt); + SpawnParticles(entity); emitterComponent->TimeSinceLastSpawn = 0; } @@ -42,30 +42,28 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID double timeLived = glfwGetTime() - it->SpawnTime; if(timeLived > particleComponent->LifeTime) { - m_World->RemoveEntity(particleID); - m_ParticleEmitter[entity].erase(it); - break; + it = m_ParticleEmitter[entity].erase(it); } else { - it++; - } - - float timeProgress = timeLived / particleComponent->LifeTime; - // The difference between the start and end value - /*float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); - it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress; - deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g); - it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress; - deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); - it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress;*/ + float timeProgress = timeLived / particleComponent->LifeTime; + // The difference between the start and end value + /*float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); + it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress; + deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g); + it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress; + deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); + it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress;*/ - ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); - VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); + ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); - transformComponent->Position += transformComponent->Velocity; + transformComponent->Position += transformComponent->Velocity * (float)dt; + + it++; + } } } } @@ -76,24 +74,26 @@ void Systems::ParticleSystem::RegisterComponents(ComponentFactory* cf) cf->Register("Particle", []() { return new Components::Particle(); }); } -void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime, double dt) + +void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) { + auto emitterComponent = m_World->GetComponent(emitterID, "ParticleEmitter"); auto emitterTransform = m_World->GetComponent(emitterID, "Transform"); glm::quat emitterOrientation = emitterTransform->Orientation; - float tempSpeed = 4 * dt; + float tempSpeed = 4; glm::vec3 speed = glm::vec3(tempSpeed); - for(int i = 0; i < spawnCount; i++) + for(int i = 0; i < emitterComponent->SpawnCount; i++) { - auto ent = m_World->CreateEntity(); - - auto particleTransform = m_World->AddComponent(ent, "Transform"); - particleTransform->Position.x = pos.x; - particleTransform->Position.y = pos.y; - particleTransform->Position.z = pos.z; + auto ent = m_World->CloneEntity(emitterComponent->ParticleTemplate); + + auto particleTransform = m_World->GetComponent(ent, "Transform"); + 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. + float spreadAngle = emitterComponent->SpreadAngle; particleTransform->Velocity = emitterOrientation * glm::vec3(0, 0, -1) * speed * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(1, 0, 0))) * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) * @@ -102,19 +102,20 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID, glm::vec3 pos, glm::vec3 testVel = glm::vec3(particleTransform->Velocity.x, -particleTransform->Velocity.y * 1.5, particleTransform->Velocity.z); //TEMP auto particle = m_World->AddComponent(ent, "Particle"); - particle->LifeTime = lifeTime; + particle->LifeTime = emitterComponent->LifeTime; particle->ScaleSpectrum.push_back(4); //TEMP - particle->ScaleSpectrum.push_back(1); //TEMP + particle->ScaleSpectrum.push_back(0); //TEMP particle->VelocitySpectrum.push_back(particleTransform->Velocity); //TEMP particle->VelocitySpectrum.push_back(testVel); //TEMP +// particle->AngularVelocitySpectrum.push_back(); +// particle->AngularVelocitySpectrum.push_back(); // Color startColor = {.4f, .45f, .2f}; // particle->ColorSpectrum.push_back(startColor); // Color endColor = {0.f, 45.f, 23.f}; // particle->ColorSpectrum.push_back(endColor); - auto model = m_World->AddComponent(ent, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + ParticleData data; data.ParticleID = ent; diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 8940794..33d14ae 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -28,17 +28,18 @@ class ParticleSystem : public System public: ParticleSystem(World* world); void RegisterComponents(ComponentFactory* cf) override; - void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; private: - void SpawnParticles(EntityID emitterID, glm::vec3 pos, float spawnCount, float spreadAngle, double lifeTime, double dt); - std::map> m_ParticleEmitter; - std::map m_TimeSinceLastSpawn; - + 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 Billboard(); + std::map> m_ParticleEmitter; + std::map m_TimeSinceLastSpawn; + + }; } diff --git a/src/World.h b/src/World.h index 8829933..c8c55c4 100755 --- a/src/World.h +++ b/src/World.h @@ -135,7 +135,14 @@ std::shared_ptr World::AddComponent(EntityID entity, std::string componentTyp template T* World::GetComponent(EntityID entity, std::string componentType) { + + /*auto it0 = m_EntityComponents.find(entity); + + if (it0 == m_EntityComponents.end()) + return nullptr;*/ + auto components = m_EntityComponents[entity]; + auto it = components.find(componentType); if (it != components.end()) { From 68761c9291359dd830a58b929aaa862cf717dc84 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 28 Apr 2014 19:12:51 +0200 Subject: [PATCH 18/27] Angular velocity support --- src/Components/Particle.h | 2 +- src/Components/ParticleEmitter.h | 2 +- src/GameWorld.cpp | 6 ++-- src/Systems/ParticleSystem.cpp | 53 +++++++++++++++++++++----------- src/Systems/ParticleSystem.h | 3 ++ 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/src/Components/Particle.h b/src/Components/Particle.h index dfa3075..a4725b3 100644 --- a/src/Components/Particle.h +++ b/src/Components/Particle.h @@ -15,7 +15,7 @@ namespace Components std::vector ScaleSpectrum; double LifeTime; std::vector VelocitySpectrum; - std::vector AngularVelocitySpectrum; + std::vector AngularVelocitySpectrum; virtual Particle* Clone() const override { return new Particle(*this); } }; diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index afb5622..6ddd302 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -29,7 +29,7 @@ struct ParticleEmitter : Component float SpreadAngle; double LifeTime; std::vector VelocitySpectrum; - std::vector AngularVelocitySpectrum; + std::vector AngularVelocitySpectrum; virtual ParticleEmitter* Clone() const override { return new ParticleEmitter(*this); } diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 8d75f10..f63c86a 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -101,9 +101,9 @@ void GameWorld::Initialize() transform->Position = glm::vec3(i * 10, 20, 0); auto emitter = AddComponent(ent, "ParticleEmitter"); emitter->LifeTime = 4; - emitter->SpawnCount = 4; - emitter->SpreadAngle = glm::pi()/4; - emitter->SpawnFrequency = 0.08; + emitter->SpawnCount = 1; + emitter->SpreadAngle = glm::pi()/20; + emitter->SpawnFrequency = 0.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 e274e3f..5ee695c 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -47,19 +47,18 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID } else { - float timeProgress = timeLived / particleComponent->LifeTime; - // The difference between the start and end value - /*float deltaColor = glm::abs(particleComponent->ColorSpectrum[0].r - particleComponent->ColorSpectrum[1].r); - it->color.r = particleComponent->ColorSpectrum[0].r + deltaColor * timeProgress; - deltaColor = glm::abs(particleComponent->ColorSpectrum[0].g - particleComponent->ColorSpectrum[1].g); - it->color.g = particleComponent->ColorSpectrum[0].g + deltaColor * timeProgress; - deltaColor = glm::abs(particleComponent->ColorSpectrum[0].b - particleComponent->ColorSpectrum[1].b); - it->color.b = particleComponent->ColorSpectrum[0].b + deltaColor * timeProgress;*/ - - ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); - VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); - + //FIX: calculate once + float timeProgress = timeLived / particleComponent->LifeTime; + //ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); + if(particleComponent->ScaleSpectrum.size() > 1) + ScaleInterpolation(timeProgress, particleComponent->ScaleSpectrum, transformComponent->Scale); + if(particleComponent->VelocitySpectrum.size() > 1) + VelocityInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); + if(particleComponent->AngularVelocitySpectrum.size() > 1) + AngularVelocityInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); + + transformComponent->Orientation *= glm::angleAxis(it->AngularVelocity, glm::vec3(0, 0, 1)); transformComponent->Position += transformComponent->Velocity * (float)dt; it++; @@ -103,12 +102,12 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = emitterComponent->LifeTime; - particle->ScaleSpectrum.push_back(4); //TEMP - particle->ScaleSpectrum.push_back(0); //TEMP + particle->ScaleSpectrum.push_back(1); //TEMP + particle->ScaleSpectrum.push_back(1); //TEMP particle->VelocitySpectrum.push_back(particleTransform->Velocity); //TEMP particle->VelocitySpectrum.push_back(testVel); //TEMP -// particle->AngularVelocitySpectrum.push_back(); -// particle->AngularVelocitySpectrum.push_back(); + //particle->AngularVelocitySpectrum.push_back(0.f); + particle->AngularVelocitySpectrum.push_back(-glm::pi()/10); // Color startColor = {.4f, .45f, .2f}; // particle->ColorSpectrum.push_back(startColor); @@ -120,8 +119,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); -// data.color = particle->ColorSpectrum[0]; -// data.Scale = particle->ScaleSpectrum[0]; + data.AngularVelocity = particle->AngularVelocitySpectrum[0]; m_ParticleEmitter[emitterID].push_back(data); } @@ -157,4 +155,23 @@ void Systems::ParticleSystem::VelocityInterpolation(double timeProgress, std::ve if(velocitySpectrum[0].z > velocitySpectrum[1].z) deltaVelocity *= -1; velocity.z = velocitySpectrum[0].z + deltaVelocity * timeProgress; +} + +void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector colorSpectrum, Color &color) +{ + float deltaColor = glm::abs(colorSpectrum[0].r - colorSpectrum[1].r); + color.r = colorSpectrum[0].r + deltaColor * timeProgress; + deltaColor = glm::abs(colorSpectrum[0].g - colorSpectrum[1].g); + color.g = colorSpectrum[0].g + deltaColor * timeProgress; + deltaColor = glm::abs(colorSpectrum[0].b - colorSpectrum[1].b); + color.b = colorSpectrum[0].b + deltaColor * timeProgress; +} + +void Systems::ParticleSystem::AngularVelocityInterpolation(double timeProgress, std::vector spectrum, float &angularVelocity) +{ + float deltaAngularVelocity = glm::abs(spectrum[0] - spectrum[1]); + if(spectrum[0] > spectrum[1]) + deltaAngularVelocity *= -1; + angularVelocity = spectrum[0] + deltaAngularVelocity * timeProgress; + } \ No newline at end of file diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 33d14ae..9e10b99 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -20,6 +20,7 @@ namespace Systems EntityID ParticleID; double SpawnTime; float Scale; + float AngularVelocity; Color color; }; @@ -35,6 +36,8 @@ private: 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 Billboard(); std::map> m_ParticleEmitter; std::map m_TimeSinceLastSpawn; From 0feb9832778ed09f5acffcce850519d3e0bb271a Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 28 Apr 2014 19:56:18 +0200 Subject: [PATCH 19/27] Made Interpolation functions look nicer --- src/Systems/ParticleSystem.cpp | 60 ++++++++++++++++------------------ 1 file changed, 29 insertions(+), 31 deletions(-) diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 5ee695c..2fd1cfe 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -24,7 +24,6 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID if(emitterComponent) { emitterComponent->TimeSinceLastSpawn += dt; - auto transformComponent = m_World->GetComponent(entity, "Transform"); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { @@ -47,7 +46,6 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID } else { - //FIX: calculate once float timeProgress = timeLived / particleComponent->LifeTime; //ColorInterpolation(timeProgress, particleComponent->ColorSpectrum, color); @@ -132,46 +130,46 @@ float Systems::ParticleSystem::RandomizeAngle(float spreadAngle) } //Interpolates the scale of the particle -void Systems::ParticleSystem::ScaleInterpolation(double timeProgress, std::vector scaleSpectrum, glm::vec3 &scale) +void Systems::ParticleSystem::ScaleInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &s) { - float deltaScale = glm::abs(scaleSpectrum[0] - scaleSpectrum[1]); - if(scaleSpectrum[0] > scaleSpectrum[1]) - deltaScale *= -1; - scale = glm::vec3(scaleSpectrum[0] + deltaScale * timeProgress); + 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 velocitySpectrum, glm::vec3 &velocity) +void Systems::ParticleSystem::VelocityInterpolation(double timeProgress, std::vector spectrum, glm::vec3 &v) { - float deltaVelocity = glm::abs(velocitySpectrum[0].x - velocitySpectrum[1].x); - if(velocitySpectrum[0].x > velocitySpectrum[1].x) - deltaVelocity *= -1; - velocity.x = velocitySpectrum[0].x + deltaVelocity * timeProgress; - deltaVelocity = glm::abs(velocitySpectrum[0].y - velocitySpectrum[1].y); - if (velocitySpectrum[0].y > velocitySpectrum[1].y) - deltaVelocity *= -1; - velocity.y = velocitySpectrum[0].y + deltaVelocity * timeProgress; - deltaVelocity = glm::abs(velocitySpectrum[0].z - velocitySpectrum[1].z); - if(velocitySpectrum[0].z > velocitySpectrum[1].z) - deltaVelocity *= -1; - velocity.z = velocitySpectrum[0].z + deltaVelocity * timeProgress; + float dVelocity = 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); + 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); + if(spectrum[0].z > spectrum[1].z) + dVelocity *= -1; + v.z = spectrum[0].z + dVelocity * timeProgress; } -void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector colorSpectrum, Color &color) +void Systems::ParticleSystem::ColorInterpolation(double timeProgress, std::vector spectrum, Color &c) { - float deltaColor = glm::abs(colorSpectrum[0].r - colorSpectrum[1].r); - color.r = colorSpectrum[0].r + deltaColor * timeProgress; - deltaColor = glm::abs(colorSpectrum[0].g - colorSpectrum[1].g); - color.g = colorSpectrum[0].g + deltaColor * timeProgress; - deltaColor = glm::abs(colorSpectrum[0].b - colorSpectrum[1].b); - color.b = colorSpectrum[0].b + deltaColor * timeProgress; + 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 &angularVelocity) +void Systems::ParticleSystem::AngularVelocityInterpolation(double timeProgress, std::vector spectrum, float &alpha) { - float deltaAngularVelocity = glm::abs(spectrum[0] - spectrum[1]); + float dAlpha = glm::abs(spectrum[0] - spectrum[1]); if(spectrum[0] > spectrum[1]) - deltaAngularVelocity *= -1; - angularVelocity = spectrum[0] + deltaAngularVelocity * timeProgress; + dAlpha *= -1; + alpha = spectrum[0] + dAlpha * timeProgress; } \ No newline at end of file From a13e9d32ec791a83f9bfb35b0d56ff0336cc1000 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 29 Apr 2014 11:51:57 +0200 Subject: [PATCH 20/27] 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; From fa9a610353ddc1dad62cf0c178b28936755da862 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 29 Apr 2014 14:21:52 +0200 Subject: [PATCH 21/27] Almost correct Orientation calculation... --- src/Systems/ParticleSystem.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 06b2397..6aa3d97 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -59,18 +59,16 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID if(particleComponent->AngularVelocitySpectrum.size() > 1) ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); //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)); -// 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); + glm::vec3 v1 = glm::normalize(particleComponent->OrientationSpectrum[0]); + glm::vec3 v2 = glm::normalize(it->Orientation); + glm::vec3 v3 = glm::cross(v1,v2); + float angle = glm::abs(glm::acos(glm::dot(v1, v2) / (v1.length() * v2.length()))); + transformComponent->Orientation = glm::angleAxis(angle, v3); + //float alpha = it->AngularVelocity * dt; //transformComponent->Orientation = transformComponent->Orientation * it->Orientation; From 262d2596811a79aa96ace7825aef0699b63054c7 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Thu, 1 May 2014 02:00:10 +0200 Subject: [PATCH 22/27] Orientation interpolation complete (v.length() != glm::length(v), the latter is correct) --- src/Systems/ParticleSystem.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 6aa3d97..48a1a91 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -59,19 +59,15 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID if(particleComponent->AngularVelocitySpectrum.size() > 1) ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); //Angular velocity interpolation - if(particleComponent->OrientationSpectrum.size() > 1) VectorInterpolation(timeProgress, particleComponent->OrientationSpectrum, it->Orientation); - glm::vec3 v1 = glm::normalize(particleComponent->OrientationSpectrum[0]); - glm::vec3 v2 = glm::normalize(it->Orientation); - glm::vec3 v3 = glm::cross(v1,v2); - float angle = glm::abs(glm::acos(glm::dot(v1, v2) / (v1.length() * v2.length()))); + 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); - - - //float alpha = it->AngularVelocity * dt; - //transformComponent->Orientation = transformComponent->Orientation * it->Orientation; transformComponent->Position += transformComponent->Velocity * (float)dt; it++; @@ -122,6 +118,8 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) // particle->AngularVelocitySpectrum.push_back(0.f); // particle->AngularVelocitySpectrum.push_back(-glm::pi()); particle->OrientationSpectrum = particle->VelocitySpectrum; +// particle->OrientationSpectrum.push_back(glm::vec3(0,1,0)); +// particle->OrientationSpectrum.push_back(glm::vec3(0,-1,0)); // Color startColor = {.4f, .45f, .2f}; // particle->ColorSpectrum.push_back(startColor); From 398b157b893d9eb52cee2b543f60f17e9f253790 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 6 May 2014 13:52:34 +0200 Subject: [PATCH 23/27] Rendering sprites --- src/GameWorld.cpp | 9 ++++++--- src/Renderer.cpp | 32 ++++++++++++++++++++++++++++++++ src/Renderer.h | 2 ++ src/Systems/ParticleSystem.cpp | 2 +- src/Systems/RenderSystem.cpp | 11 +++++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 7d25c46..801cd6e 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -104,14 +104,17 @@ void GameWorld::Initialize() emitter->SpawnCount = 1; emitter->SpreadAngle = glm::pi()/20; emitter->SpawnFrequency = 1.008; + emitter->ScaleSpectrum.push_back(glm::vec3(1)); + emitter->ScaleSpectrum.push_back(glm::vec3(0)); auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; auto particleEnt = CreateEntity(); AddComponent(particleEnt, "Transform"); - model = AddComponent(particleEnt, "Model"); - model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; - +// model = AddComponent(particleEnt, "Model"); +// model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + auto spriteComponent = AddComponent(particleEnt, "Sprite"); + spriteComponent->SpriteFile = "Textures/Sprites/SeriousParticle.png"; emitter->ParticleTemplate = particleEnt; } } diff --git a/src/Renderer.cpp b/src/Renderer.cpp index e7dfed6..c8cf06e 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -267,6 +267,29 @@ void Renderer::DrawScene() } } + for (auto tuple : TexturesToRender) + { + Texture* texture; + glm::mat4 modelMatrix; + std::tie(texture, modelMatrix) = tuple; + + //MVP = m_Camera->ProjectionMatrix() * glm::translate(glm::mat4(), m_Camera->Position()) * modelMatrix; + MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation())) * modelMatrix ; + depthMVP = depthCameraMatrix * modelMatrix; + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "model"), 1, GL_FALSE, glm::value_ptr(modelMatrix)); + glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "view"), 1, GL_FALSE, glm::value_ptr(m_Camera->ViewMatrix())); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, *texture); + glBindVertexArray(m_ScreenQuad); + glDrawArrays(GL_TRIANGLES, 0, 6); + } + + + + #ifdef DEBUG // Debug draw model normals if (m_DrawNormals) @@ -318,6 +341,8 @@ void Renderer::DrawShadowMap() glDrawArrays(GL_TRIANGLES, texGroup.StartIndex, texGroup.EndIndex - texGroup.StartIndex + 1); } } + + } void Renderer::DrawDebugShadowMap() @@ -376,6 +401,12 @@ void Renderer::AddModelToDraw(Model* model, glm::vec3 position, glm::quat orient ModelsToRender.push_back(std::make_tuple(model, modelMatrix, visible, shadowCaster)); } +void Renderer::AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale) +{ + glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); + TexturesToRender.push_back(std::make_tuple(texture, modelMatrix)); +} + void Renderer::AddPointLightToDraw( glm::vec3 _position, glm::vec3 _specular, @@ -536,6 +567,7 @@ void Renderer::ClearStuff() { AABBsToRender.clear(); ModelsToRender.clear(); + TexturesToRender.clear(); Light_position.clear(); Light_specular.clear(); Light_diffuse.clear(); diff --git a/src/Renderer.h b/src/Renderer.h index 8d0da1f..7d00b13 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -23,6 +23,7 @@ public: int HEIGHT, WIDTH; std::list> ModelsToRender; + std::list> TexturesToRender; int Lights; std::vector Light_position; std::vector Light_specular; @@ -40,6 +41,7 @@ public: void DrawText(); void AddModelToDraw(Model* model, glm::vec3 position, glm::quat orientation, glm::vec3 scale, bool visible, bool shadowCaster); + void AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale); void AddTextToDraw(); void AddPointLightToDraw( glm::vec3 _position, diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 48a1a91..660e39c 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -111,7 +111,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = emitterComponent->LifeTime; - particle->ScaleSpectrum.push_back(glm::vec3(1)); //TEMP + particle->ScaleSpectrum = emitterComponent->ScaleSpectrum; //TEMP //particle->ScaleSpectrum.push_back(glm::vec3(1,4,1)); //TEMP particle->VelocitySpectrum.push_back(particleTransform->Velocity); //TEMP particle->VelocitySpectrum.push_back(testVel); //TEMP diff --git a/src/Systems/RenderSystem.cpp b/src/Systems/RenderSystem.cpp index 9e2b426..d2102d1 100755 --- a/src/Systems/RenderSystem.cpp +++ b/src/Systems/RenderSystem.cpp @@ -55,6 +55,17 @@ void Systems::RenderSystem::UpdateEntity(double dt, EntityID entity, EntityID pa m_Renderer->GetCamera()->NearClip(cameraComponent->NearClip); m_Renderer->GetCamera()->FarClip(cameraComponent->FarClip); } + + auto spriteComponent = m_World->GetComponent(entity, "Sprite"); + if(spriteComponent != nullptr) + { + //TEMP + Texture* texture = m_World->GetResourceManager()->Load("Texture", spriteComponent->SpriteFile); + //glBindTexture(GL_TEXTURE_2D, texture); + auto transform = m_World->GetComponent(spriteComponent->Entity, "Transform"); + glm::quat orientation2D = glm::angleAxis(glm::eulerAngles(transform->Orientation).z, glm::vec3(0, 0, -1)); + m_Renderer->AddTextureToDraw(texture, transform->Position, orientation2D, transform->Scale); + } } void Systems::RenderSystem::Initialize() From 1a365bf06990a0276834820e70a4e656d4def2d0 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Wed, 7 May 2014 21:34:19 +0200 Subject: [PATCH 24/27] Billboarding on sprites --- src/Components/ParticleEmitter.h | 3 +- src/GameWorld.cpp | 17 ++++--- src/Renderer.cpp | 24 +++++++-- src/Renderer.h | 2 +- src/Systems/ParticleSystem.cpp | 86 +++++++++++++++++++++----------- src/Systems/ParticleSystem.h | 1 - 6 files changed, 90 insertions(+), 43 deletions(-) diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index ae66fca..1b30c8d 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -28,7 +28,8 @@ struct ParticleEmitter : Component std::vector ScaleSpectrum; float SpreadAngle; double LifeTime; - std::vector VelocitySpectrum; + bool UseGoalVelocity; + glm::vec3 GoalVelocity; std::vector AngularVelocitySpectrum; std::vector OrientationSpectrum; //Keep? diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 801cd6e..0b19842 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -99,20 +99,23 @@ void GameWorld::Initialize() auto transform = AddComponent(ent, "Transform"); transform->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); transform->Position = glm::vec3(i * 10, 20, 0); + auto emitter = AddComponent(ent, "ParticleEmitter"); - emitter->LifeTime = 4; + emitter->LifeTime = 0.3; emitter->SpawnCount = 1; - emitter->SpreadAngle = glm::pi()/20; - emitter->SpawnFrequency = 1.008; - emitter->ScaleSpectrum.push_back(glm::vec3(1)); - emitter->ScaleSpectrum.push_back(glm::vec3(0)); + emitter->SpreadAngle = glm::pi()/4; + emitter->SpawnFrequency = 0.0008; + emitter->ScaleSpectrum.push_back(glm::vec3(0.05f)); + emitter->UseGoalVelocity = false; + emitter->GoalVelocity = glm::vec3(4, -4, 0); +// emitter->OrientationSpectrum.push_back(glm::vec3(0, 1, 0)); +// emitter->OrientationSpectrum.push_back(glm::vec3(0, -1, 0)); + emitter->AngularVelocitySpectrum.push_back(glm::pi() / 100); auto model = AddComponent(ent, "Model"); model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; auto particleEnt = CreateEntity(); AddComponent(particleEnt, "Transform"); -// model = AddComponent(particleEnt, "Model"); -// model->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; auto spriteComponent = AddComponent(particleEnt, "Sprite"); spriteComponent->SpriteFile = "Textures/Sprites/SeriousParticle.png"; emitter->ParticleTemplate = particleEnt; diff --git a/src/Renderer.cpp b/src/Renderer.cpp index c8cf06e..f7960d5 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -271,10 +271,13 @@ void Renderer::DrawScene() { Texture* texture; glm::mat4 modelMatrix; - std::tie(texture, modelMatrix) = tuple; - - //MVP = m_Camera->ProjectionMatrix() * glm::translate(glm::mat4(), m_Camera->Position()) * modelMatrix; - MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation())) * modelMatrix ; + glm::mat4 billboardMatrix; + std::tie(texture, modelMatrix, billboardMatrix) = tuple; + + + //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix ); + MVP = cameraMatrix * billboardMatrix * modelMatrix; + depthMVP = depthCameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); @@ -404,7 +407,18 @@ void Renderer::AddModelToDraw(Model* model, glm::vec3 position, glm::quat orient void Renderer::AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat orientation, glm::vec3 scale) { glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); - TexturesToRender.push_back(std::make_tuple(texture, modelMatrix)); + + glm::vec3 camToParticle = glm::normalize(m_Camera->Position() - position); + glm::vec3 up = glm::vec3(0,1,0); + glm::vec3 rightVec = glm::cross(up, camToParticle); + + glm::mat4 billboardMatrix; + billboardMatrix[0] = glm::vec4(rightVec, 0); + billboardMatrix[1] = glm::vec4(up, 0); + billboardMatrix[2] = glm::vec4(camToParticle, 0); + //billboardMatrix[3] = glm::vec4(position, 0); + + TexturesToRender.push_back(std::make_tuple(texture, modelMatrix, billboardMatrix)); } void Renderer::AddPointLightToDraw( diff --git a/src/Renderer.h b/src/Renderer.h index 7d00b13..17485aa 100755 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -23,7 +23,7 @@ public: int HEIGHT, WIDTH; std::list> ModelsToRender; - std::list> TexturesToRender; + std::list> TexturesToRender; int Lights; std::vector Light_position; std::vector Light_specular; diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index 660e39c..8e384c5 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -31,6 +31,9 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID emitterComponent->TimeSinceLastSpawn = 0; } + + + std::list::iterator it; for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { @@ -55,19 +58,35 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID // Velocity interpolation if(particleComponent->VelocitySpectrum.size() > 1) VectorInterpolation(timeProgress, particleComponent->VelocitySpectrum, transformComponent->Velocity); + // Angular velocity interpolation - if(particleComponent->AngularVelocitySpectrum.size() > 1) - ScalarInterpolation(timeProgress, particleComponent->AngularVelocitySpectrum, it->AngularVelocity); + 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); + } - 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->Position += transformComponent->Velocity * (float)dt; it++; @@ -98,8 +117,8 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) auto particleTransform = m_World->GetComponent(ent, "Transform"); particleTransform->Position = emitterTransform->Position; - particleTransform->Scale = glm::vec3(1, 1, 1); + particleTransform->Orientation = emitterOrientation; //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 * @@ -107,33 +126,44 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 1, 0))) * glm::normalize(glm::angleAxis(RandomizeAngle(spreadAngle), glm::vec3(0, 0, 1))); - glm::vec3 testVel = glm::vec3(particleTransform->Velocity.x, -particleTransform->Velocity.y * 1.5, particleTransform->Velocity.z); //TEMP - auto particle = m_World->AddComponent(ent, "Particle"); particle->LifeTime = emitterComponent->LifeTime; - particle->ScaleSpectrum = emitterComponent->ScaleSpectrum; //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()); - particle->OrientationSpectrum = particle->VelocitySpectrum; -// particle->OrientationSpectrum.push_back(glm::vec3(0,1,0)); -// particle->OrientationSpectrum.push_back(glm::vec3(0,-1,0)); - -// Color startColor = {.4f, .45f, .2f}; -// particle->ColorSpectrum.push_back(startColor); -// Color endColor = {0.f, 45.f, 23.f}; -// particle->ColorSpectrum.push_back(endColor); - + particle->ScaleSpectrum = emitterComponent->ScaleSpectrum; + particle->VelocitySpectrum.push_back(particleTransform->Velocity); + + if (emitterComponent->ScaleSpectrum.size() > 0) + { + if (emitterComponent->ScaleSpectrum.size() > 1) + { + particle->ScaleSpectrum = emitterComponent->ScaleSpectrum; + } + else + { + particleTransform->Scale = emitterComponent->ScaleSpectrum[0]; + } + } + else + { + particleTransform->Scale = glm::vec3(1, 1, 1); + } + + if(emitterComponent->UseGoalVelocity) + particle->VelocitySpectrum.push_back(emitterComponent->GoalVelocity); + particle->OrientationSpectrum = emitterComponent->OrientationSpectrum; + if(particle->OrientationSpectrum.size() != 0) + particleTransform->Orientation = glm::angleAxis(0.f, particle->OrientationSpectrum[0]); + particle->AngularVelocitySpectrum = emitterComponent->AngularVelocitySpectrum; ParticleData data; data.ParticleID = ent; data.SpawnTime = glfwGetTime(); - //data.AngularVelocity = particle->AngularVelocitySpectrum[0]; - data.Orientation = particle->OrientationSpectrum[0]; - + if (particle->AngularVelocitySpectrum.size() != 0) + data.AngularVelocity = particle->AngularVelocitySpectrum[0]; + if (particle->OrientationSpectrum.size() != 0) + data.Orientation = particle->OrientationSpectrum[0]; + else data.Orientation = emitterOrientation * glm::vec3(0,0,-1); + m_ParticleEmitter[emitterID].push_back(data); } } diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index 4dff51e..082c957 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -19,7 +19,6 @@ namespace Systems { EntityID ParticleID; double SpawnTime; - float Scale; float AngularVelocity; glm::vec3 Orientation; Color color; From 5d48ea7e2aa15bc2765f93a87dc6ca844d970b3f Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 12 May 2014 22:05:56 +0200 Subject: [PATCH 25/27] not reversed input for Camera... --- src/Systems/FreeSteeringSystem.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Systems/FreeSteeringSystem.cpp b/src/Systems/FreeSteeringSystem.cpp index 8373c80..d59b475 100755 --- a/src/Systems/FreeSteeringSystem.cpp +++ b/src/Systems/FreeSteeringSystem.cpp @@ -56,19 +56,19 @@ bool Systems::FreeSteeringSystem::FreeSteeringInputController::OnCommand(const E } else if (event.Command == "+cam_right") { - Movement.x += 1.f; + Movement.x -= 1.f; } else if (event.Command == "-cam_right") { - Movement.x -= 1.f; + Movement.x += 1.f; } else if (event.Command == "+cam_left") { - Movement.x += -1.f; + Movement.x -= -1.f; } else if (event.Command == "-cam_left") { - Movement.x -= -1.f; + Movement.x += -1.f; } else if (event.Command == "+up") { From 19a2879104b842dfdd7916cb72ef1f655e2cd3e2 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Mon, 12 May 2014 22:21:27 +0200 Subject: [PATCH 26/27] Emitter childed to a wheel. BUG FIXED: Billboarding calculation. --- assets | 2 +- src/GameWorld.cpp | 33 +++++++++++++++++-- src/Renderer.cpp | 10 +++--- src/Systems/ParticleSystem.cpp | 15 +++++---- src/Systems/ParticleSystem.h | 7 +++- .../Returngeance/Returngeance.vcxproj.filters | 3 -- 6 files changed, 52 insertions(+), 18 deletions(-) diff --git a/assets b/assets index 8b6c48b..e2b54e6 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 8b6c48b26b3bbc10f5e66c1f59fec6ca935f641b +Subproject commit e2b54e6212eb1233e6f9935b9e5d7c0295f9ffc6 diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 979376b..1cae957 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -436,6 +436,31 @@ void GameWorld::Initialize() Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; CommitEntity(wheel); + + auto entity = CreateEntity(wheel); + auto transformComponent = AddComponent(entity, "Transform"); + /*transformComponent->Position = glm::vec3(0,3,0);*/ + transformComponent->Scale = glm::vec3(3,3,3); + transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); + auto emitterComponent = AddComponent(entity, "ParticleEmitter"); + emitterComponent->SpawnCount = 2; + emitterComponent->SpawnFrequency = 0.01; + emitterComponent->SpreadAngle = glm::pi()/4; + emitterComponent->UseGoalVelocity = false; + emitterComponent->LifeTime = 2.0; + emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.1)); + auto modelComponent = AddComponent(entity, "Model"); + modelComponent->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + CommitEntity(entity); + + auto particleEntity = CreateEntity(entity); + auto TEMP = AddComponent(particleEntity, "Transform"); + TEMP->Scale = glm::vec3(0); + auto spriteComponent = AddComponent(particleEntity, "Sprite"); + spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; + emitterComponent->ParticleTemplate = particleEntity; + + CommitEntity(particleEntity); } CommitEntity(tank); @@ -528,6 +553,10 @@ void GameWorld::Initialize() GetSystem("SoundSystem")->PlaySound(emitter); CommitEntity(entity); }*/ + + { + + } } void GameWorld::Update(double dt) @@ -548,7 +577,7 @@ void GameWorld::RegisterSystems() m_SystemFactory.Register("InputSystem", [this]() { return new Systems::InputSystem(this, m_EventBroker); }); m_SystemFactory.Register("DebugSystem", [this]() { return new Systems::DebugSystem(this, m_EventBroker); }); //m_SystemFactory.Register("CollisionSystem", [this]() { return new Systems::CollisionSystem(this); }); - ////m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this); }); + m_SystemFactory.Register("ParticleSystem", [this]() { return new Systems::ParticleSystem(this, m_EventBroker); }); //m_SystemFactory.Register("PlayerSystem", [this]() { return new Systems::PlayerSystem(this); }); m_SystemFactory.Register("FreeSteeringSystem", [this]() { return new Systems::FreeSteeringSystem(this, m_EventBroker); }); m_SystemFactory.Register("TankSteeringSystem", [this]() { return new Systems::TankSteeringSystem(this, m_EventBroker); }); @@ -564,7 +593,7 @@ void GameWorld::AddSystems() AddSystem("InputSystem"); AddSystem("DebugSystem"); //AddSystem("CollisionSystem"); - ////AddSystem("ParticleSystem"); + AddSystem("ParticleSystem"); //AddSystem("PlayerSystem"); AddSystem("FreeSteeringSystem"); AddSystem("TankSteeringSystem"); diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 99f27a9..2905891 100755 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -274,10 +274,9 @@ void Renderer::DrawScene() glm::mat4 billboardMatrix; std::tie(texture, modelMatrix, billboardMatrix) = tuple; - //MVP = cameraMatrix * glm::inverse(glm::toMat4(m_Camera->Orientation()) * modelMatrix ); - MVP = cameraMatrix * billboardMatrix * modelMatrix; - + MVP = cameraMatrix * modelMatrix * billboardMatrix; + depthMVP = depthCameraMatrix * modelMatrix; glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "MVP"), 1, GL_FALSE, glm::value_ptr(MVP)); glUniformMatrix4fv(glGetUniformLocation(m_ShaderProgram.GetHandle(), "DepthMVP"), 1, GL_FALSE, glm::value_ptr(depthMVP)); @@ -410,11 +409,12 @@ void Renderer::AddTextureToDraw(Texture* texture, glm::vec3 position, glm::quat glm::vec3 camToParticle = glm::normalize(m_Camera->Position() - position); glm::vec3 up = glm::vec3(0,1,0); - glm::vec3 rightVec = glm::cross(up, camToParticle); + glm::vec3 rightVec = glm::normalize(glm::cross(up, camToParticle)); + glm::vec3 up2 = glm::normalize(glm::cross(camToParticle, rightVec)); glm::mat4 billboardMatrix; billboardMatrix[0] = glm::vec4(rightVec, 0); - billboardMatrix[1] = glm::vec4(up, 0); + billboardMatrix[1] = glm::vec4(up2, 0); billboardMatrix[2] = glm::vec4(camToParticle, 0); //billboardMatrix[3] = glm::vec4(position, 0); diff --git a/src/Systems/ParticleSystem.cpp b/src/Systems/ParticleSystem.cpp index e77e9c9..0882f9d 100644 --- a/src/Systems/ParticleSystem.cpp +++ b/src/Systems/ParticleSystem.cpp @@ -4,6 +4,11 @@ #include "World.h" +void Systems::ParticleSystem::Initialize() +{ + m_TransformSystem = m_World->GetSystem("TransformSystem"); +} + void Systems::ParticleSystem::Update(double dt) { @@ -19,16 +24,13 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID if(emitterComponent) { emitterComponent->TimeSinceLastSpawn += dt; - auto transformComponent = m_World->GetComponent(entity, "Transform"); + auto emitterTransformComponent = m_World->GetComponent(entity, "Transform"); if(emitterComponent->TimeSinceLastSpawn > emitterComponent->SpawnFrequency) { SpawnParticles(entity); emitterComponent->TimeSinceLastSpawn = 0; } - - - std::list::iterator it; for(it = m_ParticleEmitter[entity].begin(); it != m_ParticleEmitter[entity].end();) { @@ -82,7 +84,7 @@ void Systems::ParticleSystem::UpdateEntity(double dt, EntityID entity, EntityID } - transformComponent->Position += transformComponent->Velocity * (float)dt; + transformComponent->Position += transformComponent->Velocity * (float)dt; it++; } @@ -101,6 +103,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) { auto emitterComponent = m_World->GetComponent(emitterID, "ParticleEmitter"); auto emitterTransform = m_World->GetComponent(emitterID, "Transform"); + glm::vec3 emitterPos = m_TransformSystem->AbsolutePosition(emitterID); glm::quat emitterOrientation = emitterTransform->Orientation; float tempSpeed = 4; @@ -111,7 +114,7 @@ void Systems::ParticleSystem::SpawnParticles(EntityID emitterID) auto ent = m_World->CloneEntity(emitterComponent->ParticleTemplate); auto particleTransform = m_World->GetComponent(ent, "Transform"); - particleTransform->Position = emitterTransform->Position; + particleTransform->Position = emitterPos; particleTransform->Orientation = emitterOrientation; //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. diff --git a/src/Systems/ParticleSystem.h b/src/Systems/ParticleSystem.h index d976105..06cbe63 100644 --- a/src/Systems/ParticleSystem.h +++ b/src/Systems/ParticleSystem.h @@ -2,6 +2,7 @@ #define ParticleSystem_h__ #include "System.h" +#include "Systems/TransformSystem.h" #include "Components/Transform.h" #include "Components/ParticleEmitter.h" #include "Components/Particle.h" @@ -25,10 +26,13 @@ namespace Systems class ParticleSystem : public System { public: - ParticleSystem(World* world); + ParticleSystem(World* world, std::shared_ptr<::EventBroker> eventBroker) + : System(world, eventBroker) { } + void RegisterComponents(ComponentFactory* cf) override; void Update(double dt) override; void UpdateEntity(double dt, EntityID entity, EntityID parent) override; + void Initialize() override; private: void SpawnParticles(EntityID emitterID); float RandomizeAngle(float spreadAngle); @@ -39,6 +43,7 @@ private: void Billboard(); std::map> m_ParticleEmitter; std::map m_TimeSinceLastSpawn; + std::shared_ptr m_TransformSystem; }; diff --git a/vs11/Returngeance/Returngeance.vcxproj.filters b/vs11/Returngeance/Returngeance.vcxproj.filters index 4128eef..cd6fb6b 100755 --- a/vs11/Returngeance/Returngeance.vcxproj.filters +++ b/vs11/Returngeance/Returngeance.vcxproj.filters @@ -273,9 +273,6 @@ Particle System\Components - - Physics\Components - GUI From df13663ca8c885f4724fa622a61a8410db8294f6 Mon Sep 17 00:00:00 2001 From: Stiffly Date: Tue, 13 May 2014 00:55:42 +0200 Subject: [PATCH 27/27] Two emitters and following camera --- assets | 2 +- src/Components/ParticleEmitter.h | 1 + src/GameWorld.cpp | 70 +++++++++++++++++++++----------- 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/assets b/assets index e2b54e6..908d0eb 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit e2b54e6212eb1233e6f9935b9e5d7c0295f9ffc6 +Subproject commit 908d0eb8034d334437e79a2e524de9c7d9310ea6 diff --git a/src/Components/ParticleEmitter.h b/src/Components/ParticleEmitter.h index 1b30c8d..bb57c99 100755 --- a/src/Components/ParticleEmitter.h +++ b/src/Components/ParticleEmitter.h @@ -23,6 +23,7 @@ struct ParticleEmitter : Component EntityID ParticleTemplate; float SpawnFrequency; + float Speed; int SpawnCount; std::vector ColorSpectrum; std::vector ScaleSpectrum; diff --git a/src/GameWorld.cpp b/src/GameWorld.cpp index 1cae957..056598c 100755 --- a/src/GameWorld.cpp +++ b/src/GameWorld.cpp @@ -69,18 +69,7 @@ void GameWorld::Initialize() CommitEntity(ground); } - { - auto camera = CreateEntity(); - auto transform = AddComponent(camera, "Transform"); - transform->Position.z = 20.f; - transform->Position.y = 10.f; - transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); - auto cameraComp = AddComponent(camera, "Camera"); - cameraComp->FarClip = 2000.f; - AddComponent(camera, "Input"); - auto freeSteering = AddComponent(camera, "FreeSteering"); - CommitEntity(camera); - } + /*{ auto jeep = CreateEntity(); @@ -398,6 +387,30 @@ void GameWorld::Initialize() Wheel->ConnectedToHandbrake = true; Wheel->TorqueRatio = 0.125f; CommitEntity(wheel); + + auto entity = CreateEntity(tank); + auto transformComponent = AddComponent(entity, "Transform"); + transformComponent->Position = glm::vec3(2,-1.7,2.0); + transformComponent->Scale = glm::vec3(3,3,3); + transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); + auto emitterComponent = AddComponent(entity, "ParticleEmitter"); + emitterComponent->SpawnCount = 2; + emitterComponent->SpawnFrequency = 0.005; + emitterComponent->SpreadAngle = glm::pi(); + emitterComponent->UseGoalVelocity = false; + emitterComponent->LifeTime = 0.5; + //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); + emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); + CommitEntity(entity); + + auto particleEntity = CreateEntity(entity); + auto TEMP = AddComponent(particleEntity, "Transform"); + TEMP->Scale = glm::vec3(0); + auto spriteComponent = AddComponent(particleEntity, "Sprite"); + spriteComponent->SpriteFile = "Models/Textures/Sprites/Dust.png"; + emitterComponent->ParticleTemplate = particleEntity; + + CommitEntity(particleEntity); } { @@ -437,20 +450,19 @@ void GameWorld::Initialize() Wheel->TorqueRatio = 0.125f; CommitEntity(wheel); - auto entity = CreateEntity(wheel); + auto entity = CreateEntity(tank); auto transformComponent = AddComponent(entity, "Transform"); - /*transformComponent->Position = glm::vec3(0,3,0);*/ + transformComponent->Position = glm::vec3(-2,-1.7,2.0); transformComponent->Scale = glm::vec3(3,3,3); transformComponent->Orientation = glm::angleAxis(glm::pi()/2, glm::vec3(1,0,0)); auto emitterComponent = AddComponent(entity, "ParticleEmitter"); emitterComponent->SpawnCount = 2; - emitterComponent->SpawnFrequency = 0.01; - emitterComponent->SpreadAngle = glm::pi()/4; + emitterComponent->SpawnFrequency = 0.005; + emitterComponent->SpreadAngle = glm::pi(); emitterComponent->UseGoalVelocity = false; - emitterComponent->LifeTime = 2.0; - emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.1)); - auto modelComponent = AddComponent(entity, "Model"); - modelComponent->ModelFile = "Models/Placeholders/PhysicsTest/PointLight.obj"; + emitterComponent->LifeTime = 0.5; + //emitterComponent->AngularVelocitySpectrum.push_back(glm::pi() / 100); + emitterComponent->ScaleSpectrum.push_back(glm::vec3(0.05)); CommitEntity(entity); auto particleEntity = CreateEntity(entity); @@ -464,8 +476,23 @@ void GameWorld::Initialize() } CommitEntity(tank); + { + auto camera = CreateEntity(tank); + auto transform = AddComponent(camera, "Transform"); + transform->Position.z = 20.f; + transform->Position.y = 7.f; + //transform->Orientation = glm::quat(glm::vec3(-glm::pi() / 8.f, 0.f, 0.f)); + transform->Orientation = glm::angleAxis(glm::pi() / 100, glm::vec3(1,0,0)); + auto cameraComp = AddComponent(camera, "Camera"); + cameraComp->FarClip = 2000.f; + AddComponent(camera, "Input"); + auto freeSteering = AddComponent(camera, "FreeSteering"); + CommitEntity(camera); + } } + + /* for(int i = 0; i < 10; i++) { @@ -554,9 +581,6 @@ void GameWorld::Initialize() CommitEntity(entity); }*/ - { - - } } void GameWorld::Update(double dt)