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 
122  inline Matrix& SetProjection() {
123  ::SetMatrixProjection(*this);
124  return *this;
125  }
126 
130  inline Matrix& SetModelview() {
131  ::SetMatrixModelview(*this);
132  return *this;
133  }
134 
138  static Matrix GetModelview() {
139  return ::GetMatrixModelview();
140  }
141 
146  return ::GetMatrixProjection();
147  }
148 
149 #ifndef RAYLIB_CPP_NO_MATH
150 
153  inline float Trace() const {
154  return ::MatrixTrace(*this);
155  }
156 
160  inline Matrix Transpose() const {
161  return ::MatrixTranspose(*this);
162  }
163 
164  inline Matrix Invert() const {
165  return ::MatrixInvert(*this);
166  }
167 
168  inline Matrix Normalize() const {
169  return ::MatrixNormalize(*this);
170  }
171 
172  static Matrix Identity() {
173  return ::MatrixIdentity();
174  }
175 
176  Matrix Add(const ::Matrix& right) {
177  return ::MatrixAdd(*this, right);
178  }
179 
180  Matrix operator+(const ::Matrix& matrix) {
181  return ::MatrixAdd(*this, matrix);
182  }
183 
184  Matrix Subtract(const ::Matrix& right) {
185  return ::MatrixSubtract(*this, right);
186  }
187 
188  Matrix operator-(const ::Matrix& matrix) {
189  return ::MatrixSubtract(*this, matrix);
190  }
191 
192  static Matrix Translate(float x, float y, float z) {
193  return ::MatrixTranslate(x, y, z);
194  }
195 
196  static Matrix Rotate(Vector3 axis, float angle) {
197  return ::MatrixRotate(axis, angle);
198  }
199 
200  static Matrix RotateXYZ(Vector3 angle) {
201  return ::MatrixRotateXYZ(angle);
202  }
203 
204  static Matrix RotateX(float angle) {
205  return ::MatrixRotateX(angle);
206  }
207 
208  static Matrix RotateY(float angle) {
209  return ::MatrixRotateY(angle);
210  }
211 
212  static Matrix RotateZ(float angle) {
213  return ::MatrixRotateZ(angle);
214  }
215 
216  static Matrix Scale(float x, float y, float z) {
217  return ::MatrixScale(x, y, z);
218  }
219 
220  Matrix Multiply(const ::Matrix& right) const {
221  return ::MatrixMultiply(*this, right);
222  }
223 
224  Matrix operator*(const ::Matrix& matrix) {
225  return ::MatrixMultiply(*this, matrix);
226  }
227 
228  static Matrix Frustum(double left, double right, double bottom, double top,
229  double near, double far) {
230  return ::MatrixFrustum(left, right, bottom, top, near, far);
231  }
232 
233  static Matrix Perspective(double fovy, double aspect, double near, double far) {
234  return ::MatrixPerspective(fovy, aspect, near, far);
235  }
236 
237  static Matrix Ortho(double left, double right, double bottom, double top,
238  double near, double far) {
239  return ::MatrixOrtho(left, right, bottom, top, near, far);
240  }
241 
242  static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up) {
243  return ::MatrixLookAt(eye, target, up);
244  }
245 
246  inline float16 ToFloatV() const {
247  return ::MatrixToFloatV(*this);
248  }
249 
250  operator float16() {
251  return ToFloatV();
252  }
253 
257  inline Matrix& SetShaderValue(::Shader shader, int uniformLoc) {
258  ::SetShaderValueMatrix(shader, uniformLoc, *this);
259  return *this;
260  }
261 
262 #endif
263 
264  protected:
265  inline void set(const ::Matrix& mat) {
266  m0 = mat.m0;
267  m1 = mat.m1;
268  m2 = mat.m2;
269  m3 = mat.m3;
270  m4 = mat.m4;
271  m5 = mat.m5;
272  m6 = mat.m6;
273  m7 = mat.m7;
274  m8 = mat.m8;
275  m9 = mat.m9;
276  m10 = mat.m10;
277  m11 = mat.m11;
278  m12 = mat.m12;
279  m13 = mat.m13;
280  m14 = mat.m14;
281  m15 = mat.m15;
282  }
283 };
284 } // namespace raylib
285 
286 
287 #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:160
raylib::Matrix
Definition: Matrix.hpp:46
raylib::Matrix::operator-
Matrix operator-(const ::Matrix &matrix)
Definition: Matrix.hpp:188
raylib::Matrix::Normalize
Matrix Normalize() const
Definition: Matrix.hpp:168
raylib::Matrix::Frustum
static Matrix Frustum(double left, double right, double bottom, double top, double near, double far)
Definition: Matrix.hpp:228
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:257
raylib::Matrix::RotateXYZ
static Matrix RotateXYZ(Vector3 angle)
Definition: Matrix.hpp:200
raylib::Matrix::Scale
static Matrix Scale(float x, float y, float z)
Definition: Matrix.hpp:216
raylib::Matrix::Translate
static Matrix Translate(float x, float y, float z)
Definition: Matrix.hpp:192
raylib::Matrix::Rotate
static Matrix Rotate(Vector3 axis, float angle)
Definition: Matrix.hpp:196
raylib::Matrix::operator+
Matrix operator+(const ::Matrix &matrix)
Definition: Matrix.hpp:180
raylib::Matrix::operator*
Matrix operator*(const ::Matrix &matrix)
Definition: Matrix.hpp:224
raylib::Matrix::RotateZ
static Matrix RotateZ(float angle)
Definition: Matrix.hpp:212
raylib::Vector3
Definition: Vector3.hpp:37
raylib::Matrix::SetProjection
Matrix & SetProjection()
Set a custom projection matrix (replaces internal projection matrix)
Definition: Matrix.hpp:122
raylib::Matrix::SetModelview
Matrix & SetModelview()
Set a custom modelview matrix (replaces internal modelview matrix)
Definition: Matrix.hpp:130
raylib::Matrix::ToFloatV
float16 ToFloatV() const
Definition: Matrix.hpp:246
raylib::Matrix::Identity
static Matrix Identity()
Definition: Matrix.hpp:172
raylib::Matrix::operator=
Matrix & operator=(const Matrix &matrix)
Definition: Matrix.hpp:95
raylib::Matrix::RotateX
static Matrix RotateX(float angle)
Definition: Matrix.hpp:204
raylib::Matrix::Perspective
static Matrix Perspective(double fovy, double aspect, double near, double far)
Definition: Matrix.hpp:233
raylib::Shader
Definition: Shader.hpp:35
raylib::Matrix::set
void set(const ::Matrix &mat)
Definition: Matrix.hpp:265
raylib::Matrix::Add
Matrix Add(const ::Matrix &right)
Definition: Matrix.hpp:176
raylib::Matrix::GetModelview
static Matrix GetModelview()
Get internal modelview matrix.
Definition: Matrix.hpp:138
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:164
raylib::Matrix::LookAt
static Matrix LookAt(Vector3 eye, Vector3 target, Vector3 up)
Definition: Matrix.hpp:242
raylib::Matrix::Subtract
Matrix Subtract(const ::Matrix &right)
Definition: Matrix.hpp:184
raylib::Matrix::Trace
float Trace() const
Returns the trace of the matrix (sum of the values along the diagonal)
Definition: Matrix.hpp:153
raylib::Matrix::RotateY
static Matrix RotateY(float angle)
Definition: Matrix.hpp:208
raylib::Matrix::GetProjection
static Matrix GetProjection()
Get internal projection matrix.
Definition: Matrix.hpp:145
raylib::Matrix::operator==
bool operator==(const ::Matrix &other)
Definition: Matrix.hpp:100
raylib::Matrix::Multiply
Matrix Multiply(const ::Matrix &right) const
Definition: Matrix.hpp:220
raylib::Matrix::Ortho
static Matrix Ortho(double left, double right, double bottom, double top, double near, double far)
Definition: Matrix.hpp:237