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 namespace MaliSDK
24 {
25  /* Identity matrix. */
26  const float Matrix::identityArray[16] =
27  {
28  1.0f, 0.0f, 0.0f, 0.0f,
29  0.0f, 1.0f, 0.0f, 0.0f,
30  0.0f, 0.0f, 1.0f, 0.0f,
31  0.0f, 0.0f, 0.0f, 1.0f,
32  };
33 
34  Matrix Matrix::identityMatrix = Matrix(identityArray);
35 
37  Matrix::Matrix(const float* array)
38  {
39  memcpy(elements, array, 16 * sizeof(float));
40  }
41 
43  Matrix::Matrix(void)
44  {
45  }
46 
48  float& Matrix::operator[] (unsigned element)
49  {
50  if (element > 15)
51  {
52  exit(1);
53  }
54  return elements[element];
55  }
56 
58  Matrix Matrix::operator* (Matrix right)
59  {
60  return multiply(this, &right);
61  }
62 
64  Matrix& Matrix::operator= (const Matrix &another)
65  {
66  if(this != &another)
67  {
68  memcpy(this->elements, another.elements, 16 * sizeof(float));
69  }
70 
71  return *this;
72  }
73 
74  float* Matrix::getAsArray(void)
75  {
76  return elements;
77  }
78 
80  Matrix Matrix::matrixPerspective(float FOV, float ratio, float zNear, float zFar)
81  {
82  Matrix result = identityMatrix;
83 
84  FOV = 1.0f / tan(FOV * 0.5f);
85 
86  result.elements[ 0] = FOV / ratio;
87  result.elements[ 5] = FOV;
88  result.elements[10] = -(zFar + zNear) / (zFar - zNear);
89  result.elements[11] = -1.0f;
90  result.elements[14] = (-2.0f * zFar * zNear) / (zFar - zNear);
91  result.elements[15] = 0.0f;
92 
93  return result;
94  }
95 
97  Matrix Matrix::matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up)
98  {
99  Matrix result = identityMatrix;
100 
101  Vec3f cameraX, cameraY;
102 
103  Vec3f cameraZ = {center.x - eye.x, center.y - eye.y, center.z - eye.z};
104  cameraZ.normalize();
105 
106  cameraX = Vec3f::cross(cameraZ, up);
107  cameraX.normalize();
108 
109  cameraY = Vec3f::cross(cameraX, cameraZ);
110 
111  /*
112  * The final cameraLookAt should look like:
113  *
114  * cameraLookAt[] = { cameraX.x, cameraY.x, -cameraZ.x, 0.0f,
115  * cameraX.y, cameraY.y, -cameraZ.y, 0.0f,
116  * cameraX.z, cameraY.z, -cameraZ.z, 0.0f,
117  * -eye.x, -eye.y, -eye.z, 1.0f };
118  */
119 
120  result[0] = cameraX.x;
121  result[1] = cameraY.x;
122  result[2] = -cameraZ.x;
123 
124  result[4] = cameraX.y;
125  result[5] = cameraY.y;
126  result[6] = -cameraZ.y;
127 
128  result[8] = cameraX.z;
129  result[9] = cameraY.z;
130  result[10] = -cameraZ.z;
131 
132  result[12] = -eye.x;
133  result[13] = -eye.y;
134  result[14] = -eye.z;
135 
136  return result;
137  }
138 
140  Matrix Matrix::multiply(Matrix *left, Matrix *right)
141  {
142  Matrix result;
143 
144  for(int row = 0; row < 4; row ++)
145  {
146  for(int column = 0; column < 4; column ++)
147  {
148  float accumulator = 0.0f;
149 
150  for(int allElements = 0; allElements < 4; allElements ++)
151  {
152  accumulator += left->elements[allElements * 4 + row] * right->elements[column * 4 + allElements];
153  }
154 
155  result.elements[column * 4 + row] = accumulator;
156  }
157  }
158 
159  return result;
160  }
161 }
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
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 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
static Matrix matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a camera matrix.
Definition: Matrix.cpp:441
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