KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
Transform.h
Go to the documentation of this file.
1
10#pragma once
11
12#include "glm/glm.hpp"
13#include <glm/gtc/quaternion.hpp>
14#define GLM_ENABLE_EXPERIMENTAL
15#include <glm/gtx/euler_angles.hpp>
16
17namespace Karma
18{
19#define KR_SMALL_NUMBER (1.e-8f)
20
35 {
36 public:
38 glm::vec1 X;
39
41 glm::vec1 Y;
42
44 glm::vec1 Z;
45
47 glm::vec1 W;
48 };
49
65 struct KARMA_API TRotator
66 {
67 TRotator();
68 TRotator(glm::vec3 EulerAngles);
69
71 float m_Pitch;
72
74 float m_Yaw;
75
77 float m_Roll;
78
83 inline TRotator Inverse() const;
84
85 inline TRotator operator*(const TRotator& Other) const
86 {
87 return TRotator(glm::vec3(m_Pitch + Other.m_Pitch, m_Yaw + Other.m_Yaw, m_Roll + Other.m_Roll));
88 }
89
90 inline glm::vec3 operator*(const glm::vec3& Translation) const
91 {
92 glm::mat4 transform = glm::eulerAngleYXZ(m_Pitch, m_Yaw, m_Roll);
93 glm::vec4 tempResult = transform * glm::vec4(Translation.x, Translation.y, Translation.z, 1.0f);
94
95 glm::vec3 returnVector(tempResult.x, tempResult.y, tempResult.z);
96 return returnVector;
97 }
98
99 inline glm::quat ToQuat() const
100 {
101 return glm::quat(glm::vec3(m_Pitch, m_Yaw, m_Roll));
102 }
103 };
104
124 class KARMA_API FTransform
125 {
126 public:
127 FTransform();
128 FTransform(glm::vec3 rotation, glm::vec3 translation, glm::vec3 scale3D);
129
130 inline const glm::vec3& GetLocation() { return GetTranslation(); }
131 inline const TRotator& GetRotation() const { return m_Rotation; }
132 inline const glm::vec3& GetTranslation() const { return m_Translation; }
133 inline const glm::vec3& GetScale3D() const { return m_Scale3D; }
134
135 static FTransform Identity();
136
137 inline void SetScale3D(const glm::vec3& newScale)
138 {
139 m_Scale3D = newScale;
140 }
141
142 inline void SetRotation(const TRotator& newRotation)
143 {
144 m_Rotation = newRotation;
145 }
146
147 inline void SetTranslation(const glm::vec3& newTranslation)
148 {
149 m_Translation = newTranslation;
150 }
151
152 FTransform GetRelativeTransform(const FTransform& RelativeToWhat) const;
153
163 FTransform operator*(const FTransform& Other) const;
164
176 inline static void Multiply(FTransform* OutTransform, const FTransform* A, const FTransform* B);
177
178 inline bool AnyHasNegativeScale(const glm::vec3& InScale3D, const glm::vec3& InOtherScale3D) const
179 {
180 return (InScale3D.x < 0.f || InScale3D.y < 0.f || InScale3D.z < 0.f
181 || InOtherScale3D.x < 0.f || InOtherScale3D.y < 0.f || InOtherScale3D.z < 0.f);
182 }
183
191 inline glm::vec3 GetSafeScaleReciprocal(const glm::vec3& InScale, float Tolerance) const
192 {
193 glm::vec3 SafeReciprocalScale;
194
195 if (glm::abs(InScale.x) <= Tolerance)
196 {
197 SafeReciprocalScale.x = 0.f;
198 }
199 else
200 {
201 SafeReciprocalScale.x = 1 / InScale.x;
202 }
203
204 if (glm::abs(InScale.y) <= Tolerance)
205 {
206 SafeReciprocalScale.y = 0.f;
207 }
208 else
209 {
210 SafeReciprocalScale.y = 1 / InScale.y;
211 }
212
213 if (glm::abs(InScale.z) <= Tolerance)
214 {
215 SafeReciprocalScale.z = 0.f;
216 }
217 else
218 {
219 SafeReciprocalScale.z = 1 / InScale.z;
220 }
221
222 return SafeReciprocalScale;
223 }
224
226 FORCEINLINE void CopyTranslation(const FTransform& Other)
227 {
228 m_Translation = Other.GetTranslation();
229 }
230
232 FORCEINLINE void CopyRotation(const FTransform& Other)
233 {
234 m_Rotation = Other.GetRotation();
235 }
236
238 FORCEINLINE void CopyScale3D(const FTransform Other)
239 {
240 m_Scale3D = Other.GetScale3D();
241 }
242
243 glm::mat4 ToMatrixWithScale() const;
244
245 void ToTransform(const glm::mat4 Matrix);
246
247 public:
248 static FTransform m_Identity;
249
250 private:
252 TRotator m_Rotation;
254 glm::vec3 m_Translation;
256 glm::vec3 m_Scale3D;
257 };
258}
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
#define FORCEINLINE
Typical inlining macro for clarity.
Definition Core.h:170
Transform composed of Scale, Rotation (as a quaternion), and Translation.
Definition Transform.h:125
FORCEINLINE void CopyTranslation(const FTransform &Other)
Definition Transform.h:226
glm::vec3 GetSafeScaleReciprocal(const glm::vec3 &InScale, float Tolerance) const
Definition Transform.h:191
FTransform operator*(const FTransform &Other) const
Return a transform that is the result of this multiplied by another transform. Order matters when com...
Definition Transform.cpp:88
static void Multiply(FTransform *OutTransform, const FTransform *A, const FTransform *B)
Create a new transform: OutTransform = A * B.
Definition Transform.cpp:97
FORCEINLINE void CopyRotation(const FTransform &Other)
Definition Transform.h:232
FORCEINLINE void CopyScale3D(const FTransform Other)
Definition Transform.h:238
Floating point quaternion that can represent a rotation about an axis in 3-D space....
Definition Transform.h:35
glm::vec1 Y
Definition Transform.h:41
glm::vec1 Z
Definition Transform.h:44
glm::vec1 X
Definition Transform.h:38
glm::vec1 W
Definition Transform.h:47
Implements a container for rotation information.
Definition Transform.h:66
TRotator Inverse() const
Returns the counter of this rotation governed by Yaw, Pitch, and Roll in the fashion above.
Definition Transform.cpp:22
float m_Roll
Definition Transform.h:77
float m_Pitch
Definition Transform.h:71
float m_Yaw
Definition Transform.h:74