7 #define inline __inline 10 #define LINMATH_H_DEFINE_VEC(n) \ 11 typedef float vec##n[n]; \ 12 static inline void vec##n##_add(vec##n r, vec##n const a, vec##n const b) \ 18 static inline void vec##n##_sub(vec##n r, vec##n const a, vec##n const b) \ 24 static inline void vec##n##_scale(vec##n r, vec##n const v, float const s) \ 30 static inline float vec##n##_mul_inner(vec##n const a, vec##n const b) \ 38 static inline float vec##n##_len(vec##n const v) \ 40 return (float) sqrt(vec##n##_mul_inner(v,v)); \ 42 static inline void vec##n##_norm(vec##n r, vec##n const v) \ 44 float k = 1.f / vec##n##_len(v); \ 45 vec##n##_scale(r, v, k); \ 54 r[0] = a[1]*b[2] - a[2]*b[1];
55 r[1] = a[2]*b[0] - a[0]*b[2];
56 r[2] = a[0]*b[1] - a[1]*b[0];
61 float p = 2.f*vec3_mul_inner(v, n);
69 r[0] = a[1]*b[2] - a[2]*b[1];
70 r[1] = a[2]*b[0] - a[0]*b[2];
71 r[2] = a[0]*b[1] - a[1]*b[0];
77 float p = 2.f*vec4_mul_inner(v, n);
89 M[i][j] = i==j ? 1.
f : 0.
f;
121 vec4_add(M[i], a[i], b[i]);
127 vec4_sub(M[i], a[i], b[i]);
133 vec4_scale(M[i], a[i], k);
138 vec4_scale(M[0], a[0], x);
139 vec4_scale(M[1], a[1], y);
140 vec4_scale(M[2], a[2], z);
141 for(i = 0; i < 4; ++
i) {
149 for(c=0; c<4; ++
c)
for(r=0; r<4; ++
r) {
152 temp[c][r] += a[k][r] * b[c][k];
162 r[j] += M[i][j] * v[i];
174 vec4
t = {
x,
y,
z, 0};
177 for (i = 0; i < 4; ++
i) {
179 M[3][
i] += vec4_mul_inner(r, t);
185 for(i=0; i<4; ++
i)
for(j=0; j<4; ++
j)
186 M[i][j] = i<3 && j<3 ? a[i] * b[j] : 0.
f;
190 float s = sinf(angle);
191 float c = cosf(angle);
194 if(vec3_len(u) > 1
e-4) {
225 float s = sinf(angle);
226 float c = cosf(angle);
228 {1.f, 0.f, 0.f, 0.f},
237 float s = sinf(angle);
238 float c = cosf(angle);
241 { 0.f, 1.f, 0.f, 0.f},
243 { 0.f, 0.f, 0.f, 1.f}
249 float s = sinf(angle);
250 float c = cosf(angle);
254 { 0.f, 0.f, 1.f, 0.f},
255 { 0.f, 0.f, 0.f, 1.f}
264 s[0] = M[0][0]*M[1][1] - M[1][0]*M[0][1];
265 s[1] = M[0][0]*M[1][2] - M[1][0]*M[0][2];
266 s[2] = M[0][0]*M[1][3] - M[1][0]*M[0][3];
267 s[3] = M[0][1]*M[1][2] - M[1][1]*M[0][2];
268 s[4] = M[0][1]*M[1][3] - M[1][1]*M[0][3];
269 s[5] = M[0][2]*M[1][3] - M[1][2]*M[0][3];
271 c[0] = M[2][0]*M[3][1] - M[3][0]*M[2][1];
272 c[1] = M[2][0]*M[3][2] - M[3][0]*M[2][2];
273 c[2] = M[2][0]*M[3][3] - M[3][0]*M[2][3];
274 c[3] = M[2][1]*M[3][2] - M[3][1]*M[2][2];
275 c[4] = M[2][1]*M[3][3] - M[3][1]*M[2][3];
276 c[5] = M[2][2]*M[3][3] - M[3][2]*M[2][3];
279 idet = 1.0f/( s[0]*c[5]-s[1]*c[4]+s[2]*c[3]+s[3]*c[2]-s[4]*c[1]+s[5]*c[0] );
281 T[0][0] = ( M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
282 T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
283 T[0][2] = ( M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
284 T[0][3] = (-M[2][1] * s[5] + M[2][2] * s[4] - M[2][3] * s[3]) * idet;
286 T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
287 T[1][1] = ( M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
288 T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
289 T[1][3] = ( M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
291 T[2][0] = ( M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
292 T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
293 T[2][2] = ( M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
294 T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
296 T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
297 T[3][1] = ( M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
298 T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
299 T[3][3] = ( M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
307 vec3_norm(R[2], R[2]);
309 s = vec3_mul_inner(R[1], R[2]);
310 vec3_scale(h, R[2], s);
311 vec3_sub(R[1], R[1], h);
312 vec3_norm(R[2], R[2]);
314 s = vec3_mul_inner(R[1], R[2]);
315 vec3_scale(h, R[2], s);
316 vec3_sub(R[1], R[1], h);
317 vec3_norm(R[1], R[1]);
319 s = vec3_mul_inner(R[0], R[1]);
320 vec3_scale(h, R[1], s);
321 vec3_sub(R[0], R[0], h);
322 vec3_norm(R[0], R[0]);
327 M[0][0] = 2.f*n/(r-l);
328 M[0][1] = M[0][2] = M[0][3] = 0.f;
330 M[1][1] = 2.f*n/(t-
b);
331 M[1][0] = M[1][2] = M[1][3] = 0.f;
333 M[2][0] = (r+l)/(r-l);
334 M[2][1] = (t+
b)/(t-b);
335 M[2][2] = -(f+
n)/(f-n);
338 M[3][2] = -2.f*(f*
n)/(f-n);
339 M[3][0] = M[3][1] = M[3][3] = 0.f;
344 M[0][1] = M[0][2] = M[0][3] = 0.f;
347 M[1][0] = M[1][2] = M[1][3] = 0.f;
349 M[2][2] = -2.f/(f-
n);
350 M[2][0] = M[2][1] = M[2][3] = 0.f;
352 M[3][0] = -(r+l)/(r-l);
353 M[3][1] = -(t+
b)/(t-b);
354 M[3][2] = -(f+
n)/(f-n);
361 float const a = 1.f / (float) tan(y_fov / 2.f);
363 m[0][0] = a / aspect;
375 m[2][2] = -((f +
n) / (f - n));
380 m[3][2] = -((2.f * f *
n) / (f - n));
395 vec3_sub(f, center, eye);
429 q[0] = q[1] = q[2] = 0.f;
448 vec3_scale(w, p, q[3]);
450 vec3_scale(w, q, p[3]);
452 r[3] = p[3]*q[3] - vec3_mul_inner(p, q);
478 vec3_scale(v, axis, sinf(angle / 2));
481 r[3] = cosf(angle / 2);
483 #define quat_norm vec4_norm 491 vec3
t = {q[0], q[1], q[2]};
492 vec3 u = {q[0], q[1], q[2]};
498 vec3_scale(t, t, q[3]);
514 M[0][0] = a2 + b2 - c2 - d2;
515 M[0][1] = 2.f*(b*c + a*
d);
516 M[0][2] = 2.f*(b*d - a*
c);
519 M[1][0] = 2*(b*c - a*
d);
520 M[1][1] = a2 - b2 + c2 - d2;
521 M[1][2] = 2.f*(c*d + a*
b);
524 M[2][0] = 2.f*(b*d + a*
c);
525 M[2][1] = 2.f*(c*d - a*
b);
526 M[2][2] = a2 - b2 - c2 + d2;
529 M[3][0] = M[3][1] = M[3][2] = 0.f;
541 R[3][0] = R[3][1] = R[3][2] = 0.f;
549 int perm[] = { 0, 1, 2, 0, 1 };
552 for(i = 0; i<3; i++) {
560 r = (float) sqrt(1.
f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]] );
564 q[1] = q[2] = q[3] = 0.f;
569 q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]])/(2.
f*r);
570 q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]])/(2.
f*r);
571 q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]])/(2.
f*r);
GLboolean GLboolean GLboolean b
static void quat_rotate(quat r, float angle, vec3 axis)
static void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M, float angle)
static void mat4x4_rotate_X(mat4x4 Q, mat4x4 M, float angle)
static void mat4x4_scale(mat4x4 M, mat4x4 a, float k)
static void quat_scale(quat r, quat v, float s)
static void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
static void mat4x4_identity(mat4x4 M)
static void mat4x4_col(vec4 r, mat4x4 M, int i)
#define LINMATH_H_DEFINE_VEC(n)
static void quat_from_mat4x4(quat q, mat4x4 M)
static void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle)
GLdouble GLdouble GLdouble w
static void mat4x4_row(vec4 r, mat4x4 M, int i)
GLfloat GLfloat GLfloat GLfloat h
static void mat4x4_perspective(mat4x4 m, float y_fov, float aspect, float n, float f)
static void quat_mul_vec3(vec3 r, quat q, vec3 v)
GLboolean GLboolean GLboolean GLboolean a
static void mat4x4_from_quat(mat4x4 M, quat q)
static void mat4x4_mul_vec4(vec4 r, mat4x4 M, vec4 v)
static void mat4x4_translate(mat4x4 T, float x, float y, float z)
static float quat_inner_product(quat a, quat b)
static void quat_mul(quat r, quat p, quat q)
static void mat4x4_add(mat4x4 M, mat4x4 a, mat4x4 b)
static void mat4x4_sub(mat4x4 M, mat4x4 a, mat4x4 b)
static void vec4_reflect(vec4 r, vec4 v, vec4 n)
static void mat4x4_frustum(mat4x4 M, float l, float r, float b, float t, float n, float f)
static void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up)
static void quat_identity(quat q)
static void mat4x4_rotate(mat4x4 R, mat4x4 M, float x, float y, float z, float angle)
static void vec4_mul_cross(vec4 r, vec4 a, vec4 b)
static void quat_conj(quat r, quat q)
static void mat4x4_dup(mat4x4 M, mat4x4 N)
static void mat4x4_invert(mat4x4 T, mat4x4 M)
static void quat_add(quat r, quat a, quat b)
static void mat4x4_transpose(mat4x4 M, mat4x4 N)
static void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b)
GLdouble GLdouble GLdouble q
static void mat4x4_translate_in_place(mat4x4 M, float x, float y, float z)
static void quat_sub(quat r, quat a, quat b)
static void mat4x4_scale_aniso(mat4x4 M, mat4x4 a, float x, float y, float z)
static void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b)
static void mat4x4_orthonormalize(mat4x4 R, mat4x4 M)
static void vec3_reflect(vec3 r, vec3 const v, vec3 const n)
static void mat4x4o_mul_quat(mat4x4 R, mat4x4 M, quat q)
static void mat4x4_from_vec3_mul_outer(mat4x4 M, vec3 a, vec3 b)