Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
Transform.hpp
Go to the documentation of this file.
1#pragma once
2#include <glm/glm.hpp>
3#include <glm/gtc/matrix_transform.hpp>
4#include <glm/gtc/constants.hpp>
5#include <glm/gtc/quaternion.hpp>
6#include <initializer_list>
7#include "core/EngineAPI.hpp"
9
10
16 float x = 0.0f;
17 float y = 0.0f;
18 float z = 0.0f;
19
20private:
21 glm::quat q{ 1.0f, 0.0f, 0.0f, 0.0f };
22 mutable float lastX = 0.0f;
23 mutable float lastY = 0.0f;
24 mutable float lastZ = 0.0f;
25
26 void syncQuatFromEuler() const {
27 if (x != lastX || y != lastY || z != lastZ) {
28 glm::quat qx = glm::angleAxis(glm::radians(x), glm::vec3(1.0f, 0.0f, 0.0f));
29 glm::quat qy = glm::angleAxis(glm::radians(y), glm::vec3(0.0f, 1.0f, 0.0f));
30 glm::quat qz = glm::angleAxis(glm::radians(z), glm::vec3(0.0f, 0.0f, 1.0f));
31 const_cast<glm::quat&>(q) = glm::normalize(qz * qy * qx);
32 lastX = x;
33 lastY = y;
34 lastZ = z;
35 }
36 }
37
39 glm::mat3 R = glm::mat3_cast(q);
40
41 // Pitch (x)
42 float sinX = -R[2][1];
43 float thetaX = 0.0f;
44 float thetaY = 0.0f;
45 float thetaZ = 0.0f;
46
47 if (sinX < 0.9999f && sinX > -0.9999f) {
48 thetaX = std::asin(sinX);
49 thetaY = std::atan2(R[2][0], R[2][2]);
50 thetaZ = std::atan2(R[0][1], R[1][1]);
51 } else {
52 // Gimbal lock case
53 thetaX = (sinX >= 0.0f) ? (3.14159265f * 0.5f) : (-3.14159265f * 0.5f);
54 thetaY = std::atan2(-R[0][2], R[0][0]);
55 thetaZ = 0.0f;
56 }
57
58 x = glm::degrees(thetaX);
59 y = glm::degrees(thetaY);
60 z = glm::degrees(thetaZ);
61 lastX = x;
62 lastY = y;
63 lastZ = z;
64 }
65
66public:
67 RotationField() = default;
68
69 RotationField(const glm::vec3& euler) {
70 x = euler.x; y = euler.y; z = euler.z;
72 }
73
74 RotationField(const glm::quat& quaternion) {
75 q = glm::normalize(quaternion);
77 }
78
79 RotationField& operator=(const glm::vec3& eulerDegrees) {
80 x = eulerDegrees.x;
81 y = eulerDegrees.y;
82 z = eulerDegrees.z;
84 return *this;
85 }
86
87 RotationField& operator=(const glm::quat& newQ) {
88 q = glm::normalize(newQ);
90 return *this;
91 }
92
93 RotationField& operator=(const std::initializer_list<float>& list) {
94 auto it = list.begin();
95 if (it != list.end()) x = *it++;
96 if (it != list.end()) y = *it++;
97 if (it != list.end()) z = *it++;
99 return *this;
100 }
101
102 operator glm::vec3() const {
103 return glm::vec3(x, y, z);
104 }
105
106 operator glm::quat() const {
108 return q;
109 }
110
111 glm::quat getQuat() const {
113 return q;
114 }
115
116 void setQuat(const glm::quat& newQ) {
117 q = glm::normalize(newQ);
119 }
120};
121
126// [ReflectClass]
128
129 // [ReflectField]
130 glm::vec3 position{ 0.0f };
131 // [ReflectField]
133 // [ReflectField]
134 glm::vec3 scale{ 1.0f };
135
136
137
142 glm::quat rotationQuat() const {
143 return rotation.getQuat();
144 }
145
150 glm::vec3 forward() const {
151 return glm::normalize(rotation.getQuat() * glm::vec3(0, 0, -1));
152 }
153
158 glm::vec3 right() const {
159 return glm::normalize(rotation.getQuat() * glm::vec3(1, 0, 0));
160 }
161
166 glm::vec3 up() const {
167 return glm::normalize(rotation.getQuat() * glm::vec3(0, 1, 0));
168 }
169
174 glm::mat4 matrix() const
175 {
176 glm::mat4 m(1.0f);
177 m = glm::translate(m, position);
178 m = m * glm::mat4_cast(rotation.getQuat());
179 m = glm::scale(m, scale);
180 return m;
181 }
182
186 Transform() = default;
187
194 Transform(glm::vec3 pos, glm::vec3 rot = glm::vec3(0.f), glm::vec3 s = glm::vec3(1.f))
195 : position(pos), rotation(rot), scale(s) {
196 }
197};
198
200
201
202
203
204
205
206
207
#define REGISTER_COMPONENT(Type, MenuPath)
Definition ComponentReflection.hpp:156
#define ENGINE_API
Definition EngineAPI.hpp:10
Compatibility layer that wraps a glm::quat but behaves like a glm::vec3 for Euler angles.
Definition Transform.hpp:15
void syncQuatFromEuler() const
Definition Transform.hpp:26
float lastY
Definition Transform.hpp:23
void setQuat(const glm::quat &newQ)
Definition Transform.hpp:116
glm::quat getQuat() const
Definition Transform.hpp:111
float lastX
Definition Transform.hpp:22
RotationField(const glm::quat &quaternion)
Definition Transform.hpp:74
void syncEulerFromQuat()
Definition Transform.hpp:38
RotationField(const glm::vec3 &euler)
Definition Transform.hpp:69
RotationField & operator=(const glm::vec3 &eulerDegrees)
Definition Transform.hpp:79
float lastZ
Definition Transform.hpp:24
RotationField()=default
float z
Definition Transform.hpp:18
float y
Definition Transform.hpp:17
glm::quat q
Definition Transform.hpp:21
float x
Definition Transform.hpp:16
RotationField & operator=(const glm::quat &newQ)
Definition Transform.hpp:87
RotationField & operator=(const std::initializer_list< float > &list)
Definition Transform.hpp:93
Represents the position, rotation, and scale of an entity in 3D space.
Definition Transform.hpp:127
glm::vec3 right() const
Gets the right direction vector in local space.
Definition Transform.hpp:158
Transform()=default
Default constructor for Transform.
glm::vec3 forward() const
Gets the forward direction vector in local space.
Definition Transform.hpp:150
Transform(glm::vec3 pos, glm::vec3 rot=glm::vec3(0.f), glm::vec3 s=glm::vec3(1.f))
Construct a new Transform object.
Definition Transform.hpp:194
RotationField rotation
Definition Transform.hpp:132
glm::vec3 position
Definition Transform.hpp:130
glm::vec3 up() const
Gets the up direction vector in local space.
Definition Transform.hpp:166
glm::quat rotationQuat() const
Converts the Euler rotation angles to a quaternion.
Definition Transform.hpp:142
glm::vec3 scale
Definition Transform.hpp:134
glm::mat4 matrix() const
Computes the model matrix representing the transform.
Definition Transform.hpp:174