raylib-cpp
C++ object-oriented wrapper library for raylib.
Matrix.hpp
1 /*
2 * LICENSE: zlib/libpng
3 *
4 * raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified,
5 * BSD-like license that allows static linking with closed source software:
6 *
7 * Copyright (c) 2020 Rob Loach (@RobLoach)
8 *
9 * This software is provided "as-is", without any express or implied warranty. In no event
10 * will the authors be held liable for any damages arising from the use of this software.
11 *
12 * Permission is granted to anyone to use this software for any purpose, including commercial
13 * applications, and to alter it and redistribute it freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not claim that you
16 * wrote the original software. If you use this software in a product, an acknowledgment
17 * in the product documentation would be appreciated but is not required.
18 *
19 * 2. Altered source versions must be plainly marked as such, and must not be misrepresented
20 * as being the original software.
21 *
22 * 3. This notice may not be removed or altered from any source distribution.
23 */
24 
25 #ifndef RAYLIB_CPP_INCLUDE_MATRIX_HPP_
26 #define RAYLIB_CPP_INCLUDE_MATRIX_HPP_
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 #include "raylib.h" // NOLINT
32 #ifndef RAYLIB_CPP_NO_MATH
33 #include "raymath.h" // NOLINT
34 #endif
35 #ifdef __cplusplus
36 }
37 #endif
38 
39 #ifndef RAYLIB_CPP_NO_MATH
40 #include <cmath>
41 #endif
42 
43 #include "./raylib-cpp-utils.hpp"
44 
45 namespace raylib {
46 class Matrix : public ::Matrix {
47  public:
48  Matrix(const ::Matrix& mat) {
49  set(mat);
50  }
51 
52  Matrix(float M0 = 0, float M1 = 0, float M2 = 0, float M3 = 0, float M4 = 0, float M5 = 0,
53  float M6 = 0, float M7 = 0, float M8 = 0, float M9 = 0, float M10 = 0, float M11 = 0,
54  float M12 = 0, float M13 = 0, float M14 = 0, float M15 = 0) {
55  m0 = M0;
56  m1 = M1;
57  m2 = M2;
58  m3 = M3;
59  m4 = M4;
60  m5 = M5;
61  m6 = M6;
62  m7 = M7;
63  m8 = M8;
64  m9 = M9;
65  m10 = M10;
66  m11 = M11;
67  m12 = M12;
68  m13 = M13;
69  m14 = M14;
70  m15 = M15;
71  }
72 
73  GETTERSETTER(float, M0, m0)
74  GETTERSETTER(float, M1, m1)
75  GETTERSETTER(float, M2, m2)
76  GETTERSETTER(float, M3, m3)
77  GETTERSETTER(float, M4, m4)
78  GETTERSETTER(float, M5, m5)
79  GETTERSETTER(float, M6, m6)
80  GETTERSETTER(float, M7, m7)
81  GETTERSETTER(float, M8, m8)
82  GETTERSETTER(float, M9, m9)
83  GETTERSETTER(float, M10, m10)
84  GETTERSETTER(float, M11, m11)
85  GETTERSETTER(float, M12, m12)
86  GETTERSETTER(float, M13, m13)
87  GETTERSETTER(float, M14, m14)
88  GETTERSETTER(float, M15, m15)
89 
90  Matrix& operator=(const ::Matrix& matrix) {
91  set(matrix);
92  return *this;
93  }
94 
95  Matrix& operator=(const Matrix& matrix) {
96  set(matrix);
97  return *this;
98  }
99 
100  bool operator==(const Matrix& other) {
101  return m0 == other.m0
102  && m1 == other.m1
103  && m2 == other.m2
104  && m3 == other.m3
105  && m4 == other.m4
106  && m5 == other.m5
107  && m6 == other.m6
108  && m7 == other.m7
109  && m8 == other.m8
110  && m9 == other.m9
111  && m10 == other.m10
112  && m11 == other.m11
113  && m12 == other.m12
114  && m13 == other.m13
115  && m14 == other.m14
116  && m15 == other.m15;
117  }
118 
119  bool operator==(::Matrix& other) { // NOLINT
120  return m0 == other.m0
121  && m1 == other.m1
122  && m2 == other.m2
123  && m3 == other.m3
124  && m4 == other.m4
125  && m5 == other.m5
126  && m6 == other.m6
127  && m7 == other.m7
128  && m8 == other.m8
129  && m9 == other.m9
130  && m10 == other.m10
131  && m11 == other.m11
132  && m12 == other.m12
133  && m13 == other.m13
134  && m14 == other.m14
135  && m15 == other.m15;
136  }
137 
141  inline Matrix& SetProjection() {
142  ::SetMatrixProjection(*this);
143  return *this;
144  }
145 
149  inline Matrix& SetModelview() {
150  ::SetMatrixModelview(*this);
151  return *this;
152  }
153 
157  static Matrix GetModelview() {
158  return ::GetMatrixModelview();
159  }
160 
165  return ::GetMatrixProjection();
166  }
167 
168 #ifndef RAYLIB_CPP_NO_MATH
169 
172  inline float Trace() const {
173  return ::MatrixTrace(*this);
174  }
175 
179  inline Matrix Transpose() const {
180  return ::MatrixTranspose(*this);
181  }
182 
183  inline Matrix Invert() const {
184  return ::MatrixInvert(*this);
185  }
186 
187  inline Matrix Normalize() const {
188  return ::MatrixNormalize(*this);
189  }
190 
191  static Matrix Identity() {
192  return ::MatrixIdentity();
193  }
194 
195  Matrix Add(const ::Matrix& right) {
196  return ::MatrixAdd(*this, right);
197  }
198 
199  Matrix operator+(const ::Matrix& matrix) {
200  return ::MatrixAdd(*this, matrix);
201  }
202 
203  Matrix Subtract(const ::Matrix& right) {
204  return ::MatrixSubtract(*this, right);
205  }
206 
207  Matrix operator-(const ::Matrix& matrix) {
208  return ::MatrixSubtract(*this, matrix);
209  }
210 
211  static Matrix Translate(float x, float y, float z) {
212  return ::MatrixTranslate(x, y, z);
213  }
214 
215  static Matrix Rotate(Vector3 axis, float angle) {
216  return ::MatrixRotate(axis, angle);
217  }
218 
219  static Matrix RotateXYZ(Vector3 angle) {
220  return ::MatrixRotateXYZ(angle);
221  }
222 
223  static Matrix RotateX(float angle) {
224  return ::MatrixRotateX(angle);
225  }
226 
227  static Matrix RotateY(float angle) {
228  return ::MatrixRotateY(angle);
229  }
230 
231  static Matrix RotateZ(float angle) {
232  return ::MatrixRotateZ(angle);
233  }
234 
235  static Matrix Scale(float x, float y, float z) {
236  return ::MatrixScale(x, y, z);
237  }
238 
239  Matrix Multiply(const ::Matrix& right) const {
240  return ::MatrixMultiply(*this, right);
241  }
242 
243  Matrix operator*(const ::Matrix& matrix) {
244  return ::MatrixMultiply(*this, matrix);
245  }
246 
247  static Matrix Frustum(double left, double right, double bottom, double top,
248  double near, double far) {
249  return ::MatrixFrustum(left, right, bottom, top, near, far);
250  }
251 
252  static Matrix Perspective(double fovy, double aspect, double near, double far) {
253  return ::MatrixPerspective(fovy, aspect, near, far);
254  }
255 
256  static Matrix Ortho(double left, double right, double bottom, double top,
257  double near, double far) {
258  return ::MatrixOrtho(left, right, bottom, top, near, far);
259  }
260 
261  static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up) {
262  return ::MatrixLookAt(eye, target, up);
263  }
264 
265  inline float16 ToFloatV() const {
266  return ::MatrixToFloatV(*this);
267  }
268 
269  operator float16() {
270  return ToFloatV();
271  }
272 
276  inline Matrix& SetShaderValue(::Shader shader, int uniformLoc) {
277  ::SetShaderValueMatrix(shader, uniformLoc, *this);
278  return *this;
279  }
280 
281 #endif
282 
283  protected:
284  inline void set(const ::Matrix& mat) {
285  m0 = mat.m0;
286  m1 = mat.m1;
287  m2 = mat.m2;
288  m3 = mat.m3;
289  m4 = mat.m4;
290  m5 = mat.m5;
291  m6 = mat.m6;
292  m7 = mat.m7;
293  m8 = mat.m8;
294  m9 = mat.m9;
295  m10 = mat.m10;
296  m11 = mat.m11;
297  m12 = mat.m12;
298  m13 = mat.m13;
299  m14 = mat.m14;
300  m15 = mat.m15;
301  }
302 };
303 } // namespace raylib
304 
305 
306 #endif // RAYLIB_CPP_INCLUDE_MATRIX_HPP_
raylib
Provides all the classes associated with raylib-cpp.
Definition: AudioDevice.hpp:31
raylib::Matrix::Transpose
Matrix Transpose() const
Transposes provided matrix.
Definition: Matrix.hpp:179
raylib::Matrix
Definition: Matrix.hpp:46
raylib::Matrix::operator-
Matrix operator-(const ::Matrix &matrix)
Definition: Matrix.hpp:207
raylib::Matrix::operator==
bool operator==(::Matrix &other)
Definition: Matrix.hpp:119
raylib::Matrix::Normalize
Matrix Normalize() const
Definition: Matrix.hpp:187
raylib::Matrix::Frustum
static Matrix Frustum(double left, double right, double bottom, double top, double near, double far)
Definition: Matrix.hpp:247
raylib::Matrix::Matrix
Matrix(const ::Matrix &mat)
Definition: Matrix.hpp:48
raylib::Matrix::SetShaderValue
Matrix & SetShaderValue(::Shader shader, int uniformLoc)
Set shader uniform value (matrix 4x4)
Definition: Matrix.hpp:276
raylib::Matrix::RotateXYZ
static Matrix RotateXYZ(Vector3 angle)
Definition: Matrix.hpp:219
raylib::Matrix::Scale
static Matrix Scale(float x, float y, float z)
Definition: Matrix.hpp:235
raylib::Matrix::Translate
static Matrix Translate(float x, float y, float z)
Definition: Matrix.hpp:211
raylib::Matrix::Rotate
static Matrix Rotate(Vector3 axis, float angle)
Definition: Matrix.hpp:215
raylib::Matrix::operator+
Matrix operator+(const ::Matrix &matrix)
Definition: Matrix.hpp:199
raylib::Matrix::operator*
Matrix operator*(const ::Matrix &matrix)
Definition: Matrix.hpp:243
raylib::Matrix::RotateZ
static Matrix RotateZ(float angle)
Definition: Matrix.hpp:231
raylib::Vector3
Definition: Vector3.hpp:37
raylib::Matrix::SetProjection
Matrix & SetProjection()
Set a custom projection matrix (replaces internal projection matrix)
Definition: Matrix.hpp:141
raylib::Matrix::SetModelview
Matrix & SetModelview()
Set a custom modelview matrix (replaces internal modelview matrix)
Definition: Matrix.hpp:149
raylib::Matrix::ToFloatV
float16 ToFloatV() const
Definition: Matrix.hpp:265
raylib::Matrix::Identity
static Matrix Identity()
Definition: Matrix.hpp:191
raylib::Matrix::operator=
Matrix & operator=(const Matrix &matrix)
Definition: Matrix.hpp:95
raylib::Matrix::RotateX
static Matrix RotateX(float angle)
Definition: Matrix.hpp:223
raylib::Matrix::Perspective
static Matrix Perspective(double fovy, double aspect, double near, double far)
Definition: Matrix.hpp:252
raylib::Shader
Definition: Shader.hpp:35
raylib::Matrix::set
void set(const ::Matrix &mat)
Definition: Matrix.hpp:284
raylib::Matrix::Add
Matrix Add(const ::Matrix &right)
Definition: Matrix.hpp:195
raylib::Matrix::GetModelview
static Matrix GetModelview()
Get internal modelview matrix.
Definition: Matrix.hpp:157
raylib::Matrix::Matrix
Matrix(float M0=0, float M1=0, float M2=0, float M3=0, float M4=0, float M5=0, float M6=0, float M7=0, float M8=0, float M9=0, float M10=0, float M11=0, float M12=0, float M13=0, float M14=0, float M15=0)
Definition: Matrix.hpp:52
raylib::Matrix::Invert
Matrix Invert() const
Definition: Matrix.hpp:183
raylib::Matrix::LookAt
static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up)
Definition: Matrix.hpp:261
raylib::Matrix::Subtract
Matrix Subtract(const ::Matrix &right)
Definition: Matrix.hpp:203
raylib::Matrix::Trace
float Trace() const
Returns the trace of the matrix (sum of the values along the diagonal)
Definition: Matrix.hpp:172
raylib::Matrix::RotateY
static Matrix RotateY(float angle)
Definition: Matrix.hpp:227
raylib::Matrix::operator==
bool operator==(const Matrix &other)
Definition: Matrix.hpp:100
raylib::Matrix::GetProjection
static Matrix GetProjection()
Get internal projection matrix.
Definition: Matrix.hpp:164
raylib::Matrix::Multiply
Matrix Multiply(const ::Matrix &right) const
Definition: Matrix.hpp:239
raylib::Matrix::Ortho
static Matrix Ortho(double left, double right, double bottom, double top, double near, double far)
Definition: Matrix.hpp:256