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)
57 vec2(
float s) { this->
x = this->
y = s; }
58 vec2(
float x,
float y) { this->x =
x; this->y =
y; }
59 vec2(
const float *vec) { memcpy(
data, vec, 2 *
sizeof(
float)); }
84 ivec2(
const T *vec) { memcpy(
data, vec, 2 *
sizeof(T)); }
109 vec3(
float x,
float y,
float z) { this->x =
x; this->y =
y; this->z =
z; }
110 vec3(
const float *vec) { memcpy(
data, vec, 3 *
sizeof(
float)); }
128 vec4(
float x,
float y,
float z,
float w) { this->x =
x; this->y =
y; this->z =
z; this->w =
w; }
129 vec4(
const float *vec) { memcpy(
data, vec, 4 *
sizeof(
float)); }
133 *
this =
vec4(vec.
x, vec.
y, vec.
z, v);
143 *
this =
vec4(a.
x, a.
y, b, c);
154 mat4(
float s) {
for (
unsigned int i = 0; i < 16; i++)
data[i] = s; }
155 mat4(
float c00,
float c01,
float c02,
float c03,
156 float c10,
float c11,
float c12,
float c13,
157 float c20,
float c21,
float c22,
float c23,
158 float c30,
float c31,
float c32,
float c33)
165 mat4(
const float *mat) { memcpy(
data, mat, 16 *
sizeof(
float)); }
175 for (
unsigned int i = 0; i < T::vector_size; i++)
176 res.data[i] = -a.data[i];
184 for (
unsigned int i = 0; i < T::vector_size; i++)
185 res.data[i] = a.data[i] * b.data[i];
193 for (
unsigned int i = 0; i < T::vector_size; i++)
194 res.data[i] = a.data[i] / b.data[i];
202 for (
unsigned int i = 0; i < T::vector_size; i++)
203 res.data[i] = a.data[i] + b.data[i];
211 for (
unsigned int i = 0; i < T::vector_size; i++)
212 res.data[i] = a.data[i] - b.data[i];
219 for (
unsigned int i = 0; i < T::vector_size; i++)
220 a.data[i] *= b.data[i];
227 for (
unsigned int i = 0; i < T::vector_size; i++)
228 a.data[i] /= b.data[i];
235 for (
unsigned int i = 0; i < T::vector_size; i++)
236 a.data[i] += b.data[i];
243 for (
unsigned int i = 0; i < T::vector_size; i++)
244 a.data[i] -= b.data[i];
251 for (
unsigned int r = 0;
r < 4;
r++)
253 for (
unsigned int c = 0; c < 4; c++)
256 for (
unsigned int k = 0; k < 4; k++)
257 sum += a.
data[
r + 4 * k] * b.
data[4 * c + k];
258 res.
data[
r + 4 * c] = sum;
268 for (
unsigned int i = 0; i < 4; i++)
275 for (
unsigned int i = 0; i < 16; i++)
283 a.
y * b.
z - b.
y * a.
z,
284 a.
z * b.
x - b.
z * a.
x,
285 a.
x * b.
y - b.
x * a.
y);
292 for (
unsigned int i = 0; i < T::vector_size; i++)
293 sum += a.data[i] * b.data[i];
300 return sqrt(
vec_dot(vec, vec));
313 for (
unsigned int i = 0; i < T::vector_size; i++)
314 res.data[i] = floor(vec.data[i]);
322 for (
unsigned int i = 0; i < T::vector_size; i++)
323 res.data[i] = round(vec.data[i]);
350 inline T
vec_clamp(
const T& vec,
const T& lo,
const T& hi)
353 for (
unsigned int i = 0; i < T::vector_size; i++)
354 res.data[i] =
clamp(vec.data[i], lo.data[i], hi.data[i]);
365 xaxis.
x, yaxis.
x, -zaxis.
x, 0.0f,
366 xaxis.
y, yaxis.
y, -zaxis.
y, 0.0f,
367 xaxis.
z, yaxis.
z, -zaxis.
z, 0.0f,
373 float cos_r = cos(radians);
374 float sin_r = sin(radians);
377 0.0
f, cos_r, sin_r, 0.0
f,
378 0.0
f, -sin_r, cos_r, 0.0
f,
379 0.0
f, 0.0
f, 0.0
f, 1.0
f);
384 float cos_r = cos(radians);
385 float sin_r = sin(radians);
387 return mat4(cos_r, 0.0
f, sin_r, 0.0
f,
388 0.0
f, 1.0
f, 0.0
f, 0.0
f,
389 -sin_r, 0.0
f, cos_r, 0.0
f,
390 0.0
f, 0.0
f, 0.0
f, 1.0
f);
395 float cos_r = cos(radians);
396 float sin_r = sin(radians);
398 return mat4(cos_r, sin_r, 0.0
f, 0.0
f,
399 -sin_r, cos_r, 0.0
f, 0.0
f,
400 0.0
f, 0.0
f, 1.0
f, 0.0
f,
401 0.0
f, 0.0
f, 0.0
f, 1.0
f);
422 float yFac = tanf(fovy *
PI / 360.0
f);
423 float xFac = yFac * aspect;
424 return mat4(1.0
f / xFac, 0.0
f, 0.0
f, 0.0
f,
425 0.0
f, 1.0
f / yFac, 0.0
f, 0.0
f,
426 0.0
f, 0.0
f, -(zf + zn) / (zf - zn), -1.0
f,
427 0.0
f, 0.0
f, -(2.0
f * zf * zn) / (zf - zn), 0.0
f);
445 float det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
446 float inv_det = 1.0f / det;
T vec_fract(const T &vec)
T vec_floor(const T &vec)
vec3 vec_rotateX(const vec3 &v, float radians)
T::vector_type & operator/=(T &a, const T &b)
vec4(const vec2 &a, const vec2 &b)
vec4(const vec2 &a, float b, float c)
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)
T::vector_type operator-(const T &a)
GLfloat GLfloat GLfloat w
vec3 vec_rotateZ(const vec3 &v, float radians)
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)
detail::ivec2< uint8_t > ubvec2
vec4(const vec3 &vec, float v)
vec3 vec_rotateY(const vec3 &v, float radians)
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
detail::ivec2< uint32_t > uvec2
T vec_round(const T &vec)
vec3 vec_project(const vec4 &vec)
detail::ivec2< int32_t > ivec2
vec3(float x, float y, float z)