philsupertramp/game-math
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
9#pragma once
10#include "mat/mat2.h"
11#include "mat/mat3.h"
12#include "mat/mat4.h"
13#include "vec/vec2.h"
14#include "vec/vec3.h"
15#include "vec/vec4.h"
16
17namespace Math::Utils {
18
26 template<class V>
27 float distance(const V& a, const V& b) {
28 return (a - b).length();
29 }
30
37 template<class T>
39 return a.normalize();
40 }
41
48 template<class T>
50 return a.normalize();
51 }
52
59 template<class T>
61 return a.normalize();
62 }
63
71 template<class T>
72 mat4<T> translate(const mat4<T>& M, const vec3<T>& V) {
73 mat4<T> translation = M;
74 vec3<T> res = vec3<T>(M[0][0], M[0][1], M[0][2]) * V.x + vec3<T>(M[1][0], M[1][1], M[1][2]) * V.y
75 + vec3<T>(M[2][0], M[2][1], M[2][2]) * V.z + vec3<T>(M[3][0], M[3][1], M[3][2]);
76 translation[3][0] = res.x;
77 translation[3][1] = res.y;
78 translation[3][2] = res.z;
79 return translation;
80 }
81
90 template<class T>
91 mat4<T> lookAt(const vec3<T>& eye, const vec3<T>& center, const vec3<T>& up) {
92 vec3<T> const f(normalize(center - eye));
93 vec3<T> const s(normalize(f.cross(up)));
94 vec3<T> const u(s.cross(f));
95
96 mat4<T> Result(1);
97 Result[0][0] = s.x;
98 Result[1][0] = s.y;
99 Result[2][0] = s.z;
100 Result[0][1] = u.x;
101 Result[1][1] = u.y;
102 Result[2][1] = u.z;
103 Result[0][2] = -f.x;
104 Result[1][2] = -f.y;
105 Result[2][2] = -f.z;
106 Result[3][0] = -(s * eye);
107 Result[3][1] = -(u * eye);
108 Result[3][2] = (f * eye);
109 return Result;
110 }
111
121 template<class T>
122 mat4<T> ortho(const float& left, const float& right, const float& bottom, const float& top) {
123 mat4<T> mat(1.0f);
124 mat[0][0] = static_cast<T>(2) / (right - left);
125 mat[1][1] = static_cast<T>(2) / (top - bottom);
126 mat[2][2] = -static_cast<T>(1);
127 mat[3][0] = -(right + left) / (right - left);
128 mat[3][1] = -(top + bottom) / (top - bottom);
129 return mat;
130 }
131
142 template<class T>
143 mat4<T>
144 perspective(const float& FOV, const float& width, const float& height, const float& zNear, const float& zFar) {
145 float halfTanFOV = tanf(FOV / static_cast<T>(2));
146 float aspect = width / height;
147 return mat4<T>(
148 static_cast<T>(1) / (aspect * halfTanFOV),
149 0,
150 0,
151 0,
152 0,
153 static_cast<T>(1) / (halfTanFOV),
154 0,
155 0,
156 0,
157 0,
158 -(zFar + zNear) / (zFar - zNear),
159 -static_cast<T>(1),
160 0,
161 0,
162 -(static_cast<T>(2) * zFar * zNear) / (zFar - zNear),
163 0);
164 }
165
173 template<class T>
174 mat4<T> angleAxis([[maybe_unused]] const float& angle, [[maybe_unused]] const vec3<T>& axis) {
175 // return mat4<T>(axis.x, axis.y, axis.z, angle);
176 return mat4<T>(0);
177 }
178
186 template<class T>
187 mat4<T> scale(const mat4<T>& mat, const float& factor) {
188 return (mat4<T>::Unit() * factor) * mat;
189 }
190 template<class T>
191 mat4<T> scale(const mat4<T>& mat, const vec3<T>& factor) {
192 mat4<T> out;
193 out[0][0] = mat[0][0] * factor[0];
194 out[0][1] = mat[0][1] * factor[0];
195 out[0][2] = mat[0][2] * factor[0];
196 out[0][3] = mat[0][3] * factor[0];
197 out[1][0] = mat[1][0] * factor[1];
198 out[1][1] = mat[1][1] * factor[1];
199 out[1][2] = mat[1][2] * factor[1];
200 out[1][3] = mat[1][3] * factor[1];
201 out[2][0] = mat[2][0] * factor[2];
202 out[2][1] = mat[2][1] * factor[2];
203 out[2][2] = mat[2][2] * factor[2];
204 out[2][3] = mat[2][3] * factor[2];
205 out[3][0] = mat[3][0] * static_cast<T>(1.0);
206 out[3][1] = mat[3][1] * static_cast<T>(1.0);
207 out[3][2] = mat[3][2] * static_cast<T>(1.0);
208 out[3][3] = mat[3][3] * static_cast<T>(1.0);
209 return out;
210 }
211
222 template<typename T, typename U>
223 vec3<T> unProject(vec3<T> const& win, mat4<T> const& model, mat4<T> const& proj, vec4<U> const& viewport) {
224 mat4<T> Inverse = (proj * model).Inverse();
225
226 vec4<T> tmp(win, T(1));
227 tmp.x = (tmp.x - T(viewport[0])) / T(viewport[2]);
228 tmp.y = (tmp.y - T(viewport[1])) / T(viewport[3]);
229 tmp.x = tmp.x * static_cast<T>(2) - static_cast<T>(1);
230 tmp.y = tmp.y * static_cast<T>(2) - static_cast<T>(1);
231
232 vec4<T> obj = Inverse * tmp;
233 obj /= obj.w;
234
235 return vec3<T>(obj);
236 }
237
246 template<class T>
247 mat4<T> rotate(const mat4<T>& m, const float& angle, vec3<T> u) {
248 T const cosAngle = cosf(angle);
249 T const sinAngle = sinf(angle);
250
251 vec3<T> axis = u.normalize();
252 vec3<T> temp((T(1) - cosAngle) * axis);
253
254 mat4<T> rot;
255 rot[0][0] = cosAngle + temp[0] * axis[0];
256 rot[0][1] = temp[0] * axis[1] + sinAngle * axis[2];
257 rot[0][2] = temp[0] * axis[2] - sinAngle * axis[1];
258
259 rot[1][0] = temp[1] * axis[0] - sinAngle * axis[2];
260 rot[1][1] = cosAngle + temp[1] * axis[1];
261 rot[1][2] = temp[1] * axis[2] + sinAngle * axis[0];
262
263 rot[2][0] = temp[2] * axis[0] + sinAngle * axis[1];
264 rot[2][1] = temp[2] * axis[1] - sinAngle * axis[0];
265 rot[2][2] = cosAngle + temp[2] * axis[2];
266
267 const vec4<T> out0 = vec4(m[0]) * rot[0][0] + vec4(m[1]) * rot[0][1] + vec4(m[2]) * rot[0][2];
268 const vec4<T> out1 = vec4(m[0]) * rot[1][0] + vec4(m[1]) * rot[1][1] + vec4(m[2]) * rot[1][2];
269 const vec4<T> out2 = vec4(m[0]) * rot[2][0] + vec4(m[1]) * rot[2][1] + vec4(m[2]) * rot[2][2];
270 const vec4<T> out3 = vec4(m[3]);
271 return mat4<T>(out0, out1, out2, out3);
272 }
273
281 template<class T>
282 vec3<T> max(const vec3<T>& a, const vec3<T>& b) {
283 return a.length() > b.length() ? a : b;
284 }
285
294 template<class T>
295 vec3<T> lerp(const vec3<T>& p1, const vec3<T>& p2, const float& v) {
296 T ax = p1[0];
297 T ay = p1[1];
298 T az = p1[2];
299 return vec3<T>(ax + v * (p2[0] - ax), ay + v * (p2[1] - ay), az + v * (p2[2] - az));
300 }
301
310 template<class T>
311 vec4<T> lerp(const vec4<T>& p1, const vec4<T>& p2, const float& v) {
312 T ax = p1[0];
313 T ay = p1[1];
314 T az = p1[2];
315 T aw = p1[3];
316 return vec4<T>(ax + v * (p2[0] - ax), ay + v * (p2[1] - ay), az + v * (p2[2] - az), aw + v * (p2[3] - aw));
317 }
318} // namespace Math::Utils
319
326template<class T>
327void* value_ptr(vec2<T>& vec) {
328 return &(vec.x);
329}
330
337template<class T>
338void* value_ptr(vec3<T>& vec) {
339 return &(vec.x);
340}
341
348template<class T>
349void* value_ptr(vec4<T>& vec) {
350 return &(vec.x);
351}
358template<class T>
359void* value_ptr(mat2<T>& mat) {
360 return &(mat[0][0]);
361}
368template<class T>
369void* value_ptr(mat3<T>& mat) {
370 return &(mat[0][0]);
371}
372
379template<class T>
380void* value_ptr(mat4<T>& mat) {
381 return &(mat[0][0]);
382}
383
390template<class T>
391const void* value_ptr(vec2<T> const& vec) {
392 return &(vec.x);
393}
394
401template<class T>
402const void* value_ptr(vec3<T> const& vec) {
403 return &(vec.x);
404}
411template<class T>
412const void* value_ptr(vec4<T> const& vec) {
413 return &(vec.x);
414}
421template<class T>
422const void* value_ptr(mat2<T> const& mat) {
423 return &(mat[0][0]);
424}
431template<class T>
432const void* value_ptr(mat3<T> const& mat) {
433 return &(mat[0][0]);
434}
441template<class T>
442const void* value_ptr(mat4<T> const& mat) {
443 return &(mat[0][0]);
444}
445
452template<class T>
453T sign(const T& d) {
454 if(d == 0.f) return 1;
455 return d < 0 ? -1 : 1;
456}
457
466template<class T>
467T abs(const T& d) {
468 if(d < 0.f) return static_cast<T>(-1) * d;
469 return d;
470}
471
472
Definition: utils.h:17
float distance(const V &a, const V &b)
Definition: utils.h:27
vec2< T > normalize(const vec2< T > &a)
Definition: utils.h:38
mat4< T > perspective(const float &FOV, const float &width, const float &height, const float &zNear, const float &zFar)
Definition: utils.h:144
mat4< T > rotate(const mat4< T > &m, const float &angle, vec3< T > u)
Definition: utils.h:247
mat4< T > lookAt(const vec3< T > &eye, const vec3< T > &center, const vec3< T > &up)
Definition: utils.h:91
mat4< T > translate(const mat4< T > &M, const vec3< T > &V)
Definition: utils.h:72
mat4< T > scale(const mat4< T > &mat, const float &factor)
Definition: utils.h:187
vec3< T > unProject(vec3< T > const &win, mat4< T > const &model, mat4< T > const &proj, vec4< U > const &viewport)
Definition: utils.h:223
vec3< T > lerp(const vec3< T > &p1, const vec3< T > &p2, const float &v)
Definition: utils.h:295
vec3< T > max(const vec3< T > &a, const vec3< T > &b)
Definition: utils.h:282
mat4< T > angleAxis(const float &angle, const vec3< T > &axis)
Definition: utils.h:174
mat4< T > ortho(const float &left, const float &right, const float &bottom, const float &top)
Definition: utils.h:122
Matrix< double > eye(size_t rows, size_t columns=0)
Definition: mat2.h:10
Definition: mat3.h:10
Definition: mat4.h:11
Definition: vec2.h:11
vec2< T > normalize() const
Definition: vec2.h:91
T x
Definition: vec2.h:29
Definition: vec3.h:14
T x
Definition: vec3.h:33
T y
Definition: vec3.h:33
T z
Definition: vec3.h:33
vec3< T > normalize() const
Definition: vec3.h:141
float length() const
Definition: vec3.h:136
vec3< T > cross(const vec3< T > &rhs) const
Definition: vec3.h:124
Definition: vec4.h:11
T w
Definition: vec4.h:29
vec4< T > normalize() const
Definition: vec4.h:118
T y
Definition: vec4.h:29
T x
Definition: vec4.h:29
T abs(const T &d)
Definition: utils.h:467
T sign(const T &d)
Definition: utils.h:453
void * value_ptr(vec2< T > &vec)
Definition: utils.h:327