23 #include "Mathematics.h"
36 1.0f, 0.0f, 0.0f, 0.0f,
37 0.0f, 1.0f, 0.0f, 0.0f,
38 0.0f, 0.0f, 1.0f, 0.0f,
39 0.0f, 0.0f, 0.0f, 1.0f,
46 memcpy(
elements, array, 16 *
sizeof(
float));
57 LOGE(
"Matrix only has 16 elements, tried to access element %d", element);
87 result = matrix[0] * (matrix[4] * matrix[8] - matrix[7] * matrix[5]);
88 result -= matrix[3] * (matrix[1] * matrix[8] - matrix[7] * matrix[2]);
89 result += matrix[6] * (matrix[1] * matrix[5] - matrix[4] * matrix[2]);
97 float determinant3x3 = 0.0f;
101 matrix3x3[0] = matrix->
elements[ 5];
102 matrix3x3[1] = matrix->
elements[ 6];
103 matrix3x3[2] = matrix->
elements[ 7];
104 matrix3x3[3] = matrix->
elements[ 9];
105 matrix3x3[4] = matrix->
elements[10];
106 matrix3x3[5] = matrix->
elements[11];
107 matrix3x3[6] = matrix->
elements[13];
108 matrix3x3[7] = matrix->
elements[14];
109 matrix3x3[8] = matrix->
elements[15];
111 result += matrix->
elements[0] * determinant3x3;
114 matrix3x3[0] = matrix->
elements[ 1];
115 matrix3x3[1] = matrix->
elements[ 2];
116 matrix3x3[2] = matrix->
elements[ 3];
117 matrix3x3[3] = matrix->
elements[ 9];
118 matrix3x3[4] = matrix->
elements[10];
119 matrix3x3[5] = matrix->
elements[11];
120 matrix3x3[6] = matrix->
elements[13];
121 matrix3x3[7] = matrix->
elements[14];
122 matrix3x3[8] = matrix->
elements[15];
124 result -= matrix->
elements[4] * determinant3x3;
127 matrix3x3[0] = matrix->
elements[ 1];
128 matrix3x3[1] = matrix->
elements[ 2];
129 matrix3x3[2] = matrix->
elements[ 3];
130 matrix3x3[3] = matrix->
elements[ 5];
131 matrix3x3[4] = matrix->
elements[ 6];
132 matrix3x3[5] = matrix->
elements[ 7];
133 matrix3x3[6] = matrix->
elements[13];
134 matrix3x3[7] = matrix->
elements[14];
135 matrix3x3[8] = matrix->
elements[15];
137 result += matrix->
elements[8] * determinant3x3;
140 matrix3x3[0] = matrix->
elements[ 1];
141 matrix3x3[1] = matrix->
elements[ 2];
142 matrix3x3[2] = matrix->
elements[ 3];
143 matrix3x3[3] = matrix->
elements[ 5];
144 matrix3x3[4] = matrix->
elements[ 6];
145 matrix3x3[5] = matrix->
elements[ 7];
146 matrix3x3[6] = matrix->
elements[ 9];
147 matrix3x3[7] = matrix->
elements[10];
148 matrix3x3[8] = matrix->
elements[11];
150 result -= matrix->
elements[12] * determinant3x3;
162 matrix3x3[0] = matrix->
elements[ 5];
163 matrix3x3[1] = matrix->
elements[ 6];
164 matrix3x3[2] = matrix->
elements[ 7];
165 matrix3x3[3] = matrix->
elements[ 9];
166 matrix3x3[4] = matrix->
elements[10];
167 matrix3x3[5] = matrix->
elements[11];
168 matrix3x3[6] = matrix->
elements[13];
169 matrix3x3[7] = matrix->
elements[14];
170 matrix3x3[8] = matrix->
elements[15];
174 matrix3x3[0] = matrix->
elements[ 1];
175 matrix3x3[1] = matrix->
elements[ 2];
176 matrix3x3[2] = matrix->
elements[ 3];
177 matrix3x3[3] = matrix->
elements[ 9];
178 matrix3x3[4] = matrix->
elements[10];
179 matrix3x3[5] = matrix->
elements[11];
180 matrix3x3[6] = matrix->
elements[13];
181 matrix3x3[7] = matrix->
elements[14];
182 matrix3x3[8] = matrix->
elements[15];
186 matrix3x3[0] = matrix->
elements[ 1];
187 matrix3x3[1] = matrix->
elements[ 2];
188 matrix3x3[2] = matrix->
elements[ 3];
189 matrix3x3[3] = matrix->
elements[ 5];
190 matrix3x3[4] = matrix->
elements[ 6];
191 matrix3x3[5] = matrix->
elements[ 7];
192 matrix3x3[6] = matrix->
elements[13];
193 matrix3x3[7] = matrix->
elements[14];
194 matrix3x3[8] = matrix->
elements[15];
198 matrix3x3[0] = matrix->
elements[ 1];
199 matrix3x3[1] = matrix->
elements[ 2];
200 matrix3x3[2] = matrix->
elements[ 3];
201 matrix3x3[3] = matrix->
elements[ 5];
202 matrix3x3[4] = matrix->
elements[ 6];
203 matrix3x3[5] = matrix->
elements[ 7];
204 matrix3x3[6] = matrix->
elements[ 9];
205 matrix3x3[7] = matrix->
elements[10];
206 matrix3x3[8] = matrix->
elements[11];
210 matrix3x3[0] = matrix->
elements[ 4];
211 matrix3x3[1] = matrix->
elements[ 6];
212 matrix3x3[2] = matrix->
elements[ 7];
213 matrix3x3[3] = matrix->
elements[ 8];
214 matrix3x3[4] = matrix->
elements[10];
215 matrix3x3[5] = matrix->
elements[11];
216 matrix3x3[6] = matrix->
elements[12];
217 matrix3x3[7] = matrix->
elements[14];
218 matrix3x3[8] = matrix->
elements[15];
222 matrix3x3[0] = matrix->
elements[ 0];
223 matrix3x3[1] = matrix->
elements[ 2];
224 matrix3x3[2] = matrix->
elements[ 3];
225 matrix3x3[3] = matrix->
elements[ 8];
226 matrix3x3[4] = matrix->
elements[10];
227 matrix3x3[5] = matrix->
elements[11];
228 matrix3x3[6] = matrix->
elements[12];
229 matrix3x3[7] = matrix->
elements[14];
230 matrix3x3[8] = matrix->
elements[15];
234 matrix3x3[0] = matrix->
elements[ 0];
235 matrix3x3[1] = matrix->
elements[ 2];
236 matrix3x3[2] = matrix->
elements[ 3];
237 matrix3x3[3] = matrix->
elements[ 4];
238 matrix3x3[4] = matrix->
elements[ 6];
239 matrix3x3[5] = matrix->
elements[ 7];
240 matrix3x3[6] = matrix->
elements[12];
241 matrix3x3[7] = matrix->
elements[14];
242 matrix3x3[8] = matrix->
elements[15];
246 matrix3x3[0] = matrix->
elements[ 0];
247 matrix3x3[1] = matrix->
elements[ 2];
248 matrix3x3[2] = matrix->
elements[ 3];
249 matrix3x3[3] = matrix->
elements[ 4];
250 matrix3x3[4] = matrix->
elements[ 6];
251 matrix3x3[5] = matrix->
elements[ 7];
252 matrix3x3[6] = matrix->
elements[ 8];
253 matrix3x3[7] = matrix->
elements[10];
254 matrix3x3[8] = matrix->
elements[11];
258 matrix3x3[0] = matrix->
elements[ 4];
259 matrix3x3[1] = matrix->
elements[ 5];
260 matrix3x3[2] = matrix->
elements[ 7];
261 matrix3x3[3] = matrix->
elements[ 8];
262 matrix3x3[4] = matrix->
elements[ 9];
263 matrix3x3[5] = matrix->
elements[11];
264 matrix3x3[6] = matrix->
elements[12];
265 matrix3x3[7] = matrix->
elements[13];
266 matrix3x3[8] = matrix->
elements[15];
270 matrix3x3[0] = matrix->
elements[ 0];
271 matrix3x3[1] = matrix->
elements[ 1];
272 matrix3x3[2] = matrix->
elements[ 3];
273 matrix3x3[3] = matrix->
elements[ 8];
274 matrix3x3[4] = matrix->
elements[ 9];
275 matrix3x3[5] = matrix->
elements[11];
276 matrix3x3[6] = matrix->
elements[12];
277 matrix3x3[7] = matrix->
elements[13];
278 matrix3x3[8] = matrix->
elements[15];
282 matrix3x3[0] = matrix->
elements[ 0];
283 matrix3x3[1] = matrix->
elements[ 1];
284 matrix3x3[2] = matrix->
elements[ 3];
285 matrix3x3[3] = matrix->
elements[ 4];
286 matrix3x3[4] = matrix->
elements[ 5];
287 matrix3x3[5] = matrix->
elements[ 7];
288 matrix3x3[6] = matrix->
elements[12];
289 matrix3x3[7] = matrix->
elements[13];
290 matrix3x3[8] = matrix->
elements[15];
294 matrix3x3[0] = matrix->
elements[ 0];
295 matrix3x3[1] = matrix->
elements[ 1];
296 matrix3x3[2] = matrix->
elements[ 3];
297 matrix3x3[3] = matrix->
elements[ 4];
298 matrix3x3[4] = matrix->
elements[ 5];
299 matrix3x3[5] = matrix->
elements[ 7];
300 matrix3x3[6] = matrix->
elements[ 8];
301 matrix3x3[7] = matrix->
elements[ 9];
302 matrix3x3[8] = matrix->
elements[11];
306 matrix3x3[0] = matrix->
elements[ 4];
307 matrix3x3[1] = matrix->
elements[ 5];
308 matrix3x3[2] = matrix->
elements[ 6];
309 matrix3x3[3] = matrix->
elements[ 8];
310 matrix3x3[4] = matrix->
elements[ 9];
311 matrix3x3[5] = matrix->
elements[10];
312 matrix3x3[6] = matrix->
elements[12];
313 matrix3x3[7] = matrix->
elements[13];
314 matrix3x3[8] = matrix->
elements[14];
318 matrix3x3[0] = matrix->
elements[ 0];
319 matrix3x3[1] = matrix->
elements[ 1];
320 matrix3x3[2] = matrix->
elements[ 2];
321 matrix3x3[3] = matrix->
elements[ 8];
322 matrix3x3[4] = matrix->
elements[ 9];
323 matrix3x3[5] = matrix->
elements[10];
324 matrix3x3[6] = matrix->
elements[12];
325 matrix3x3[7] = matrix->
elements[13];
326 matrix3x3[8] = matrix->
elements[14];
330 matrix3x3[0] = matrix->
elements[ 0];
331 matrix3x3[1] = matrix->
elements[ 1];
332 matrix3x3[2] = matrix->
elements[ 2];
333 matrix3x3[3] = matrix->
elements[ 4];
334 matrix3x3[4] = matrix->
elements[ 5];
335 matrix3x3[5] = matrix->
elements[ 6];
336 matrix3x3[6] = matrix->
elements[12];
337 matrix3x3[7] = matrix->
elements[13];
338 matrix3x3[8] = matrix->
elements[14];
342 matrix3x3[0] = matrix->
elements[ 0];
343 matrix3x3[1] = matrix->
elements[ 1];
344 matrix3x3[2] = matrix->
elements[ 2];
345 matrix3x3[3] = matrix->
elements[ 4];
346 matrix3x3[4] = matrix->
elements[ 5];
347 matrix3x3[5] = matrix->
elements[ 6];
348 matrix3x3[6] = matrix->
elements[ 8];
349 matrix3x3[7] = matrix->
elements[ 9];
350 matrix3x3[8] = matrix->
elements[10];
366 for(
int allElements = 0; allElements < 16; allElements ++)
429 FOV = 1.0f / tan(FOV * 0.5
f);
433 result.
elements[10] = -(zFar + zNear) / (zFar - zNear);
435 result.
elements[14] = (-2.0f * zFar * zNear) / (zFar - zNear);
445 Vec3f cameraX, cameraY;
447 Vec3f cameraZ = {center.
x - eye.
x, center.
y - eye.
y, center.
z - eye.
z};
464 result[0] = cameraX.
x;
465 result[1] = cameraY.
x;
466 result[2] = -cameraZ.
x;
468 result[4] = cameraX.
y;
469 result[5] = cameraY.
y;
470 result[6] = -cameraZ.
y;
472 result[8] = cameraX.
z;
473 result[9] = cameraY.
z;
474 result[10] = -cameraZ.
z;
493 result.
elements[10] = -2.0f / (zFar - zNear);
494 result.
elements[14] = -(zFar + zNear) / (zFar - zNear);
539 for(
int row = 0; row < 4; row ++)
541 for(
int column = 0; column < 4; column ++)
547 float accumulator = 0.0f;
548 for(
int allElements = 0; allElements < 4; allElements ++)
550 accumulator += left->
elements[allElements * 4 + row] * right->
elements[column * 4 + allElements];
552 result.
elements[column * 4 + row] = accumulator;
589 Vec4f extendedVertex;
591 extendedVertex.
x = vertex->
x;
592 extendedVertex.
y = vertex->
y;
593 extendedVertex.
z = vertex->
z;
594 extendedVertex.
w = 1.0f;
596 result.
x = extendedVertex.
x * matrix->
elements[ 0];
597 result.
x += extendedVertex.
y * matrix->
elements[ 4];
598 result.
x += extendedVertex.
z * matrix->
elements[ 8];
599 result.
x += extendedVertex.
w * matrix->
elements[12];
601 result.
y = extendedVertex.
x * matrix->
elements[ 1];
602 result.
y += extendedVertex.
y * matrix->
elements[ 5];
603 result.
y += extendedVertex.
z * matrix->
elements[ 9];
604 result.
y += extendedVertex.
w * matrix->
elements[13];
606 result.
z = extendedVertex.
x * matrix->
elements[ 2];
607 result.
z += extendedVertex.
y * matrix->
elements[ 6];
608 result.
z += extendedVertex.
z * matrix->
elements[10];
609 result.
z += extendedVertex.
w * matrix->
elements[14];
617 for(
int row = 0; row < 4; row ++)
619 for(
int column = 0; column < 4; column ++)
const float identityArray[16]
void print(void)
Print the matrix.
float & operator[](unsigned element)
Array operator for accessing the elements of the matrix.
Matrix operator*(Matrix right)
Multiply operator to post multiply a matrix by another.
static Matrix identityMatrix
The identity matrix.
static Vec3f cross(const Vec3f &vector1, const Vec3f &vector2)
Calculate cross product between two 3D floating point vectors.
Functions for manipulating matrices.
Matrix(void)
Default constructor.
static Matrix matrixInvert(Matrix *matrix)
Get the inverse of a matrix.
static Matrix createTranslation(float x, float y, float z)
Create and return a translation matrix.
static Matrix matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
Create and return an orthographic projection matrix.
static Matrix createScaling(float x, float y, float z)
Create and return a scaling matrix.
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
float * getAsArray(void)
Get the matrix elements as a column major order array.
A 3D floating point vector.
static Matrix matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a camera matrix.
static Vec4f vertexTransform(Vec4f *vector, Matrix *matrix)
Transform a 4D vertex by a matrix.
static Matrix createRotationY(float angle)
Create and return a rotation matrix around the y-axis matrix.
static Matrix createRotationZ(float angle)
Create and return a rotation matrix around the z-axis matrix.
static float matrixDeterminant(float *matrix)
Calculate determinant of supplied 3x3 matrix.
float degreesToRadians(float degrees)
Convert an angle in degrees to radians.
static void matrixTranspose(Matrix *matrix)
Transpose a matrix in-place.
GLint GLint GLint GLint GLint x
static Matrix multiply(Matrix *left, Matrix *right)
Multiply 2 matrices to return a third.
Matrix & operator=(const Matrix &another)
Overloading assingment operater to do deep copy of the Matrix elements.
float elements[16]
A 16 element floating point array used to represent a 4x4 matrix.
A 4D floating point vector.
static Matrix createRotationX(float angle)
Create and return a rotation matrix around the x-axis matrix.
void normalize(void)
Normalize 3D floating point vector.
static Matrix matrixScale(Matrix *matrix, float scale)
Scale each element in a matrix by a constant.