raylib-cpp
C++ object-oriented wrapper library for raylib.
Loading...
Searching...
No Matches
Matrix.hpp
1#ifndef RAYLIB_CPP_INCLUDE_MATRIX_HPP_
2#define RAYLIB_CPP_INCLUDE_MATRIX_HPP_
3
4#include "./raylib.hpp"
5#include "./raylib-cpp-utils.hpp"
6#include "./raymath.hpp"
7#include "./RadiansDegrees.hpp"
8#include "./Vector4.hpp"
9
10#ifndef RAYLIB_CPP_NO_MATH
11#include <cmath>
12#endif
13
14namespace raylib {
18class Matrix : public ::Matrix {
19 public:
20 struct LocalSpace_t {};
21 constexpr static LocalSpace_t LocalSpace{};
22 struct GlobalSpace_t {};
23 constexpr static GlobalSpace_t GlobalSpace{};
24
25 Matrix(const ::Matrix& mat) : ::Matrix{
26 mat.m0, mat.m4, mat.m8, mat.m12,
27 mat.m1, mat.m5, mat.m9, mat.m13,
28 mat.m2, mat.m6, mat.m10, mat.m14,
29 mat.m3, mat.m7, mat.m11, mat.m15} {
30 // Nothing.
31 }
32
33 Matrix(
34 float m0 = 0, float m4 = 0, float m8 = 0, float m12 = 0,
35 float m1 = 0, float m5 = 0, float m9 = 0, float m13 = 0,
36 float m2 = 0, float m6 = 0, float m10 = 0, float m14 = 0,
37 float m3 = 0, float m7 = 0, float m11 = 0, float m15 = 0) :
38 ::Matrix{
39 m0, m4, m8, m12,
40 m1, m5, m9, m13,
41 m2, m6, m10, m14,
42 m3, m7, m11, m15} {
43 // Nothing.
44 }
45
46 GETTERSETTER(float, M0, m0)
47 GETTERSETTER(float, M1, m1)
48 GETTERSETTER(float, M2, m2)
49 GETTERSETTER(float, M3, m3)
50 GETTERSETTER(float, M4, m4)
51 GETTERSETTER(float, M5, m5)
52 GETTERSETTER(float, M6, m6)
53 GETTERSETTER(float, M7, m7)
54 GETTERSETTER(float, M8, m8)
55 GETTERSETTER(float, M9, m9)
56 GETTERSETTER(float, M10, m10)
57 GETTERSETTER(float, M11, m11)
58 GETTERSETTER(float, M12, m12)
59 GETTERSETTER(float, M13, m13)
60 GETTERSETTER(float, M14, m14)
61 GETTERSETTER(float, M15, m15)
62
63 Matrix& operator=(const ::Matrix& matrix) {
64 set(matrix);
65 return *this;
66 }
67
68 Matrix& operator=(const Matrix& matrix) {
69 set(matrix);
70 return *this;
71 }
72
73 bool operator==(const ::Matrix& other) {
74 return m0 == other.m0
75 && m1 == other.m1
76 && m2 == other.m2
77 && m3 == other.m3
78 && m4 == other.m4
79 && m5 == other.m5
80 && m6 == other.m6
81 && m7 == other.m7
82 && m8 == other.m8
83 && m9 == other.m9
84 && m10 == other.m10
85 && m11 == other.m11
86 && m12 == other.m12
87 && m13 == other.m13
88 && m14 == other.m14
89 && m15 == other.m15;
90 }
91
92 bool operator!=(const ::Matrix& other) {
93 return !(*this == other);
94 }
95
96#ifndef RAYLIB_CPP_NO_MATH
100 inline float Trace() const {
101 return ::MatrixTrace(*this);
102 }
103
107 inline Matrix Transpose() const {
108 return ::MatrixTranspose(*this);
109 }
110
114 inline Matrix Invert() const {
115 return ::MatrixInvert(*this);
116 }
117
121 static Matrix Identity() {
122 return ::MatrixIdentity();
123 }
124
129 inline Matrix TranslateToOrigin() const {
130 return CreateTranslate(-m12, -m13, -m14);
131 }
132
136 inline Matrix Add(const ::Matrix& right) const {
137 return ::MatrixAdd(*this, right);
138 }
139
143 inline Matrix operator+(const ::Matrix& matrix) const {
144 return ::MatrixAdd(*this, matrix);
145 }
146
150 inline Matrix Subtract(const ::Matrix& right) const {
151 return ::MatrixSubtract(*this, right);
152 }
153
157 inline Matrix operator-(const ::Matrix& matrix) const {
158 return ::MatrixSubtract(*this, matrix);
159 }
160
164 inline Matrix Multiply(const ::Matrix& right) const {
165 return ::MatrixMultiply(*this, right);
166 }
167
171 inline Matrix operator*(const ::Matrix& matrix) const {
172 return ::MatrixMultiply(*this, matrix);
173 }
174
178 static Matrix CreateTranslate(float x, float y, float z) {
179 return ::MatrixTranslate(x, y, z);
180 }
181
185 inline Matrix Translate(GlobalSpace_t, float x, float y, float z) const {
186 return (*this) * CreateTranslate(x, y, z);
187 }
188
192 inline Matrix Translate(LocalSpace_t, float x, float y, float z) const {
193 return CreateTranslate(x, y, z) * (*this);
194 }
195
199 inline Matrix Translate(float x, float y, float z) const {
200 return Translate(GlobalSpace, x, y, z);
201 }
202
206 static Matrix CreateTranslate(Vector3 translation) {
207 return ::MatrixTranslate(translation.x, translation.y, translation.z);
208 }
209
213 Matrix Translate(GlobalSpace_t, Vector3 translation) const {
214 return (*this) * CreateTranslate(translation.x, translation.y, translation.z);
215 }
216
220 Matrix Translate(LocalSpace_t, Vector3 translation) const {
221 return CreateTranslate(translation.x, translation.y, translation.z) * (*this);
222 }
223
227 inline Matrix Translate(Vector3 translation) const {
228 return Translate(GlobalSpace, translation.x, translation.y, translation.z);
229 }
230
234 static Matrix CreateRotate(Vector3 axis, Radian angle) {
235 return ::MatrixRotate(axis, angle);
236 }
237
241 Matrix Rotate(GlobalSpace_t, Vector3 axis, Radian angle) const {
242 return (*this) * CreateRotate(axis, angle);
243 }
244
248 Matrix Rotate(LocalSpace_t, Vector3 axis, Radian angle) const {
249 auto toOrigin = TranslateToOrigin();
250 return (*this) * toOrigin * CreateRotate(axis, angle) * toOrigin.Invert();
251 }
252
256 inline Matrix Rotate(Vector3 axis, Radian angle) const {
257 return Rotate(LocalSpace, axis, angle);
258 }
259
264 auto [axis, angle] = quat.ToAxisAngle();
265 return CreateRotate(axis, angle);
266 }
267
271 Matrix Rotate(GlobalSpace_t, Quaternion quat) const {
272 return (*this) * CreateRotate(quat);
273 }
274
278 Matrix Rotate(LocalSpace_t, Quaternion quat) const {
279 auto toOrigin = TranslateToOrigin();
280 return (*this) * toOrigin * CreateRotate(quat) * toOrigin.Invert();
281 }
282
286 inline Matrix Rotate(Quaternion quat) const {
287 return Rotate(LocalSpace, quat);
288 }
289
294 return ::MatrixRotateXYZ(angle);
295 }
296
300 Matrix RotateXYZ(GlobalSpace_t, Vector3 angle) const {
301 return (*this) * CreateRotateXYZ(angle);
302 }
303
307 Matrix RotateXYZ(LocalSpace_t, Vector3 angle) const {
308 auto toOrigin = TranslateToOrigin();
309 return (*this) * toOrigin * CreateRotateXYZ(angle) * toOrigin.Invert();
310 }
311
315 inline Matrix RotateXYZ(Vector3 angle) const {
316 return RotateXYZ(LocalSpace, angle);
317 }
318
323 return CreateRotateXYZ(Vector3{x, y, z});
324 }
325
329 Matrix RotateXYZ(GlobalSpace_t, Radian x, Radian y, Radian z) const {
330 return (*this) * CreateRotateXYZ(x, y, z);
331 }
332
336 Matrix RotateXYZ(LocalSpace_t, Radian x, Radian y, Radian z) const {
337 auto toOrigin = TranslateToOrigin();
338 return (*this) * toOrigin * CreateRotateXYZ(x, y, z) * toOrigin.Invert();
339 }
340
344 inline Matrix RotateXYZ(Radian x, Radian y, Radian z) const {
345 return RotateXYZ(LocalSpace, x, y, z);
346 }
347
352 return ::MatrixRotateX(angle);
353 }
354
358 Matrix RotateX(GlobalSpace_t, Radian angle) const {
359 return (*this) * CreateRotateX(angle);
360 }
361
365 Matrix RotateX(LocalSpace_t, Radian angle) const {
366 auto toOrigin = TranslateToOrigin();
367 return (*this) * toOrigin * CreateRotateX(angle) * toOrigin.Invert();
368 }
369
373 inline Matrix RotateX(Radian angle) const {
374 return RotateX(LocalSpace, angle);
375 }
376
381 return ::MatrixRotateY(angle);
382 }
383
387 Matrix RotateY(GlobalSpace_t, Radian angle) const {
388 return (*this) * CreateRotateY(angle);
389 }
390
394 Matrix RotateY(LocalSpace_t, Radian angle) const {
395 auto toOrigin = TranslateToOrigin();
396 return (*this) * toOrigin * CreateRotateY(angle) * toOrigin.Invert();
397 }
398
402 inline Matrix RotateY(Radian angle) const {
403 return RotateY(LocalSpace, angle);
404 }
405
410 return ::MatrixRotateZ(angle);
411 }
412
416 Matrix RotateZ(GlobalSpace_t, Radian angle) const {
417 return (*this) * CreateRotateZ(angle);
418 }
419
423 Matrix RotateZ(LocalSpace_t, Radian angle) const {
424 auto toOrigin = TranslateToOrigin();
425 return (*this) * toOrigin * CreateRotateZ(angle) * toOrigin.Invert();
426 }
427
431 inline Matrix RotateZ(Radian angle) const {
432 return RotateZ(LocalSpace, angle);
433 }
434
438 static Matrix CreateScale(float x, float y, float z) {
439 return ::MatrixScale(x, y, z);
440 }
441
445 Matrix Scale(GlobalSpace_t, float x, float y, float z) const {
446 return (*this) * CreateScale(x, y, z);
447 }
448 inline Matrix Scale(GlobalSpace_t, float all) const {
449 return Scale(GlobalSpace, all, all, all);
450 }
451
455 Matrix Scale(LocalSpace_t, float x, float y, float z) const {
456 auto toOrigin = TranslateToOrigin();
457 return (*this) * toOrigin * CreateScale(x, y, z) * toOrigin.Invert();
458 }
459 inline Matrix Scale(LocalSpace_t, float all) const {
460 return Scale(LocalSpace, all, all, all);
461 }
462
466 inline Matrix Scale(float x, float y, float z) const {
467 return Scale(LocalSpace, x, y, z);
468 }
469 inline Matrix Scale(float all) const {
470 return Scale(LocalSpace, all);
471 }
472
473 static Matrix Frustum(double left, double right, double bottom, double top,
474 double near, double far) {
475 return ::MatrixFrustum(left, right, bottom, top, near, far);
476 }
477
478 static Matrix Perspective(double fovy, double aspect, double near, double far) {
479 return ::MatrixPerspective(fovy, aspect, near, far);
480 }
481
482 static Matrix Ortho(double left, double right, double bottom, double top,
483 double near, double far) {
484 return ::MatrixOrtho(left, right, bottom, top, near, far);
485 }
486
487 static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up) {
488 return ::MatrixLookAt(eye, target, up);
489 }
490
491 float16 ToFloatV() const {
492 return ::MatrixToFloatV(*this);
493 }
494
495 operator float16() {
496 return ToFloatV();
497 }
498
502 Matrix& SetShaderValue(const ::Shader& shader, int uniformLoc) {
503 ::SetShaderValueMatrix(shader, uniformLoc, *this);
504 return *this;
505 }
506
507 static Matrix GetCamera(const ::Camera& camera) {
508 return ::GetCameraMatrix(camera);
509 }
510
511 static Matrix GetCamera(const ::Camera2D& camera) {
512 return ::GetCameraMatrix2D(camera);
513 }
514
515#endif
516
517 protected:
518 void set(const ::Matrix& mat) {
519 m0 = mat.m0;
520 m1 = mat.m1;
521 m2 = mat.m2;
522 m3 = mat.m3;
523 m4 = mat.m4;
524 m5 = mat.m5;
525 m6 = mat.m6;
526 m7 = mat.m7;
527 m8 = mat.m8;
528 m9 = mat.m9;
529 m10 = mat.m10;
530 m11 = mat.m11;
531 m12 = mat.m12;
532 m13 = mat.m13;
533 m14 = mat.m14;
534 m15 = mat.m15;
535 }
536};
537
538using Transform = Matrix; // raylib::Transform is also a matrix
539} // namespace raylib
540
541using RMatrix = raylib::Matrix;
542
543#endif // RAYLIB_CPP_INCLUDE_MATRIX_HPP_
Matrix type (OpenGL style 4x4 - right handed, column major)
Definition: Matrix.hpp:18
Matrix RotateZ(Radian angle) const
Rotates the current matrix in local space around the Z axis.
Definition: Matrix.hpp:431
Matrix operator+(const ::Matrix &matrix) const
Elementwise matrix addition.
Definition: Matrix.hpp:143
static Matrix CreateRotate(Quaternion quat)
Creates a rotation matrix around the given quaternion.
Definition: Matrix.hpp:263
Matrix Rotate(GlobalSpace_t, Quaternion quat) const
Rotates the current matrix in global space around the given quaternion.
Definition: Matrix.hpp:271
Matrix Translate(float x, float y, float z) const
Translates the current matrix in global space.
Definition: Matrix.hpp:199
static Matrix CreateRotate(Vector3 axis, Radian angle)
Creates a rotation matrix around the given axis.
Definition: Matrix.hpp:234
Matrix Translate(LocalSpace_t, Vector3 translation) const
Translates the current matrix in local space.
Definition: Matrix.hpp:220
Matrix RotateZ(LocalSpace_t, Radian angle) const
Rotates the current matrix in local space around the Z axis.
Definition: Matrix.hpp:423
static Matrix CreateTranslate(float x, float y, float z)
Creates a translation matrix.
Definition: Matrix.hpp:178
Matrix RotateZ(GlobalSpace_t, Radian angle) const
Rotates the current matrix in global space around the Z axis.
Definition: Matrix.hpp:416
Matrix Rotate(GlobalSpace_t, Vector3 axis, Radian angle) const
Rotates the current matrix in global space around the given axis.
Definition: Matrix.hpp:241
static Matrix CreateTranslate(Vector3 translation)
Creates a translation matrix.
Definition: Matrix.hpp:206
static Matrix CreateRotateY(Radian angle)
Creates a rotation matrix around the Y axis.
Definition: Matrix.hpp:380
Matrix Translate(GlobalSpace_t, float x, float y, float z) const
Translates the current matrix in global space.
Definition: Matrix.hpp:185
static Matrix CreateScale(float x, float y, float z)
Creates a scale matrix.
Definition: Matrix.hpp:438
Matrix TranslateToOrigin() const
Creates a matrix that when multiplied by the current matrix will translate back to the origin (New fu...
Definition: Matrix.hpp:129
Matrix RotateY(GlobalSpace_t, Radian angle) const
Rotates the current matrix in global space around the Y axis.
Definition: Matrix.hpp:387
Matrix Rotate(LocalSpace_t, Quaternion quat) const
Rotates the current matrix in local space around the given quaternion.
Definition: Matrix.hpp:278
Matrix Rotate(Vector3 axis, Radian angle) const
Rotates the current matrix in local space around the given axis.
Definition: Matrix.hpp:256
static Matrix CreateRotateXYZ(Radian x, Radian y, Radian z)
Creates a rotation matrix using the provided euler angles.
Definition: Matrix.hpp:322
Matrix Translate(GlobalSpace_t, Vector3 translation) const
Translates the current matrix in global space.
Definition: Matrix.hpp:213
Matrix RotateY(LocalSpace_t, Radian angle) const
Rotates the current matrix in local space around the Y axis.
Definition: Matrix.hpp:394
Matrix Rotate(Quaternion quat) const
Rotates the current matrix in local space around the given quaternion.
Definition: Matrix.hpp:286
Matrix RotateXYZ(Radian x, Radian y, Radian z) const
Rotates the current matrix using the provided euler angles in local space.
Definition: Matrix.hpp:344
Matrix RotateXYZ(GlobalSpace_t, Radian x, Radian y, Radian z) const
Rotates the current matrix using the provided euler angles in global space.
Definition: Matrix.hpp:329
Matrix RotateY(Radian angle) const
Rotates the current matrix in local space around the Y axis.
Definition: Matrix.hpp:402
Matrix RotateXYZ(Vector3 angle) const
Rotates the current matrix using the provided euler angles in local space.
Definition: Matrix.hpp:315
Matrix RotateXYZ(LocalSpace_t, Radian x, Radian y, Radian z) const
Rotates the current matrix using the provided euler angles in local space.
Definition: Matrix.hpp:336
Matrix & SetShaderValue(const ::Shader &shader, int uniformLoc)
Set shader uniform value (matrix 4x4)
Definition: Matrix.hpp:502
Matrix Scale(LocalSpace_t, float x, float y, float z) const
Scales the current matrix in local space.
Definition: Matrix.hpp:455
float Trace() const
Returns the trace of the matrix (sum of the values along the diagonal)
Definition: Matrix.hpp:100
Matrix Transpose() const
Transposes provided matrix.
Definition: Matrix.hpp:107
static Matrix CreateRotateX(Radian angle)
Creates a rotation matrix around the X axis.
Definition: Matrix.hpp:351
Matrix Scale(GlobalSpace_t, float x, float y, float z) const
Scales the current matrix in global space.
Definition: Matrix.hpp:445
Matrix operator-(const ::Matrix &matrix) const
Elementwise matrix subtraction.
Definition: Matrix.hpp:157
Matrix Add(const ::Matrix &right) const
Elementwise matrix addition.
Definition: Matrix.hpp:136
Matrix Translate(Vector3 translation) const
Translates the current matrix in global space.
Definition: Matrix.hpp:227
Matrix Invert() const
Inverts provided matrix.
Definition: Matrix.hpp:114
static Matrix Identity()
Creates an identity matrix.
Definition: Matrix.hpp:121
Matrix Translate(LocalSpace_t, float x, float y, float z) const
Translates the current matrix in local space.
Definition: Matrix.hpp:192
Matrix operator*(const ::Matrix &matrix) const
Matrix multiplication.
Definition: Matrix.hpp:171
static Matrix CreateRotateXYZ(Vector3 angle)
Creates a rotation matrix using the provided euler angles.
Definition: Matrix.hpp:293
Matrix RotateX(LocalSpace_t, Radian angle) const
Rotates the current matrix in local space around the X axis.
Definition: Matrix.hpp:365
Matrix RotateX(GlobalSpace_t, Radian angle) const
Rotates the current matrix in global space around the X axis.
Definition: Matrix.hpp:358
Matrix RotateXYZ(LocalSpace_t, Vector3 angle) const
Rotates the current matrix using the provided euler angles in local space.
Definition: Matrix.hpp:307
Matrix Scale(float x, float y, float z) const
Scales the current matrix in local space.
Definition: Matrix.hpp:466
Matrix RotateXYZ(GlobalSpace_t, Vector3 angle) const
Rotates the current matrix using the provided euler angles in global space.
Definition: Matrix.hpp:300
Matrix Rotate(LocalSpace_t, Vector3 axis, Radian angle) const
Rotates the current matrix in local space around the given axis.
Definition: Matrix.hpp:248
Matrix Subtract(const ::Matrix &right) const
Elementwise matrix subtraction.
Definition: Matrix.hpp:150
static Matrix CreateRotateZ(Radian angle)
Creates a rotation matrix around the given axis.
Definition: Matrix.hpp:409
Matrix Multiply(const ::Matrix &right) const
Matrix multiplication.
Definition: Matrix.hpp:164
Matrix RotateX(Radian angle) const
Rotates the current matrix in local space around the X axis.
Definition: Matrix.hpp:373
Radian type (allows automatic worry free conversion between radians and degrees)
Vector3 type.
Definition: Vector3.hpp:19
Vector4 type.
Definition: Vector4.hpp:20