21 #ifndef VECTOR_MATH_H__
22 #define VECTOR_MATH_H__
29 #define PI 3.141592653f
35 const typename T::data_type *
value_ptr(
const T& vec)
59 vec2(
const float *vec) { memcpy(
data, vec, 2 *
sizeof(
float)); }
81 uvec2(
const uint32_t *vec) { memcpy(
data, vec, 2 *
sizeof(uint32_t)); }
101 vec3(
const float *vec) { memcpy(
data, vec, 3 *
sizeof(
float)); }
120 vec4(
const float *vec) { memcpy(
data, vec, 4 *
sizeof(
float)); }
124 *
this =
vec4(vec.
c.x, vec.
c.y, vec.
c.z, v);
129 *
this =
vec4(a.
c.x, a.
c.y, b.
c.x, b.
c.y);
140 mat4(
float s) {
for (
unsigned int i = 0; i < 16; i++)
data[i] = s; }
141 mat4(
float c00,
float c01,
float c02,
float c03,
142 float c10,
float c11,
float c12,
float c13,
143 float c20,
float c21,
float c22,
float c23,
144 float c30,
float c31,
float c32,
float c33)
151 mat4(
const float *mat) { memcpy(
data, mat, 16 *
sizeof(
float)); }
161 for (
unsigned int i = 0; i < T::vector_size; i++)
162 res.data[i] = -a.data[i];
170 for (
unsigned int i = 0; i < T::vector_size; i++)
171 res.data[i] = a.data[i] * b.data[i];
179 for (
unsigned int i = 0; i < T::vector_size; i++)
180 res.data[i] = a.data[i] / b.data[i];
188 for (
unsigned int i = 0; i < T::vector_size; i++)
189 res.data[i] = a.data[i] + b.data[i];
197 for (
unsigned int i = 0; i < T::vector_size; i++)
198 res.data[i] = a.data[i] - b.data[i];
205 for (
unsigned int i = 0; i < T::vector_size; i++)
206 a.data[i] *= b.data[i];
213 for (
unsigned int i = 0; i < T::vector_size; i++)
214 a.data[i] /= b.data[i];
221 for (
unsigned int i = 0; i < T::vector_size; i++)
222 a.data[i] += b.data[i];
229 for (
unsigned int i = 0; i < T::vector_size; i++)
230 a.data[i] -= b.data[i];
237 for (
unsigned int r = 0;
r < 4;
r++)
239 for (
unsigned int c = 0; c < 4; c++)
242 for (
unsigned int k = 0; k < 4; k++)
243 sum += a.
data[
r + 4 * k] * b.
data[4 * c + k];
244 res.
data[
r + 4 * c] = sum;
254 for (
unsigned int i = 0; i < 4; i++)
261 for (
unsigned int i = 0; i < 16; i++)
269 a.
c.y * b.
c.z - b.
c.y * a.
c.z,
270 a.
c.z * b.
c.x - b.
c.z * a.
c.x,
271 a.
c.x * b.
c.y - b.
c.x * a.
c.y);
278 for (
unsigned int i = 0; i < T::vector_size; i++)
279 sum += a.data[i] * b.data[i];
286 return sqrt(
vec_dot(vec, vec));
299 for (
unsigned int i = 0; i < T::vector_size; i++)
300 res.data[i] = floor(vec.data[i]);
327 inline T
vec_clamp(
const T& vec,
const T& lo,
const T& hi)
330 for (
unsigned int i = 0; i < T::vector_size; i++)
331 res.data[i] =
clamp(vec.data[i], lo.data[i], hi.data[i]);
342 xaxis.
c.x, yaxis.
c.x, -zaxis.
c.x, 0.0f,
343 xaxis.
c.y, yaxis.
c.y, -zaxis.
c.y, 0.0f,
344 xaxis.
c.z, yaxis.
c.z, -zaxis.
c.z, 0.0f,
350 float cos_r = cos(radians);
351 float sin_r = sin(radians);
354 0.0
f, cos_r, sin_r, 0.0
f,
355 0.0
f, -sin_r, cos_r, 0.0
f,
356 0.0
f, 0.0
f, 0.0
f, 1.0
f);
361 float cos_r = cos(radians);
362 float sin_r = sin(radians);
364 return mat4(cos_r, 0.0
f, sin_r, 0.0
f,
365 0.0
f, 1.0
f, 0.0
f, 0.0
f,
366 -sin_r, 0.0
f, cos_r, 0.0
f,
367 0.0
f, 0.0
f, 0.0
f, 1.0
f);
372 float cos_r = cos(radians);
373 float sin_r = sin(radians);
375 return mat4(cos_r, sin_r, 0.0
f, 0.0
f,
376 -sin_r, cos_r, 0.0
f, 0.0
f,
377 0.0
f, 0.0
f, 1.0
f, 0.0
f,
378 0.0
f, 0.0
f, 0.0
f, 1.0
f);
384 float yFac = tanf(fovy *
PI / 360.0
f);
385 float xFac = yFac * aspect;
386 return mat4(1.0
f / xFac, 0.0
f, 0.0
f, 0.0
f,
387 0.0
f, 1.0
f / yFac, 0.0
f, 0.0
f,
388 0.0
f, 0.0
f, -(zf + zn) / (zf - zn), -1.0
f,
389 0.0
f, 0.0
f, -(2.0
f * zf * zn) / (zf - zn), 0.0
f);
407 float det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
408 float inv_det = 1.0f / det;
T vec_fract(const T &vec)
T vec_floor(const T &vec)
T::vector_type & operator/=(T &a, const T &b)
vec4(const vec2 &a, const vec2 &b)
GLboolean GLboolean GLboolean GLboolean a
float vec_dot(const T &a, const T &b)
mat4 mat_look_at(const vec3 &eye, const vec3 ¢er, const vec3 &up)
vec3 vec_cross(const vec3 &a, const vec3 &b)
T::vector_type operator/(const T &a, const T &b)
T::vector_type operator*(const T &a, const T &b)
T::vector_type operator+(const T &a, const T &b)
const T::data_type * value_ptr(const T &vec)
mat4 mat_rotate_y(float radians)
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
mat4(float c00, float c01, float c02, float c03, float c10, float c11, float c12, float c13, float c20, float c21, float c22, float c23, float c30, float c31, float c32, float c33)
vec4(float x, float y, float z, float w)
float vec_length(const T &vec)
uvec2(const uint32_t *vec)
T::vector_type operator-(const T &a)
GLfloat GLfloat GLfloat w
T::vector_type & operator+=(T &a, const T &b)
mat4 mat_rotate_z(float radians)
mat4 mat_rotate_x(float radians)
mat4 mat_perspective_fov(float fovy, float aspect, float zn, float zf)
vec4(const vec3 &vec, float v)
GLint GLint GLint GLint GLint x
T::vector_type & operator*=(T &a, const T &b)
T vec_clamp(const T &vec, const T &lo, const T &hi)
mat4 mat_inverse(const mat4 &a)
T::vector_type & operator-=(T &a, const T &b)
T clamp(T value, T lo, T hi)
T vec_normalize(const T &vec)
GLboolean GLboolean GLboolean b
uvec2(uint32_t x, uint32_t y)
vec3 vec_project(const vec4 &vec)
vec3(float x, float y, float z)