GCP Assignment 1
Mat4.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Vec4.h"
4 #include "Vec3.h"
5 #include "Convert.h"
6 
10 namespace Maths
11 {
15  struct Mat4
16  {
24  float m[16];
25 
29  Mat4() : m{
30  0.0f,0.0f,0.0f,0.0f,
31  0.0f,0.0f,0.0f,0.0f,
32  0.0f,0.0f,0.0f,0.0f,
33  0.0f,0.0f,0.0f,0.0f
34  } {}
35 
43  Mat4(Vec4 v1, Vec4 v2, Vec4 v3, Vec4 v4) :
44  m{
45  v1.x, v1.y, v1.z, v1.w,
46  v2.x, v2.y, v2.z, v2.w,
47  v3.x, v3.y, v3.z, v3.w,
48  v4.x, v4.y, v4.z, v4.w,
49  } {}
50 
57  {
58  m[0] += mIn.m[0]; m[1] += mIn.m[1]; m[2] += mIn.m[2]; m[3] += mIn.m[3];
59  m[4] += mIn.m[4]; m[5] += mIn.m[5]; m[6] += mIn.m[6]; m[7] += mIn.m[7];
60  m[8] += mIn.m[8]; m[9] += mIn.m[9]; m[10] += mIn.m[10]; m[11] += mIn.m[11];
61  m[12] += mIn.m[12]; m[13] += mIn.m[13]; m[14] += mIn.m[14]; m[15] += mIn.m[15];
62  return this;
63  }
64 
71  {
72  m[0] -= mIn.m[0]; m[1] -= mIn.m[1]; m[2] -= mIn.m[2]; m[3] -= mIn.m[3];
73  m[4] -= mIn.m[4]; m[5] -= mIn.m[5]; m[6] -= mIn.m[6]; m[7] -= mIn.m[7];
74  m[8] -= mIn.m[8]; m[9] -= mIn.m[9]; m[10] -= mIn.m[10]; m[11] -= mIn.m[11];
75  m[12] -= mIn.m[12]; m[13] -= mIn.m[13]; m[14] -= mIn.m[14]; m[15] -= mIn.m[15];
76  return this;
77  }
78 
83  {
84  m[0] = 1.0f; m[1] = 0.0f; m[2] = 0.0f; m[3] = 0.0f;
85  m[4] = 0.0f; m[5] = 1.0f; m[6] = 0.0f; m[7] = 0.0f;
86  m[8] = 0.0f; m[9] = 0.0f; m[10] = 1.0f; m[11] = 0.0f;
87  m[12] = 0.0f; m[13] = 0.0f; m[14] = 0.0f; m[15] = 1.0f;
88  }
89 
97  void setAsPerspectiveMatrix(float fovy, float aspect, float n, float f)
98  {
99  float const a = tanf(fovy / 2.0f);
100 
101  m[0] = 1.0f / (aspect * a); m[1] = 0.0f; m[2] = 0.0f; m[3] = 0.0f;
102  m[4] = 0.0f; m[5] = 1.0f / a; m[6] = 0.0f; m[7] = 0.0f;
103  m[8] = 0.0f; m[9] = 0.0f; m[10] = -((f + n) / (f - n)); m[11] = -((2.0f * f * n) / (f - n));
104  m[12] = 0.0f; m[13] = 0.0f; m[14] = -1.0f; m[15] = 0.0f;
105  }
106 
111  void setPos(Vec3 pv) //uses row1w, row2w & row3w
112  {
113  /*x y z*/ m[3] = pv.x;
114  /*x y z*/ m[7] = pv.y;
115  /*x y z*/ m[11] = pv.z;
116  /*x y z w*/
117  }
118  void setPos(Mat4 &matrix, Vec3 pos);
119 
125  void scale(Mat4 &matrix, float sv);
126 
132  void translate(Mat4 &matrix, Vec3 tv);
133 
140  void rotateAlongX(Mat4 &matrix, float angle);
141 
148  void rotateAlongY(Mat4 &matrix, float angle);
149 
156  void rotateAlongZ(Mat4 &matrix, float angle);
157 
158  void rotatePointAroundXAxis(Mat4 &matrix, Maths::Vec3 axisPoint, float angle);
159  void rotatePointAroundYAxis(Mat4 &matrix, Maths::Vec3 axisPoint, float angle);
160  void rotatePointAroundZAxis(Mat4 &matrix, Maths::Vec3 axisPoint, float angle);
161 
166  Vec3 getPos() { return Vec3(m[3], m[7], m[11]); }
167 
172  float* getMatrixArray()
173  {
174  return &m[0];
175  }
176  };
177 
184  inline Mat4 operator + (Mat4 mIn, float s)
185  {
186  Mat4 mOut;
187  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;
188  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;
189  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;
190  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;
191  return mOut;
192  }
193 
200  inline Mat4 operator + (Mat4 mInA, Mat4 mInB)
201  {
202  Mat4 mOut;
203  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];
204  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];
205  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];
206  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];
207  return mOut;
208  }
209 
216  inline Mat4 operator - (Mat4 mInA, float s)
217  {
218  Mat4 mOut;
219  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;
220  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;
221  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;
222  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;
223  return mOut;
224  }
225 
232  inline Mat4 operator - (Mat4 mInA, Mat4 mInB)
233  {
234  Mat4 mOut;
235  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];
236  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];
237  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];
238  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];
239  return mOut;
240  }
241 
248  inline Mat4 operator * (Mat4 mInA, float s)
249  {
250  Mat4 mOut;
251  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;
252  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;
253  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;
254  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;
255  return mOut;
256  }
257 
265  inline Mat4 operator * (Mat4 mInA, Mat4 mInB)
266  {
267  Mat4 mOut;
268 
269  //row 1
270  //(row1x * row1x) + (row1y * row2x) + (row1z * row3x) + (row1w * row4x)
271  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]);
272  //(row1x * row1y) + (row1y * row2y) + (row1z * row3y) + (row1w * row4y)
273  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]);
274  //(row1x * row1z) + (row1y * row2z) + (row1z * row3z) + (row1w * row4z)
275  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]);
276  //(row1x * row1w) + (row1y * row2w) + (row1z * row3w) + (row1w * row4w)
277  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]);
278 
279  //row 2
280  //(row2x * row1x) + (row2y * row2x) + (row2z * row3x) + (row2w * row4x)
281  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]);
282  //(row2x * row1y) + (row2y * row2y) + (row2z * row3y) + (row2w * row4y)
283  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]);
284  //(row2x * row1z) + (row2y * row2z) + (row2z * row3z) +(row2w * row4z)
285  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]);
286  //(row2x * row1w) + (row2y * row2w) + (row2z * row3w) + (row2w * row4w)
287  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]);
288 
289  //row 3
290  //(row3x * row1x) + (row3y * row2x) + (row3z * row3x) + (row3w * row4x)
291  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]);
292  //(row3x * row1y) + (row3y * row2y) + (row3z * row3y) + (row3w * row4y)
293  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]);
294  //(row3x * row1z) + (row3y * row2z) + (row3z * row3z) + (row3w * row4z)
295  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]);
296  //(row3x * row1w) + (row3y * row2w) + (row3z * row3w) + (row3w * row4w)
297  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]);
298 
299  //row 4
300  //(row4x * row1x) + (row4y * row2x) + (row4z * row3x) + (row4w * row4x)
301  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]);
302  //(row4x * row1y) + (row4y * row2y) + (row4z * row3y) + (row4w * row4y)
303  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]);
304  //(row4x * row1z) + (row4y * row2z) + (row4z * row3z) + (row4w * row4z)
305  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]);
306  //(row4x * row1w) + (row4y * row2w) + (row4z * row3w) + (row4w * row4w)
307  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]);
308 
309  return mOut;
310  }
311 
312 }// End of Maths namespace
float z
Definition: Vec4.h:19
void rotateAlongX(Mat4 &matrix, float angle)
Rotate the matrix using the angle of rotation along the x axis.
Definition: Mat4.cpp:46
void rotatePointAroundZAxis(Mat4 &matrix, Maths::Vec3 axisPoint, float angle)
Definition: Mat4.cpp:115
void rotateAlongY(Mat4 &matrix, float angle)
Rotate the matrix using the angle of rotation along the y axis.
Definition: Mat4.cpp:61
Vec3 getPos()
Gets the position of the matrix.
Definition: Mat4.h:166
Contains the Vec3 structure with functions and overloaded operators.
Definition: Vec3.h:16
Mat4()
Constructs the Mat4 setting the values of the matrix to 0.
Definition: Mat4.h:29
Mat4(Vec4 v1, Vec4 v2, Vec4 v3, Vec4 v4)
Constructs the Mat4 setting the values to the input vectors.
Definition: Mat4.h:43
Mat4 operator-(Mat4 mInA, float s)
Overloads the - operator allowing a Mat4 to be added to a scalar.
Definition: Mat4.h:216
void scale(Mat4 &matrix, float sv)
Scales the matrix using the input vector.
Definition: Mat4.cpp:16
float m[16]
Definition: Mat4.h:24
float x
Position variables.
Definition: Vec4.h:19
void rotatePointAroundXAxis(Mat4 &matrix, Maths::Vec3 axisPoint, float angle)
Definition: Mat4.cpp:91
float y
Definition: Vec3.h:19
float w
Definition: Vec4.h:19
Contains the Vec4 structure with functions and overloaded operators.
Definition: Vec4.h:16
Mat4 operator*(Mat4 mInA, float s)
Overloads the * operator allowing a Mat4 to be multiplied to a scalar.
Definition: Mat4.h:248
Contains the Mat4 structure with functions and overloaded operators. This is row major.
Definition: Mat4.h:15
void rotateAlongZ(Mat4 &matrix, float angle)
Rotate the matrix using the angle of rotation along the z axis.
Definition: Mat4.cpp:76
Mat4 * operator-=(Mat4 mIn)
Overloads the -= operator.
Definition: Mat4.h:70
float * getMatrixArray()
A function to get a pointer to the first index of the array.
Definition: Mat4.h:172
void translate(Mat4 &matrix, Vec3 tv)
Translates the matrix using the input vector.
Definition: Mat4.cpp:31
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:97
Mat4 operator+(Mat4 mIn, float s)
Overloads the + operator allowing a Mat4 to be added to a scalar.
Definition: Mat4.h:184
float y
Definition: Vec4.h:19
void setAsIdentityMatrix()
Sets the values of the matrix to that of a identity matrix.
Definition: Mat4.h:82
The namespace for all maths code.
Definition: Convert.cpp:5
void rotatePointAroundYAxis(Mat4 &matrix, Maths::Vec3 axisPoint, float angle)
Definition: Mat4.cpp:103
void setPos(Vec3 pv)
Sets the position of the matrix using the input vector.
Definition: Mat4.h:111
float z
Definition: Vec3.h:19
Mat4 * operator+=(Mat4 mIn)
Overloads the += operator.
Definition: Mat4.h:56
float x
Position variables.
Definition: Vec3.h:19