RavEngine
Loading...
Searching...
No Matches
mathtypes.hpp
1#pragma once
2
3#include "glm/mat4x4.hpp"
6#include "Array.hpp"
7#include "CTTI.hpp"
8#include <iostream>
9
10//defines the vector and quaternion types
11//can change these to 32 bit float instead of 64 bit
12
13typedef glm::ivec2 vector2i;
14typedef glm::ivec3 vector3i;
15typedef glm::ivec4 vector4i;
16
17#define DOUBLE_PRECISION 0
18#if DOUBLE_PRECISION
19
20
21//a double-precision 3-component vector
22typedef glm::dvec3 vector3;
23typedef glm::dvec4 vector4;
24typedef glm::dvec2 vector2;
25
26//a double-precision quaternion
28
29typedef glm::dmat4x4 matrix4;
30typedef glm::dmat3x3 matrix3;
31
32typedef double decimalType;
33
34#else
35//a float 3-component vector
36typedef glm::vec3 vector3;
37typedef glm::vec4 vector4;
38typedef glm::vec2 vector2;
39
40//a float-precision quaternion
41typedef glm::quat quaternion;
42
43typedef glm::mat4x4 matrix4;
44typedef glm::mat3x3 matrix3;
45
46typedef float decimalType;
47#endif
48
50 return glm::quat_identity<decimalType, glm::packed_highp>();
51}
52
53//constant vector directions
54constexpr vector3 vector3_right = vector3(1, 0, 0);
55constexpr vector3 vector3_up = vector3(0, 1, 0);
56constexpr vector3 vector3_forward = vector3(0, 0, -1);
57
58static inline std::ostream& operator<<(std::ostream& os, const vector3& vec){
59 os << "vector3(" << vec.x << ", " << vec.y << ", " << vec.z << ")";
60 return os;
61}
62
63static inline std::ostream& operator<<(std::ostream& os, const glm::vec4& vec) {
64 os << "fvec4(" << vec.x << ", " << vec.y << ", " << vec.z << ", " << vec.w << ")";
65 return os;
66}
67
68
69static inline std::ostream& operator<<(std::ostream& os, const quaternion& quat){
70 os << "quat(" << quat.x << ", " << quat.y << ", " << quat.z << ", " << quat.w << ")";
71 return os;
72}
73
74static inline std::ostream& operator<<(std::ostream& os, const matrix4& mat){
75 os << "mat4(";
76 for(uint8_t i = 0; i < 4; i++){
77 for(uint8_t j = 0; j < 4; j++){
78 os << mat[i][j] << " ";
79 }
80 os << "\n";
81 }
82 os << ")";
83 return os;
84}
85
86#define print_vec3(vec) "vector3(" << vec.x << ", " << vec.y << ", " << vec.z << ")"
87#define print_quat(quat) "quat(" << quat.x << ", " << quat.y << ", " << quat.z << ", " << quat.w << ")"
88
89namespace RavEngine {
90 constexpr decimalType PI = 3.1415926535897932385;
91
92 typedef Array<decimalType, 3> RawVec3;
93 typedef Array<decimalType, 4> RawQuat;
94
95 static_assert(std::is_trivially_copyable_v<RawVec3>, "RawVec3 is not trivially copyable!");
96 static_assert(!std::is_fundamental_v<RawVec3>, "RawVec3 is fundamental!");
97 static_assert(!std::is_same_v<RawVec3,void>,"RawVec3 is void!");
98 static_assert(is_eligible<RawVec3>,"RawVec3 is not eligible for CTTI!");
99}
100
101static inline RavEngine::RawVec3 Vec3toRaw(const vector3& vec) {
102 return { vec.x, vec.y, vec.z };
103}
104
105static inline vector3 RawToVec3(const RavEngine::RawVec3& raw) {
106 return vector3(raw[0], raw[1], raw[2]);
107}
108
109static inline RavEngine::RawQuat QuatToRaw(const quaternion& quat) {
110 return { quat.w,quat.x,quat.y,quat.z };
111}
112
113static inline quaternion RawToQuat(const RavEngine::RawQuat& raw) {
114 return quaternion(raw[0], raw[1], raw[2], raw[3]);
115}
116
118 vector3 translate, scale;
119 quaternion rotate;
120};
121
122struct Bounds {
123 float min[3]{ 0,0,0 };
124 float max[3]{ 0,0,0 };
125};
126
127template<typename T>
128struct dim_t {
129 T width = 0, height = 0;
130};
131
132namespace RMath {
133 // use these instead of glm::perspective or glm::ortho
134 // these also implement reverse Z.
135
136 template<typename T>
137 auto orthoProjection(T left, T right, T bottom, T top, T zNear, T zFar) {
138 return glm::orthoRH_ZO(left, right, bottom, top, zFar, zNear);
139 }
140
141 template<typename T>
142 auto perspectiveProjection(T fovy, T aspect, T zNear, T zFar) {
143 return glm::perspectiveRH_ZO(fovy, aspect, zFar, zNear);
144 }
145
149 static inline bool pointInAABB(const vector3& point, const vector3& boxHalfExts) {
150 return (point.x <= boxHalfExts.x && point.x >= -boxHalfExts.x) &&
151 (point.y <= boxHalfExts.y && point.y >= -boxHalfExts.y) &&
152 (point.z <= boxHalfExts.z && point.z >= -boxHalfExts.z);
153 }
154}
155
156//ssize_t on MSVC
157#if defined(_MSC_VER)
158#include <BaseTsd.h>
159#include <intsafe.h>
160typedef SSIZE_T ssize_t;
161#endif
Definition concurrentqueue.h:747
GLM_FUNC_DECL mat< 4, 4, T, defaultp > perspectiveRH_ZO(T fovy, T aspect, T near, T far)
Definition matrix_clip_space.inl:233
GLM_FUNC_DECL mat< 4, 4, T, defaultp > orthoRH_ZO(T left, T right, T bottom, T top, T zNear, T zFar)
Definition matrix_clip_space.inl:42
qua< float, defaultp > quat
Quaternion of single-precision floating-point numbers.
Definition quaternion_float.hpp:35
GLM_FUNC_DECL GLM_CONSTEXPR qua< T, Q > quat_identity()
Definition quaternion.inl:9
Definition Animation.hpp:6
Definition mathtypes.hpp:122
Definition mathtypes.hpp:128
Definition qualifier.hpp:36
Definition type_quat.hpp:20
Definition qualifier.hpp:35
Definition mathtypes.hpp:117