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; } }