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) 2013-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 /* [matrixIdentity] */
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 /* [matrixIdentity] */
49 /* [matrixTranslate] */
50 void matrixTranslate(float* matrix, float x, float y, float z)
51 {
52  float temporaryMatrix[16];
53  matrixIdentityFunction(temporaryMatrix);
54  temporaryMatrix[12] = x;
55  temporaryMatrix[13] = y;
56  temporaryMatrix[14] = z;
57  matrixMultiply(matrix,temporaryMatrix,matrix);
58 }
59 /* [matrixTranslate] */
60 
61 /* [matrixMultiply] */
62 void matrixMultiply(float* destination, float* operand1, float* operand2)
63 {
64  float theResult[16];
65  int row, column = 0;
66  int i,j = 0;
67  for(i = 0; i < 4; i++)
68  {
69  for(j = 0; j < 4; j++)
70  {
71  theResult[4 * i + j] = operand1[j] * operand2[4 * i] + operand1[4 + j] * operand2[4 * i + 1] +
72  operand1[8 + j] * operand2[4 * i + 2] + operand1[12 + j] * operand2[4 * i + 3];
73  }
74  }
75 
76  for(int i = 0; i < 16; i++)
77  {
78  destination[i] = theResult[i];
79  }
80 }
81 /* [matrixMultiply] */
82 
83 /* [matrixFrustum] */
84 void matrixFrustum(float* matrix, float left, float right, float bottom, float top, float zNear, float zFar)
85 {
86  float temp, xDistance, yDistance, zDistance;
87  temp = 2.0 *zNear;
88  xDistance = right - left;
89  yDistance = top - bottom;
90  zDistance = zFar - zNear;
91  matrixIdentityFunction(matrix);
92  matrix[0] = temp / xDistance;
93  matrix[5] = temp / yDistance;
94  matrix[8] = (right + left) / xDistance;
95  matrix[9] = (top + bottom) / yDistance;
96  matrix[10] = (-zFar - zNear) / zDistance;
97  matrix[11] = -1.0f;
98  matrix[14] = (-temp * zFar) / zDistance;
99  matrix[15] = 0.0f;
100 }
101 /* [matrixFrustum] */
102 
103 /* [matrixPerspective] */
104 void matrixPerspective(float* matrix, float fieldOfView, float aspectRatio, float zNear, float zFar)
105 {
106  float ymax, xmax;
107  ymax = zNear * tanf(fieldOfView * M_PI / 360.0);
108  xmax = ymax * aspectRatio;
109  matrixFrustum(matrix, -xmax, xmax, -ymax, ymax, zNear, zFar);
110 }
111 /* [matrixPerspective] */
112 
113 /* [matrixRotate] */
114 void matrixRotateX(float* matrix, float angle)
115 {
116  float tempMatrix[16];
117  matrixIdentityFunction(tempMatrix);
118 
119  tempMatrix[5] = cos(matrixDegreesToRadians(angle));
120  tempMatrix[9] = -sin(matrixDegreesToRadians(angle));
121  tempMatrix[6] = sin(matrixDegreesToRadians(angle));
122  tempMatrix[10] = cos(matrixDegreesToRadians(angle));
123  matrixMultiply(matrix, tempMatrix, matrix);
124 }
125 
126 void matrixRotateY(float *matrix, float angle)
127 {
128  float tempMatrix[16];
129  matrixIdentityFunction(tempMatrix);
130 
131  tempMatrix[0] = cos(matrixDegreesToRadians(angle));
132  tempMatrix[8] = sin(matrixDegreesToRadians(angle));
133  tempMatrix[2] = -sin(matrixDegreesToRadians(angle));
134  tempMatrix[10] = cos(matrixDegreesToRadians(angle));
135  matrixMultiply(matrix, tempMatrix, matrix);
136 }
137 
138 void matrixRotateZ(float *matrix, float angle)
139 {
140  float tempMatrix[16];
141  matrixIdentityFunction(tempMatrix);
142 
143  tempMatrix[0] = cos(matrixDegreesToRadians(angle));
144  tempMatrix[4] = -sin(matrixDegreesToRadians(angle));
145  tempMatrix[1] = sin(matrixDegreesToRadians(angle));
146  tempMatrix[5] = cos(matrixDegreesToRadians(angle));
147  matrixMultiply(matrix, tempMatrix, matrix);
148 }
149 /* [matrixRotate] */
150 
151 /* [matrixScale] */
152 void matrixScale(float* matrix, float x, float y, float z)
153 {
154  float tempMatrix[16];
155  matrixIdentityFunction(tempMatrix);
156 
157  tempMatrix[0] = x;
158  tempMatrix[5] = y;
159  tempMatrix[10] = z;
160  matrixMultiply(matrix, tempMatrix, matrix);
161 }
162 /* [matrixScale] */
163 
164 float matrixDegreesToRadians(float degrees)
165 {
166  return M_PI * degrees / 180.0f;
167 }
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