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 "AstcTextures.h"
22 #include "Matrix.h"
23 
24 #include <cmath>
25 #include <cstdlib>
26 
27 namespace AstcTextures
28 {
29  /* Identity matrix. */
30  const float Matrix::identityArray[16] =
31  {
32  1.0f, 0.0f, 0.0f, 0.0f,
33  0.0f, 1.0f, 0.0f, 0.0f,
34  0.0f, 0.0f, 1.0f, 0.0f,
35  0.0f, 0.0f, 0.0f, 1.0f
36  };
37 
38  /* Please see header for specification. */
39  Matrix::Matrix(const float* array)
40  {
41  memcpy(elements, array, 16 * sizeof(float));
42  }
43 
44  /* Please see header for specification. */
45  Matrix Matrix::identityMatrix = Matrix(identityArray);
46 
47  /* Please see header for specification. */
48  Matrix::Matrix(void){}
49 
50  /* Please see header for specification. */
51  float* Matrix::getAsArray(void)
52  {
53  return elements;
54  }
55 
56  /* Please see header for specification. */
57  float& Matrix::operator[](unsigned element)
58  {
59  if (element > 15)
60  {
61  LOGE("Matrix only has 16 elements, tried to access element %d", element);
62  exit(1);
63  }
64 
65  return elements[element];
66  }
67 
68  /* Please see header for specification. */
69  Matrix Matrix::operator*(Matrix right)
70  {
71  return multiply(this, &right);
72  }
73 
74  /* Please see header for specification. */
75  Matrix& Matrix::operator=(const Matrix &another)
76  {
77  if(this != &another)
78  {
79  memcpy(this->elements, another.elements, 16 * sizeof(float));
80  }
81 
82  return *this;
83  }
84 
85  /* Please see header for specification. */
86  Matrix Matrix::matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
87  {
88  Matrix result = identityMatrix;
89 
90  result.elements[0] = 2.0f / (right - left);
91  result.elements[12] = -(right + left) / (right - left);
92 
93  result.elements[5] = 2.0f / (top - bottom);
94  result.elements[13] = -(top + bottom) / (top - bottom);
95 
96  result.elements[10] = -2.0f / (zFar - zNear);
97  result.elements[14] = -(zFar + zNear) / (zFar - zNear);
98 
99  return result;
100  }
101 
102  /* Please see header for specification. */
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  /* Please see header for specification. */
120  Matrix Matrix::createRotationX(float angle)
121  {
122  Matrix result = identityMatrix;
123  float angle_converted_to_radians = M_PI * angle / 180.0f;
124 
125  result.elements[5] = cosf(angle_converted_to_radians);
126  result.elements[9] = -sinf(angle_converted_to_radians);
127  result.elements[6] = sinf(angle_converted_to_radians);
128  result.elements[10] = cosf(angle_converted_to_radians);
129 
130  return result;
131  }
132 
133  /* Please see header for specification. */
134  Matrix Matrix::createRotationY(float angle)
135  {
136  Matrix result = identityMatrix;
137  float angle_converted_to_radians = M_PI * angle / 180.0f;
138 
139  result.elements[0] = cosf(angle_converted_to_radians);
140  result.elements[8] = sinf(angle_converted_to_radians);
141  result.elements[2] = -sinf(angle_converted_to_radians);
142  result.elements[10] = cosf(angle_converted_to_radians);
143 
144  return result;
145  }
146 
147  /* Please see header for specification. */
148  Matrix Matrix::createRotationZ(float angle)
149  {
150  Matrix result = identityMatrix;
151  float angle_converted_to_radians = M_PI * angle / 180.0f;
152 
153  result.elements[0] = cosf(angle_converted_to_radians);
154  result.elements[4] = -sinf(angle_converted_to_radians);
155  result.elements[1] = sinf(angle_converted_to_radians);
156  result.elements[5] = cosf(angle_converted_to_radians);
157 
158  return result;
159  }
160 
161  /* Please see header for specification. */
162  Matrix Matrix::multiply(Matrix *left, Matrix *right)
163  {
164  Matrix result;
165 
166  for(int row = 0; row < 4; row++)
167  {
168  for(int column = 0; column < 4; column ++)
169  {
170  float accumulator = 0.0f;
171 
172  for(int allElements = 0; allElements < 4; allElements ++)
173  {
174  accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
175  }
176 
177  result.elements[column * 4 + row] = accumulator;
178  }
179  }
180 
181  return result;
182  }
183 }
const float identityArray[16]
Definition: Matrix.cpp:34
static Matrix identityMatrix
The identity matrix.
Definition: Matrix.h:99
static Matrix createRotationY(float angle)
Create and return a rotation matrix around the y-axis matrix.
Definition: Matrix.cpp:135
float angle
Definition: Native.cpp:158
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:87
float & operator[](unsigned element)
Array operator for accessing the elements of the matrix.
Definition: Matrix.cpp:58
static const float identityArray[]
A 4x4 identity Matrix;.
Definition: Matrix.h:51
GLfloat GLfloat f
Definition: gl2ext.h:2707
#define M_PI
The value of pi.
Definition: Mathematics.h:37
Matrix operator*(Matrix right)
Multiply operator to post multiply a matrix by another.
Definition: Matrix.cpp:70
static Matrix createRotationX(float angle)
Create and return a rotation matrix around the x-axis matrix.
Definition: Matrix.cpp:121
static Matrix multiply(Matrix *left, Matrix *right)
Multiply 2 matrices to return a third.
Definition: Matrix.cpp:163
float * getAsArray(void)
Get the matrix elements as a column major order array.
Definition: Matrix.cpp:52
Matrix & operator=(const Matrix &another)
Overloading assingment operater to do deep copy of the Matrix elements.
Definition: Matrix.cpp:76
GLint left
Definition: gl2ext.h:2704
#define LOGE(...)
Definition: AstcTextures.h:30
static Matrix createRotationZ(float angle)
Create and return a rotation matrix around the z-axis matrix.
Definition: Matrix.cpp:149
static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar)
Create and return a perspective projection matrix.
Definition: Matrix.cpp:104
Matrix(void)
Default constructor.
Definition: Matrix.cpp:49
float elements[16]
A 16 element floating point array used to represent a 4x4 matrix.
Definition: Matrix.h:36
GLint GLint bottom
Definition: gl2ext.h:2704