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 "Matrix.h"
22 
23 #include <cmath>
24 #include <cstring>
25 #include <cstdio>
26 #include <cstdlib>
27 
28 #include <android/log.h>
29 
30 namespace MaliSDK
31 {
32  /* Identity matrix. */
33  const float Matrix::identityArray[16] =
34  {
35  1.0f, 0.0f, 0.0f, 0.0f,
36  0.0f, 1.0f, 0.0f, 0.0f,
37  0.0f, 0.0f, 1.0f, 0.0f,
38  0.0f, 0.0f, 0.0f, 1.0f,
39  };
40 
41  Matrix Matrix::identityMatrix = Matrix(identityArray);
42 
43  Matrix::Matrix(const float* array)
44  {
45  memcpy(elements, array, 16 * sizeof(float));
46  }
47 
48  Matrix::Matrix(void)
49  {
50  }
51 
52  float& Matrix::operator[] (unsigned element)
53  {
54  if (element > 15)
55  {
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  Matrix Matrix::createScaling(float x, float y, float z)
82  {
83  Matrix result = identityMatrix;
84 
85  result.elements[ 0] = x;
86  result.elements[ 5] = y;
87  result.elements[10] = z;
88 
89  return result;
90  }
91 
92  Matrix Matrix::createTranslation(float x, float y, float z)
93  {
94  Matrix result = identityMatrix;
95 
96  result.elements[12] = x;
97  result.elements[13] = y;
98  result.elements[14] = z;
99 
100  return result;
101  }
102 
103  Matrix Matrix::matrixPerspective(float FOV, float ratio, float zNear, float zFar)
104  {
105  Matrix result = identityMatrix;
106 
107  FOV = 1.0f / tan(FOV * 0.5f);
108 
109  result.elements[ 0] = FOV / ratio;
110  result.elements[ 5] = FOV;
111  result.elements[10] = -(zFar + zNear) / (zFar - zNear);
112  result.elements[11] = -1.0f;
113  result.elements[14] = (-2.0f * zFar * zNear) / (zFar - zNear);
114  result.elements[15] = 0.0f;
115 
116  return result;
117  }
118 
119 
120  Matrix Matrix::multiply(Matrix *left, Matrix *right)
121  {
122  Matrix result;
123 
124  for(int row = 0; row < 4; row ++)
125  {
126  for(int column = 0; column < 4; column ++)
127  {
128  float accumulator = 0.0f;
129  for(int allElements = 0; allElements < 4; allElements ++)
130  {
131  accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
132  }
133  result.elements[column * 4 + row] = accumulator;
134  }
135  }
136 
137  return result;
138  }
139 }
const float identityArray[16]
Definition: Matrix.cpp:34
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
static Matrix identityMatrix
The identity matrix.
Definition: Matrix.h:90
Matrix(void)
Default constructor.
Definition: Matrix.cpp:49
static const float identityArray[]
A 4x4 identity Matrix;.
Definition: Matrix.h:50
static Matrix createTranslation(float x, float y, float z)
Create and return a translation matrix.
Definition: Matrix.cpp:414
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 * getAsArray(void)
Get the matrix elements as a column major order array.
Definition: Matrix.cpp:78
GLfloat GLfloat f
Definition: gl2ext.h:2707
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
float elements[16]
A 16 element floating point array used to represent a 4x4 matrix.
Definition: Matrix.h:38
GLint y
Definition: gl2ext.h:179