21 #ifndef VECTOR_MATH_H__
22 #define VECTOR_MATH_H__
28 #define PI 3.141592653f
50 vec2(
const float *vec) { memcpy(
data, vec, 2 *
sizeof(
float)); }
67 vec3(
const float *vec) { memcpy(
data, vec, 3 *
sizeof(
float)); }
84 vec4(
const float *vec) { memcpy(
data, vec, 4 *
sizeof(
float)); }
88 *
this =
vec4(vec.
c.x, vec.
c.y, vec.
c.z, v);
93 *
this =
vec4(a.
c.x, a.
c.y, b.
c.x, b.
c.y);
102 mat4(
float s) {
for (
unsigned int i = 0; i < 16; i++)
data[i] = s; }
103 mat4(
float c00,
float c01,
float c02,
float c03,
104 float c10,
float c11,
float c12,
float c13,
105 float c20,
float c21,
float c22,
float c23,
106 float c30,
float c31,
float c32,
float c33)
113 mat4(
const float *mat) { memcpy(
data, mat, 16 *
sizeof(
float)); }
120 for (
unsigned int i = 0; i < T::vector_size; i++)
121 res.data[i] = -a.data[i];
129 for (
unsigned int i = 0; i < T::vector_size; i++)
130 res.data[i] = a.data[i] * b.data[i];
138 for (
unsigned int i = 0; i < T::vector_size; i++)
139 res.data[i] = a.data[i] / b.data[i];
147 for (
unsigned int i = 0; i < T::vector_size; i++)
148 res.data[i] = a.data[i] + b.data[i];
156 for (
unsigned int i = 0; i < T::vector_size; i++)
157 res.data[i] = a.data[i] - b.data[i];
164 for (
unsigned int i = 0; i < T::vector_size; i++)
165 a.data[i] *= b.data[i];
172 for (
unsigned int i = 0; i < T::vector_size; i++)
173 a.data[i] /= b.data[i];
180 for (
unsigned int i = 0; i < T::vector_size; i++)
181 a.data[i] += b.data[i];
188 for (
unsigned int i = 0; i < T::vector_size; i++)
189 a.data[i] -= b.data[i];
196 for (
unsigned int r = 0;
r < 4;
r++)
198 for (
unsigned int c = 0; c < 4; c++)
201 for (
unsigned int k = 0; k < 4; k++)
202 sum += a.
data[
r + 4 * k] * b.
data[4 * c + k];
203 res.
data[
r + 4 * c] = sum;
213 for (
unsigned int i = 0; i < 4; i++)
220 for (
unsigned int i = 0; i < 16; i++)
228 a.
c.y * b.
c.z - b.
c.y * a.
c.z,
229 a.
c.z * b.
c.x - b.
c.z * a.
c.x,
230 a.
c.x * b.
c.y - b.
c.x * a.
c.y);
237 for (
unsigned int i = 0; i < T::vector_size; i++)
238 sum += a.data[i] * b.data[i];
245 return sqrt(
vec_dot(vec, vec));
258 for (
unsigned int i = 0; i < T::vector_size; i++)
259 res.data[i] = floor(vec.data[i]);
281 xaxis.
c.x, yaxis.
c.x, -zaxis.
c.x, 0.0f,
282 xaxis.
c.y, yaxis.
c.y, -zaxis.
c.y, 0.0f,
283 xaxis.
c.z, yaxis.
c.z, -zaxis.
c.z, 0.0f,
290 float yFac = tanf(fovy *
PI / 360.0
f);
291 float xFac = yFac * aspect;
292 return mat4(1.0
f / xFac, 0.0
f, 0.0
f, 0.0
f,
293 0.0
f, 1.0
f / yFac, 0.0
f, 0.0
f,
294 0.0
f, 0.0
f, -(zf + zn) / (zf - zn), -1.0
f,
295 0.0
f, 0.0
f, -(2.0
f * zf * zn) / (zf - zn), 0.0
f);
313 float det = a0 * b5 - a1 * b4 + a2 * b3 + a3 * b2 - a4 * b1 + a5 * b0;
314 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)
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
T::vector_type & operator+=(T &a, const T &b)
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)
mat4 mat_inverse(const mat4 &a)
T::vector_type & operator-=(T &a, const T &b)
T vec_normalize(const T &vec)
GLboolean GLboolean GLboolean b
vec3 vec_project(const vec4 &vec)
vec3(float x, float y, float z)