CoriEngine
Loading...
Searching...
No Matches
Transform.cpp
Go to the documentation of this file.
1#include "Transform.hpp"
3
4namespace Cori {
5 namespace World {
6 namespace Systems {
7 void Transform::OnUpdate([[maybe_unused]] Core::GameTimer& gameTimer) {
9 UpdateTransform();
10 }
11
12 void Transform::UpdateTransform() {
14
15 for (const auto entity : view1) {
16 UpdateTransformRecursive(entity, glm::mat3(1.0f), 1, false, false);
17 }
19 }
20
21 void Transform::UpdateTransformRecursive(entt::entity entity, const glm::mat3& parentTransform, const uint8_t parentDepth, const bool parentTransformDirty, const bool parentDepthDirty) {
22 auto& transform = m_Owner.GetRegistry().get<Components::Entity::Transform>(entity);
23 const bool transformDirty = transform.m_DirtyTransform || parentTransformDirty;
24 const bool layerDirty = transform.m_DirtyDepth || parentDepthDirty;
25
26 if (transformDirty) {
27 if (!transform.GetDetachedState()) {
28 transform.m_WorldTransform = parentTransform * transform.GetLocalTransform();
29 transform.m_LastParentTransform = parentTransform;
30 } else {
31 transform.m_WorldTransform = transform.m_LastParentTransform * transform.GetLocalTransform();
32 }
33 transform.m_DirtyTransform = false;
34 }
35 if (layerDirty) {
36 int16_t unclamped = parentDepth + transform.GetLocalDepthOffset();
37 if (unclamped < 0 || unclamped > 255) {
38 uint8_t clamped = static_cast<uint8_t>(std::clamp(unclamped, static_cast<int16_t>(0), static_cast<int16_t>(255)));
39 CORI_CORE_WARN_TAGGED({ Logger::Tags::World::Self, Logger::Tags::World::Scene::Self }, "Final calculated depth for Entity '{}' is '{}' which is outside of allowed range [0, 255], it will be clamped to '{}'", Entity{ { m_Owner.GetRegistry(), entity } }.GetDebugData(), unclamped, clamped);
40 transform.m_WorldDepth = clamped;
41 transform.m_DirtyDepth = false;
42 } else {
43 transform.m_WorldDepth = static_cast<uint8_t>(unclamped);
44 transform.m_DirtyDepth = false;
45 }
46 }
47 const auto& hierarchy = m_Owner.GetRegistry().get<Components::Entity::Hierarchy>(entity);
48 entt::entity currentChild = hierarchy.m_FirstChild;
49 while (m_Owner.GetRegistry().valid(currentChild)) {
50 UpdateTransformRecursive(currentChild, transform.m_WorldTransform, transform.m_WorldDepth, transformDirty, layerDirty);
51 currentChild = m_Owner.GetRegistry().get<Components::Entity::Hierarchy>(currentChild).m_NextSibling;
52 }
53
54 }
55
57 m_Owner.GetRegistry().on_construct<Components::Entity::Transform>().connect<&Transform::OnTransformCreate>(this);
58 return true;
59 }
60
61 void Transform::OnTransformCreate(entt::registry& registry, entt::entity entity) {
62 Entity e = entt::handle{ registry, entity };
64 tr.m_Owner = e;
65 }
66 }
67 }
68}
#define CORI_CORE_WARN_TAGGED(...)
Definition Logger.hpp:1038
#define CORI_PROFILE_FUNCTION()
Definition Profiler.hpp:9
A GameTimer is responsible for managing everything that is connected with time, ticks,...
Definition Time.hpp:8
Entities are the essential part of WorldSystem.
Definition Entity.hpp:25
decltype(auto) GetComponents()
Retries the references to the requested components of the entity.
Definition Entity.hpp:74
entt::registry & GetRegistry()
Retrieves the EnTT registry.
SceneHandle m_Owner
Definition System.hpp:38
void OnUpdate(Core::GameTimer &gameTimer) override
Definition Transform.cpp:7
Anything connected to WorldSystem (ECS) is in this namespace.
Global engine namespace.
static constexpr char Self[]
Definition Logger.hpp:122
static constexpr char Self[]
Definition Logger.hpp:119
Flags an Entity transform for recalculation. Only for internal usage!
Every Entity has a transform component by default, essential for rendering.