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) 2012-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 "Matrix.h"
22 
23 #include "Mathematics.h"
24 #include "Platform.h"
25 
26 #include <cmath>
27 #include <cstring>
28 #include <cstdio>
29 #include <cstdlib>
30 
31 namespace MaliSDK
32 {
33  /* Identity matrix. */
34  const float identityArray[16] =
35  {
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,
40  };
41 
42  Matrix Matrix::identityMatrix = Matrix(identityArray);
43 
44  Matrix::Matrix(const float* array)
45  {
46  memcpy(elements, array, 16 * sizeof(float));
47  }
48 
50  {
51  }
52 
53  float& Matrix::operator[] (unsigned element)
54  {
55  if (element > 15)
56  {
57  LOGE("Matrix only has 16 elements, tried to access element %d", element);
58  exit(1);
59  }
60  return elements[element];
61  }
62 
64  {
65  return multiply(this, &right);
66  }
67 
68  Matrix& Matrix::operator= (const Matrix &another)
69  {
70  if(this != &another)
71  {
72  memcpy(this->elements, another.elements, 16 * sizeof(float));
73  }
74 
75  return *this;
76  }
77 
78  float* Matrix::getAsArray(void)
79  {
80  return elements;
81  }
82 
83  float Matrix::matrixDeterminant(float *matrix)
84  {
85  float result = 0.0f;
86 
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]);
90 
91  return result;
92  }
93 
95  {
96  float matrix3x3[9];
97  float determinant3x3 = 0.0f;
98  float result = 0.0f;
99 
100  /* Remove (i, j) (1, 1) to form new 3x3 matrix. */
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];
110  determinant3x3 = matrixDeterminant(matrix3x3);
111  result += matrix->elements[0] * determinant3x3;
112 
113  /* Remove (i, j) (1, 2) to form new 3x3 matrix. */
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];
123  determinant3x3 = matrixDeterminant(matrix3x3);
124  result -= matrix->elements[4] * determinant3x3;
125 
126  /* Remove (i, j) (1, 3) to form new 3x3 matrix. */
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];
136  determinant3x3 = matrixDeterminant(matrix3x3);
137  result += matrix->elements[8] * determinant3x3;
138 
139  /* Remove (i, j) (1, 4) to form new 3x3 matrix. */
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];
149  determinant3x3 = matrixDeterminant(matrix3x3);
150  result -= matrix->elements[12] * determinant3x3;
151 
152  return result;
153  }
154 
156  {
157  Matrix result;
158  float matrix3x3[9];
159 
160  /* Find the cofactor of each element. */
161  /* Element (i, j) (1, 1) */
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];
171  result.elements[0] = matrixDeterminant(matrix3x3);
172 
173  /* Element (i, j) (1, 2) */
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];
183  result.elements[4] = -matrixDeterminant(matrix3x3);
184 
185  /* Element (i, j) (1, 3) */
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];
195  result.elements[8] = matrixDeterminant(matrix3x3);
196 
197  /* Element (i, j) (1, 4) */
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];
207  result.elements[12] = -matrixDeterminant(matrix3x3);
208 
209  /* Element (i, j) (2, 1) */
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];
219  result.elements[1] = -matrixDeterminant(matrix3x3);
220 
221  /* Element (i, j) (2, 2) */
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];
231  result.elements[5] = matrixDeterminant(matrix3x3);
232 
233  /* Element (i, j) (2, 3) */
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];
243  result.elements[9] = -matrixDeterminant(matrix3x3);
244 
245  /* Element (i, j) (2, 4) */
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];
255  result.elements[13] = matrixDeterminant(matrix3x3);
256 
257  /* Element (i, j) (3, 1) */
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];
267  result.elements[2] = matrixDeterminant(matrix3x3);
268 
269  /* Element (i, j) (3, 2) */
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];
279  result.elements[6] = -matrixDeterminant(matrix3x3);
280 
281  /* Element (i, j) (3, 3) */
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];
291  result.elements[10] = matrixDeterminant(matrix3x3);
292 
293  /* Element (i, j) (3, 4) */
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];
303  result.elements[14] = -matrixDeterminant(matrix3x3);
304 
305  /* Element (i, j) (4, 1) */
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];
315  result.elements[3] = -matrixDeterminant(matrix3x3);
316 
317  /* Element (i, j) (4, 2) */
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];
327  result.elements[7] = matrixDeterminant(matrix3x3);
328 
329  /* Element (i, j) (4, 3) */
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];
339  result.elements[11] = -matrixDeterminant(matrix3x3);
340 
341  /* Element (i, j) (4, 4) */
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];
351  result.elements[15] = matrixDeterminant(matrix3x3);
352 
353  /* The adjoint is the transpose of the cofactor matrix. */
354  matrixTranspose(&result);
355 
356  /* The inverse is the adjoint divided by the determinant. */
357  result = matrixScale(&result, 1.0f / matrixDeterminant(matrix));
358 
359  return result;
360  }
361 
363  {
364  Matrix result;
365 
366  for(int allElements = 0; allElements < 16; allElements ++)
367  {
368  result.elements[allElements] = matrix->elements[allElements] * scale;
369  }
370 
371  return result;
372  }
373 
375  {
376  float temp;
377 
378  temp = matrix->elements[1];
379  matrix->elements[1] = matrix->elements[4];
380  matrix->elements[4] = temp;
381 
382  temp = matrix->elements[2];
383  matrix->elements[2] = matrix->elements[8];
384  matrix->elements[8] = temp;
385 
386  temp = matrix->elements[3];
387  matrix->elements[3] = matrix->elements[12];
388  matrix->elements[12] = temp;
389 
390  temp = matrix->elements[6];
391  matrix->elements[6] = matrix->elements[9];
392  matrix->elements[9] = temp;
393 
394  temp = matrix->elements[7];
395  matrix->elements[7] = matrix->elements[13];
396  matrix->elements[13] = temp;
397 
398  temp = matrix->elements[11];
399  matrix->elements[11] = matrix->elements[14];
400  matrix->elements[14] = temp;
401  }
402 
403  Matrix Matrix::createScaling(float x, float y, float z)
404  {
405  Matrix result = identityMatrix;
406 
407  result.elements[ 0] = x;
408  result.elements[ 5] = y;
409  result.elements[10] = z;
410 
411  return result;
412  }
413 
414  Matrix Matrix::createTranslation(float x, float y, float z)
415  {
416  Matrix result = identityMatrix;
417 
418  result.elements[12] = x;
419  result.elements[13] = y;
420  result.elements[14] = z;
421 
422  return result;
423  }
424 
425  Matrix Matrix::matrixPerspective(float FOV, float ratio, float zNear, float zFar)
426  {
427  Matrix result = identityMatrix;
428 
429  FOV = 1.0f / tan(FOV * 0.5f);
430 
431  result.elements[ 0] = FOV / ratio;
432  result.elements[ 5] = FOV;
433  result.elements[10] = -(zFar + zNear) / (zFar - zNear);
434  result.elements[11] = -1.0f;
435  result.elements[14] = (-2.0f * zFar * zNear) / (zFar - zNear);
436  result.elements[15] = 0.0f;
437 
438  return result;
439  }
440 
442  {
443  Matrix result = identityMatrix;
444 
445  Vec3f cameraX, cameraY;
446 
447  Vec3f cameraZ = {center.x - eye.x, center.y - eye.y, center.z - eye.z};
448  cameraZ.normalize();
449 
450  cameraX = Vec3f::cross(cameraZ, up);
451  cameraX.normalize();
452 
453  cameraY = Vec3f::cross(cameraX, cameraZ);
454 
455  /*
456  * The final cameraLookAt should look like:
457  *
458  * cameraLookAt[] = { cameraX.x, cameraY.x, -cameraZ.x, 0.0f,
459  * cameraX.y, cameraY.y, -cameraZ.y, 0.0f,
460  * cameraX.z, cameraY.z, -cameraZ.z, 0.0f,
461  * -eye.x, -eye.y, -eye.z, 1.0f };
462  */
463 
464  result[0] = cameraX.x;
465  result[1] = cameraY.x;
466  result[2] = -cameraZ.x;
467 
468  result[4] = cameraX.y;
469  result[5] = cameraY.y;
470  result[6] = -cameraZ.y;
471 
472  result[8] = cameraX.z;
473  result[9] = cameraY.z;
474  result[10] = -cameraZ.z;
475 
476  result[12] = -eye.x;
477  result[13] = -eye.y;
478  result[14] = -eye.z;
479 
480  return result;
481  }
482 
483  Matrix Matrix::matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
484  {
485  Matrix result = identityMatrix;
486 
487  result.elements[ 0] = 2.0f / (right - left);
488  result.elements[12] = -(right + left) / (right - left);
489 
490  result.elements[ 5] = 2.0f / (top - bottom);
491  result.elements[13] = -(top + bottom) / (top - bottom);
492 
493  result.elements[10] = -2.0f / (zFar - zNear);
494  result.elements[14] = -(zFar + zNear) / (zFar - zNear);
495 
496  return result;
497  }
498 
500  {
501  Matrix result = identityMatrix;
502 
503  result.elements[ 5] = cos(degreesToRadians(angle));
504  result.elements[ 9] = -sin(degreesToRadians(angle));
505  result.elements[ 6] = sin(degreesToRadians(angle));
506  result.elements[10] = cos(degreesToRadians(angle));
507 
508  return result;
509  }
510 
512  {
513  Matrix result = identityMatrix;
514 
515  result.elements[ 0] = cos(degreesToRadians(angle));
516  result.elements[ 8] = sin(degreesToRadians(angle));
517  result.elements[ 2] = -sin(degreesToRadians(angle));
518  result.elements[10] = cos(degreesToRadians(angle));
519 
520  return result;
521  }
522 
524  {
525  Matrix result = identityMatrix;
526 
527  result.elements[0] = cos(degreesToRadians(angle));
528  result.elements[4] = -sin(degreesToRadians(angle));
529  result.elements[1] = sin(degreesToRadians(angle));
530  result.elements[5] = cos(degreesToRadians(angle));
531 
532  return result;
533  }
534 
536  {
537  Matrix result;
538 
539  for(int row = 0; row < 4; row ++)
540  {
541  for(int column = 0; column < 4; column ++)
542  {
543  /*result.elements[row * 4 + column] = left->elements[0 + row * 4] * right->elements[column + 0 * 4];
544  result.elements[row * 4 + column] += left->elements[1 + row * 4] * right->elements[column + 1 * 4];
545  result.elements[row * 4 + column] += left->elements[2 + row * 4] * right->elements[column + 2 * 4];
546  result.elements[row * 4 + column] += left->elements[3 + row * 4] * right->elements[column + 3 * 4];*/
547  float accumulator = 0.0f;
548  for(int allElements = 0; allElements < 4; allElements ++)
549  {
550  accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
551  }
552  result.elements[column * 4 + row] = accumulator;
553  }
554  }
555 
556  return result;
557  }
558 
560  {
561  Vec4f result;
562 
563  result.x = vertex->x * matrix->elements[ 0];
564  result.x += vertex->y * matrix->elements[ 4];
565  result.x += vertex->z * matrix->elements[ 8];
566  result.x += vertex->w * matrix->elements[12];
567 
568  result.y = vertex->x * matrix->elements[ 1];
569  result.y += vertex->y * matrix->elements[ 5];
570  result.y += vertex->z * matrix->elements[ 9];
571  result.y += vertex->w * matrix->elements[13];
572 
573  result.z = vertex->x * matrix->elements[ 2];
574  result.z += vertex->y * matrix->elements[ 6];
575  result.z += vertex->z * matrix->elements[10];
576  result.z += vertex->w * matrix->elements[14];
577 
578  result.w = vertex->x * matrix->elements[ 3];
579  result.w += vertex->y * matrix->elements[ 7];
580  result.w += vertex->z * matrix->elements[11];
581  result.w += vertex->w * matrix->elements[15];
582 
583  return result;
584  }
585 
587  {
588  Vec3f result;
589  Vec4f extendedVertex;
590 
591  extendedVertex.x = vertex->x;
592  extendedVertex.y = vertex->y;
593  extendedVertex.z = vertex->z;
594  extendedVertex.w = 1.0f;
595 
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];
600 
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];
605 
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];
610 
611  return result;
612  }
613 
614  void Matrix::print(void)
615  {
616  LOGI("\n");
617  for(int row = 0; row < 4; row ++)
618  {
619  for(int column = 0; column < 4; column ++)
620  {
621  LOGI("%.1f\t", elements[column * 4 + row]);
622  }
623  LOGI("\n");
624  }
625  LOGI("\n");
626  }
627 }
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
Functions for manipulating matrices.
Definition: Matrix.h:31
Matrix(void)
Default constructor.
Definition: Matrix.cpp:49
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
A 3D floating point vector.
Definition: VectorTypes.h:83
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
A 4D floating point vector.
Definition: VectorTypes.h:127
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
void normalize(void)
Normalize 3D floating point vector.
Definition: VectorTypes.h:91
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