Shkyera Engine
Easy to use, game engine for Python
Loading...
Searching...
No Matches
TransformComponent.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <iostream>
4#include <string>
5
7#include <glm/glm.hpp>
8#include <glm/gtc/matrix_transform.hpp>
9#include <glm/gtc/quaternion.hpp>
10
11#define GLM_ENABLE_EXPERIMENTAL
12#include <glm/gtx/matrix_decompose.hpp>
13#include <glm/gtx/string_cast.hpp>
14
16#include <ECS/Registry.hpp>
17#include <Math/Utils.hpp>
18
19namespace shkyera {
20
21class TransformComponent : public BaseComponent<TransformComponent> {
22 public:
23 TransformComponent() = default;
24
25 glm::vec3& getPosition() { return _position; }
26 const glm::vec3& getPosition() const { return _position; }
27 void setPosition(const glm::vec3& position) { _position = position; }
28
29 glm::vec3& getOrientation() { return _orientation; }
30 const glm::vec3& getOrientation() const { return _orientation; }
31 void setOrientation(const glm::vec3& orientation) { _orientation = orientation; }
32
33 glm::vec3& getScale() { return _scale; }
34 const glm::vec3& getScale() const { return _scale; }
35 void setScale(const glm::vec3& scale) { _scale = scale; }
36
37 glm::mat4 getRotationMatrix() const {
38 glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), _orientation.y, glm::vec3(0.0f, 1.0f, 0.0f));
39 rotationMatrix = glm::rotate(rotationMatrix, _orientation.x, glm::vec3(1.0f, 0.0f, 0.0f));
40 rotationMatrix = glm::rotate(rotationMatrix, _orientation.z, glm::vec3(0.0f, 0.0f, 1.0f));
41 return rotationMatrix;
42 }
43
44 glm::mat4 getTransformMatrix() const {
45 glm::mat4 translationMatrix = glm::translate(glm::mat4(1.0f), _position);
47 glm::mat4 scaleMatrix = glm::scale(glm::mat4(1.0f), _scale);
48
50 }
51
52 static glm::mat4 getGlobalRotationMatrix(Entity entity, std::shared_ptr<Registry> registry) {
53 std::vector<glm::mat4> transforms;
55 transforms.emplace_back(std::move(localTransform));
56
57 const auto& hierarchy = registry->getHierarchy();
58 while (const auto& parentOpt = hierarchy.getParent(entity)) {
60 if (registry->hasComponent<TransformComponent>(entity)) {
62 transforms.emplace_back(std::move(localParentTransform));
63 }
64 }
65
66 auto result = glm::mat4(1.0);
67 for (auto it = transforms.rbegin(); it != transforms.rend(); ++it) {
68 result = result * (*it);
69 }
70 return result;
71 }
72
73 static glm::mat4 getGlobalTransformMatrix(Entity entity, std::shared_ptr<Registry> registry) {
74 std::vector<glm::mat4> transforms;
76 transforms.emplace_back(std::move(localTransform));
77
78 const auto& hierarchy = registry->getHierarchy();
79 while (const auto& parentOpt = hierarchy.getParent(entity)) {
81 if (registry->hasComponent<TransformComponent>(entity)) {
83 transforms.emplace_back(std::move(localParentTransform));
84 }
85 }
86
87 auto result = glm::mat4(1.0);
88 for (auto it = transforms.rbegin(); it != transforms.rend(); ++it) {
89 result = result * (*it);
90 }
91 return result;
92 }
93
94 private:
95 glm::vec3 _position{0.0, 0.0, 0.0};
96 glm::vec3 _orientation{0.0, 0.0, 0.0};
97 glm::vec3 _scale{1.0, 1.0, 1.0};
98};
99
100} // namespace shkyera
Base component for implementing update functionality.
Definition BaseComponent.hpp:17
Definition TransformComponent.hpp:21
glm::mat4 getTransformMatrix() const
Definition TransformComponent.hpp:44
static glm::mat4 getGlobalRotationMatrix(Entity entity, std::shared_ptr< Registry > registry)
Definition TransformComponent.hpp:52
void setScale(const glm::vec3 &scale)
Definition TransformComponent.hpp:35
void setOrientation(const glm::vec3 &orientation)
Definition TransformComponent.hpp:31
static glm::mat4 getGlobalTransformMatrix(Entity entity, std::shared_ptr< Registry > registry)
Definition TransformComponent.hpp:73
const glm::vec3 & getOrientation() const
Definition TransformComponent.hpp:30
void setPosition(const glm::vec3 &position)
Definition TransformComponent.hpp:27
const glm::vec3 & getPosition() const
Definition TransformComponent.hpp:26
const glm::vec3 & getScale() const
Definition TransformComponent.hpp:34
glm::vec3 _position
Definition TransformComponent.hpp:95
glm::vec3 & getScale()
Definition TransformComponent.hpp:33
glm::vec3 & getOrientation()
Definition TransformComponent.hpp:29
glm::mat4 getRotationMatrix() const
Definition TransformComponent.hpp:37
glm::vec3 _orientation
Definition TransformComponent.hpp:96
glm::vec3 & getPosition()
Definition TransformComponent.hpp:25
glm::vec3 _scale
Definition TransformComponent.hpp:97
Definition Asset.hpp:6
Definition Clock.hpp:9