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 "krpch.h"
13
14#include "glm/glm.hpp"
15#include <glm/gtc/quaternion.hpp>
16
17#include <glm/gtx/euler_angles.hpp>
18
19namespace Karma
20{
21#define KR_SMALL_NUMBER (1.e-8f)
22
35 {
36 public:
38 glm::vec1 X;
39
41 glm::vec1 Y;
42
44 glm::vec1 Z;
45
47 glm::vec1 W;
48 };
49
64 struct KARMA_API TRotator
65 {
66 TRotator();
67 TRotator(glm::vec3 EulerAngles);
68
70 float m_Pitch;
71
73 float m_Yaw;
74
76 float m_Roll;
77
82 inline TRotator Inverse() const;
83
84 inline TRotator operator*(const TRotator& Other) const
85 {
86 return TRotator(glm::vec3(m_Pitch + Other.m_Pitch, m_Yaw + Other.m_Yaw, m_Roll + Other.m_Roll));
87 }
88
89 inline glm::vec3 operator*(const glm::vec3& Translation) const
90 {
91 glm::mat4 transform = glm::eulerAngleYXZ(m_Pitch, m_Yaw, m_Roll);
92 glm::vec4 tempResult = transform * glm::vec4(Translation.x, Translation.y, Translation.z, 1.0f);
93
94 glm::vec3 returnVector(tempResult.x, tempResult.y, tempResult.z);
95 return returnVector;
96
97 }
98 };
99
115 class KARMA_API FTransform
116 {
117 public:
118 FTransform();
119 FTransform(glm::vec3 rotation, glm::vec3 translation, glm::vec3 scale3D);
120
121 inline const glm::vec3& GetLocation() { return GetTranslation(); }
122 inline const TRotator& GetRotation() const { return m_Rotation; }
123 inline const glm::vec3& GetTranslation() const { return m_Translation; }
124 inline const glm::vec3& GetScale3D() const { return m_Scale3D; }
125
126 static FTransform Identity();
127
128 inline void SetScale3D(const glm::vec3& newScale)
129 {
130 m_Scale3D = newScale;
131 }
132
133 inline void SetRotation(const TRotator& newRotation)
134 {
135 m_Rotation = newRotation;
136 }
137
138 inline void SetTranslation(const glm::vec3& newTranslation)
139 {
140 m_Translation = newTranslation;
141 }
142
143 FTransform GetRelativeTransform(const FTransform& RelativeToWhat) const;
144
152 FTransform operator*(const FTransform& Other) const;
153
163 inline static void Multiply(FTransform* OutTransform, const FTransform* A, const FTransform* B);
164
165 inline bool AnyHasNegativeScale(const glm::vec3& InScale3D, const glm::vec3& InOtherScale3D) const
166 {
167 return (InScale3D.x < 0.f || InScale3D.y < 0.f || InScale3D.z < 0.f
168 || InOtherScale3D.x < 0.f || InOtherScale3D.y < 0.f || InOtherScale3D.z < 0.f);
169 }
170
178 inline glm::vec3 GetSafeScaleReciprocal(const glm::vec3& InScale, float Tolerance) const
179 {
180 glm::vec3 SafeReciprocalScale;
181
182 if (glm::abs(InScale.x) <= Tolerance)
183 {
184 SafeReciprocalScale.x = 0.f;
185 }
186 else
187 {
188 SafeReciprocalScale.x = 1 / InScale.x;
189 }
190
191 if (glm::abs(InScale.y) <= Tolerance)
192 {
193 SafeReciprocalScale.y = 0.f;
194 }
195 else
196 {
197 SafeReciprocalScale.y = 1 / InScale.y;
198 }
199
200 if (glm::abs(InScale.z) <= Tolerance)
201 {
202 SafeReciprocalScale.z = 0.f;
203 }
204 else
205 {
206 SafeReciprocalScale.z = 1 / InScale.z;
207 }
208
209 return SafeReciprocalScale;
210 }
211
213 FORCEINLINE void CopyTranslation(const FTransform& Other)
214 {
215 m_Translation = Other.GetTranslation();
216 }
217
219 FORCEINLINE void CopyRotation(const FTransform& Other)
220 {
221 m_Rotation = Other.GetRotation();
222 }
223
225 FORCEINLINE void CopyScale3D(const FTransform Other)
226 {
227 m_Scale3D = Other.GetScale3D();
228 }
229
230 public:
231 static FTransform m_Identity;
232
233 private:
235 TRotator m_Rotation;
237 glm::vec3 m_Translation;
239 glm::vec3 m_Scale3D;
240 };
241}
#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:157
Definition Transform.h:116
FORCEINLINE void CopyTranslation(const FTransform &Other)
Definition Transform.h:213
glm::vec3 GetSafeScaleReciprocal(const glm::vec3 &InScale, float Tolerance) const
Definition Transform.h:178
FTransform operator*(const FTransform &Other) const
Definition Transform.cpp:87
static void Multiply(FTransform *OutTransform, const FTransform *A, const FTransform *B)
Definition Transform.cpp:96
FORCEINLINE void CopyRotation(const FTransform &Other)
Definition Transform.h:219
FORCEINLINE void CopyScale3D(const FTransform Other)
Definition Transform.h:225
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
Definition Transform.h:65
TRotator Inverse() const
Definition Transform.cpp:21
float m_Roll
Definition Transform.h:76
float m_Pitch
Definition Transform.h:70
float m_Yaw
Definition Transform.h:73