Polyphase Game Engine
Loading...
Searching...
No Matches
Maths.h
Go to the documentation of this file.
1// SGDK case-collision shim. WSL2's DrvFs on /mnt/c is case-insensitive by
2// default, so when SGDK's sgdk/inc/ext/console.h does `#include "maths.h"`
3// (lowercase), gcc can case-insensitively match this file (`Maths.h`, capital)
4// via the engine's -I path. PolyphaseSGDK.h defines POLYPHASE_IN_SGDK_INCLUDE
5// while pulling SGDK headers — in that scope we DON'T want to load engine
6// content; we want to delegate to SGDK's real maths.h instead. Use
7// #include_next (gcc extension) to skip the engine entry and resume the
8// search at -idirafter $(GDK)/inc where SGDK's lives. Critical: we must NOT
9// #pragma once on this branch — otherwise gcc remembers this file as
10// "loaded" and a later engine `#include "Maths.h"` (which DOES want engine
11// content) gets short-circuited.
12#if defined(POLYPHASE_IN_SGDK_INCLUDE)
13#include_next <maths.h>
14#else
15
16#pragma once
17
18#include <stdint.h>
19
20#if PLATFORM_WINDOWS && _DEBUG
21#define GLM_FORCE_INLINE
22#endif
23
24#ifndef GLM_FORCE_DEPTH_ZERO_TO_ONE
25#define GLM_FORCE_DEPTH_ZERO_TO_ONE 1
26#endif
27
28#include <glm/glm.hpp>
29#include <glm/gtx/transform.hpp>
30#include <glm/gtx/quaternion.hpp>
31#include <glm/gtx/rotate_vector.hpp>
32#include <glm/gtx/matrix_decompose.hpp>
33#include <glm/gtx/matrix_transform_2d.hpp>
34#include <glm/gtx/vector_angle.hpp>
35#include <glm/gtx/euler_angles.hpp>
36#include <glm/gtc/matrix_transform.hpp>
37#include <glm/gtc/type_ptr.hpp>
38
39#include <vector>
40
41#define PI 3.14159265359f
42#define DEGREES_TO_RADIANS (PI / 180)
43#define RADIANS_TO_DEGREES (180 / PI)
44
45class Maths
46{
47public:
48
49 template<typename T>
50 static T RandRange(T min, T max)
51 {
52 int32_t retRand = min;
53 if (max >= min)
54 {
55 retRand = min + rand() % (max + 1 - min);
56 }
57 return retRand;
58 }
59
60 static int32_t RandIndexWeighted(const std::vector<float>& weights);
61
62 static void SeedRand(uint32_t seed);
63
64 static float Damp(float source, float target, float smoothing, float deltaTime);
65 static glm::vec3 Damp(glm::vec3 source, glm::vec3 target, float smoothing, float deltaTime);
66 static glm::vec4 Damp(glm::vec4 source, glm::vec4 target, float smoothing, float deltaTime);
67 static float DampAngle(float source, float target, float smoothing, float deltaTime);
68
69 static float Approach(float source, float target, float speed, float deltaTime);
70 static glm::vec3 Approach(glm::vec3 source, glm::vec3 target, float speed, float deltaTime);
71 static glm::vec4 Approach(glm::vec4 source, glm::vec4 target, float speed, float deltaTime);
72 static float ApproachAngle(float source, float target, float speed, float deltaTime);
73 static float NormalizeRange(float value, float start, float end);
74 static float Map(float inX, float inMin, float inMax, float outMin, float outMax);
75 static float MapClamped(float inX, float inMin, float inMax, float outMin, float outMax);
76 static float WindRelativeAngle(float angle0, float angle1);
77
78 static glm::vec3 SafeNormalize(glm::vec3 vector);
79
80 static bool IsPowerOfTwo(uint32_t number);
81 static uint32_t NextPowerOfTwo(uint32_t x); // ceil; 0->1, 1->1, 5->8
82 static uint32_t PrevPowerOfTwo(uint32_t x); // floor; 0->1, 1->1, 5->4
83
84 // UUID Generation
85 // Engine assets use reserved UUIDs: 0x0001000000000000 - 0x0001FFFFFFFFFFFF
86 static constexpr uint64_t ENGINE_UUID_BASE = 0x0001000000000000ULL;
87 static uint64_t GenerateAssetUuid();
88
89 static glm::vec3 ExtractPosition(const glm::mat4& mat);
90 static glm::quat ExtractRotation(const glm::mat4& mat);
91 static glm::vec3 ExtractScale(const glm::mat4& mat);
92
93 static float RotateYawTowardDirection(float srcYaw, glm::vec3 dir, float speed, float deltaTime);
94
95 static glm::vec3 VectorToRotation(glm::vec3 direction);
96 static glm::quat VectorToQuat(glm::vec3 direction);
97 static glm::vec3 QuatToVector(glm::quat quat);
98 static glm::vec3 RotationToVector(glm::vec3 rotation);
99
100 static glm::vec4 LinearToSrgb(const glm::vec4& linearColor);
101 static glm::vec4 SrgbToLinear(glm::vec4 srgbColor);
102
103
104private:
105
106 static bool mRandSeeded;
107};
108
109template<>
110float Maths::RandRange<float>(float min, float max);
111
112template<>
113glm::vec2 Maths::RandRange<glm::vec2>(glm::vec2 min, glm::vec2 max);
114
115template<>
116glm::vec3 Maths::RandRange<glm::vec3>(glm::vec3 min, glm::vec3 max);
117
118template<>
119glm::vec4 Maths::RandRange<glm::vec4>(glm::vec4 min, glm::vec4 max);
120
121#endif // POLYPHASE_IN_SGDK_INCLUDE
Definition Maths.h:46
static glm::vec3 QuatToVector(glm::quat quat)
Definition Maths.cpp:431
static glm::vec4 SrgbToLinear(glm::vec4 srgbColor)
Definition Maths.cpp:460
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:452
static T RandRange(T min, T max)
Definition Maths.h:50
static glm::vec3 ExtractPosition(const glm::mat4 &mat)
Definition Maths.cpp:280
static glm::quat VectorToQuat(glm::vec3 direction)
Definition Maths.cpp:421
static glm::vec3 VectorToRotation(glm::vec3 direction)
Definition Maths.cpp:409
static glm::vec3 RotationToVector(glm::vec3 rotation)
Definition Maths.cpp:437
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 uint32_t NextPowerOfTwo(uint32_t x)
Definition Maths.cpp:260
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:398
static uint32_t PrevPowerOfTwo(uint32_t x)
Definition Maths.cpp:270
static int32_t RandIndexWeighted(const std::vector< float > &weights)
Definition Maths.cpp:366
static glm::vec3 SafeNormalize(glm::vec3 vector)
Definition Maths.cpp:242
static uint64_t GenerateAssetUuid()
Definition Maths.cpp:468
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:86
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:403
static glm::vec3 ExtractScale(const glm::mat4 &mat)
Definition Maths.cpp:346
static glm::quat ExtractRotation(const glm::mat4 &mat)
Definition Maths.cpp:300
static float DampAngle(float source, float target, float smoothing, float deltaTime)
Definition Maths.cpp:73