36 0.5f, 0.0f, 0.0f, 0.0f,
37 0.0f, 0.5f, 0.0f, 0.0f,
38 0.0f, 0.0f, 0.5f, 0.0f,
39 0.5f, 0.5f, 0.5f, 1.0f,
46 1.0f, 0.0f, 0.0f, 0.0f,
47 0.0f, 1.0f, 0.0f, 0.0f,
48 0.0f, 0.0f, 1.0f, 0.0f,
49 0.0f, 0.0f, 0.0f, 1.0f,
57 memcpy(
elements, array, 16 *
sizeof(
float));
66 ASSERT(element <= 15,
"Invalid matrix element index.");
80 memcpy(this->
elements, another.elements, 16 *
sizeof(
float));
91 result.
elements[5] = cos(angleInRadians);
92 result.elements[9] = -sin(angleInRadians);
93 result.elements[6] = sin(angleInRadians);
94 result.elements[10] = cos(angleInRadians);
104 result.
elements[0] = cos(angleInRadians);
105 result.elements[8] = sin(angleInRadians);
106 result.elements[2] = -sin(angleInRadians);
107 result.elements[10] = cos(angleInRadians);
117 result.
elements[0] = cos(angleInRarians);
118 result.elements[4] = -sin(angleInRarians);
119 result.elements[1] = sin(angleInRarians);
120 result.elements[5] = cos(angleInRarians);
130 result.elements[5] =
y;
131 result.elements[10] = z;
141 result.elements[13] =
y;
142 result.elements[14] = z;
156 result = matrix[0] * (matrix[4] * matrix[8] - matrix[7] * matrix[5]);
157 result -= matrix[3] * (matrix[1] * matrix[8] - matrix[7] * matrix[2]);
158 result += matrix[6] * (matrix[1] * matrix[5] - matrix[4] * matrix[2]);
166 float determinant3x3 = 0.0f;
170 matrix3x3[0] = matrix->elements[5];
171 matrix3x3[1] = matrix->elements[6];
172 matrix3x3[2] = matrix->elements[7];
173 matrix3x3[3] = matrix->elements[9];
174 matrix3x3[4] = matrix->elements[10];
175 matrix3x3[5] = matrix->elements[11];
176 matrix3x3[6] = matrix->elements[13];
177 matrix3x3[7] = matrix->elements[14];
178 matrix3x3[8] = matrix->elements[15];
180 result += matrix->elements[0] * determinant3x3;
183 matrix3x3[0] = matrix->elements[1];
184 matrix3x3[1] = matrix->elements[2];
185 matrix3x3[2] = matrix->elements[3];
186 matrix3x3[3] = matrix->elements[9];
187 matrix3x3[4] = matrix->elements[10];
188 matrix3x3[5] = matrix->elements[11];
189 matrix3x3[6] = matrix->elements[13];
190 matrix3x3[7] = matrix->elements[14];
191 matrix3x3[8] = matrix->elements[15];
193 result -= matrix->elements[4] * determinant3x3;
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[13];
203 matrix3x3[7] = matrix->elements[14];
204 matrix3x3[8] = matrix->elements[15];
206 result += matrix->elements[8] * determinant3x3;
209 matrix3x3[0] = matrix->elements[1];
210 matrix3x3[1] = matrix->elements[2];
211 matrix3x3[2] = matrix->elements[3];
212 matrix3x3[3] = matrix->elements[5];
213 matrix3x3[4] = matrix->elements[6];
214 matrix3x3[5] = matrix->elements[7];
215 matrix3x3[6] = matrix->elements[9];
216 matrix3x3[7] = matrix->elements[10];
217 matrix3x3[8] = matrix->elements[11];
219 result -= matrix->elements[12] * determinant3x3;
231 matrix3x3[0] = matrix->elements[5];
232 matrix3x3[1] = matrix->elements[6];
233 matrix3x3[2] = matrix->elements[7];
234 matrix3x3[3] = matrix->elements[9];
235 matrix3x3[4] = matrix->elements[10];
236 matrix3x3[5] = matrix->elements[11];
237 matrix3x3[6] = matrix->elements[13];
238 matrix3x3[7] = matrix->elements[14];
239 matrix3x3[8] = matrix->elements[15];
243 matrix3x3[0] = matrix->elements[1];
244 matrix3x3[1] = matrix->elements[2];
245 matrix3x3[2] = matrix->elements[3];
246 matrix3x3[3] = matrix->elements[9];
247 matrix3x3[4] = matrix->elements[10];
248 matrix3x3[5] = matrix->elements[11];
249 matrix3x3[6] = matrix->elements[13];
250 matrix3x3[7] = matrix->elements[14];
251 matrix3x3[8] = matrix->elements[15];
255 matrix3x3[0] = matrix->elements[1];
256 matrix3x3[1] = matrix->elements[2];
257 matrix3x3[2] = matrix->elements[3];
258 matrix3x3[3] = matrix->elements[5];
259 matrix3x3[4] = matrix->elements[6];
260 matrix3x3[5] = matrix->elements[7];
261 matrix3x3[6] = matrix->elements[13];
262 matrix3x3[7] = matrix->elements[14];
263 matrix3x3[8] = matrix->elements[15];
267 matrix3x3[0] = matrix->elements[1];
268 matrix3x3[1] = matrix->elements[2];
269 matrix3x3[2] = matrix->elements[3];
270 matrix3x3[3] = matrix->elements[5];
271 matrix3x3[4] = matrix->elements[6];
272 matrix3x3[5] = matrix->elements[7];
273 matrix3x3[6] = matrix->elements[9];
274 matrix3x3[7] = matrix->elements[10];
275 matrix3x3[8] = matrix->elements[11];
279 matrix3x3[0] = matrix->elements[4];
280 matrix3x3[1] = matrix->elements[6];
281 matrix3x3[2] = matrix->elements[7];
282 matrix3x3[3] = matrix->elements[8];
283 matrix3x3[4] = matrix->elements[10];
284 matrix3x3[5] = matrix->elements[11];
285 matrix3x3[6] = matrix->elements[12];
286 matrix3x3[7] = matrix->elements[14];
287 matrix3x3[8] = matrix->elements[15];
291 matrix3x3[0] = matrix->elements[0];
292 matrix3x3[1] = matrix->elements[2];
293 matrix3x3[2] = matrix->elements[3];
294 matrix3x3[3] = matrix->elements[8];
295 matrix3x3[4] = matrix->elements[10];
296 matrix3x3[5] = matrix->elements[11];
297 matrix3x3[6] = matrix->elements[12];
298 matrix3x3[7] = matrix->elements[14];
299 matrix3x3[8] = matrix->elements[15];
303 matrix3x3[0] = matrix->elements[0];
304 matrix3x3[1] = matrix->elements[2];
305 matrix3x3[2] = matrix->elements[3];
306 matrix3x3[3] = matrix->elements[4];
307 matrix3x3[4] = matrix->elements[6];
308 matrix3x3[5] = matrix->elements[7];
309 matrix3x3[6] = matrix->elements[12];
310 matrix3x3[7] = matrix->elements[14];
311 matrix3x3[8] = matrix->elements[15];
315 matrix3x3[0] = matrix->elements[0];
316 matrix3x3[1] = matrix->elements[2];
317 matrix3x3[2] = matrix->elements[3];
318 matrix3x3[3] = matrix->elements[4];
319 matrix3x3[4] = matrix->elements[6];
320 matrix3x3[5] = matrix->elements[7];
321 matrix3x3[6] = matrix->elements[8];
322 matrix3x3[7] = matrix->elements[10];
323 matrix3x3[8] = matrix->elements[11];
327 matrix3x3[0] = matrix->elements[4];
328 matrix3x3[1] = matrix->elements[5];
329 matrix3x3[2] = matrix->elements[7];
330 matrix3x3[3] = matrix->elements[8];
331 matrix3x3[4] = matrix->elements[9];
332 matrix3x3[5] = matrix->elements[11];
333 matrix3x3[6] = matrix->elements[12];
334 matrix3x3[7] = matrix->elements[13];
335 matrix3x3[8] = matrix->elements[15];
339 matrix3x3[0] = matrix->elements[0];
340 matrix3x3[1] = matrix->elements[1];
341 matrix3x3[2] = matrix->elements[3];
342 matrix3x3[3] = matrix->elements[8];
343 matrix3x3[4] = matrix->elements[9];
344 matrix3x3[5] = matrix->elements[11];
345 matrix3x3[6] = matrix->elements[12];
346 matrix3x3[7] = matrix->elements[13];
347 matrix3x3[8] = matrix->elements[15];
351 matrix3x3[0] = matrix->elements[0];
352 matrix3x3[1] = matrix->elements[1];
353 matrix3x3[2] = matrix->elements[3];
354 matrix3x3[3] = matrix->elements[4];
355 matrix3x3[4] = matrix->elements[5];
356 matrix3x3[5] = matrix->elements[7];
357 matrix3x3[6] = matrix->elements[12];
358 matrix3x3[7] = matrix->elements[13];
359 matrix3x3[8] = matrix->elements[15];
363 matrix3x3[0] = matrix->elements[0];
364 matrix3x3[1] = matrix->elements[1];
365 matrix3x3[2] = matrix->elements[3];
366 matrix3x3[3] = matrix->elements[4];
367 matrix3x3[4] = matrix->elements[5];
368 matrix3x3[5] = matrix->elements[7];
369 matrix3x3[6] = matrix->elements[8];
370 matrix3x3[7] = matrix->elements[9];
371 matrix3x3[8] = matrix->elements[11];
375 matrix3x3[0] = matrix->elements[4];
376 matrix3x3[1] = matrix->elements[5];
377 matrix3x3[2] = matrix->elements[6];
378 matrix3x3[3] = matrix->elements[8];
379 matrix3x3[4] = matrix->elements[9];
380 matrix3x3[5] = matrix->elements[10];
381 matrix3x3[6] = matrix->elements[12];
382 matrix3x3[7] = matrix->elements[13];
383 matrix3x3[8] = matrix->elements[14];
387 matrix3x3[0] = matrix->elements[0];
388 matrix3x3[1] = matrix->elements[1];
389 matrix3x3[2] = matrix->elements[2];
390 matrix3x3[3] = matrix->elements[8];
391 matrix3x3[4] = matrix->elements[9];
392 matrix3x3[5] = matrix->elements[10];
393 matrix3x3[6] = matrix->elements[12];
394 matrix3x3[7] = matrix->elements[13];
395 matrix3x3[8] = matrix->elements[14];
399 matrix3x3[0] = matrix->elements[0];
400 matrix3x3[1] = matrix->elements[1];
401 matrix3x3[2] = matrix->elements[2];
402 matrix3x3[3] = matrix->elements[4];
403 matrix3x3[4] = matrix->elements[5];
404 matrix3x3[5] = matrix->elements[6];
405 matrix3x3[6] = matrix->elements[12];
406 matrix3x3[7] = matrix->elements[13];
407 matrix3x3[8] = matrix->elements[14];
411 matrix3x3[0] = matrix->elements[0];
412 matrix3x3[1] = matrix->elements[1];
413 matrix3x3[2] = matrix->elements[2];
414 matrix3x3[3] = matrix->elements[4];
415 matrix3x3[4] = matrix->elements[5];
416 matrix3x3[5] = matrix->elements[6];
417 matrix3x3[6] = matrix->elements[8];
418 matrix3x3[7] = matrix->elements[9];
419 matrix3x3[8] = matrix->elements[10];
435 Vec3f zVector = { center.x - eye.x, center.y - eye.y, center.z - eye.z };
454 result[0] = xVector.x;
455 result[1] = yVector.x;
456 result[2] = -zVector.x;
458 result[4] = xVector.y;
459 result[5] = yVector.y;
460 result[6] = -zVector.y;
462 result[8] = xVector.z;
463 result[9] = yVector.z;
464 result[10] = -zVector.z;
477 FOV = 1.0f / tan(FOV * 0.5
f);
479 result.elements[0] = FOV / ratio;
480 result.elements[5] = FOV;
481 result.elements[10] = -(zFar + zNear) / (zFar - zNear);
482 result.elements[11] = -1.0f;
483 result.elements[14] = (-2.0f * zFar * zNear) / (zFar - zNear);
484 result.elements[15] = 0.0f;
493 for (
int allElements = 0; allElements < 16; allElements++)
495 result.elements[allElements] = matrix->elements[allElements] *
scale;
505 temp = matrix->elements[1];
506 matrix->elements[1] = matrix->elements[4];
507 matrix->elements[4] = temp;
509 temp = matrix->elements[2];
510 matrix->elements[2] = matrix->elements[8];
511 matrix->elements[8] = temp;
513 temp = matrix->elements[3];
514 matrix->elements[3] = matrix->elements[12];
515 matrix->elements[12] = temp;
517 temp = matrix->elements[6];
518 matrix->elements[6] = matrix->elements[9];
519 matrix->elements[9] = temp;
521 temp = matrix->elements[7];
522 matrix->elements[7] = matrix->elements[13];
523 matrix->elements[13] = temp;
525 temp = matrix->elements[11];
526 matrix->elements[11] = matrix->elements[14];
527 matrix->elements[14] = temp;
534 for (
int row = 0; row < 4; row++)
536 for (
int column = 0; column < 4; column++)
542 float accumulator = 0.0f;
544 for (
int allElements = 0; allElements < 4; allElements++)
546 accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
549 result.elements[column * 4 + row] = accumulator;
560 result.
x = vertex->x * matrix->elements[0];
561 result.x += vertex->y * matrix->elements[4];
562 result.x += vertex->z * matrix->elements[8];
563 result.x += vertex->w * matrix->elements[12];
565 result.y = vertex->x * matrix->elements[1];
566 result.y += vertex->y * matrix->elements[5];
567 result.y += vertex->z * matrix->elements[9];
568 result.y += vertex->w * matrix->elements[13];
570 result.z = vertex->x * matrix->elements[2];
571 result.z += vertex->y * matrix->elements[6];
572 result.z += vertex->z * matrix->elements[10];
573 result.z += vertex->w * matrix->elements[14];
575 result.w = vertex->x * matrix->elements[3];
576 result.w += vertex->y * matrix->elements[7];
577 result.w += vertex->z * matrix->elements[11];
578 result.w += vertex->w * matrix->elements[15];
const float identityArray[16]
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 float dot(Vec3f &vector1, Vec3f &vector2)
Calculate dot product between two 3D floating point vectors.
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 createScaling(float x, float y, float z)
Create and return a scaling matrix.
static const float biasArray[]
A 4x4 bias Matrix;.
static Matrix matrixLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a look at matrix.
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
static Matrix biasMatrix
The bias matrix.
float * getAsArray(void)
Get the matrix elements as a column major order array.
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.