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 #include <cstring>
27 
28 namespace AstcTextures
29 {
30  /* Identity matrix. */
31  const float Matrix::identityArray[16] =
32  {
33  1.0f, 0.0f, 0.0f, 0.0f,
34  0.0f, 1.0f, 0.0f, 0.0f,
35  0.0f, 0.0f, 1.0f, 0.0f,
36  0.0f, 0.0f, 0.0f, 1.0f
37  };
38 
39  /* Please see header for specification. */
40  Matrix::Matrix(const float* array)
41  {
42  memcpy(elements, array, 16 * sizeof(float));
43  }
44 
45  /* Please see header for specification. */
47 
48  /* Please see header for specification. */
50 
51  /* Please see header for specification. */
52  float* Matrix::getAsArray(void)
53  {
54  return elements;
55  }
56 
57  /* Please see header for specification. */
58  float& Matrix::operator[](unsigned element)
59  {
60  if (element > 15)
61  {
62  LOGE("Matrix only has 16 elements, tried to access element %d", element);
63  exit(1);
64  }
65 
66  return elements[element];
67  }
68 
69  /* Please see header for specification. */
71  {
72  return multiply(this, &right);
73  }
74 
75  /* Please see header for specification. */
76  Matrix& Matrix::operator=(const Matrix &another)
77  {
78  if(this != &another)
79  {
80  memcpy(this->elements, another.elements, 16 * sizeof(float));
81  }
82 
83  return *this;
84  }
85 
86  /* Please see header for specification. */
87  Matrix Matrix::matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
88  {
89  Matrix result = identityMatrix;
90 
91  result.elements[0] = 2.0f / (right - left);
92  result.elements[12] = -(right + left) / (right - left);
93 
94  result.elements[5] = 2.0f / (top - bottom);
95  result.elements[13] = -(top + bottom) / (top - bottom);
96 
97  result.elements[10] = -2.0f / (zFar - zNear);
98  result.elements[14] = -(zFar + zNear) / (zFar - zNear);
99 
100  return result;
101  }
102 
103  /* Please see header for specification. */
104  Matrix Matrix::matrixPerspective(float FOV, float ratio, float zNear, float zFar)
105  {
106  Matrix result = identityMatrix;
107 
108  FOV = 1.0f / tan(FOV * 0.5f);
109 
110  result.elements[ 0] = FOV / ratio;
111  result.elements[ 5] = FOV;
112  result.elements[10] = -(zFar + zNear) / (zFar - zNear);
113  result.elements[11] = -1.0f;
114  result.elements[14] = (-2.0f * zFar * zNear) / (zFar - zNear);
115  result.elements[15] = 0.0f;
116 
117  return result;
118  }
119 
120  /* Please see header for specification. */
122  {
123  Matrix result = identityMatrix;
124  float angle_converted_to_radians = M_PI * angle / 180.0f;
125 
126  result.elements[5] = cosf(angle_converted_to_radians);
127  result.elements[9] = -sinf(angle_converted_to_radians);
128  result.elements[6] = sinf(angle_converted_to_radians);
129  result.elements[10] = cosf(angle_converted_to_radians);
130 
131  return result;
132  }
133 
134  /* Please see header for specification. */
136  {
137  Matrix result = identityMatrix;
138  float angle_converted_to_radians = M_PI * angle / 180.0f;
139 
140  result.elements[0] = cosf(angle_converted_to_radians);
141  result.elements[8] = sinf(angle_converted_to_radians);
142  result.elements[2] = -sinf(angle_converted_to_radians);
143  result.elements[10] = cosf(angle_converted_to_radians);
144 
145  return result;
146  }
147 
148  /* Please see header for specification. */
150  {
151  Matrix result = identityMatrix;
152  float angle_converted_to_radians = M_PI * angle / 180.0f;
153 
154  result.elements[0] = cosf(angle_converted_to_radians);
155  result.elements[4] = -sinf(angle_converted_to_radians);
156  result.elements[1] = sinf(angle_converted_to_radians);
157  result.elements[5] = cosf(angle_converted_to_radians);
158 
159  return result;
160  }
161 
162  /* Please see header for specification. */
164  {
165  Matrix result;
166 
167  for(int row = 0; row < 4; row++)
168  {
169  for(int column = 0; column < 4; column ++)
170  {
171  float accumulator = 0.0f;
172 
173  for(int allElements = 0; allElements < 4; allElements ++)
174  {
175  accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
176  }
177 
178  result.elements[column * 4 + row] = accumulator;
179  }
180  }
181 
182  return result;
183  }
184 }
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
Functions for manipulating matrices.
Definition: Matrix.h:29
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