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