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 #include <cmath>
23 
24 void matrixIdentityFunction(float* matrix)
25 {
26  if(matrix == NULL)
27  {
28  return;
29  }
30 
31  matrix[0] = 1.0f;
32  matrix[1] = 0.0f;
33  matrix[2] = 0.0f;
34  matrix[3] = 0.0f;
35  matrix[4] = 0.0f;
36  matrix[5] = 1.0f;
37  matrix[6] = 0.0f;
38  matrix[7] = 0.0f;
39  matrix[8] = 0.0f;
40  matrix[9] = 0.0f;
41  matrix[10] = 1.0f;
42  matrix[11] = 0.0f;
43  matrix[12] = 0.0f;
44  matrix[13] = 0.0f;
45  matrix[14] = 0.0f;
46  matrix[15] = 1.0f;
47 }
48 
49 void matrixTranslate(float* matrix, float x, float y, float z)
50 {
51  float temporaryMatrix[16];
52  matrixIdentityFunction(temporaryMatrix);
53  temporaryMatrix[12] = x;
54  temporaryMatrix[13] = y;
55  temporaryMatrix[14] = z;
56  matrixMultiply(matrix,temporaryMatrix,matrix);
57 }
58 
59 void matrixMultiply(float* destination, float* operand1, float* operand2)
60 {
61  float theResult[16];
62  int row, column = 0;
63  int i,j = 0;
64  for(i = 0; i < 4; i++)
65  {
66  for(j = 0; j < 4; j++)
67  {
68  theResult[4 * i + j] = operand1[j] * operand2[4 * i] + operand1[4 + j] * operand2[4 * i + 1] +
69  operand1[8 + j] * operand2[4 * i + 2] + operand1[12 + j] * operand2[4 * i + 3];
70  }
71  }
72 
73  for(int i = 0; i < 16; i++)
74  {
75  destination[i] = theResult[i];
76  }
77 }
78 
79 void matrixFrustum(float* matrix, float left, float right, float bottom, float top, float zNear, float zFar)
80 {
81  float temp, xDistance, yDistance, zDistance;
82  temp = 2.0 *zNear;
83  xDistance = right - left;
84  yDistance = top - bottom;
85  zDistance = zFar - zNear;
86  matrixIdentityFunction(matrix);
87  matrix[0] = temp / xDistance;
88  matrix[5] = temp / yDistance;
89  matrix[8] = (right + left) / xDistance;
90  matrix[9] = (top + bottom) / yDistance;
91  matrix[10] = (-zFar - zNear) / zDistance;
92  matrix[11] = -1.0f;
93  matrix[14] = (-temp * zFar) / zDistance;
94  matrix[15] = 0.0f;
95 }
96 
97 void matrixPerspective(float* matrix, float fieldOfView, float aspectRatio, float zNear, float zFar)
98 {
99  float ymax, xmax;
100  ymax = zNear * tanf(fieldOfView * M_PI / 360.0);
101  xmax = ymax * aspectRatio;
102  matrixFrustum(matrix, -xmax, xmax, -ymax, ymax, zNear, zFar);
103 }
104 
105 void matrixRotateX(float* matrix, float angle)
106 {
107  float tempMatrix[16];
108  matrixIdentityFunction(tempMatrix);
109 
110  tempMatrix[5] = cos(matrixDegreesToRadians(angle));
111  tempMatrix[9] = -sin(matrixDegreesToRadians(angle));
112  tempMatrix[6] = sin(matrixDegreesToRadians(angle));
113  tempMatrix[10] = cos(matrixDegreesToRadians(angle));
114  matrixMultiply(matrix, tempMatrix, matrix);
115 }
116 
117 void matrixRotateY(float *matrix, float angle)
118 {
119  float tempMatrix[16];
120  matrixIdentityFunction(tempMatrix);
121 
122  tempMatrix[0] = cos(matrixDegreesToRadians(angle));
123  tempMatrix[8] = sin(matrixDegreesToRadians(angle));
124  tempMatrix[2] = -sin(matrixDegreesToRadians(angle));
125  tempMatrix[10] = cos(matrixDegreesToRadians(angle));
126  matrixMultiply(matrix, tempMatrix, matrix);
127 }
128 
129 void matrixRotateZ(float *matrix, float angle)
130 {
131  float tempMatrix[16];
132  matrixIdentityFunction(tempMatrix);
133 
134  tempMatrix[0] = cos(matrixDegreesToRadians(angle));
135  tempMatrix[4] = -sin(matrixDegreesToRadians(angle));
136  tempMatrix[1] = sin(matrixDegreesToRadians(angle));
137  tempMatrix[5] = cos(matrixDegreesToRadians(angle));
138  matrixMultiply(matrix, tempMatrix, matrix);
139 }
140 
141 void matrixScale(float* matrix, float x, float y, float z)
142 {
143  float tempMatrix[16];
144  matrixIdentityFunction(tempMatrix);
145 
146  tempMatrix[0] = x;
147  tempMatrix[5] = y;
148  tempMatrix[10] = z;
149  matrixMultiply(matrix, tempMatrix, matrix);
150 }
151 
152 float matrixDegreesToRadians(float degrees)
153 {
154  return M_PI * degrees / 180.0f;
155 }
void matrixMultiply(float *destination, float *operand1, float *operand2)
Takes 2 matrices and multiplies them together. Then stores the result in a third matrix.
Definition: Matrix.cpp:58
void matrixScale(float *matrix, float x, float y, float z)
Scales a matrix by a given factor in the x, y and z axis.
Definition: Matrix.cpp:140
float matrixDegreesToRadians(float degrees)
Function to convert degrees into Radians.
Definition: Matrix.cpp:152
void matrixIdentityFunction(float *matrix)
Takes a 4 * 4 and sets the elements to the Identity function.
Definition: Matrix.cpp:23
void matrixPerspective(float *matrix, float fieldOfView, float aspectRatio, float zNear, float zFar)
Create a perspective projection matrix and store the results in the first parameter.
Definition: Matrix.cpp:96
float angle
Definition: Native.cpp:158
void matrixRotateX(float *matrix, float angle)
Rotates a matrix around the x axis by a given angle.
Definition: Matrix.cpp:104
#define M_PI
The value of pi.
Definition: Mathematics.h:37
void matrixTranslate(float *matrix, float x, float y, float z)
Takes in a 4 * 4 matrix and translates it by the vector defined by x y and z.
Definition: Matrix.cpp:48
void matrixRotateZ(float *matrix, float angle)
Rotates a matrix around the Z axis by a given angle.
Definition: Matrix.cpp:128
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
GLint left
Definition: gl2ext.h:2704
void matrixFrustum(float *matrix, float left, float right, float bottom, float top, float zNear, float zFar)
Create a viewing frustum and store the result in the first parameter. This is usually called by matri...
Definition: Matrix.cpp:78
void matrixRotateY(float *matrix, float angle)
Rotates a matrix around the y axis by a given angle.
Definition: Matrix.cpp:116
GLint y
Definition: gl2ext.h:179
GLint GLint bottom
Definition: gl2ext.h:2704