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) 2015-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 #ifndef PI
24 #define PI 3.141592653f
25 #endif
26 #include <math.h>
27 
28 struct vec2
29 {
30  float x;
31  float y;
32 
33  vec2() : x(0.0f), y(0.0f) { }
34  vec2(float X, float Y) : x(X), y(Y){ }
35  explicit vec2(float S) : x(S), y(S) { }
36  vec2 operator + (const vec2 &rhs) const { return vec2(x + rhs.x, y + rhs.y); }
37  vec2 operator * (const vec2 &rhs) const { return vec2(x * rhs.x, y * rhs.y); }
38  vec2 operator - (const vec2 &rhs) const { return vec2(x - rhs.x, y - rhs.y); }
39  vec2 operator * (const float s) const { return vec2(x * s, y * s); }
40  vec2 operator / (const float s) const { return vec2(x / s, y / s); }
41 
42  vec2 &operator *= (const float s) { *this = *this * s; return *this; }
43  vec2 &operator += (const vec2 &rhs) { *this = *this + rhs; return *this; }
44  vec2 &operator *= (const vec2 &rhs) { *this = *this * rhs; return *this; }
45  vec2 &operator -= (const vec2 &rhs) { *this = *this - rhs; return *this; }
46 
47  float &operator [] (unsigned int i) { return (&x)[i]; }
48  const float &operator [] (unsigned int i) const { return (&x)[i]; }
49 };
50 
51 struct vec3
52 {
53  float x;
54  float y;
55  float z;
56 
57  vec3() : x(0.0f), y(0.0f), z(0.0f) { }
58  vec3(float X, float Y, float Z) : x(X), y(Y), z(Z) { }
59  explicit vec3(float S) : x(S), y(S), z(S) { }
60  vec3 operator - () const { return vec3(-x, -y, -z); }
61  vec3 operator + (const vec3 &rhs) const { return vec3(x + rhs.x, y + rhs.y, z + rhs.z); }
62  vec3 operator * (const vec3 &rhs) const { return vec3(x * rhs.x, y * rhs.y, z * rhs.z); }
63  vec3 operator - (const vec3 &rhs) const { return vec3(x - rhs.x, y - rhs.y, z - rhs.z); }
64  vec3 operator * (const float s) const { return vec3(x * s, y * s, z * s); }
65  vec3 operator / (const float s) const { return vec3(x / s, y / s, z / s); }
66 
67  vec3 &operator += (const vec3 &rhs) { *this = *this + rhs; return *this; }
68  vec3 &operator *= (const vec3 &rhs) { *this = *this * rhs; return *this; }
69  vec3 &operator -= (const vec3 &rhs) { *this = *this - rhs; return *this; }
70 
71  float &operator [] (unsigned int i) { return (&x)[i]; }
72  const float &operator [] (unsigned int i) const { return (&x)[i]; }
73 };
74 
75 struct vec4
76 {
77  float x;
78  float y;
79  float z;
80  float w;
81 
82  vec4() : x(0.0f), y(0.0f), z(0.0f), w(0.0f) { }
83  vec4(vec3 V, float W) : x(V.x), y(V.y), z(V.z), w(W) { }
84  vec4(float X, float Y, float Z, float W) : x(X), y(Y), z(Z), w(W) { }
85  explicit vec4(float S) : x(S), y(S), z(S), w(S) { }
86  vec4 operator - () const { return vec4(-x, -y, -z, -w); }
87  vec4 operator + (const vec4 &rhs) const { return vec4(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w); }
88  vec4 operator * (const vec4 &rhs) const { return vec4(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w); }
89  vec4 operator - (const vec4 &rhs) const { return vec4(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w); }
90  vec4 operator * (const float s) const { return vec4(x * s, y * s, z * s, w * s); }
91  vec4 operator / (const float s) const { return vec4(x / s, y / s, z / s, w / s); }
92 
93  vec4 &operator *= (const float s) { *this = *this * s; return *this; }
94  vec4 &operator += (const vec4 &rhs) { *this = *this + rhs; return *this; }
95  vec4 &operator *= (const vec4 &rhs) { *this = *this * rhs; return *this; }
96  vec4 &operator -= (const vec4 &rhs) { *this = *this - rhs; return *this; }
97 
98  float &operator [] (unsigned int i) { return (&x)[i]; }
99  const float &operator [] (unsigned int i) const { return (&x)[i]; }
100 
101  vec3 xyz() const { return vec3(x, y, z); }
102 };
103 
104 struct mat4
105 {
106  vec4 x, y, z, w; // columns
107 
108  mat4() { }
109  explicit mat4(float s) : x(0.0f), y(0.0f), z(0.0f), w(0.0f)
110  {
111  x.x = s;
112  y.y = s;
113  z.z = s;
114  w.w = s;
115  }
116 
117  mat4 operator * (const mat4 &rhs)
118  {
119  mat4 m;
120  for (int lrow = 0; lrow < 4; ++lrow)
121  {
122  for (int rcol = 0; rcol < 4; ++rcol)
123  {
124  m[rcol][lrow] = 0.0f;
125  for (int k = 0; k < 4; ++k)
126  {
127  m[rcol][lrow] += (*this)[k][lrow] * rhs[rcol][k];
128  }
129  }
130  }
131  return m;
132  }
133 
134  mat4 operator * (const float s)
135  {
136  mat4 m = *this;
137  m.x *= s;
138  m.y *= s;
139  m.z *= s;
140  m.w *= s;
141  return m;
142  }
143 
144  vec4 operator * (const vec4 &rhs)
145  {
146  return x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w;
147  }
148 
149  vec4 &operator [] (unsigned int i) { return (&x)[i]; }
150  const vec4 &operator [] (unsigned int i) const { return (&x)[i]; }
151  const float *value_ptr() const { return &(x[0]); }
152  float *value_ptr() { return &(x[0]); }
153 };
154 
155 static vec3 normalize(const vec3 &v)
156 {
157  return v / sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
158 }
159 
160 static mat4 perspective(float fovy, float aspect, float z_near, float z_far)
161 {
162  mat4 m(1.0f);
163  float invtf = 1.0f / tan(fovy * 0.5f);
164  m[0].x = invtf / aspect;
165  m[1].y = invtf;
166  m[2].z = -(z_far + z_near) / (z_far - z_near);
167  m[2].w = -1.0f;
168  m[3].z = (-2.0f * z_far * z_near) / (z_far - z_near);
169  m[3].w = 0.0f;
170  return m;
171 }
172 
173 static mat4 orthographic(float left, float right, float bottom, float top, float z_near, float z_far)
174 {
175  mat4 m(1.0f);
176  m[0].x = 2.0f / (right - left);
177  m[3].x = -(right + left) / (right - left);
178  m[1].y = 2.0f / (top - bottom);
179  m[3].y = -(top + bottom) / (top - bottom);
180  m[2].z = -2.0f / (z_far - z_near);
181  m[3].z = -(z_far + z_near) / (z_far - z_near);
182  return m;
183 }
184 
185 static mat4 rotateX(float rad)
186 {
187  float co = cosf(rad); float si = sinf(rad);
188  mat4 m(1.0f);
189  m[1][1] = co; m[1][2] = -si; m[2][1] = si; m[2][2] = co;
190  return m;
191 }
192 
193 static mat4 rotateY(float rad)
194 {
195  float co = cosf(rad); float si = sinf(rad);
196  mat4 m(1.0f);
197  m[0][0] = co; m[0][2] = si; m[2][0] = -si; m[2][2] = co;
198  return m;
199 }
200 
201 static mat4 rotateZ(float rad)
202 {
203  float co = cosf(rad); float si = sinf(rad);
204  mat4 m(1.0f);
205  m[0][0] = co; m[1][0] = -si; m[0][1] = si; m[1][1] = co;
206  return m;
207 }
208 
209 static mat4 translate(float x, float y, float z)
210 {
211  mat4 m(1.0f);
212  m[3][0] = x; m[3][1] = y; m[3][2] = z; m[3][3] = 1.0f;
213  return m;
214 }
215 
216 static mat4 translate(const vec3 &v)
217 {
218  mat4 m(1.0f);
219  m[3][0] = v.x; m[3][1] = v.y; m[3][2] = v.z;
220  return m;
221 }
222 
223 static mat4 scale(float x, float y, float z)
224 {
225  mat4 m(1.0f);
226  m[0][0] = x; m[1][1] = y; m[2][2] = z;
227  return m;
228 }
229 
230 static mat4 scale(float s)
231 {
232  return scale(s, s, s);
233 }
234 
235 #endif
const GLfloat * v
Definition: gl2ext.h:2231
vec3()
Definition: matrix.h:57
vec4 operator+(const vec4 &rhs) const
Definition: matrix.h:87
vec2()
Definition: matrix.h:33
vec2 & operator-=(const vec2 &rhs)
Definition: matrix.h:45
const float z_far
vec3 & operator+=(const vec3 &rhs)
Definition: matrix.h:67
vec3 xyz() const
Definition: matrix.h:101
vec4 & operator-=(const vec4 &rhs)
Definition: matrix.h:96
Definition: matrix.h:51
vec2 operator*(const vec2 &rhs) const
Definition: matrix.h:37
static mat4 rotateY(float rad)
Definition: matrix.h:193
Definition: matrix.h:28
vec2 & operator+=(const vec2 &rhs)
Definition: matrix.h:43
vec2(float X, float Y)
Definition: matrix.h:34
float & operator[](unsigned int i)
Definition: matrix.h:47
vec4 y
Definition: matrix.h:106
vec2 & operator*=(const float s)
Definition: matrix.h:42
static mat4 perspective(float fovy, float aspect, float z_near, float z_far)
Definition: matrix.h:160
vec2 operator-(const vec2 &rhs) const
Definition: matrix.h:38
vec3 & operator*=(const vec3 &rhs)
Definition: matrix.h:68
vec3 operator*(const vec3 &rhs) const
Definition: matrix.h:62
float z
Definition: matrix.h:55
vec4 operator/(const float s) const
Definition: matrix.h:91
vec4 & operator[](unsigned int i)
Definition: matrix.h:148
Definition: matrix.h:75
const float * value_ptr() const
Definition: matrix.h:151
vec4(vec3 V, float W)
Definition: matrix.h:83
vec3 & operator-=(const vec3 &rhs)
Definition: matrix.h:69
vec3(float S)
Definition: matrix.h:59
GLfloat GLfloat GLfloat w
Definition: gl2ext.h:2701
static mat4 rotateZ(float rad)
Definition: matrix.h:201
float & operator[](unsigned int i)
Definition: matrix.h:98
GLfloat GLfloat f
Definition: gl2ext.h:2707
static mat4 orthographic(float left, float right, float bottom, float top, float z_near, float z_far)
Definition: matrix.h:173
float y
Definition: matrix.h:31
float w
Definition: matrix.h:80
vec4 operator-() const
Definition: matrix.h:86
float x
Definition: matrix.h:53
static vec3 normalize(const vec3 &v)
Definition: matrix.h:155
vec3 operator+(const vec3 &rhs) const
Definition: matrix.h:61
float y
Definition: matrix.h:54
vec4 & operator*=(const float s)
Definition: matrix.h:93
const float z_near
mat4()
Definition: matrix.h:108
vec4 & operator+=(const vec4 &rhs)
Definition: matrix.h:94
static mat4 rotateX(float rad)
Definition: matrix.h:185
vec4 w
Definition: matrix.h:106
GLint GLint GLint GLint GLint x
Definition: gl2ext.h:574
vec2 operator+(const vec2 &rhs) const
Definition: matrix.h:36
Definition: matrix.h:104
vec4 operator*(const vec4 &rhs) const
Definition: matrix.h:88
GLint left
Definition: gl2ext.h:2704
float z
Definition: matrix.h:79
vec3 operator-() const
Definition: matrix.h:60
vec4 z
Definition: matrix.h:106
float x
Definition: matrix.h:30
const GLfloat * m
Definition: gl2ext.h:2508
vec2(float S)
Definition: matrix.h:35
vec4(float S)
Definition: matrix.h:85
vec4 x
Definition: matrix.h:106
float x
Definition: matrix.h:77
static mat4 translate(float x, float y, float z)
Definition: matrix.h:209
static mat4 scale(float x, float y, float z)
Definition: matrix.h:223
float y
Definition: matrix.h:78
GLint y
Definition: gl2ext.h:179
vec3 operator/(const float s) const
Definition: matrix.h:65
mat4 operator*(const mat4 &rhs)
Definition: matrix.h:117
float * value_ptr()
Definition: matrix.h:152
GLint GLint bottom
Definition: gl2ext.h:2704
float & operator[](unsigned int i)
Definition: matrix.h:71
vec4()
Definition: matrix.h:82
mat4(float s)
Definition: matrix.h:109
vec3(float X, float Y, float Z)
Definition: matrix.h:58
vec4(float X, float Y, float Z, float W)
Definition: matrix.h:84
vec2 operator/(const float s) const
Definition: matrix.h:40