From 53ef7497360936d7d37702753e43087f745c0c3c Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 10 Dec 2015 10:15:14 +0100 Subject: [PATCH 1/3] Fixed absoute positioning, I think... --- include/Engine/Rendering/RenderQueueFactory.h | 4 ++ src/Engine/Rendering/RenderQueueFactory.cpp | 49 +++++++++++++++---- 2 files changed, 44 insertions(+), 9 deletions(-) diff --git a/include/Engine/Rendering/RenderQueueFactory.h b/include/Engine/Rendering/RenderQueueFactory.h index f1535cdb..b273b672 100644 --- a/include/Engine/Rendering/RenderQueueFactory.h +++ b/include/Engine/Rendering/RenderQueueFactory.h @@ -22,6 +22,10 @@ private: void FillLights(World* world, RenderQueue* renderQueue); glm::mat4 ModelMatrix(World* world, EntityID entity); + + glm::vec3 AbsolutePosition(World* world, EntityID entity); + glm::quat AbsoluteOrientation(World* world, EntityID entity); + glm::vec3 AbsoluteScale(World* world, EntityID entity); }; #endif \ No newline at end of file diff --git a/src/Engine/Rendering/RenderQueueFactory.cpp b/src/Engine/Rendering/RenderQueueFactory.cpp index 0ada02af..81d7391e 100644 --- a/src/Engine/Rendering/RenderQueueFactory.cpp +++ b/src/Engine/Rendering/RenderQueueFactory.cpp @@ -15,20 +15,51 @@ void RenderQueueFactory::Update(World* world) glm::mat4 RenderQueueFactory::ModelMatrix(World* world, EntityID entity) { - //should really return absolute model matrix based on parents position, scale and orientation - //GetAbsolutePosition(World* world, ComponentWrapper transformComponent) + glm::vec3 position = AbsolutePosition(world, entity); + glm::quat orientation = AbsoluteOrientation(world, entity); + glm::vec3 scale = AbsoluteScale(world, entity); - ComponentWrapper transformComponent = world->GetComponent(entity, "Transform"); - glm::vec3 position = transformComponent["Position"]; - glm::vec3 scale = transformComponent["Scale"]; - glm::vec3 oritentation = transformComponent["Orientation"]; + glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(orientation) * glm::scale(scale); + return modelMatrix; +} + + +glm::vec3 RenderQueueFactory::AbsolutePosition(World* world, EntityID entity) +{ + glm::vec3 position; + + do { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + position += AbsoluteOrientation(world, entity) * (glm::vec3)transform["Position"]; + entity = world->GetParent(entity); + } while (entity != 0); + + return position; +} + +glm::quat RenderQueueFactory::AbsoluteOrientation(World* world, EntityID entity) +{ + glm::quat orientation; + + do { + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + orientation = glm::quat((glm::vec3)transform["Orientation"]) * orientation; + entity = world->GetParent(entity); + } while (entity != 0); + + return orientation; +} + +glm::vec3 RenderQueueFactory::AbsoluteScale(World* world, EntityID entity) +{ + ComponentWrapper transform = world->GetComponent(entity, "Transform"); + glm::vec3 scale = (glm::vec3)transform["Scale"]; - glm::mat4 modelMatrix = glm::translate(glm::mat4(), position) * glm::toMat4(glm::quat(oritentation)) * glm::scale(scale); EntityID parent = world->GetParent(entity); if (parent != 0) { - return ModelMatrix(world, parent) * modelMatrix; + return AbsoluteScale(world, parent) * scale; } else { - return modelMatrix; + return scale; } } From 352074da04bf27c7e72020166c17c9f1c692fcb7 Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 10 Dec 2015 10:17:08 +0100 Subject: [PATCH 2/3] Created base for systems --- include/Engine/Core/System.h | 25 ++++++++++++ include/Engine/Core/SystemPipeline.h | 58 ++++++++++++++++++++++++++++ include/Game/Game.h | 2 + src/Game/Game.cpp | 5 +++ 4 files changed, 90 insertions(+) create mode 100644 include/Engine/Core/System.h create mode 100644 include/Engine/Core/SystemPipeline.h diff --git a/include/Engine/Core/System.h b/include/Engine/Core/System.h new file mode 100644 index 00000000..8e8c71b9 --- /dev/null +++ b/include/Engine/Core/System.h @@ -0,0 +1,25 @@ +#ifndef System_h__ +#define System_h__ + +#include "EventBroker.h" +#include "World.h" +#include "ComponentWrapper.h" + +class System +{ + friend class SystemPipeline; + +public: + System(const EventBroker* eventBroker, std::string componentType) + : m_EventBroker(eventBroker) + , m_ComponentType(componentType) + { } + + virtual void Update(World* world, ComponentWrapper& component, double dt) = 0; + +private: + const EventBroker* m_EventBroker; + std::string m_ComponentType; +}; + +#endif \ No newline at end of file diff --git a/include/Engine/Core/SystemPipeline.h b/include/Engine/Core/SystemPipeline.h new file mode 100644 index 00000000..24f98121 --- /dev/null +++ b/include/Engine/Core/SystemPipeline.h @@ -0,0 +1,58 @@ +#ifndef SystemPipeline_h__ +#define SystemPipeline_h__ + +#include "../Common.h" +#include "EventBroker.h" +#include "System.h" +#include "World.h" + +class SystemPipeline +{ +public: + SystemPipeline(const EventBroker* eventBroker) + : m_EventBroker(eventBroker) + { } + ~SystemPipeline() + { + for (auto& pair : m_Systems) { + for (auto& system : pair.second) { + delete system; + } + } + } + + template + void AddSystem(Arguments... args) + { + System* system = new T(m_EventBroker, args...); + if (!system->m_ComponentType.empty()) { + m_Systems[system->m_ComponentType].push_back(system); + } else { + LOG_ERROR("Failed to add system \"%s\": Missing component type!", typeid(T).name()); + delete system; + } + } + + void Update(World* world, double dt) + { + for (auto& pair : m_Systems) { + const std::string& componentName = pair.first; + auto& systems = pair.second; + const ComponentPool* pool = world->GetComponents(componentName); + if (pool == nullptr) { + continue; + } + for (auto& component : *pool) { + for (auto& system : systems) { + system->Update(world, component, dt); + } + } + } + } + +private: + const EventBroker* m_EventBroker; + std::unordered_map> m_Systems; +}; + +#endif \ No newline at end of file diff --git a/include/Game/Game.h b/include/Game/Game.h index b561119a..d52bc8cb 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -11,6 +11,7 @@ #include "Rendering/RenderQueueFactory.h" #include "Core/EKeyDown.h" #include "Core/EntityXMLFile.h" +#include "Core/SystemPipeline.h" class Game { @@ -29,6 +30,7 @@ private: InputManager* m_InputManager; GUI::Frame* m_FrameStack; World* m_World; + SystemPipeline* m_SystemPipeline; RenderQueueFactory* m_RenderQueueFactory; EventRelay m_EKeyUp; diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index eb62587e..efa8c4b7 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -41,6 +41,9 @@ Game::Game(int argc, char* argv[]) if (!mapToLoad.empty()) { ResourceManager::Load(mapToLoad)->PopulateWorld(m_World); } + + // Create system pipeline + m_SystemPipeline = new SystemPipeline(m_EventBroker); m_LastTime = glfwGetTime(); @@ -64,6 +67,8 @@ void Game::Tick() m_Renderer->Update(dt); m_EventBroker->Swap(); + // Iterate through systems and update world! + m_SystemPipeline->Update(m_World, dt); testTick(dt); m_RenderQueueFactory->Update(m_World); From 0367826f1666c7b34e597d86a60bef3b12a71eef Mon Sep 17 00:00:00 2001 From: sippeangelo Date: Thu, 10 Dec 2015 10:17:18 +0100 Subject: [PATCH 3/3] Added RaptorCopter example system --- assets | 2 +- include/Game/Game.h | 1 + include/Game/RaptorCopterSystem.h | 16 ++++ resources/Schema/Components.xsd | 1 + resources/Schema/Components/RaptorCopter.xml | 4 + resources/Schema/Components/RaptorCopter.xsd | 14 +++ resources/Schema/Entities/Test.xml | 93 ++++++++++++-------- resources/Schema/Types/Entity.xsd | 1 + src/Game/Game.cpp | 1 + 9 files changed, 95 insertions(+), 38 deletions(-) create mode 100644 include/Game/RaptorCopterSystem.h create mode 100644 resources/Schema/Components/RaptorCopter.xml create mode 100644 resources/Schema/Components/RaptorCopter.xsd diff --git a/assets b/assets index 8a7ecf53..b3746822 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 8a7ecf53f7ad7b8c36d778d0927fd77f7c6f6db0 +Subproject commit b37468222e45ec0b2116f1543c578cb9784d43f2 diff --git a/include/Game/Game.h b/include/Game/Game.h index d52bc8cb..500ecf3a 100644 --- a/include/Game/Game.h +++ b/include/Game/Game.h @@ -12,6 +12,7 @@ #include "Core/EKeyDown.h" #include "Core/EntityXMLFile.h" #include "Core/SystemPipeline.h" +#include "RaptorCopterSystem.h" class Game { diff --git a/include/Game/RaptorCopterSystem.h b/include/Game/RaptorCopterSystem.h new file mode 100644 index 00000000..55647004 --- /dev/null +++ b/include/Game/RaptorCopterSystem.h @@ -0,0 +1,16 @@ +#include "Common.h" +#include "Core/System.h" + +class RaptorCopterSystem : public System +{ +public: + RaptorCopterSystem(const EventBroker* eventBroker) + : System(eventBroker, "RaptorCopter") + { } + + virtual void Update(World* world, ComponentWrapper& raptorCopter, double dt) override + { + ComponentWrapper& transform = world->GetComponent(raptorCopter.EntityID, "Transform"); + (glm::vec3&)transform["Orientation"] += (float)(double)raptorCopter["Speed"] * (float)dt * (glm::vec3)raptorCopter["Axis"]; + } +}; \ No newline at end of file diff --git a/resources/Schema/Components.xsd b/resources/Schema/Components.xsd index 54896b69..d4160700 100644 --- a/resources/Schema/Components.xsd +++ b/resources/Schema/Components.xsd @@ -4,4 +4,5 @@ + \ No newline at end of file diff --git a/resources/Schema/Components/RaptorCopter.xml b/resources/Schema/Components/RaptorCopter.xml new file mode 100644 index 00000000..cc1ece52 --- /dev/null +++ b/resources/Schema/Components/RaptorCopter.xml @@ -0,0 +1,4 @@ + + 0 + + \ No newline at end of file diff --git a/resources/Schema/Components/RaptorCopter.xsd b/resources/Schema/Components/RaptorCopter.xsd new file mode 100644 index 00000000..23ee8a96 --- /dev/null +++ b/resources/Schema/Components/RaptorCopter.xsd @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/resources/Schema/Entities/Test.xml b/resources/Schema/Entities/Test.xml index 4e7eafcf..2c3ea18e 100644 --- a/resources/Schema/Entities/Test.xml +++ b/resources/Schema/Entities/Test.xml @@ -37,57 +37,76 @@ - - Models/Core/UnitRaptor.obj - - + - - + + + + Models/Core/UnitRaptor.obj + + - - - - - Models/Core/UnitCube.obj - - - - - - - - - + - - Models/Core/UnitCube.obj - - - - - - - - - - - - - Models/Core/UnitCube.obj - - + + 20 + + + + + + + + + + + Models/Core/UnitCylinder.obj + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + + + + + + + + + Models/Core/UnitCube.obj + + + + + diff --git a/resources/Schema/Types/Entity.xsd b/resources/Schema/Types/Entity.xsd index 49d0f895..695ae7d1 100644 --- a/resources/Schema/Types/Entity.xsd +++ b/resources/Schema/Types/Entity.xsd @@ -13,6 +13,7 @@ + diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index efa8c4b7..15dde3a0 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -44,6 +44,7 @@ Game::Game(int argc, char* argv[]) // Create system pipeline m_SystemPipeline = new SystemPipeline(m_EventBroker); + m_SystemPipeline->AddSystem(); m_LastTime = glfwGetTime();