Polyphase Game Engine
Loading...
Searching...
No Matches
Maths.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdint.h>
4
5#if PLATFORM_WINDOWS && _DEBUG
6#define GLM_FORCE_INLINE
7#endif
8
9#ifndef GLM_FORCE_DEPTH_ZERO_TO_ONE
10#define GLM_FORCE_DEPTH_ZERO_TO_ONE 1
11#endif
12
13#include <glm/glm.hpp>
14#include <glm/gtx/transform.hpp>
15#include <glm/gtx/quaternion.hpp>
16#include <glm/gtx/rotate_vector.hpp>
17#include <glm/gtx/matrix_decompose.hpp>
18#include <glm/gtx/matrix_transform_2d.hpp>
19#include <glm/gtx/vector_angle.hpp>
20#include <glm/gtx/euler_angles.hpp>
21#include <glm/gtc/matrix_transform.hpp>
22#include <glm/gtc/type_ptr.hpp>
23
24#include <vector>
25
26#define PI 3.14159265359f
27#define DEGREES_TO_RADIANS (PI / 180)
28#define RADIANS_TO_DEGREES (180 / PI)
29
30class Maths
31{
32public:
33
34 template<typename T>
35 static T RandRange(T min, T max)
36 {
37 int32_t retRand = min;
38 if (max >= min)
39 {
40 retRand = min + rand() % (max + 1 - min);
41 }
42 return retRand;
43 }
44
45 static int32_t RandIndexWeighted(const std::vector<float>& weights);
46
47 static void SeedRand(uint32_t seed);
48
49 static float Damp(float source, float target, float smoothing, float deltaTime);
50 static glm::vec3 Damp(glm::vec3 source, glm::vec3 target, float smoothing, float deltaTime);
51 static glm::vec4 Damp(glm::vec4 source, glm::vec4 target, float smoothing, float deltaTime);
52 static float DampAngle(float source, float target, float smoothing, float deltaTime);
53
54 static float Approach(float source, float target, float speed, float deltaTime);
55 static glm::vec3 Approach(glm::vec3 source, glm::vec3 target, float speed, float deltaTime);
56 static glm::vec4 Approach(glm::vec4 source, glm::vec4 target, float speed, float deltaTime);
57 static float ApproachAngle(float source, float target, float speed, float deltaTime);
58 static float NormalizeRange(float value, float start, float end);
59 static float Map(float inX, float inMin, float inMax, float outMin, float outMax);
60 static float MapClamped(float inX, float inMin, float inMax, float outMin, float outMax);
61 static float WindRelativeAngle(float angle0, float angle1);
62
63 static glm::vec3 SafeNormalize(glm::vec3 vector);
64
65 static bool IsPowerOfTwo(uint32_t number);
66
67 // UUID Generation
68 // Engine assets use reserved UUIDs: 0x0001000000000000 - 0x0001FFFFFFFFFFFF
69 static constexpr uint64_t ENGINE_UUID_BASE = 0x0001000000000000ULL;
70 static uint64_t GenerateAssetUuid();
71
72 static glm::vec3 ExtractPosition(const glm::mat4& mat);
73 static glm::quat ExtractRotation(const glm::mat4& mat);
74 static glm::vec3 ExtractScale(const glm::mat4& mat);
75
76 static float RotateYawTowardDirection(float srcYaw, glm::vec3 dir, float speed, float deltaTime);
77
78 static glm::vec3 VectorToRotation(glm::vec3 direction);
79 static glm::quat VectorToQuat(glm::vec3 direction);
80 static glm::vec3 QuatToVector(glm::quat quat);
81 static glm::vec3 RotationToVector(glm::vec3 rotation);
82
83 static glm::vec4 LinearToSrgb(const glm::vec4& linearColor);
84 static glm::vec4 SrgbToLinear(glm::vec4 srgbColor);
85
86
87private:
88
89 static bool mRandSeeded;
90};
91
92template<>
93float Maths::RandRange<float>(float min, float max);
94
95template<>
96glm::vec2 Maths::RandRange<glm::vec2>(glm::vec2 min, glm::vec2 max);
97
98template<>
99glm::vec3 Maths::RandRange<glm::vec3>(glm::vec3 min, glm::vec3 max);
100
101template<>
102glm::vec4 Maths::RandRange<glm::vec4>(glm::vec4 min, glm::vec4 max);
Definition Maths.h:31
static glm::vec3 QuatToVector(glm::quat quat)
Definition Maths.cpp:411
static glm::vec4 SrgbToLinear(glm::vec4 srgbColor)
Definition Maths.cpp:440
static float Approach(float source, float target, float speed, float deltaTime)
Definition Maths.cpp:104
static glm::vec4 LinearToSrgb(const glm::vec4 &linearColor)
Definition Maths.cpp:432
static T RandRange(T min, T max)
Definition Maths.h:35
static glm::vec3 ExtractPosition(const glm::mat4 &mat)
Definition Maths.cpp:260
static glm::quat VectorToQuat(glm::vec3 direction)
Definition Maths.cpp:401
static glm::vec3 VectorToRotation(glm::vec3 direction)
Definition Maths.cpp:389
static glm::vec3 RotationToVector(glm::vec3 rotation)
Definition Maths.cpp:417
static float NormalizeRange(float value, float start, float end)
Definition Maths.cpp:199
static float WindRelativeAngle(float angle0, float angle1)
Definition Maths.cpp:229
static float MapClamped(float inX, float inMin, float inMax, float outMin, float outMax)
Definition Maths.cpp:215
static float Damp(float source, float target, float smoothing, float deltaTime)
Definition Maths.cpp:49
static float Map(float inX, float inMin, float inMax, float outMin, float outMax)
Definition Maths.cpp:207
static void SeedRand(uint32_t seed)
Definition Maths.cpp:378
static int32_t RandIndexWeighted(const std::vector< float > &weights)
Definition Maths.cpp:346
static glm::vec3 SafeNormalize(glm::vec3 vector)
Definition Maths.cpp:242
static uint64_t GenerateAssetUuid()
Definition Maths.cpp:448
static float ApproachAngle(float source, float target, float speed, float deltaTime)
Definition Maths.cpp:166
static constexpr uint64_t ENGINE_UUID_BASE
Definition Maths.h:69
static bool IsPowerOfTwo(uint32_t number)
Definition Maths.cpp:255
static float RotateYawTowardDirection(float srcYaw, glm::vec3 dir, float speed, float deltaTime)
Definition Maths.cpp:383
static glm::vec3 ExtractScale(const glm::mat4 &mat)
Definition Maths.cpp:326
static glm::quat ExtractRotation(const glm::mat4 &mat)
Definition Maths.cpp:280
static float DampAngle(float source, float target, float smoothing, float deltaTime)
Definition Maths.cpp:73