OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Matrix.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include "Common.h"
22 #include "Matrix.h"
23 
24 #include <cmath>
25 #include <cstring>
26 #include <cstdio>
27 #include <cstdlib>
28 
29 namespace MaliSDK
30 {
31  /* Identity matrix. */
32  const float Matrix::identityArray[16] =
33  {
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,
38  };
39 
40  Matrix Matrix::identityMatrix = Matrix(identityArray);
41 
42  Matrix::Matrix(const float* array)
43  {
44  memcpy(elements, array, 16 * sizeof(float));
45  }
46 
47  Matrix::Matrix(void)
48  {
49  }
50 
51  float& Matrix::operator[] (unsigned element)
52  {
53  if (element > 15)
54  {
55  LOGE("Matrix only has 16 elements, tried to access element %d", element);
56  exit(1);
57  }
58  return elements[element];
59  }
60 
61  Matrix Matrix::operator* (Matrix right)
62  {
63  return multiply(this, &right);
64  }
65 
66  Matrix& Matrix::operator= (const Matrix &another)
67  {
68  if(this != &another)
69  {
70  memcpy(this->elements, another.elements, 16 * sizeof(float));
71  }
72 
73  return *this;
74  }
75 
76  float* Matrix::getAsArray(void)
77  {
78  return elements;
79  }
80 
81  float Matrix::matrixDeterminant(float *matrix)
82  {
83  float result = 0.0f;
84 
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]);
88 
89  return result;
90  }
91 
92  float Matrix::matrixDeterminant(Matrix *matrix)
93  {
94  float matrix3x3[9];
95  float determinant3x3 = 0.0f;
96  float result = 0.0f;
97 
98  /* Remove (i, j) (1, 1) to form new 3x3 matrix. */
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];
108  determinant3x3 = matrixDeterminant(matrix3x3);
109  result += matrix->elements[0] * determinant3x3;
110 
111  /* Remove (i, j) (1, 2) to form new 3x3 matrix. */
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];
121  determinant3x3 = matrixDeterminant(matrix3x3);
122  result -= matrix->elements[4] * determinant3x3;
123 
124  /* Remove (i, j) (1, 3) to form new 3x3 matrix. */
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];
134  determinant3x3 = matrixDeterminant(matrix3x3);
135  result += matrix->elements[8] * determinant3x3;
136 
137  /* Remove (i, j) (1, 4) to form new 3x3 matrix. */
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];
147  determinant3x3 = matrixDeterminant(matrix3x3);
148  result -= matrix->elements[12] * determinant3x3;
149 
150  return result;
151  }
152 
153  Matrix Matrix::matrixInvert(Matrix *matrix)
154  {
155  Matrix result;
156  float matrix3x3[9];
157 
158  /* Find the cofactor of each element. */
159  /* Element (i, j) (1, 1) */
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];
169  result.elements[0] = matrixDeterminant(matrix3x3);
170 
171  /* Element (i, j) (1, 2) */
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];
181  result.elements[4] = -matrixDeterminant(matrix3x3);
182 
183  /* Element (i, j) (1, 3) */
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];
193  result.elements[8] = matrixDeterminant(matrix3x3);
194 
195  /* Element (i, j) (1, 4) */
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];
205  result.elements[12] = -matrixDeterminant(matrix3x3);
206 
207  /* Element (i, j) (2, 1) */
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];
217  result.elements[1] = -matrixDeterminant(matrix3x3);
218 
219  /* Element (i, j) (2, 2) */
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];
229  result.elements[5] = matrixDeterminant(matrix3x3);
230 
231  /* Element (i, j) (2, 3) */
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];
241  result.elements[9] = -matrixDeterminant(matrix3x3);
242 
243  /* Element (i, j) (2, 4) */
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];
253  result.elements[13] = matrixDeterminant(matrix3x3);
254 
255  /* Element (i, j) (3, 1) */
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];
265  result.elements[2] = matrixDeterminant(matrix3x3);
266 
267  /* Element (i, j) (3, 2) */
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];
277  result.elements[6] = -matrixDeterminant(matrix3x3);
278 
279  /* Element (i, j) (3, 3) */
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];
289  result.elements[10] = matrixDeterminant(matrix3x3);
290 
291  /* Element (i, j) (3, 4) */
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];
301  result.elements[14] = -matrixDeterminant(matrix3x3);
302 
303  /* Element (i, j) (4, 1) */
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];
313  result.elements[3] = -matrixDeterminant(matrix3x3);
314 
315  /* Element (i, j) (4, 2) */
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];
325  result.elements[7] = matrixDeterminant(matrix3x3);
326 
327  /* Element (i, j) (4, 3) */
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];
337  result.elements[11] = -matrixDeterminant(matrix3x3);
338 
339  /* Element (i, j) (4, 4) */
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];
349  result.elements[15] = matrixDeterminant(matrix3x3);
350 
351  /* The adjoint is the transpose of the cofactor matrix. */
352  matrixTranspose(&result);
353 
354  /* The inverse is the adjoint divided by the determinant. */
355  result = matrixScale(&result, 1.0f / matrixDeterminant(matrix));
356 
357  return result;
358  }
359 
360  Matrix Matrix::matrixScale(Matrix *matrix, float scale)
361  {
362  Matrix result;
363 
364  for(int allElements = 0; allElements < 16; allElements ++)
365  {
366  result.elements[allElements] = matrix->elements[allElements] * scale;
367  }
368 
369  return result;
370  }
371 
372  void Matrix::matrixTranspose(Matrix *matrix)
373  {
374  float temp;
375 
376  temp = matrix->elements[1];
377  matrix->elements[1] = matrix->elements[4];
378  matrix->elements[4] = temp;
379 
380  temp = matrix->elements[2];
381  matrix->elements[2] = matrix->elements[8];
382  matrix->elements[8] = temp;
383 
384  temp = matrix->elements[3];
385  matrix->elements[3] = matrix->elements[12];
386  matrix->elements[12] = temp;
387 
388  temp = matrix->elements[6];
389  matrix->elements[6] = matrix->elements[9];
390  matrix->elements[9] = temp;
391 
392  temp = matrix->elements[7];
393  matrix->elements[7] = matrix->elements[13];
394  matrix->elements[13] = temp;
395 
396  temp = matrix->elements[11];
397  matrix->elements[11] = matrix->elements[14];
398  matrix->elements[14] = temp;
399  }
400 
401  Matrix Matrix::createScaling(float x, float y, float z)
402  {
403  Matrix result = identityMatrix;
404 
405  result.elements[ 0] = x;
406  result.elements[ 5] = y;
407  result.elements[10] = z;
408 
409  return result;
410  }
411 
412  Matrix Matrix::createTranslation(float x, float y, float z)
413  {
414  Matrix result = identityMatrix;
415 
416  result.elements[12] = x;
417  result.elements[13] = y;
418  result.elements[14] = z;
419 
420  return result;
421  }
422 
423  Matrix Matrix::matrixPerspective(float FOV, float ratio, float zNear, float zFar)
424  {
425  Matrix result = identityMatrix;
426 
427  FOV = 1.0f / tan(FOV * 0.5f);
428 
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;
435 
436  return result;
437  }
438 
439  Matrix Matrix::matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up)
440  {
441  Matrix result = identityMatrix;
442 
443  Vec3f cameraX, cameraY;
444 
445  Vec3f cameraZ = {center.x - eye.x, center.y - eye.y, center.z - eye.z};
446  cameraZ.normalize();
447 
448  cameraX = Vec3f::cross(cameraZ, up);
449  cameraX.normalize();
450 
451  cameraY = Vec3f::cross(cameraX, cameraZ);
452 
453  /*
454  * The final cameraLookAt should look like:
455  *
456  * cameraLookAt[] = { cameraX.x, cameraY.x, -cameraZ.x, 0.0f,
457  * cameraX.y, cameraY.y, -cameraZ.y, 0.0f,
458  * cameraX.z, cameraY.z, -cameraZ.z, 0.0f,
459  * -eye.x, -eye.y, -eye.z, 1.0f };
460  */
461 
462  result[0] = cameraX.x;
463  result[1] = cameraY.x;
464  result[2] = -cameraZ.x;
465 
466  result[4] = cameraX.y;
467  result[5] = cameraY.y;
468  result[6] = -cameraZ.y;
469 
470  result[8] = cameraX.z;
471  result[9] = cameraY.z;
472  result[10] = -cameraZ.z;
473 
474  result[12] = -eye.x;
475  result[13] = -eye.y;
476  result[14] = -eye.z;
477 
478  return result;
479  }
480 
481  Matrix Matrix::matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
482  {
483  Matrix result = identityMatrix;
484 
485  result.elements[ 0] = 2.0f / (right - left);
486  result.elements[12] = -(right + left) / (right - left);
487 
488  result.elements[ 5] = 2.0f / (top - bottom);
489  result.elements[13] = -(top + bottom) / (top - bottom);
490 
491  result.elements[10] = -2.0f / (zFar - zNear);
492  result.elements[14] = -(zFar + zNear) / (zFar - zNear);
493 
494  return result;
495  }
496 
497  Matrix Matrix::createRotationX(float angle)
498  {
499  Matrix result = identityMatrix;
500 
501  result.elements[ 5] = cos(degreesToRadians(angle));
502  result.elements[ 9] = -sin(degreesToRadians(angle));
503  result.elements[ 6] = sin(degreesToRadians(angle));
504  result.elements[10] = cos(degreesToRadians(angle));
505 
506  return result;
507  }
508 
509  Matrix Matrix::createRotationY(float angle)
510  {
511  Matrix result = identityMatrix;
512 
513  result.elements[ 0] = cos(degreesToRadians(angle));
514  result.elements[ 8] = sin(degreesToRadians(angle));
515  result.elements[ 2] = -sin(degreesToRadians(angle));
516  result.elements[10] = cos(degreesToRadians(angle));
517 
518  return result;
519  }
520 
521  Matrix Matrix::createRotationZ(float angle)
522  {
523  Matrix result = identityMatrix;
524 
525  result.elements[0] = cos(degreesToRadians(angle));
526  result.elements[4] = -sin(degreesToRadians(angle));
527  result.elements[1] = sin(degreesToRadians(angle));
528  result.elements[5] = cos(degreesToRadians(angle));
529 
530  return result;
531  }
532 
533  Matrix Matrix::multiply(Matrix *left, Matrix *right)
534  {
535  Matrix result;
536 
537  for(int row = 0; row < 4; row ++)
538  {
539  for(int column = 0; column < 4; column ++)
540  {
541  /*result.elements[row * 4 + column] = left->elements[0 + row * 4] * right->elements[column + 0 * 4];
542  result.elements[row * 4 + column] += left->elements[1 + row * 4] * right->elements[column + 1 * 4];
543  result.elements[row * 4 + column] += left->elements[2 + row * 4] * right->elements[column + 2 * 4];
544  result.elements[row * 4 + column] += left->elements[3 + row * 4] * right->elements[column + 3 * 4];*/
545  float accumulator = 0.0f;
546  for(int allElements = 0; allElements < 4; allElements ++)
547  {
548  accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
549  }
550  result.elements[column * 4 + row] = accumulator;
551  }
552  }
553 
554  return result;
555  }
556 
557  Vec4f Matrix::vertexTransform(Vec4f *vertex, Matrix *matrix)
558  {
559  Vec4f result;
560 
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];
565 
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];
570 
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];
575 
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];
580 
581  return result;
582  }
583 
584  Vec3f Matrix::vertexTransform(Vec3f *vertex, Matrix *matrix)
585  {
586  Vec3f result;
587  Vec4f extendedVertex;
588 
589  extendedVertex.x = vertex->x;
590  extendedVertex.y = vertex->y;
591  extendedVertex.z = vertex->z;
592  extendedVertex.w = 1.0f;
593 
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];
598 
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];
603 
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];
608 
609  return result;
610  }
611 
612  void Matrix::print(void)
613  {
614  LOGI("\n");
615  for(int row = 0; row < 4; row ++)
616  {
617  for(int column = 0; column < 4; column ++)
618  {
619  LOGI("%.1f\t", elements[column * 4 + row]);
620  }
621  LOGI("\n");
622  }
623  LOGI("\n");
624  }
625 }
const float identityArray[16]
Definition: Matrix.cpp:34
void print(void)
Print the matrix.
Definition: Matrix.cpp:614
float & operator[](unsigned element)
Array operator for accessing the elements of the matrix.
Definition: Matrix.cpp:53
Matrix operator*(Matrix right)
Multiply operator to post multiply a matrix by another.
Definition: Matrix.cpp:63
#define LOGI(...)
Definition: AstcTextures.h:29
static Matrix identityMatrix
The identity matrix.
Definition: Matrix.h:90
static Vec3f cross(const Vec3f &vector1, const Vec3f &vector2)
Calculate cross product between two 3D floating point vectors.
Definition: VectorTypes.h:108
Matrix(void)
Default constructor.
Definition: Matrix.cpp:49
static const float identityArray[]
A 4x4 identity Matrix;.
Definition: Matrix.h:50
static Matrix matrixInvert(Matrix *matrix)
Get the inverse of a matrix.
Definition: Matrix.cpp:155
static Matrix createTranslation(float x, float y, float z)
Create and return a translation matrix.
Definition: Matrix.cpp:414
static Matrix matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
Create and return an orthographic projection matrix.
Definition: Matrix.cpp:483
static Matrix createScaling(float x, float y, float z)
Create and return a scaling matrix.
Definition: Matrix.cpp:403
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
Definition: Matrix.cpp:425
float angle
Definition: Native.cpp:158
float * getAsArray(void)
Get the matrix elements as a column major order array.
Definition: Matrix.cpp:78
GLfloat GLfloat f
Definition: gl2ext.h:2707
static Matrix matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a camera matrix.
Definition: Matrix.cpp:441
static Vec4f vertexTransform(Vec4f *vector, Matrix *matrix)
Transform a 4D vertex by a matrix.
Definition: Matrix.cpp:559
Matrix scale
Definition: RotoZoom.cpp:64
static Matrix createRotationY(float angle)
Create and return a rotation matrix around the y-axis matrix.
Definition: Matrix.cpp:511
static Matrix createRotationZ(float angle)
Create and return a rotation matrix around the z-axis matrix.
Definition: Matrix.cpp:523
static float matrixDeterminant(float *matrix)
Calculate determinant of supplied 3x3 matrix.
Definition: Matrix.cpp:83
float degreesToRadians(float degrees)
Convert an angle in degrees to radians.
Definition: Mathematics.h:86
static void matrixTranspose(Matrix *matrix)
Transpose a matrix in-place.
Definition: Matrix.cpp:374
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
static Matrix multiply(Matrix *left, Matrix *right)
Multiply 2 matrices to return a third.
Definition: Matrix.cpp:535
Matrix & operator=(const Matrix &another)
Overloading assingment operater to do deep copy of the Matrix elements.
Definition: Matrix.cpp:68
GLint left
Definition: gl2ext.h:2704
#define LOGE(...)
Definition: AstcTextures.h:30
float elements[16]
A 16 element floating point array used to represent a 4x4 matrix.
Definition: Matrix.h:38
static Matrix createRotationX(float angle)
Create and return a rotation matrix around the x-axis matrix.
Definition: Matrix.cpp:499
GLint y
Definition: gl2ext.h:179
static Matrix matrixScale(Matrix *matrix, float scale)
Scale each element in a matrix by a constant.
Definition: Matrix.cpp:362
GLint GLint bottom
Definition: gl2ext.h:2704