OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Matrix.h
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 #ifndef MATRIX_H
22 #define MATRIX_H
23 
24 #include <cmath>
25 #include <cstring>
26 #include <cstdio>
27 #include <cstdlib>
28 
29 #ifndef M_PI
30 
33 #define M_PI 3.14159265358979323846f
34 #endif /* M_PI */
35 
36 namespace MaliSDK
37 {
42  inline float degreesToRadians(float degrees)
43  {
44  return M_PI * degrees / 180.0f;
45  }
46 
52  class Vec3f
53  {
54  public:
55  float x, y, z;
56 
60  void normalize(void)
61  {
62  float length = sqrt(x * x + y * y + z * z);
63 
64  x /= length;
65  y /= length;
66  z /= length;
67  }
68 
77  static Vec3f cross(const Vec3f& vector1, const Vec3f& vector2)
78  {
79  /* Floating point vector to be returned. */
80  Vec3f crossProduct;
81 
82  crossProduct.x = (vector1.y * vector2.z) - (vector1.z * vector2.y);
83  crossProduct.y = (vector1.z * vector2.x) - (vector1.x * vector2.z);
84  crossProduct.z = (vector1.x * vector2.y) - (vector1.y * vector2.x);
85 
86  return crossProduct;
87  }
88  };
89 
90 
96  class Vec4f
97  {
98  public:
99  float x, y, z, w;
100 
104  void normalize(void)
105  {
106  float length = sqrt(x * x + y * y + z * z + w * w);
107 
108  x /= length;
109  y /= length;
110  z /= length;
111  w /= length;
112  }
113  };
114 
118  class Matrix
119  {
120  private:
125  float elements[16];
126 
136  static Matrix multiply(Matrix *left, Matrix *right);
137 
141  static const float identityArray[];
142  public:
148  float* getAsArray(void);
149 
153  Matrix(void);
154 
162  float& operator[] (unsigned element);
163 
171  Matrix operator* (Matrix right);
172 
176  Matrix& operator=(const Matrix &another);
177 
183  Matrix(const float* array);
184 
190  static Matrix identityMatrix;
191 
203  static Matrix matrixPerspective(float FOV, float ratio, float zNear, float zFar);
204 
214  static Matrix matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up);
215  };
216 }
217 #endif /* MATRIX_H */
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: Matrix.h:77
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
A 3D floating point vector.
Definition: VectorTypes.h:83
GLfloat GLfloat GLfloat w
Definition: gl2ext.h:2701
static Matrix matrixCameraLookAt(Vec3f eye, Vec3f center, Vec3f up)
Create and return a camera matrix.
Definition: Matrix.cpp:441
#define M_PI
PI value approximation.
Definition: Matrix.h:33
void normalize(void)
Normalize 4D floating point vector.
Definition: Matrix.h:104
float degreesToRadians(float degrees)
Convert an angle in degrees to radians.
Definition: Mathematics.h:86
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
GLenum GLuint GLenum GLsizei length
Definition: gl2ext.h:134
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
void normalize(void)
Normalize 3D floating point vector.
Definition: Matrix.h:60