Level H Engine
Mat4.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Vec4.h"
4 #include "Vec3.h"
5 
9 struct Mat4
10 {
18  float m[16];
19 
23  Mat4() : m{
24  0.0f,0.0f,0.0f,0.0f,
25  0.0f,0.0f,0.0f,0.0f,
26  0.0f,0.0f,0.0f,0.0f,
27  0.0f,0.0f,0.0f,0.0f
28  } {}
29 
37  Mat4(Vec4 v1, Vec4 v2, Vec4 v3, Vec4 v4) :
38  m{
39  v1.x, v1.y, v1.z, v1.w,
40  v2.x, v2.y, v2.z, v2.w,
41  v3.x, v3.y, v3.z, v3.w,
42  v4.x, v4.y, v4.z, v4.w,
43  } {}
44 
51  {
52  m[0] += mIn.m[0]; m[1] += mIn.m[1]; m[2] += mIn.m[2]; m[3] += mIn.m[3];
53  m[4] += mIn.m[4]; m[5] += mIn.m[5]; m[6] += mIn.m[6]; m[7] += mIn.m[7];
54  m[8] += mIn.m[8]; m[9] += mIn.m[9]; m[10] += mIn.m[10]; m[11] += mIn.m[11];
55  m[12] += mIn.m[12]; m[13] += mIn.m[13]; m[14] += mIn.m[14]; m[15] += mIn.m[15];
56  return this;
57  }
58 
65  {
66  m[0] -= mIn.m[0]; m[1] -= mIn.m[1]; m[2] -= mIn.m[2]; m[3] -= mIn.m[3];
67  m[4] -= mIn.m[4]; m[5] -= mIn.m[5]; m[6] -= mIn.m[6]; m[7] -= mIn.m[7];
68  m[8] -= mIn.m[8]; m[9] -= mIn.m[9]; m[10] -= mIn.m[10]; m[11] -= mIn.m[11];
69  m[12] -= mIn.m[12]; m[13] -= mIn.m[13]; m[14] -= mIn.m[14]; m[15] -= mIn.m[15];
70  return this;
71  }
72 
77  {
78  m[0] = 1.0f; m[1] = 0.0f; m[2] = 0.0f; m[3] = 0.0f;
79  m[4] = 0.0f; m[5] = 1.0f; m[6] = 0.0f; m[7] = 0.0f;
80  m[8] = 0.0f; m[9] = 0.0f; m[10] = 1.0f; m[11] = 0.0f;
81  m[12] = 0.0f; m[13] = 0.0f; m[14] = 0.0f; m[15] = 1.0f;
82  }
83 
91  void setAsPerspectiveMatrix(float fovy, float aspect, float n, float f)
92  {
93  float const a = tanf(fovy / 2.0f);
94 
95  m[0] = 1.0f / (aspect * a); m[1] = 0.0f; m[2] = 0.0f; m[3] = 0.0f;
96  m[4] = 0.0f; m[5] = 1.0f / a; m[6] = 0.0f; m[7] = 0.0f;
97  m[8] = 0.0f; m[9] = 0.0f; m[10] = -((f + n) / (f - n)); m[11] = -((2.0f * f * n) / (f - n));
98  m[12] = 0.0f; m[13] = 0.0f; m[14] = -1.0f; m[15] = 0.0f;
99  }
100 
108  void setAsOrthogonalProjectionMatrix(float w, float h, float n, float f)
109  {
110  m[0] = w * 0.5f; m[1] = 0.0f; m[2] = 0.0f; m[3] = 0.0f;
111  m[4] = 0.0f; m[5] = h * 0.5f; m[6] = 0.0f; m[7] = 0.0f;
112  m[8] = 0.0f; m[9] = 0.0f; m[10] = 1 / (f - n); m[11] = -n / (f - n);
113  m[12] = 0.0f; m[13] = 0.0f; m[14] = 0.0f; m[15] = 1.0f;
114  }
115 
120  void setPos(Vec3 pv) //uses row1w, row2w & row3w
121  {
122  /*x y z*/ m[3] = pv.x;
123  /*x y z*/ m[7] = pv.y;
124  /*x y z*/ m[11] = pv.z;
125  /*x y z w*/
126  }
127  void setPos(Mat4 &matrix, Vec3 pos);
128 
134  void scale(Mat4 &matrix, float sv);
135 
141  void scale(Mat4 &matrix, Vec3 &sv);
142 
148  void translate(Mat4 &matrix, Vec3 tv);
149 
155  void Mat4::rotate(Mat4 &matrix, Vec3 angle);
156 
162  void rotateAlongX(Mat4 &matrix, float angle);
163 
169  void rotateAlongY(Mat4 &matrix, float angle);
170 
176  void rotateAlongZ(Mat4 &matrix, float angle);
177 
184  void rotatePointAroundXAxis(Mat4 &matrix, Vec3 axisPoint, float angle);
185 
192  void rotatePointAroundYAxis(Mat4 &matrix, Vec3 axisPoint, float angle);
193 
200  void rotatePointAroundZAxis(Mat4 &matrix, Vec3 axisPoint, float angle);
201 
206  void getInverse(Mat4 &matrix);
207 
212  Vec3 getPos() { return Vec3(m[3], m[7], m[11]); }
213 
218  float* getMatrixArray()
219  {
220  return &m[0];
221  }
222 };
223 
230 inline Mat4 operator + (Mat4 mIn, float s)
231 {
232  Mat4 mOut;
233  mOut.m[0] = mIn.m[0] + s; mOut.m[1] = mIn.m[1] + s; mOut.m[2] = mIn.m[2] + s; mOut.m[3] = mIn.m[3] + s;
234  mOut.m[4] = mIn.m[4] + s; mOut.m[5] = mIn.m[5] + s; mOut.m[6] = mIn.m[6] + s; mOut.m[7] = mIn.m[7] + s;
235  mOut.m[8] = mIn.m[8] + s; mOut.m[9] = mIn.m[9] + s; mOut.m[10] = mIn.m[10] + s; mOut.m[11] = mIn.m[11] + s;
236  mOut.m[12] = mIn.m[12] + s; mOut.m[13] = mIn.m[13] + s; mOut.m[14] = mIn.m[14] + s; mOut.m[15] = mIn.m[15] + s;
237  return mOut;
238 }
239 
246 inline Mat4 operator + (Mat4 mInA, Mat4 mInB)
247 {
248  Mat4 mOut;
249  mOut.m[0] = mInA.m[0] + mInB.m[0]; mOut.m[1] = mInA.m[1] + mInB.m[1]; mOut.m[2] = mInA.m[2] + mInB.m[2]; mOut.m[3] = mInA.m[3] + mInB.m[3];
250  mOut.m[4] = mInA.m[4] + mInB.m[4]; mOut.m[5] = mInA.m[5] + mInB.m[5]; mOut.m[6] = mInA.m[6] + mInB.m[6]; mOut.m[7] = mInA.m[7] + mInB.m[7];
251  mOut.m[8] = mInA.m[8] + mInB.m[8]; mOut.m[9] = mInA.m[9] + mInB.m[9]; mOut.m[10] = mInA.m[10] + mInB.m[10]; mOut.m[11] = mInA.m[11] + mInB.m[11];
252  mOut.m[12] = mInA.m[12] + mInB.m[12]; mOut.m[13] = mInA.m[13] + mInB.m[13]; mOut.m[14] = mInA.m[14] + mInB.m[14]; mOut.m[15] = mInA.m[15] + mInB.m[15];
253  return mOut;
254 }
255 
262 inline Mat4 operator - (Mat4 mInA, float s)
263 {
264  Mat4 mOut;
265  mOut.m[0] = mInA.m[0] - s; mOut.m[1] = mInA.m[1] - s; mOut.m[2] = mInA.m[2] - s; mOut.m[3] = mInA.m[3] - s;
266  mOut.m[4] = mInA.m[4] - s; mOut.m[5] = mInA.m[5] - s; mOut.m[6] = mInA.m[6] - s; mOut.m[7] = mInA.m[7] - s;
267  mOut.m[8] = mInA.m[8] - s; mOut.m[9] = mInA.m[9] - s; mOut.m[10] = mInA.m[10] - s; mOut.m[11] = mInA.m[11] - s;
268  mOut.m[12] = mInA.m[12] - s; mOut.m[13] = mInA.m[13] - s; mOut.m[14] = mInA.m[14] - s; mOut.m[15] = mInA.m[15] - s;
269  return mOut;
270 }
271 
278 inline Mat4 operator - (Mat4 mInA, Mat4 mInB)
279 {
280  Mat4 mOut;
281  mOut.m[0] = mInA.m[0] - mInB.m[0]; mOut.m[1] = mInA.m[1] - mInB.m[1]; mOut.m[2] = mInA.m[2] - mInB.m[2]; mOut.m[3] = mInA.m[3] - mInB.m[3];
282  mOut.m[4] = mInA.m[4] - mInB.m[4]; mOut.m[5] = mInA.m[5] - mInB.m[5]; mOut.m[6] = mInA.m[6] - mInB.m[6]; mOut.m[7] = mInA.m[7] - mInB.m[7];
283  mOut.m[8] = mInA.m[8] - mInB.m[8]; mOut.m[9] = mInA.m[9] - mInB.m[9]; mOut.m[10] = mInA.m[10] - mInB.m[10]; mOut.m[11] = mInA.m[11] - mInB.m[11];
284  mOut.m[12] = mInA.m[12] - mInB.m[12]; mOut.m[13] = mInA.m[13] - mInB.m[13]; mOut.m[14] = mInA.m[14] - mInB.m[14]; mOut.m[15] = mInA.m[15] - mInB.m[15];
285  return mOut;
286 }
287 
294 inline Mat4 operator * (Mat4 mInA, float s)
295 {
296  Mat4 mOut;
297  mOut.m[0] = mInA.m[0] * s; mOut.m[1] = mInA.m[1] * s; mOut.m[2] = mInA.m[2] * s; mOut.m[3] = mInA.m[3] * s;
298  mOut.m[4] = mInA.m[4] * s; mOut.m[5] = mInA.m[5] * s; mOut.m[6] = mInA.m[6] * s; mOut.m[7] = mInA.m[7] * s;
299  mOut.m[8] = mInA.m[8] * s; mOut.m[9] = mInA.m[9] * s; mOut.m[10] = mInA.m[10] * s; mOut.m[11] = mInA.m[11] * s;
300  mOut.m[12] = mInA.m[12] * s; mOut.m[13] = mInA.m[13] * s; mOut.m[14] = mInA.m[14] * s; mOut.m[15] = mInA.m[15] * s;
301  return mOut;
302 }
303 
311 inline Mat4 operator * (Mat4 mInA, Mat4 mInB)
312 {
313  Mat4 mOut;
314 
315  //row 1
316  //(row1x * row1x) + (row1y * row2x) + (row1z * row3x) + (row1w * row4x)
317  mOut.m[0] = (mInA.m[0] * mInB.m[0]) + (mInA.m[1] * mInB.m[4]) + (mInA.m[2] * mInB.m[8]) + (mInA.m[3] * mInB.m[12]);
318  //(row1x * row1y) + (row1y * row2y) + (row1z * row3y) + (row1w * row4y)
319  mOut.m[1] = (mInA.m[0] * mInB.m[1]) + (mInA.m[1] * mInB.m[5]) + (mInA.m[2] * mInB.m[9]) + (mInA.m[3] * mInB.m[13]);
320  //(row1x * row1z) + (row1y * row2z) + (row1z * row3z) + (row1w * row4z)
321  mOut.m[2] = (mInA.m[0] * mInB.m[2]) + (mInA.m[1] * mInB.m[6]) + (mInA.m[2] * mInB.m[10]) + (mInA.m[3] * mInB.m[14]);
322  //(row1x * row1w) + (row1y * row2w) + (row1z * row3w) + (row1w * row4w)
323  mOut.m[3] = (mInA.m[0] * mInB.m[3]) + (mInA.m[1] * mInB.m[7]) + (mInA.m[2] * mInB.m[11]) + (mInA.m[3] * mInB.m[15]);
324 
325  //row 2
326  //(row2x * row1x) + (row2y * row2x) + (row2z * row3x) + (row2w * row4x)
327  mOut.m[4] = (mInA.m[4] * mInB.m[0]) + (mInA.m[5] * mInB.m[4]) + (mInA.m[6] * mInB.m[8]) + (mInA.m[7] * mInB.m[12]);
328  //(row2x * row1y) + (row2y * row2y) + (row2z * row3y) + (row2w * row4y)
329  mOut.m[5] = (mInA.m[4] * mInB.m[1]) + (mInA.m[5] * mInB.m[5]) + (mInA.m[6] * mInB.m[9]) + (mInA.m[7] * mInB.m[13]);
330  //(row2x * row1z) + (row2y * row2z) + (row2z * row3z) +(row2w * row4z)
331  mOut.m[6] = (mInA.m[4] * mInB.m[2]) + (mInA.m[5] * mInB.m[6]) + (mInA.m[6] * mInB.m[10]) + (mInA.m[7] * mInB.m[14]);
332  //(row2x * row1w) + (row2y * row2w) + (row2z * row3w) + (row2w * row4w)
333  mOut.m[7] = (mInA.m[4] * mInB.m[3]) + (mInA.m[5] * mInB.m[7]) + (mInA.m[6] * mInB.m[11]) + (mInA.m[7] * mInB.m[15]);
334 
335  //row 3
336  //(row3x * row1x) + (row3y * row2x) + (row3z * row3x) + (row3w * row4x)
337  mOut.m[8] = (mInA.m[8] * mInB.m[0]) + (mInA.m[9] * mInB.m[4]) + (mInA.m[10] * mInB.m[8]) + (mInA.m[11] * mInB.m[12]);
338  //(row3x * row1y) + (row3y * row2y) + (row3z * row3y) + (row3w * row4y)
339  mOut.m[9] = (mInA.m[8] * mInB.m[1]) + (mInA.m[9] * mInB.m[5]) + (mInA.m[10] * mInB.m[9]) + (mInA.m[11] * mInB.m[13]);
340  //(row3x * row1z) + (row3y * row2z) + (row3z * row3z) + (row3w * row4z)
341  mOut.m[10] = (mInA.m[8] * mInB.m[2]) + (mInA.m[9] * mInB.m[6]) + (mInA.m[10] * mInB.m[10]) + (mInA.m[11] * mInB.m[14]);
342  //(row3x * row1w) + (row3y * row2w) + (row3z * row3w) + (row3w * row4w)
343  mOut.m[11] = (mInA.m[8] * mInB.m[3]) + (mInA.m[9] * mInB.m[7]) + (mInA.m[10] * mInB.m[11]) + (mInA.m[11] * mInB.m[15]);
344 
345  //row 4
346  //(row4x * row1x) + (row4y * row2x) + (row4z * row3x) + (row4w * row4x)
347  mOut.m[12] = (mInA.m[12] * mInB.m[0]) + (mInA.m[13] * mInB.m[4]) + (mInA.m[14] * mInB.m[8]) + (mInA.m[15] * mInB.m[12]);
348  //(row4x * row1y) + (row4y * row2y) + (row4z * row3y) + (row4w * row4y)
349  mOut.m[13] = (mInA.m[12] * mInB.m[1]) + (mInA.m[13] * mInB.m[5]) + (mInA.m[14] * mInB.m[9]) + (mInA.m[15] * mInB.m[13]);
350  //(row4x * row1z) + (row4y * row2z) + (row4z * row3z) + (row4w * row4z)
351  mOut.m[14] = (mInA.m[12] * mInB.m[2]) + (mInA.m[13] * mInB.m[6]) + (mInA.m[14] * mInB.m[10]) + (mInA.m[15] * mInB.m[14]);
352  //(row4x * row1w) + (row4y * row2w) + (row4z * row3w) + (row4w * row4w)
353  mOut.m[15] = (mInA.m[12] * mInB.m[3]) + (mInA.m[13] * mInB.m[7]) + (mInA.m[14] * mInB.m[11]) + (mInA.m[15] * mInB.m[15]);
354 
355  return mOut;
356 }
void setAsOrthogonalProjectionMatrix(float w, float h, float n, float f)
Sets the values of the matrix to that of a Orthogonal Projection matrix.
Definition: Mat4.h:108
Mat4()
Constructs the Mat4 setting the values of the matrix to 0.
Definition: Mat4.h:23
float y
Definition: Vec4.h:11
float y
Definition: Vec3.h:11
Mat4 * operator-=(Mat4 mIn)
Overloads the -= operator.
Definition: Mat4.h:64
Mat4 operator+(Mat4 mIn, float s)
Overloads the + operator allowing a Mat4 to be added to a scalar.
Definition: Mat4.h:230
Mat4 operator*(Mat4 mInA, float s)
Overloads the * operator allowing a Mat4 to be multiplied to a scalar.
Definition: Mat4.h:294
void rotateAlongY(Mat4 &matrix, float angle)
Rotate the matrix using the angle of rotation along the y axis.
Definition: Mat4.cpp:83
void getInverse(Mat4 &matrix)
A function to get the inverse of a matrix.
Definition: Mat4.cpp:156
float x
Position variables.
Definition: Vec3.h:11
Contains the Vec4 structure with functions and overloaded operators.
Definition: Vec4.h:8
void scale(Mat4 &matrix, float sv)
Scales the matrix using the input vector.
Definition: Mat4.cpp:16
void translate(Mat4 &matrix, Vec3 tv)
Translates the matrix using the input vector.
Definition: Mat4.cpp:46
void rotatePointAroundYAxis(Mat4 &matrix, Vec3 axisPoint, float angle)
Rotate the matrix around a point using the angle of rotation along the y axis.
Definition: Mat4.cpp:125
void rotateAlongZ(Mat4 &matrix, float angle)
Rotate the matrix using the angle of rotation along the z axis.
Definition: Mat4.cpp:98
void rotatePointAroundZAxis(Mat4 &matrix, Vec3 axisPoint, float angle)
Rotate the matrix around a point using the angle of rotation along the z axis.
Definition: Mat4.cpp:137
Mat4 operator-(Mat4 mInA, float s)
Overloads the - operator allowing a Mat4 to be added to a scalar.
Definition: Mat4.h:262
float * getMatrixArray()
A function to get a pointer to the first index of the array.
Definition: Mat4.h:218
Vec3 getPos()
Gets the position of the matrix.
Definition: Mat4.h:212
float m[16]
Definition: Mat4.h:18
void setAsPerspectiveMatrix(float fovy, float aspect, float n, float f)
Sets the values of the matrix to that of a Perspective matrix.
Definition: Mat4.h:91
Mat4(Vec4 v1, Vec4 v2, Vec4 v3, Vec4 v4)
Constructs the Mat4 setting the values to the input vectors.
Definition: Mat4.h:37
float x
Position variables.
Definition: Vec4.h:11
Contains the Vec3 structure with functions and overloaded operators.
Definition: Vec3.h:8
void setAsIdentityMatrix()
Sets the values of the matrix to that of a identity matrix.
Definition: Mat4.h:76
float z
Definition: Vec3.h:11
Mat4 * operator+=(Mat4 mIn)
Overloads the += operator.
Definition: Mat4.h:50
float w
Definition: Vec4.h:11
float z
Definition: Vec4.h:11
Contains the Mat4 structure with functions and overloaded operators. This is row major.
Definition: Mat4.h:9
void setPos(Vec3 pv)
Sets the position of the matrix using the input vector.
Definition: Mat4.h:120
void rotateAlongX(Mat4 &matrix, float angle)
Rotate the matrix using the angle of rotation along the x axis.
Definition: Mat4.cpp:68
void rotatePointAroundXAxis(Mat4 &matrix, Vec3 axisPoint, float angle)
Rotate the matrix around a point using the angle of rotation along the x axis.
Definition: Mat4.cpp:113
void rotate(Mat4 &matrix, Vec3 angle)
Rotate the matrix using the angle of rotation along the axis.
Definition: Mat4.cpp:61