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-cpp-utils.hpp"
5#include "./raylib.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,
35 float m4 = 0,
36 float m8 = 0,
37 float m12 = 0,
38 float m1 = 0,
39 float m5 = 0,
40 float m9 = 0,
41 float m13 = 0,
42 float m2 = 0,
43 float m6 = 0,
44 float m10 = 0,
45 float m14 = 0,
46 float m3 = 0,
47 float m7 = 0,
48 float m11 = 0,
49 float m15 = 0)
50 : ::Matrix{m0, m4, m8, m12, m1, m5, m9, m13, m2, m6, m10, m14, m3, m7, m11, m15} {
51 // Nothing.
52 }
53
54 GETTERSETTER(float, M0, m0)
55 GETTERSETTER(float, M1, m1)
56 GETTERSETTER(float, M2, m2)
57 GETTERSETTER(float, M3, m3)
58 GETTERSETTER(float, M4, m4)
59 GETTERSETTER(float, M5, m5)
60 GETTERSETTER(float, M6, m6)
61 GETTERSETTER(float, M7, m7)
62 GETTERSETTER(float, M8, m8)
63 GETTERSETTER(float, M9, m9)
64 GETTERSETTER(float, M10, m10)
65 GETTERSETTER(float, M11, m11)
66 GETTERSETTER(float, M12, m12)
67 GETTERSETTER(float, M13, m13)
68 GETTERSETTER(float, M14, m14)
69 GETTERSETTER(float, M15, m15)
70
71 Matrix& operator=(const ::Matrix& matrix) {
72 if (this != &matrix) {
73 set(matrix);
74 }
75 return *this;
76 }
77
78 Matrix& operator=(const Matrix& matrix) {
79 if (this != &matrix) {
80 set(matrix);
81 }
82 return *this;
83 }
84
85 bool operator==(const ::Matrix& other) {
86 return m0 == other.m0 && m1 == other.m1 && m2 == other.m2 && m3 == other.m3 && m4 == other.m4 &&
87 m5 == other.m5 && m6 == other.m6 && m7 == other.m7 && m8 == other.m8 && m9 == other.m9 &&
88 m10 == other.m10 && m11 == other.m11 && m12 == other.m12 && m13 == other.m13 && m14 == other.m14 &&
89 m15 == other.m15;
90 }
91
92 bool operator!=(const ::Matrix& other) { return !(*this == other); }
93
94#ifndef RAYLIB_CPP_NO_MATH
98 inline float Trace() const {
99 return ::MatrixTrace(*this);
100 }
101
105 inline Matrix Transpose() const {
106 return ::MatrixTranspose(*this);
107 }
108
112 inline Matrix Invert() const {
113 return ::MatrixInvert(*this);
114 }
115
119 static Matrix Identity() {
120 return ::MatrixIdentity();
121 }
122
127 inline Matrix TranslateToOrigin() const {
128 return CreateTranslate(-m12, -m13, -m14);
129 }
130
134 inline Matrix Add(const ::Matrix& right) const {
135 return ::MatrixAdd(*this, right);
136 }
137
141 inline Matrix operator+(const ::Matrix& matrix) const {
142 return ::MatrixAdd(*this, matrix);
143 }
144
148 inline Matrix Subtract(const ::Matrix& right) const {
149 return ::MatrixSubtract(*this, right);
150 }
151
155 inline Matrix operator-(const ::Matrix& matrix) const {
156 return ::MatrixSubtract(*this, matrix);
157 }
158
162 inline Matrix Multiply(const ::Matrix& right) const {
163 return ::MatrixMultiply(*this, right);
164 }
165
169 inline Matrix operator*(const ::Matrix& matrix) const {
170 return ::MatrixMultiply(*this, matrix);
171 }
172
176 static Matrix CreateTranslate(float x, float y, float z) {
177 return ::MatrixTranslate(x, y, z);
178 }
179
183 inline Matrix Translate(GlobalSpace_t, float x, float y, float z) const {
184 return (*this) * CreateTranslate(x, y, z);
185 }
186
190 inline Matrix Translate(LocalSpace_t, float x, float y, float z) const {
191 return CreateTranslate(x, y, z) * (*this);
192 }
193
197 inline Matrix Translate(float x, float y, float z) const {
198 return Translate(GlobalSpace, x, y, z);
199 }
200
204 static Matrix CreateTranslate(Vector3 translation) {
205 return ::MatrixTranslate(translation.x, translation.y, translation.z);
206 }
207
211 Matrix Translate(GlobalSpace_t, Vector3 translation) const {
212 return (*this) * CreateTranslate(translation.x, translation.y, translation.z);
213 }
214
218 Matrix Translate(LocalSpace_t, Vector3 translation) const {
219 return CreateTranslate(translation.x, translation.y, translation.z) * (*this);
220 }
221
225 inline Matrix Translate(Vector3 translation) const {
226 return Translate(GlobalSpace, translation.x, translation.y, translation.z);
227 }
228
232 static Matrix CreateRotate(Vector3 axis, Radian angle) {
233 return ::MatrixRotate(axis, angle);
234 }
235
239 Matrix Rotate(GlobalSpace_t, Vector3 axis, Radian angle) const {
240 return (*this) * CreateRotate(axis, angle);
241 }
242
246 Matrix Rotate(LocalSpace_t, Vector3 axis, Radian angle) const {
247 auto toOrigin = TranslateToOrigin();
248 return (*this) * toOrigin * CreateRotate(axis, angle) * toOrigin.Invert();
249 }
250
254 inline Matrix Rotate(Vector3 axis, Radian angle) const {
255 return Rotate(LocalSpace, axis, angle);
256 }
257
262 auto [axis, angle] = quat.ToAxisAngle();
263 return CreateRotate(axis, angle);
264 }
265
269 Matrix Rotate(GlobalSpace_t, Quaternion quat) const {
270 return (*this) * CreateRotate(quat);
271 }
272
276 Matrix Rotate(LocalSpace_t, Quaternion quat) const {
277 auto toOrigin = TranslateToOrigin();
278 return (*this) * toOrigin * CreateRotate(quat) * toOrigin.Invert();
279 }
280
284 inline Matrix Rotate(Quaternion quat) const {
285 return Rotate(LocalSpace, quat);
286 }
287
292 return ::MatrixRotateXYZ(angle);
293 }
294
298 Matrix RotateXYZ(GlobalSpace_t, Vector3 angle) const {
299 return (*this) * CreateRotateXYZ(angle);
300 }
301
305 Matrix RotateXYZ(LocalSpace_t, Vector3 angle) const {
306 auto toOrigin = TranslateToOrigin();
307 return (*this) * toOrigin * CreateRotateXYZ(angle) * toOrigin.Invert();
308 }
309
313 inline Matrix RotateXYZ(Vector3 angle) const {
314 return RotateXYZ(LocalSpace, angle);
315 }
316
321 return CreateRotateXYZ(Vector3{x, y, z});
322 }
323
327 Matrix RotateXYZ(GlobalSpace_t, Radian x, Radian y, Radian z) const {
328 return (*this) * CreateRotateXYZ(x, y, z);
329 }
330
334 Matrix RotateXYZ(LocalSpace_t, Radian x, Radian y, Radian z) const {
335 auto toOrigin = TranslateToOrigin();
336 return (*this) * toOrigin * CreateRotateXYZ(x, y, z) * toOrigin.Invert();
337 }
338
342 inline Matrix RotateXYZ(Radian x, Radian y, Radian z) const {
343 return RotateXYZ(LocalSpace, x, y, z);
344 }
345
350 return ::MatrixRotateX(angle);
351 }
352
356 Matrix RotateX(GlobalSpace_t, Radian angle) const {
357 return (*this) * CreateRotateX(angle);
358 }
359
363 Matrix RotateX(LocalSpace_t, Radian angle) const {
364 auto toOrigin = TranslateToOrigin();
365 return (*this) * toOrigin * CreateRotateX(angle) * toOrigin.Invert();
366 }
367
371 inline Matrix RotateX(Radian angle) const {
372 return RotateX(LocalSpace, angle);
373 }
374
379 return ::MatrixRotateY(angle);
380 }
381
385 Matrix RotateY(GlobalSpace_t, Radian angle) const {
386 return (*this) * CreateRotateY(angle);
387 }
388
392 Matrix RotateY(LocalSpace_t, Radian angle) const {
393 auto toOrigin = TranslateToOrigin();
394 return (*this) * toOrigin * CreateRotateY(angle) * toOrigin.Invert();
395 }
396
400 inline Matrix RotateY(Radian angle) const {
401 return RotateY(LocalSpace, angle);
402 }
403
408 return ::MatrixRotateZ(angle);
409 }
410
414 Matrix RotateZ(GlobalSpace_t, Radian angle) const {
415 return (*this) * CreateRotateZ(angle);
416 }
417
421 Matrix RotateZ(LocalSpace_t, Radian angle) const {
422 auto toOrigin = TranslateToOrigin();
423 return (*this) * toOrigin * CreateRotateZ(angle) * toOrigin.Invert();
424 }
425
429 inline Matrix RotateZ(Radian angle) const {
430 return RotateZ(LocalSpace, angle);
431 }
432
436 static Matrix CreateScale(float x, float y, float z) {
437 return ::MatrixScale(x, y, z);
438 }
439
443 Matrix Scale(GlobalSpace_t, float x, float y, float z) const {
444 return (*this) * CreateScale(x, y, z);
445 }
446
450 Matrix Scale(LocalSpace_t, float x, float y, float z) const {
451 auto toOrigin = TranslateToOrigin();
452 return (*this) * toOrigin * CreateScale(x, y, z) * toOrigin.Invert();
453 }
454
458 inline Matrix Scale(float x, float y, float z) const {
459 return Scale(LocalSpace, x, y, z);
460 }
461
462 inline Matrix Scale(float all) const {
463 return Scale(LocalSpace, all, all, all);
464 }
465
466 static Matrix Frustum(double left, double right, double bottom, double top,
467 double near, double far) {
468 return ::MatrixFrustum(left, right, bottom, top, near, far);
469 }
470
471 static Matrix Perspective(double fovy, double aspect, double near, double far) {
472 return ::MatrixPerspective(fovy, aspect, near, far);
473 }
474
475 static Matrix Ortho(double left, double right, double bottom, double top, double near, double far) {
476 return ::MatrixOrtho(left, right, bottom, top, near, far);
477 }
478
479 static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up) { return ::MatrixLookAt(eye, target, up); }
480
481 [[nodiscard]] float16 ToFloatV() const { return ::MatrixToFloatV(*this); }
482
483 operator float16() const { return ToFloatV(); }
484
488 Matrix& SetShaderValue(const ::Shader& shader, int uniformLoc) {
489 ::SetShaderValueMatrix(shader, uniformLoc, *this);
490 return *this;
491 }
492
493 static Matrix GetCamera(const ::Camera& camera) { return ::GetCameraMatrix(camera); }
494
495 static Matrix GetCamera(const ::Camera2D& camera) { return ::GetCameraMatrix2D(camera); }
496
497#endif
498protected:
499 void set(const ::Matrix& mat) {
500 m0 = mat.m0;
501 m1 = mat.m1;
502 m2 = mat.m2;
503 m3 = mat.m3;
504 m4 = mat.m4;
505 m5 = mat.m5;
506 m6 = mat.m6;
507 m7 = mat.m7;
508 m8 = mat.m8;
509 m9 = mat.m9;
510 m10 = mat.m10;
511 m11 = mat.m11;
512 m12 = mat.m12;
513 m13 = mat.m13;
514 m14 = mat.m14;
515 m15 = mat.m15;
516 }
517};
518
519using Transform = Matrix; // raylib::Transform is also a matrix
520} // namespace raylib
521
522using RMatrix = raylib::Matrix;
523
524#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:429
Matrix operator+(const ::Matrix &matrix) const
Elementwise matrix addition.
Definition: Matrix.hpp:141
static Matrix CreateRotate(Quaternion quat)
Creates a rotation matrix around the given quaternion.
Definition: Matrix.hpp:261
Matrix Rotate(GlobalSpace_t, Quaternion quat) const
Rotates the current matrix in global space around the given quaternion.
Definition: Matrix.hpp:269
Matrix Translate(float x, float y, float z) const
Translates the current matrix in global space.
Definition: Matrix.hpp:197
static Matrix CreateRotate(Vector3 axis, Radian angle)
Creates a rotation matrix around the given axis.
Definition: Matrix.hpp:232
Matrix Translate(LocalSpace_t, Vector3 translation) const
Translates the current matrix in local space.
Definition: Matrix.hpp:218
Matrix RotateZ(LocalSpace_t, Radian angle) const
Rotates the current matrix in local space around the Z axis.
Definition: Matrix.hpp:421
static Matrix CreateTranslate(float x, float y, float z)
Creates a translation matrix.
Definition: Matrix.hpp:176
Matrix RotateZ(GlobalSpace_t, Radian angle) const
Rotates the current matrix in global space around the Z axis.
Definition: Matrix.hpp:414
Matrix Rotate(GlobalSpace_t, Vector3 axis, Radian angle) const
Rotates the current matrix in global space around the given axis.
Definition: Matrix.hpp:239
static Matrix CreateTranslate(Vector3 translation)
Creates a translation matrix.
Definition: Matrix.hpp:204
static Matrix CreateRotateY(Radian angle)
Creates a rotation matrix around the Y axis.
Definition: Matrix.hpp:378
Matrix Translate(GlobalSpace_t, float x, float y, float z) const
Translates the current matrix in global space.
Definition: Matrix.hpp:183
static Matrix CreateScale(float x, float y, float z)
Creates a scale matrix.
Definition: Matrix.hpp:436
Matrix TranslateToOrigin() const
Creates a matrix that when multiplied by the current matrix will translate back to the origin (New fu...
Definition: Matrix.hpp:127
Matrix RotateY(GlobalSpace_t, Radian angle) const
Rotates the current matrix in global space around the Y axis.
Definition: Matrix.hpp:385
Matrix Rotate(LocalSpace_t, Quaternion quat) const
Rotates the current matrix in local space around the given quaternion.
Definition: Matrix.hpp:276
Matrix Rotate(Vector3 axis, Radian angle) const
Rotates the current matrix in local space around the given axis.
Definition: Matrix.hpp:254
static Matrix CreateRotateXYZ(Radian x, Radian y, Radian z)
Creates a rotation matrix using the provided euler angles.
Definition: Matrix.hpp:320
Matrix Translate(GlobalSpace_t, Vector3 translation) const
Translates the current matrix in global space.
Definition: Matrix.hpp:211
Matrix RotateY(LocalSpace_t, Radian angle) const
Rotates the current matrix in local space around the Y axis.
Definition: Matrix.hpp:392
Matrix Rotate(Quaternion quat) const
Rotates the current matrix in local space around the given quaternion.
Definition: Matrix.hpp:284
Matrix RotateXYZ(Radian x, Radian y, Radian z) const
Rotates the current matrix using the provided euler angles in local space.
Definition: Matrix.hpp:342
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:327
Matrix RotateY(Radian angle) const
Rotates the current matrix in local space around the Y axis.
Definition: Matrix.hpp:400
Matrix RotateXYZ(Vector3 angle) const
Rotates the current matrix using the provided euler angles in local space.
Definition: Matrix.hpp:313
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:334
Matrix & SetShaderValue(const ::Shader &shader, int uniformLoc)
Set shader uniform value (matrix 4x4)
Definition: Matrix.hpp:488
Matrix Scale(LocalSpace_t, float x, float y, float z) const
Scales the current matrix in local space.
Definition: Matrix.hpp:450
float Trace() const
Returns the trace of the matrix (sum of the values along the diagonal)
Definition: Matrix.hpp:98
Matrix Transpose() const
Transposes provided matrix.
Definition: Matrix.hpp:105
static Matrix CreateRotateX(Radian angle)
Creates a rotation matrix around the X axis.
Definition: Matrix.hpp:349
Matrix Scale(GlobalSpace_t, float x, float y, float z) const
Scales the current matrix in global space.
Definition: Matrix.hpp:443
Matrix operator-(const ::Matrix &matrix) const
Elementwise matrix subtraction.
Definition: Matrix.hpp:155
Matrix Add(const ::Matrix &right) const
Elementwise matrix addition.
Definition: Matrix.hpp:134
Matrix Translate(Vector3 translation) const
Translates the current matrix in global space.
Definition: Matrix.hpp:225
Matrix Invert() const
Inverts provided matrix.
Definition: Matrix.hpp:112
static Matrix Identity()
Creates an identity matrix.
Definition: Matrix.hpp:119
Matrix Translate(LocalSpace_t, float x, float y, float z) const
Translates the current matrix in local space.
Definition: Matrix.hpp:190
Matrix operator*(const ::Matrix &matrix) const
Matrix multiplication.
Definition: Matrix.hpp:169
static Matrix CreateRotateXYZ(Vector3 angle)
Creates a rotation matrix using the provided euler angles.
Definition: Matrix.hpp:291
Matrix RotateX(LocalSpace_t, Radian angle) const
Rotates the current matrix in local space around the X axis.
Definition: Matrix.hpp:363
Matrix RotateX(GlobalSpace_t, Radian angle) const
Rotates the current matrix in global space around the X axis.
Definition: Matrix.hpp:356
Matrix RotateXYZ(LocalSpace_t, Vector3 angle) const
Rotates the current matrix using the provided euler angles in local space.
Definition: Matrix.hpp:305
Matrix Scale(float x, float y, float z) const
Scales the current matrix in local space.
Definition: Matrix.hpp:458
Matrix RotateXYZ(GlobalSpace_t, Vector3 angle) const
Rotates the current matrix using the provided euler angles in global space.
Definition: Matrix.hpp:298
Matrix Rotate(LocalSpace_t, Vector3 axis, Radian angle) const
Rotates the current matrix in local space around the given axis.
Definition: Matrix.hpp:246
Matrix Subtract(const ::Matrix &right) const
Elementwise matrix subtraction.
Definition: Matrix.hpp:148
static Matrix CreateRotateZ(Radian angle)
Creates a rotation matrix around the given axis.
Definition: Matrix.hpp:407
Matrix Multiply(const ::Matrix &right) const
Matrix multiplication.
Definition: Matrix.hpp:162
Matrix RotateX(Radian angle) const
Rotates the current matrix in local space around the X axis.
Definition: Matrix.hpp:371
Radian type (allows automatic worry free conversion between radians and degrees)
Vector3 type.
Definition: Vector3.hpp:20
Vector4 type.
Definition: Vector4.hpp:21
All raylib-cpp classes and functions appear in the raylib namespace.
Definition: AudioDevice.hpp:8