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  inline float Trace() const {
170  return ::MatrixTrace(*this);
171  }
172 
173  inline Matrix Transpose() const {
174  return ::MatrixTranspose(*this);
175  }
176 
177  inline Matrix Invert() const {
178  return ::MatrixInvert(*this);
179  }
180 
181  inline Matrix Normalize() const {
182  return ::MatrixNormalize(*this);
183  }
184 
185  static Matrix Identity() {
186  return ::MatrixIdentity();
187  }
188 
189  Matrix Add(const ::Matrix& right) {
190  return ::MatrixAdd(*this, right);
191  }
192 
193  Matrix operator+(const ::Matrix& matrix) {
194  return ::MatrixAdd(*this, matrix);
195  }
196 
197  Matrix Subtract(const ::Matrix& right) {
198  return ::MatrixSubtract(*this, right);
199  }
200 
201  Matrix operator-(const ::Matrix& matrix) {
202  return ::MatrixSubtract(*this, matrix);
203  }
204 
205  static Matrix Translate(float x, float y, float z) {
206  return ::MatrixTranslate(x, y, z);
207  }
208 
209  static Matrix Rotate(Vector3 axis, float angle) {
210  return ::MatrixRotate(axis, angle);
211  }
212 
213  static Matrix RotateXYZ(Vector3 angle) {
214  return ::MatrixRotateXYZ(angle);
215  }
216 
217  static Matrix RotateX(float angle) {
218  return ::MatrixRotateX(angle);
219  }
220 
221  static Matrix RotateY(float angle) {
222  return ::MatrixRotateY(angle);
223  }
224 
225  static Matrix RotateZ(float angle) {
226  return ::MatrixRotateZ(angle);
227  }
228 
229  static Matrix Scale(float x, float y, float z) {
230  return ::MatrixScale(x, y, z);
231  }
232 
233  Matrix Multiply(const ::Matrix& right) const {
234  return ::MatrixMultiply(*this, right);
235  }
236 
237  Matrix operator*(const ::Matrix& matrix) {
238  return ::MatrixMultiply(*this, matrix);
239  }
240 
241  static Matrix Frustum(double left, double right, double bottom, double top,
242  double near, double far) {
243  return ::MatrixFrustum(left, right, bottom, top, near, far);
244  }
245 
246  static Matrix Perspective(double fovy, double aspect, double near, double far) {
247  return ::MatrixPerspective(fovy, aspect, near, far);
248  }
249 
250  static Matrix Ortho(double left, double right, double bottom, double top,
251  double near, double far) {
252  return ::MatrixOrtho(left, right, bottom, top, near, far);
253  }
254 
255  static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up) {
256  return ::MatrixLookAt(eye, target, up);
257  }
258 
259  inline float16 ToFloatV() const {
260  return ::MatrixToFloatV(*this);
261  }
262 
263  operator float16() {
264  return ToFloatV();
265  }
266 
270  inline Matrix& SetShaderValue(::Shader shader, int uniformLoc) {
271  ::SetShaderValueMatrix(shader, uniformLoc, *this);
272  return *this;
273  }
274 
275 #endif
276 
277  protected:
278  inline void set(const ::Matrix& mat) {
279  m0 = mat.m0;
280  m1 = mat.m1;
281  m2 = mat.m2;
282  m3 = mat.m3;
283  m4 = mat.m4;
284  m5 = mat.m5;
285  m6 = mat.m6;
286  m7 = mat.m7;
287  m8 = mat.m8;
288  m9 = mat.m9;
289  m10 = mat.m10;
290  m11 = mat.m11;
291  m12 = mat.m12;
292  m13 = mat.m13;
293  m14 = mat.m14;
294  m15 = mat.m15;
295  }
296 };
297 } // namespace raylib
298 
299 
300 #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
Definition: Matrix.hpp:173
raylib::Matrix
Definition: Matrix.hpp:46
raylib::Matrix::operator-
Matrix operator-(const ::Matrix &matrix)
Definition: Matrix.hpp:201
raylib::Matrix::operator==
bool operator==(::Matrix &other)
Definition: Matrix.hpp:119
raylib::Matrix::Normalize
Matrix Normalize() const
Definition: Matrix.hpp:181
raylib::Matrix::Frustum
static Matrix Frustum(double left, double right, double bottom, double top, double near, double far)
Definition: Matrix.hpp:241
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:270
raylib::Matrix::RotateXYZ
static Matrix RotateXYZ(Vector3 angle)
Definition: Matrix.hpp:213
raylib::Matrix::Scale
static Matrix Scale(float x, float y, float z)
Definition: Matrix.hpp:229
raylib::Matrix::Translate
static Matrix Translate(float x, float y, float z)
Definition: Matrix.hpp:205
raylib::Matrix::Rotate
static Matrix Rotate(Vector3 axis, float angle)
Definition: Matrix.hpp:209
raylib::Matrix::operator+
Matrix operator+(const ::Matrix &matrix)
Definition: Matrix.hpp:193
raylib::Matrix::operator*
Matrix operator*(const ::Matrix &matrix)
Definition: Matrix.hpp:237
raylib::Matrix::RotateZ
static Matrix RotateZ(float angle)
Definition: Matrix.hpp:225
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:259
raylib::Matrix::Identity
static Matrix Identity()
Definition: Matrix.hpp:185
raylib::Matrix::operator=
Matrix & operator=(const Matrix &matrix)
Definition: Matrix.hpp:95
raylib::Matrix::RotateX
static Matrix RotateX(float angle)
Definition: Matrix.hpp:217
raylib::Matrix::Perspective
static Matrix Perspective(double fovy, double aspect, double near, double far)
Definition: Matrix.hpp:246
raylib::Shader
Definition: Shader.hpp:35
raylib::Matrix::set
void set(const ::Matrix &mat)
Definition: Matrix.hpp:278
raylib::Matrix::Add
Matrix Add(const ::Matrix &right)
Definition: Matrix.hpp:189
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:177
raylib::Matrix::LookAt
static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up)
Definition: Matrix.hpp:255
raylib::Matrix::Subtract
Matrix Subtract(const ::Matrix &right)
Definition: Matrix.hpp:197
raylib::Matrix::Trace
float Trace() const
Definition: Matrix.hpp:169
raylib::Matrix::RotateY
static Matrix RotateY(float angle)
Definition: Matrix.hpp:221
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:233
raylib::Matrix::Ortho
static Matrix Ortho(double left, double right, double bottom, double top, double near, double far)
Definition: Matrix.hpp:250