Karma Engine
Loading...
Searching...
No Matches
Transform.h
1#pragma once
2
3#include "krpch.h"
4
5#include "glm/glm.hpp"
6#include <glm/gtc/quaternion.hpp>
7
8#include <glm/gtx/euler_angles.hpp>
9
10namespace Karma
11{
12#define KR_SMALL_NUMBER (1.e-8f)
13
25 struct KARMA_API TQuaternion
26 {
27 public:
29 glm::vec1 X;
30
32 glm::vec1 Y;
33
35 glm::vec1 Z;
36
38 glm::vec1 W;
39 };
40
55 struct KARMA_API TRotator
56 {
57 TRotator();
58 TRotator(glm::vec3 EulerAngles);
59
61 float m_Pitch;
62
64 float m_Yaw;
65
67 float m_Roll;
68
73 inline TRotator Inverse() const;
74
75 inline TRotator operator*(const TRotator& Other) const
76 {
77 return TRotator(glm::vec3(m_Pitch + Other.m_Pitch, m_Yaw + Other.m_Yaw, m_Roll + Other.m_Roll));
78 }
79
80 inline glm::vec3 operator*(const glm::vec3& Translation) const
81 {
82 glm::mat4 transform = glm::eulerAngleYXZ(m_Pitch, m_Yaw, m_Roll);
83 glm::vec4 tempResult = transform * glm::vec4(Translation.x, Translation.y, Translation.z, 1.0f);
84
85 glm::vec3 returnVector(tempResult.x, tempResult.y, tempResult.z);
86 return returnVector;
87
88 }
89 };
90
106 class KARMA_API FTransform
107 {
108 public:
109 FTransform();
110 FTransform(glm::vec3 rotation, glm::vec3 translation, glm::vec3 scale3D);
111
112 inline const glm::vec3& GetLocation() { return GetTranslation(); }
113 inline const TRotator& GetRotation() const { return m_Rotation; }
114 inline const glm::vec3& GetTranslation() const { return m_Translation; }
115 inline const glm::vec3& GetScale3D() const { return m_Scale3D; }
116
117 static FTransform Identity();
118
119 inline void SetScale3D(const glm::vec3& newScale)
120 {
121 m_Scale3D = newScale;
122 }
123
124 inline void SetRotation(const TRotator& newRotation)
125 {
126 m_Rotation = newRotation;
127 }
128
129 inline void SetTranslation(const glm::vec3& newTranslation)
130 {
131 m_Translation = newTranslation;
132 }
133
134 FTransform GetRelativeTransform(const FTransform& RelativeToWhat) const;
135
143 FTransform operator*(const FTransform& Other) const;
144
154 inline static void Multiply(FTransform* OutTransform, const FTransform* A, const FTransform* B);
155
156 inline bool AnyHasNegativeScale(const glm::vec3& InScale3D, const glm::vec3& InOtherScale3D) const
157 {
158 return (InScale3D.x < 0.f || InScale3D.y < 0.f || InScale3D.z < 0.f
159 || InOtherScale3D.x < 0.f || InOtherScale3D.y < 0.f || InOtherScale3D.z < 0.f);
160 }
161
169 inline glm::vec3 GetSafeScaleReciprocal(const glm::vec3& InScale, float Tolerance) const
170 {
171 glm::vec3 SafeReciprocalScale;
172
173 if (glm::abs(InScale.x) <= Tolerance)
174 {
175 SafeReciprocalScale.x = 0.f;
176 }
177 else
178 {
179 SafeReciprocalScale.x = 1 / InScale.x;
180 }
181
182 if (glm::abs(InScale.y) <= Tolerance)
183 {
184 SafeReciprocalScale.y = 0.f;
185 }
186 else
187 {
188 SafeReciprocalScale.y = 1 / InScale.y;
189 }
190
191 if (glm::abs(InScale.z) <= Tolerance)
192 {
193 SafeReciprocalScale.z = 0.f;
194 }
195 else
196 {
197 SafeReciprocalScale.z = 1 / InScale.z;
198 }
199
200 return SafeReciprocalScale;
201 }
202
204 FORCEINLINE void CopyTranslation(const FTransform& Other)
205 {
206 m_Translation = Other.GetTranslation();
207 }
208
210 FORCEINLINE void CopyRotation(const FTransform& Other)
211 {
212 m_Rotation = Other.GetRotation();
213 }
214
216 FORCEINLINE void CopyScale3D(const FTransform Other)
217 {
218 m_Scale3D = Other.GetScale3D();
219 }
220
221 public:
222 static FTransform m_Identity;
223
224 private:
226 TRotator m_Rotation;
228 glm::vec3 m_Translation;
230 glm::vec3 m_Scale3D;
231 };
232}
Definition Transform.h:107
FORCEINLINE void CopyTranslation(const FTransform &Other)
Definition Transform.h:204
glm::vec3 GetSafeScaleReciprocal(const glm::vec3 &InScale, float Tolerance) const
Definition Transform.h:169
FORCEINLINE void CopyRotation(const FTransform &Other)
Definition Transform.h:210
FORCEINLINE void CopyScale3D(const FTransform Other)
Definition Transform.h:216
Definition Transform.h:26
glm::vec1 Y
Definition Transform.h:32
glm::vec1 Z
Definition Transform.h:35
glm::vec1 X
Definition Transform.h:29
glm::vec1 W
Definition Transform.h:38
Definition Transform.h:56
float m_Roll
Definition Transform.h:67
float m_Pitch
Definition Transform.h:61
float m_Yaw
Definition Transform.h:64