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 transpose(const mat4 &m)
161 {
162  vec4 a = m.x;
163  vec4 b = m.y;
164  vec4 c = m.z;
165  vec4 d = m.w;
166  mat4 result;
167  result.x = vec4(a.x, b.x, c.x, d.x);
168  result.y = vec4(a.y, b.y, c.y, d.y);
169  result.z = vec4(a.z, b.z, c.z, d.z);
170  result.w = vec4(a.w, b.w, c.w, d.w);
171  return result;
172 }
173 
174 static mat4 perspective(float fovy, float aspect, float z_near, float z_far)
175 {
176  mat4 m(1.0f);
177  float invtf = 1.0f / tan(fovy * 0.5f);
178  m[0].x = invtf / aspect;
179  m[1].y = invtf;
180  m[2].z = -(z_far + z_near) / (z_far - z_near);
181  m[2].w = -1.0f;
182  m[3].z = (-2.0f * z_far * z_near) / (z_far - z_near);
183  m[3].w = 0.0f;
184  return m;
185 }
186 
187 static mat4 orthographic(float left, float right, float bottom, float top, float z_near, float z_far)
188 {
189  mat4 m(1.0f);
190  m[0].x = 2.0f / (right - left);
191  m[3].x = -(right + left) / (right - left);
192  m[1].y = 2.0f / (top - bottom);
193  m[3].y = -(top + bottom) / (top - bottom);
194  m[2].z = -2.0f / (z_far - z_near);
195  m[3].z = -(z_far + z_near) / (z_far - z_near);
196  return m;
197 }
198 
199 static mat4 rotateX(float rad)
200 {
201  float co = cosf(rad); float si = sinf(rad);
202  mat4 m(1.0f);
203  m[1][1] = co; m[1][2] = -si; m[2][1] = si; m[2][2] = co;
204  return m;
205 }
206 
207 static mat4 rotateY(float rad)
208 {
209  float co = cosf(rad); float si = sinf(rad);
210  mat4 m(1.0f);
211  m[0][0] = co; m[0][2] = si; m[2][0] = -si; m[2][2] = co;
212  return m;
213 }
214 
215 static mat4 rotateZ(float rad)
216 {
217  float co = cosf(rad); float si = sinf(rad);
218  mat4 m(1.0f);
219  m[0][0] = co; m[1][0] = -si; m[0][1] = si; m[1][1] = co;
220  return m;
221 }
222 
223 static mat4 translate(float x, float y, float z)
224 {
225  mat4 m(1.0f);
226  m[3][0] = x; m[3][1] = y; m[3][2] = z; m[3][3] = 1.0f;
227  return m;
228 }
229 
230 static mat4 translate(const vec3 &v)
231 {
232  mat4 m(1.0f);
233  m[3][0] = v.x; m[3][1] = v.y; m[3][2] = v.z;
234  return m;
235 }
236 
237 static mat4 scale(float x, float y, float z)
238 {
239  mat4 m(1.0f);
240  m[0][0] = x; m[1][1] = y; m[2][2] = z;
241  return m;
242 }
243 
244 static mat4 scale(float s)
245 {
246  return scale(s, s, s);
247 }
248 
249 #endif
static mat4 scale(float x, float y, float z)
Definition: matrix.h:237
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
GLboolean GLboolean GLboolean GLboolean a
Definition: gl2ext.h:306
static mat4 perspective(float fovy, float aspect, float z_near, float z_far)
Definition: matrix.h:174
vec4 & operator-=(const vec4 &rhs)
Definition: matrix.h:96
Definition: matrix.h:51
vec2 operator*(const vec2 &rhs) const
Definition: matrix.h:37
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 transpose(const mat4 &m)
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
static mat4 translate(float x, float y, float z)
Definition: matrix.h:223
GLfloat GLfloat GLfloat w
Definition: gl2ext.h:2701
float & operator[](unsigned int i)
Definition: matrix.h:98
static mat4 rotateY(float rad)
Definition: matrix.h:207
GLfloat GLfloat f
Definition: gl2ext.h:2707
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
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
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
static vec3 normalize(const vec3 &v)
Definition: matrix.h:155
vec3 operator-() const
Definition: matrix.h:60
vec4 z
Definition: matrix.h:106
GLboolean GLboolean GLboolean b
Definition: gl2ext.h:306
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
static mat4 rotateX(float rad)
Definition: matrix.h:199
vec4 x
Definition: matrix.h:106
float x
Definition: matrix.h:77
static mat4 orthographic(float left, float right, float bottom, float top, float z_near, float z_far)
Definition: matrix.h:187
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
static mat4 rotateZ(float rad)
Definition: matrix.h:215
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