24 #define PI 3.141592653f
34 vec2(
float X,
float Y) :
x(X),
y(Y){ }
35 explicit vec2(
float S) :
x(S),
y(S) { }
48 const float &
operator [] (
unsigned int i)
const {
return (&
x)[i]; }
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) { }
72 const float &
operator [] (
unsigned int i)
const {
return (&
x)[i]; }
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) { }
99 const float &
operator [] (
unsigned int i)
const {
return (&
x)[i]; }
120 for (
int lrow = 0; lrow < 4; ++lrow)
122 for (
int rcol = 0; rcol < 4; ++rcol)
124 m[rcol][lrow] = 0.0f;
125 for (
int k = 0; k < 4; ++k)
126 m[rcol][lrow] += (*
this)[k][lrow] * rhs[rcol][k];
145 return x * rhs.
x +
y * rhs.
y +
z * rhs.
z +
w * rhs.
w;
156 return v / sqrt(v.
x * v.
x + v.
y * v.
y + v.
z * v.
z);
162 float invtf = 1.0f / tan(fovy * 0.5
f);
163 m[0].
x = invtf / aspect;
165 m[2].
z = -(far + near) / (far - near);
167 m[3].
z = (-2.0f * far * near) / (far - near);
175 m[0].
x = 2.0f / (right -
left);
176 m[3].
x = -(right +
left) / (right - left);
177 m[1].
y = 2.0f / (top -
bottom);
178 m[3].
y = -(top +
bottom) / (top - bottom);
179 m[2].
z = -2.0f / (far - near);
180 m[3].
z = -(far + near) / (far - near);
191 inv[0] = m[5] * m[10] * m[15] -
192 m[5] * m[11] * m[14] -
193 m[9] * m[6] * m[15] +
194 m[9] * m[7] * m[14] +
195 m[13] * m[6] * m[11] -
196 m[13] * m[7] * m[10];
198 inv[4] = -m[4] * m[10] * m[15] +
199 m[4] * m[11] * m[14] +
200 m[8] * m[6] * m[15] -
201 m[8] * m[7] * m[14] -
202 m[12] * m[6] * m[11] +
203 m[12] * m[7] * m[10];
205 inv[8] = m[4] * m[9] * m[15] -
206 m[4] * m[11] * m[13] -
207 m[8] * m[5] * m[15] +
208 m[8] * m[7] * m[13] +
209 m[12] * m[5] * m[11] -
212 inv[12] = -m[4] * m[9] * m[14] +
213 m[4] * m[10] * m[13] +
214 m[8] * m[5] * m[14] -
215 m[8] * m[6] * m[13] -
216 m[12] * m[5] * m[10] +
219 inv[1] = -m[1] * m[10] * m[15] +
220 m[1] * m[11] * m[14] +
221 m[9] * m[2] * m[15] -
222 m[9] * m[3] * m[14] -
223 m[13] * m[2] * m[11] +
224 m[13] * m[3] * m[10];
226 inv[5] = m[0] * m[10] * m[15] -
227 m[0] * m[11] * m[14] -
228 m[8] * m[2] * m[15] +
229 m[8] * m[3] * m[14] +
230 m[12] * m[2] * m[11] -
231 m[12] * m[3] * m[10];
233 inv[9] = -m[0] * m[9] * m[15] +
234 m[0] * m[11] * m[13] +
235 m[8] * m[1] * m[15] -
236 m[8] * m[3] * m[13] -
237 m[12] * m[1] * m[11] +
240 inv[13] = m[0] * m[9] * m[14] -
241 m[0] * m[10] * m[13] -
242 m[8] * m[1] * m[14] +
243 m[8] * m[2] * m[13] +
244 m[12] * m[1] * m[10] -
247 inv[2] = m[1] * m[6] * m[15] -
248 m[1] * m[7] * m[14] -
249 m[5] * m[2] * m[15] +
250 m[5] * m[3] * m[14] +
251 m[13] * m[2] * m[7] -
254 inv[6] = -m[0] * m[6] * m[15] +
255 m[0] * m[7] * m[14] +
256 m[4] * m[2] * m[15] -
257 m[4] * m[3] * m[14] -
258 m[12] * m[2] * m[7] +
261 inv[10] = m[0] * m[5] * m[15] -
262 m[0] * m[7] * m[13] -
263 m[4] * m[1] * m[15] +
264 m[4] * m[3] * m[13] +
265 m[12] * m[1] * m[7] -
268 inv[14] = -m[0] * m[5] * m[14] +
269 m[0] * m[6] * m[13] +
270 m[4] * m[1] * m[14] -
271 m[4] * m[2] * m[13] -
272 m[12] * m[1] * m[6] +
275 inv[3] = -m[1] * m[6] * m[11] +
276 m[1] * m[7] * m[10] +
277 m[5] * m[2] * m[11] -
278 m[5] * m[3] * m[10] -
282 inv[7] = m[0] * m[6] * m[11] -
283 m[0] * m[7] * m[10] -
284 m[4] * m[2] * m[11] +
285 m[4] * m[3] * m[10] +
289 inv[11] = -m[0] * m[5] * m[11] +
291 m[4] * m[1] * m[11] -
296 inv[15] = m[0] * m[5] * m[10] -
298 m[4] * m[1] * m[10] +
303 float det = m[0] * inv[0] + m[1] * inv[4] + m[2] * inv[8] + m[3] * inv[12];
309 return inv_mat * det;
314 float co = cosf(rad);
float si = sinf(rad);
316 m[1][1] = co; m[1][2] = -si; m[2][1] = si; m[2][2] = co;
322 float co = cosf(rad);
float si = sinf(rad);
324 m[0][0] = co; m[0][2] = si; m[2][0] = -si; m[2][2] = co;
330 float co = cosf(rad);
float si = sinf(rad);
332 m[0][0] = co; m[1][0] = -si; m[0][1] = si; m[1][1] = co;
339 m[3][0] =
x; m[3][1] =
y; m[3][2] = z; m[3][3] = 1.0f;
346 m[3][0] = v.
x; m[3][1] = v.
y; m[3][2] = v.
z;
353 m[0][0] =
x; m[1][1] =
y; m[2][2] = z;
359 return scale(s, s, s);
vec4 operator+(const vec4 &rhs) const
vec2 & operator-=(const vec2 &rhs)
vec3 & operator+=(const vec3 &rhs)
vec4 & operator-=(const vec4 &rhs)
vec2 operator*(const vec2 &rhs) const
vec2 & operator+=(const vec2 &rhs)
float & operator[](unsigned int i)
vec2 & operator*=(const float s)
vec2 operator-(const vec2 &rhs) const
vec3 & operator*=(const vec3 &rhs)
vec3 operator*(const vec3 &rhs) const
static mat4 scale(float x, float y, float z)
vec4 operator/(const float s) const
static mat4 rotateY(float rad)
vec4 & operator[](unsigned int i)
const float * value_ptr() const
vec3 & operator-=(const vec3 &rhs)
static mat4 orthographic(float left, float right, float bottom, float top, float near, float far)
static mat4 inverse(const mat4 &op)
GLfloat GLfloat GLfloat w
static vec3 normalize(const vec3 &v)
static mat4 rotateZ(float rad)
float & operator[](unsigned int i)
static mat4 rotateX(float rad)
vec3 operator+(const vec3 &rhs) const
vec4 & operator*=(const float s)
vec4 & operator+=(const vec4 &rhs)
GLint GLint GLint GLint GLint x
vec2 operator+(const vec2 &rhs) const
vec4 operator*(const vec4 &rhs) const
static mat4 perspective(float fovy, float aspect, float near, float far)
static mat4 translate(float x, float y, float z)
vec3 operator/(const float s) const
mat4 operator*(const mat4 &rhs)
float & operator[](unsigned int i)
vec3(float X, float Y, float Z)
vec4(float X, float Y, float Z, float W)
vec2 operator/(const float s) const