34 1.0f, 0.0f, 0.0f, 0.0f,
35 0.0f, 1.0f, 0.0f, 0.0f,
36 0.0f, 0.0f, 1.0f, 0.0f,
37 0.0f, 0.0f, 0.0f, 1.0f,
44 memcpy(
elements, array, 16 *
sizeof(
float));
55 LOGE(
"Matrix only has 16 elements, tried to access element %d", element);
70 memcpy(this->
elements, another.elements, 16 *
sizeof(
float));
85 result = matrix[0] * (matrix[4] * matrix[8] - matrix[7] * matrix[5]);
86 result -= matrix[3] * (matrix[1] * matrix[8] - matrix[7] * matrix[2]);
87 result += matrix[6] * (matrix[1] * matrix[5] - matrix[4] * matrix[2]);
95 float determinant3x3 = 0.0f;
99 matrix3x3[0] = matrix->elements[ 5];
100 matrix3x3[1] = matrix->elements[ 6];
101 matrix3x3[2] = matrix->elements[ 7];
102 matrix3x3[3] = matrix->elements[ 9];
103 matrix3x3[4] = matrix->elements[10];
104 matrix3x3[5] = matrix->elements[11];
105 matrix3x3[6] = matrix->elements[13];
106 matrix3x3[7] = matrix->elements[14];
107 matrix3x3[8] = matrix->elements[15];
109 result += matrix->elements[0] * determinant3x3;
112 matrix3x3[0] = matrix->elements[ 1];
113 matrix3x3[1] = matrix->elements[ 2];
114 matrix3x3[2] = matrix->elements[ 3];
115 matrix3x3[3] = matrix->elements[ 9];
116 matrix3x3[4] = matrix->elements[10];
117 matrix3x3[5] = matrix->elements[11];
118 matrix3x3[6] = matrix->elements[13];
119 matrix3x3[7] = matrix->elements[14];
120 matrix3x3[8] = matrix->elements[15];
122 result -= matrix->elements[4] * determinant3x3;
125 matrix3x3[0] = matrix->elements[ 1];
126 matrix3x3[1] = matrix->elements[ 2];
127 matrix3x3[2] = matrix->elements[ 3];
128 matrix3x3[3] = matrix->elements[ 5];
129 matrix3x3[4] = matrix->elements[ 6];
130 matrix3x3[5] = matrix->elements[ 7];
131 matrix3x3[6] = matrix->elements[13];
132 matrix3x3[7] = matrix->elements[14];
133 matrix3x3[8] = matrix->elements[15];
135 result += matrix->elements[8] * determinant3x3;
138 matrix3x3[0] = matrix->elements[ 1];
139 matrix3x3[1] = matrix->elements[ 2];
140 matrix3x3[2] = matrix->elements[ 3];
141 matrix3x3[3] = matrix->elements[ 5];
142 matrix3x3[4] = matrix->elements[ 6];
143 matrix3x3[5] = matrix->elements[ 7];
144 matrix3x3[6] = matrix->elements[ 9];
145 matrix3x3[7] = matrix->elements[10];
146 matrix3x3[8] = matrix->elements[11];
148 result -= matrix->elements[12] * determinant3x3;
160 matrix3x3[0] = matrix->elements[ 5];
161 matrix3x3[1] = matrix->elements[ 6];
162 matrix3x3[2] = matrix->elements[ 7];
163 matrix3x3[3] = matrix->elements[ 9];
164 matrix3x3[4] = matrix->elements[10];
165 matrix3x3[5] = matrix->elements[11];
166 matrix3x3[6] = matrix->elements[13];
167 matrix3x3[7] = matrix->elements[14];
168 matrix3x3[8] = matrix->elements[15];
172 matrix3x3[0] = matrix->elements[ 1];
173 matrix3x3[1] = matrix->elements[ 2];
174 matrix3x3[2] = matrix->elements[ 3];
175 matrix3x3[3] = matrix->elements[ 9];
176 matrix3x3[4] = matrix->elements[10];
177 matrix3x3[5] = matrix->elements[11];
178 matrix3x3[6] = matrix->elements[13];
179 matrix3x3[7] = matrix->elements[14];
180 matrix3x3[8] = matrix->elements[15];
184 matrix3x3[0] = matrix->elements[ 1];
185 matrix3x3[1] = matrix->elements[ 2];
186 matrix3x3[2] = matrix->elements[ 3];
187 matrix3x3[3] = matrix->elements[ 5];
188 matrix3x3[4] = matrix->elements[ 6];
189 matrix3x3[5] = matrix->elements[ 7];
190 matrix3x3[6] = matrix->elements[13];
191 matrix3x3[7] = matrix->elements[14];
192 matrix3x3[8] = matrix->elements[15];
196 matrix3x3[0] = matrix->elements[ 1];
197 matrix3x3[1] = matrix->elements[ 2];
198 matrix3x3[2] = matrix->elements[ 3];
199 matrix3x3[3] = matrix->elements[ 5];
200 matrix3x3[4] = matrix->elements[ 6];
201 matrix3x3[5] = matrix->elements[ 7];
202 matrix3x3[6] = matrix->elements[ 9];
203 matrix3x3[7] = matrix->elements[10];
204 matrix3x3[8] = matrix->elements[11];
208 matrix3x3[0] = matrix->elements[ 4];
209 matrix3x3[1] = matrix->elements[ 6];
210 matrix3x3[2] = matrix->elements[ 7];
211 matrix3x3[3] = matrix->elements[ 8];
212 matrix3x3[4] = matrix->elements[10];
213 matrix3x3[5] = matrix->elements[11];
214 matrix3x3[6] = matrix->elements[12];
215 matrix3x3[7] = matrix->elements[14];
216 matrix3x3[8] = matrix->elements[15];
220 matrix3x3[0] = matrix->elements[ 0];
221 matrix3x3[1] = matrix->elements[ 2];
222 matrix3x3[2] = matrix->elements[ 3];
223 matrix3x3[3] = matrix->elements[ 8];
224 matrix3x3[4] = matrix->elements[10];
225 matrix3x3[5] = matrix->elements[11];
226 matrix3x3[6] = matrix->elements[12];
227 matrix3x3[7] = matrix->elements[14];
228 matrix3x3[8] = matrix->elements[15];
232 matrix3x3[0] = matrix->elements[ 0];
233 matrix3x3[1] = matrix->elements[ 2];
234 matrix3x3[2] = matrix->elements[ 3];
235 matrix3x3[3] = matrix->elements[ 4];
236 matrix3x3[4] = matrix->elements[ 6];
237 matrix3x3[5] = matrix->elements[ 7];
238 matrix3x3[6] = matrix->elements[12];
239 matrix3x3[7] = matrix->elements[14];
240 matrix3x3[8] = matrix->elements[15];
244 matrix3x3[0] = matrix->elements[ 0];
245 matrix3x3[1] = matrix->elements[ 2];
246 matrix3x3[2] = matrix->elements[ 3];
247 matrix3x3[3] = matrix->elements[ 4];
248 matrix3x3[4] = matrix->elements[ 6];
249 matrix3x3[5] = matrix->elements[ 7];
250 matrix3x3[6] = matrix->elements[ 8];
251 matrix3x3[7] = matrix->elements[10];
252 matrix3x3[8] = matrix->elements[11];
256 matrix3x3[0] = matrix->elements[ 4];
257 matrix3x3[1] = matrix->elements[ 5];
258 matrix3x3[2] = matrix->elements[ 7];
259 matrix3x3[3] = matrix->elements[ 8];
260 matrix3x3[4] = matrix->elements[ 9];
261 matrix3x3[5] = matrix->elements[11];
262 matrix3x3[6] = matrix->elements[12];
263 matrix3x3[7] = matrix->elements[13];
264 matrix3x3[8] = matrix->elements[15];
268 matrix3x3[0] = matrix->elements[ 0];
269 matrix3x3[1] = matrix->elements[ 1];
270 matrix3x3[2] = matrix->elements[ 3];
271 matrix3x3[3] = matrix->elements[ 8];
272 matrix3x3[4] = matrix->elements[ 9];
273 matrix3x3[5] = matrix->elements[11];
274 matrix3x3[6] = matrix->elements[12];
275 matrix3x3[7] = matrix->elements[13];
276 matrix3x3[8] = matrix->elements[15];
280 matrix3x3[0] = matrix->elements[ 0];
281 matrix3x3[1] = matrix->elements[ 1];
282 matrix3x3[2] = matrix->elements[ 3];
283 matrix3x3[3] = matrix->elements[ 4];
284 matrix3x3[4] = matrix->elements[ 5];
285 matrix3x3[5] = matrix->elements[ 7];
286 matrix3x3[6] = matrix->elements[12];
287 matrix3x3[7] = matrix->elements[13];
288 matrix3x3[8] = matrix->elements[15];
292 matrix3x3[0] = matrix->elements[ 0];
293 matrix3x3[1] = matrix->elements[ 1];
294 matrix3x3[2] = matrix->elements[ 3];
295 matrix3x3[3] = matrix->elements[ 4];
296 matrix3x3[4] = matrix->elements[ 5];
297 matrix3x3[5] = matrix->elements[ 7];
298 matrix3x3[6] = matrix->elements[ 8];
299 matrix3x3[7] = matrix->elements[ 9];
300 matrix3x3[8] = matrix->elements[11];
304 matrix3x3[0] = matrix->elements[ 4];
305 matrix3x3[1] = matrix->elements[ 5];
306 matrix3x3[2] = matrix->elements[ 6];
307 matrix3x3[3] = matrix->elements[ 8];
308 matrix3x3[4] = matrix->elements[ 9];
309 matrix3x3[5] = matrix->elements[10];
310 matrix3x3[6] = matrix->elements[12];
311 matrix3x3[7] = matrix->elements[13];
312 matrix3x3[8] = matrix->elements[14];
316 matrix3x3[0] = matrix->elements[ 0];
317 matrix3x3[1] = matrix->elements[ 1];
318 matrix3x3[2] = matrix->elements[ 2];
319 matrix3x3[3] = matrix->elements[ 8];
320 matrix3x3[4] = matrix->elements[ 9];
321 matrix3x3[5] = matrix->elements[10];
322 matrix3x3[6] = matrix->elements[12];
323 matrix3x3[7] = matrix->elements[13];
324 matrix3x3[8] = matrix->elements[14];
328 matrix3x3[0] = matrix->elements[ 0];
329 matrix3x3[1] = matrix->elements[ 1];
330 matrix3x3[2] = matrix->elements[ 2];
331 matrix3x3[3] = matrix->elements[ 4];
332 matrix3x3[4] = matrix->elements[ 5];
333 matrix3x3[5] = matrix->elements[ 6];
334 matrix3x3[6] = matrix->elements[12];
335 matrix3x3[7] = matrix->elements[13];
336 matrix3x3[8] = matrix->elements[14];
340 matrix3x3[0] = matrix->elements[ 0];
341 matrix3x3[1] = matrix->elements[ 1];
342 matrix3x3[2] = matrix->elements[ 2];
343 matrix3x3[3] = matrix->elements[ 4];
344 matrix3x3[4] = matrix->elements[ 5];
345 matrix3x3[5] = matrix->elements[ 6];
346 matrix3x3[6] = matrix->elements[ 8];
347 matrix3x3[7] = matrix->elements[ 9];
348 matrix3x3[8] = matrix->elements[10];
364 for(
int allElements = 0; allElements < 16; allElements ++)
366 result.elements[allElements] = matrix->elements[allElements] *
scale;
376 temp = matrix->elements[1];
377 matrix->elements[1] = matrix->elements[4];
378 matrix->elements[4] = temp;
380 temp = matrix->elements[2];
381 matrix->elements[2] = matrix->elements[8];
382 matrix->elements[8] = temp;
384 temp = matrix->elements[3];
385 matrix->elements[3] = matrix->elements[12];
386 matrix->elements[12] = temp;
388 temp = matrix->elements[6];
389 matrix->elements[6] = matrix->elements[9];
390 matrix->elements[9] = temp;
392 temp = matrix->elements[7];
393 matrix->elements[7] = matrix->elements[13];
394 matrix->elements[13] = temp;
396 temp = matrix->elements[11];
397 matrix->elements[11] = matrix->elements[14];
398 matrix->elements[14] = temp;
406 result.elements[ 5] =
y;
407 result.elements[10] = z;
417 result.elements[13] =
y;
418 result.elements[14] = z;
427 FOV = 1.0f / tan(FOV * 0.5
f);
429 result.elements[ 0] = FOV / ratio;
430 result.elements[ 5] = FOV;
431 result.elements[10] = -(zFar + zNear) / (zFar - zNear);
432 result.elements[11] = -1.0f;
433 result.elements[14] = (-2.0f * zFar * zNear) / (zFar - zNear);
434 result.elements[15] = 0.0f;
443 Vec3f cameraX, cameraY;
445 Vec3f cameraZ = {center.x - eye.x, center.y - eye.y, center.z - eye.z};
462 result[0] = cameraX.x;
463 result[1] = cameraY.x;
464 result[2] = -cameraZ.x;
466 result[4] = cameraX.y;
467 result[5] = cameraY.y;
468 result[6] = -cameraZ.y;
470 result[8] = cameraX.z;
471 result[9] = cameraY.z;
472 result[10] = -cameraZ.z;
486 result.elements[12] = -(right +
left) / (right - left);
488 result.elements[ 5] = 2.0f / (top -
bottom);
489 result.elements[13] = -(top +
bottom) / (top - bottom);
491 result.elements[10] = -2.0f / (zFar - zNear);
492 result.elements[14] = -(zFar + zNear) / (zFar - zNear);
537 for(
int row = 0; row < 4; row ++)
539 for(
int column = 0; column < 4; column ++)
545 float accumulator = 0.0f;
546 for(
int allElements = 0; allElements < 4; allElements ++)
548 accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
550 result.elements[column * 4 + row] = accumulator;
561 result.
x = vertex->x * matrix->elements[ 0];
562 result.x += vertex->y * matrix->elements[ 4];
563 result.x += vertex->z * matrix->elements[ 8];
564 result.x += vertex->w * matrix->elements[12];
566 result.y = vertex->x * matrix->elements[ 1];
567 result.y += vertex->y * matrix->elements[ 5];
568 result.y += vertex->z * matrix->elements[ 9];
569 result.y += vertex->w * matrix->elements[13];
571 result.z = vertex->x * matrix->elements[ 2];
572 result.z += vertex->y * matrix->elements[ 6];
573 result.z += vertex->z * matrix->elements[10];
574 result.z += vertex->w * matrix->elements[14];
576 result.w = vertex->x * matrix->elements[ 3];
577 result.w += vertex->y * matrix->elements[ 7];
578 result.w += vertex->z * matrix->elements[11];
579 result.w += vertex->w * matrix->elements[15];
587 Vec4f extendedVertex;
589 extendedVertex.
x = vertex->x;
590 extendedVertex.y = vertex->y;
591 extendedVertex.z = vertex->z;
592 extendedVertex.w = 1.0f;
594 result.x = extendedVertex.x * matrix->elements[ 0];
595 result.x += extendedVertex.y * matrix->elements[ 4];
596 result.x += extendedVertex.z * matrix->elements[ 8];
597 result.x += extendedVertex.w * matrix->elements[12];
599 result.y = extendedVertex.x * matrix->elements[ 1];
600 result.y += extendedVertex.y * matrix->elements[ 5];
601 result.y += extendedVertex.z * matrix->elements[ 9];
602 result.y += extendedVertex.w * matrix->elements[13];
604 result.z = extendedVertex.x * matrix->elements[ 2];
605 result.z += extendedVertex.y * matrix->elements[ 6];
606 result.z += extendedVertex.z * matrix->elements[10];
607 result.z += extendedVertex.w * matrix->elements[14];
615 for(
int row = 0; row < 4; row ++)
617 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.
Matrix(void)
Default constructor.
static const float identityArray[]
A 4x4 identity Matrix;.
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.
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.
static Matrix createRotationX(float angle)
Create and return a rotation matrix around the x-axis matrix.
static Matrix matrixScale(Matrix *matrix, float scale)
Scale each element in a matrix by a constant.