Cogs.Core
TransformSystem.h
1#pragma once
2
3#include "Systems/ComponentSystem.h"
4
5#include "Components/Core/TransformComponent.h"
6
7#include <glm/gtc/quaternion.hpp>
8#include <glm/gtx/transform.hpp>
9
10namespace Cogs
11{
12 namespace Core
13 {
15 struct LocalTransform : public glm::mat4
16 {
17 };
18
20 struct WorldTransform : public glm::mat4
21 {
22 };
23
26 {
27 bool cached = false;
28 bool changed = false;
29 };
30
35 class COGSCORE_DLL_API TransformSystem : public ComponentSystemWithDataPools<TransformComponent, LocalTransform, WorldTransform, TransformState>
36 {
37 public:
38 TransformSystem(Memory::Allocator * allocator, SizeType capacity) : ComponentSystemWithDataPools(allocator, capacity) {}
39
40 void handleOriginUpdate(class Context* context);
41
43 void update(class Context * context) override;
44
45 void updateLocalTransform(const TransformComponent & component)
46 {
47 auto offset = component.parent ? glm::vec3(0, 0, 0) : glm::vec3(component.coordinates - origin);
48
49 if (component.transformFlags) {
50 getLocalTransform(&component) = glm::translate(glm::mat4(1.f), offset) * component.transform.matrix;
51 } else {
52 getLocalTransform(&component) = glm::translate(glm::mat4(1.f), offset + component.transform.trs.position) *
53 glm::mat4_cast(component.transform.trs.rotation) * glm::scale(component.transform.trs.scale);
54 }
55 }
56
57 /*
58 Set an explicit local transform to the given component.
59 */
60 void setLocalTransform(TransformComponent* component, const glm::mat4& matrix);
61
66 {
67 getData<TransformState>(&component).cached = false;
68
69 updateLocalTransform(component);
70 updateLocalToWorldTransform(component, true);
71 }
72
74 void setOrigin(const glm::dvec3& newOrigin) { originUpdate = newOrigin; }
75
77 glm::dvec3 getOrigin() const { return origin; }
78
79 glm::mat4 & getLocalToWorld(const TransformComponent * component)
80 {
81 return getData<WorldTransform>(component);
82 }
83
84 glm::mat4 & getLocalTransform(const TransformComponent * component)
85 {
86 return getData<LocalTransform>(component);
87 }
88
89 void setTransform(TransformComponent * component, glm::mat4 transformMatrix)
90 {
91 getLocalToWorld(component) = transformMatrix;
92
93 component->unsetFlag(ComponentModel::ComponentFlags::Changed);
94 }
95
96 void copyTransform(const TransformComponent * source, TransformComponent * destination)
97 {
98 setTransform(destination, getLocalToWorld(source));
99 }
100
101 bool hasChanged(const TransformComponent * component)
102 {
103 return getData<TransformState>(component).changed;
104 }
105
106 void updateLocalToWorldTransform(const TransformComponent & component, bool dirty = false);
107
108 inline glm::vec3 engineFromWorldCoords(glm::dvec3 c) { return glm::vec3(c - origin); }
109 inline glm::dvec3 worldFromEngineCoords(glm::vec3 c) { return origin + glm::dvec3(c); }
110
111 private:
112 glm::dvec3 origin = glm::dvec3(0.0);
113 glm::dvec3 originUpdate = glm::dvec3(0.0);
114 };
115 }
116}
Component system template with multiple parallel structures per component stored in separate pools si...
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
Defines a 4x4 transformation matrix for the entity and a global offset for root entities.
uint32_t transformFlags
Transform flags.
ComponentModel::ComponentHandle parent
Parent transform of the component.
glm::dvec3 coordinates
Global coordinates.
The transform system handles TransformComponent instances, calculating local and global transform dat...
void setOrigin(const glm::dvec3 &newOrigin)
Sets the Origin offset of the scene.
glm::dvec3 getOrigin() const
Gets the Origin offset of the scene.
void updateTransformData(const TransformComponent &component)
Force an update of the transform data associated with the given component.
Base allocator implementation.
Definition: Allocator.h:30
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
ComponentIndex SizeType
Type used to track the size of pools.
Definition: Component.h:19
Defines local transform data.
glm::quat rotation
Rotation given as a quaternion.
glm::vec3 scale
Scale factor to apply to each of the axes.
glm::vec3 position
Local position relative to the global coordinates, or the parent coordinate system if the parent fiel...
Defines the state of a transform.
Defines world transform data.
glm::mat4 matrix
Complete transformation.