14#define inline __inline
17#define LINMATH_H_DEFINE_VEC(n) \
18 typedef float vec##n[n]; \
19 static inline void vec##n##_add(vec##n r, vec##n const a, vec##n const b) { \
21 for (i = 0; i < n; ++i) r[i] = a[i] + b[i]; \
23 static inline void vec##n##_sub(vec##n r, vec##n const a, vec##n const b) { \
25 for (i = 0; i < n; ++i) r[i] = a[i] - b[i]; \
27 static inline void vec##n##_scale(vec##n r, vec##n const v, float const s) { \
29 for (i = 0; i < n; ++i) r[i] = v[i] * s; \
31 static inline float vec##n##_mul_inner(vec##n const a, vec##n const b) { \
34 for (i = 0; i < n; ++i) p += b[i] * a[i]; \
37 static inline float vec##n##_len(vec##n const v) { \
38 return (float)sqrt(vec##n##_mul_inner(v, v)); \
40 static inline void vec##n##_norm(vec##n r, vec##n const v) { \
41 float k = 1.f / vec##n##_len(v); \
42 vec##n##_scale(r, v, k); \
45LINMATH_H_DEFINE_VEC(2)
46LINMATH_H_DEFINE_VEC(3)
47LINMATH_H_DEFINE_VEC(4)
49static inline
void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b) {
50 r[0] = a[1] * b[2] - a[2] * b[1];
51 r[1] = a[2] * b[0] - a[0] * b[2];
52 r[2] = a[0] * b[1] - a[1] * b[0];
55static inline void vec3_reflect(vec3 r, vec3
const v, vec3
const n) {
56 float p = 2.f * vec3_mul_inner(v, n);
58 for (i = 0; i < 3; ++i) r[i] = v[i] - p * n[i];
61static inline void vec4_mul_cross(vec4 r, vec4 a, vec4 b) {
62 r[0] = a[1] * b[2] - a[2] * b[1];
63 r[1] = a[2] * b[0] - a[0] * b[2];
64 r[2] = a[0] * b[1] - a[1] * b[0];
68static inline void vec4_reflect(vec4 r, vec4 v, vec4 n) {
69 float p = 2.f * vec4_mul_inner(v, n);
71 for (i = 0; i < 4; ++i) r[i] = v[i] - p * n[i];
74typedef vec4 mat4x4[4];
75static inline void mat4x4_identity(mat4x4 M) {
77 for (i = 0; i < 4; ++i)
78 for (j = 0; j < 4; ++j) M[i][j] = i == j ? 1.f : 0.f;
80static inline void mat4x4_dup(mat4x4 M, mat4x4 N) {
82 for (i = 0; i < 4; ++i)
83 for (j = 0; j < 4; ++j) M[i][j] = N[i][j];
85static inline void mat4x4_row(vec4 r, mat4x4 M,
int i) {
87 for (k = 0; k < 4; ++k) r[k] = M[k][i];
89static inline void mat4x4_col(vec4 r, mat4x4 M,
int i) {
91 for (k = 0; k < 4; ++k) r[k] = M[i][k];
93static inline void mat4x4_transpose(mat4x4 M, mat4x4 N) {
95 for (j = 0; j < 4; ++j)
96 for (i = 0; i < 4; ++i) M[i][j] = N[j][i];
98static inline void mat4x4_add(mat4x4 M, mat4x4 a, mat4x4 b) {
100 for (i = 0; i < 4; ++i) vec4_add(M[i], a[i], b[i]);
102static inline void mat4x4_sub(mat4x4 M, mat4x4 a, mat4x4 b) {
104 for (i = 0; i < 4; ++i) vec4_sub(M[i], a[i], b[i]);
106static inline void mat4x4_scale(mat4x4 M, mat4x4 a,
float k) {
108 for (i = 0; i < 4; ++i) vec4_scale(M[i], a[i], k);
110static inline void mat4x4_scale_aniso(mat4x4 M, mat4x4 a,
float x,
float y,
113 vec4_scale(M[0], a[0], x);
114 vec4_scale(M[1], a[1], y);
115 vec4_scale(M[2], a[2], z);
116 for (i = 0; i < 4; ++i) {
120static inline void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b) {
123 for (c = 0; c < 4; ++c)
124 for (r = 0; r < 4; ++r) {
126 for (k = 0; k < 4; ++k) temp[c][r] += a[k][r] * b[c][k];
130static inline void mat4x4_mul_vec4(vec4 r, mat4x4 M, vec4 v) {
132 for (j = 0; j < 4; ++j) {
134 for (i = 0; i < 4; ++i) r[j] += M[i][j] * v[i];
137static inline void mat4x4_translate(mat4x4 T,
float x,
float y,
float z) {
143static inline void mat4x4_translate_in_place(mat4x4 M,
float x,
float y,
145 vec4 t = {x, y, z, 0};
148 for (i = 0; i < 4; ++i) {
150 M[3][i] += vec4_mul_inner(r, t);
153static inline void mat4x4_from_vec3_mul_outer(mat4x4 M, vec3 a, vec3 b) {
155 for (i = 0; i < 4; ++i)
156 for (j = 0; j < 4; ++j) M[i][j] = i < 3 && j < 3 ? a[i] * b[j] : 0.f;
158static inline void mat4x4_rotate(mat4x4 R, mat4x4 M,
float x,
float y,
float z,
160 float s = sinf(angle);
161 float c = cosf(angle);
164 if (vec3_len(u) > 1e-4) {
168 mat4x4_from_vec3_mul_outer(T, u, u);
177 mat4x4_scale(S, S, s);
182 mat4x4_scale(C, C, c);
193static inline void mat4x4_rotate_X(mat4x4 Q, mat4x4 M,
float angle) {
194 float s = sinf(angle);
195 float c = cosf(angle);
196 mat4x4 R = {{1.f, 0.f, 0.f, 0.f},
199 {0.f, 0.f, 0.f, 1.f}};
202static inline void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M,
float angle) {
203 float s = sinf(angle);
204 float c = cosf(angle);
205 mat4x4 R = {{c, 0.f, s, 0.f},
206 {0.f, 1.f, 0.f, 0.f},
208 {0.f, 0.f, 0.f, 1.f}};
211static inline void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M,
float angle) {
212 float s = sinf(angle);
213 float c = cosf(angle);
214 mat4x4 R = {{c, s, 0.f, 0.f},
216 {0.f, 0.f, 1.f, 0.f},
217 {0.f, 0.f, 0.f, 1.f}};
220static inline void mat4x4_invert(mat4x4 T, mat4x4 M) {
224 s[0] = M[0][0] * M[1][1] - M[1][0] * M[0][1];
225 s[1] = M[0][0] * M[1][2] - M[1][0] * M[0][2];
226 s[2] = M[0][0] * M[1][3] - M[1][0] * M[0][3];
227 s[3] = M[0][1] * M[1][2] - M[1][1] * M[0][2];
228 s[4] = M[0][1] * M[1][3] - M[1][1] * M[0][3];
229 s[5] = M[0][2] * M[1][3] - M[1][2] * M[0][3];
231 c[0] = M[2][0] * M[3][1] - M[3][0] * M[2][1];
232 c[1] = M[2][0] * M[3][2] - M[3][0] * M[2][2];
233 c[2] = M[2][0] * M[3][3] - M[3][0] * M[2][3];
234 c[3] = M[2][1] * M[3][2] - M[3][1] * M[2][2];
235 c[4] = M[2][1] * M[3][3] - M[3][1] * M[2][3];
236 c[5] = M[2][2] * M[3][3] - M[3][2] * M[2][3];
239 idet = 1.0f / (s[0] * c[5] - s[1] * c[4] + s[2] * c[3] + s[3] * c[2] -
240 s[4] * c[1] + s[5] * c[0]);
242 T[0][0] = (M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
243 T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
244 T[0][2] = (M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
245 T[0][3] = (-M[2][1] * s[5] + M[2][2] * s[4] - M[2][3] * s[3]) * idet;
247 T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
248 T[1][1] = (M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
249 T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
250 T[1][3] = (M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
252 T[2][0] = (M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
253 T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
254 T[2][2] = (M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
255 T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
257 T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
258 T[3][1] = (M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
259 T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
260 T[3][3] = (M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
262static inline void mat4x4_orthonormalize(mat4x4 R, mat4x4 M) {
267 vec3_norm(R[2], R[2]);
269 s = vec3_mul_inner(R[1], R[2]);
270 vec3_scale(h, R[2], s);
271 vec3_sub(R[1], R[1], h);
272 vec3_norm(R[2], R[2]);
274 s = vec3_mul_inner(R[1], R[2]);
275 vec3_scale(h, R[2], s);
276 vec3_sub(R[1], R[1], h);
277 vec3_norm(R[1], R[1]);
279 s = vec3_mul_inner(R[0], R[1]);
280 vec3_scale(h, R[1], s);
281 vec3_sub(R[0], R[0], h);
282 vec3_norm(R[0], R[0]);
285static inline void mat4x4_frustum(mat4x4 M,
float l,
float r,
float b,
float t,
287 M[0][0] = 2.f * n / (r - l);
288 M[0][1] = M[0][2] = M[0][3] = 0.f;
290 M[1][1] = 2.f * n / (t - b);
291 M[1][0] = M[1][2] = M[1][3] = 0.f;
293 M[2][0] = (r + l) / (r - l);
294 M[2][1] = (t + b) / (t - b);
295 M[2][2] = -(f + n) / (f - n);
298 M[3][2] = -2.f * (f * n) / (f - n);
299 M[3][0] = M[3][1] = M[3][3] = 0.f;
301static inline void mat4x4_ortho(mat4x4 M,
float l,
float r,
float b,
float t,
303 M[0][0] = 2.f / (r - l);
304 M[0][1] = M[0][2] = M[0][3] = 0.f;
306 M[1][1] = 2.f / (t - b);
307 M[1][0] = M[1][2] = M[1][3] = 0.f;
309 M[2][2] = -2.f / (f - n);
310 M[2][0] = M[2][1] = M[2][3] = 0.f;
312 M[3][0] = -(r + l) / (r - l);
313 M[3][1] = -(t + b) / (t - b);
314 M[3][2] = -(f + n) / (f - n);
317static inline void mat4x4_perspective(mat4x4 m,
float y_fov,
float aspect,
321 float const a = 1.f / (float)tan(y_fov / 2.f);
323 m[0][0] = a / aspect;
335 m[2][2] = -((f + n) / (f - n));
340 m[3][2] = -((2.f * f * n) / (f - n));
343static inline void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up) {
354 vec3_sub(f, center, eye);
357 vec3_mul_cross(s, f, up);
360 vec3_mul_cross(t, s, f);
382 mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]);
385typedef float quat[4];
386static inline void quat_identity(quat q) {
387 q[0] = q[1] = q[2] = 0.f;
390static inline void quat_add(quat r, quat a, quat b) {
392 for (i = 0; i < 4; ++i) r[i] = a[i] + b[i];
394static inline void quat_sub(quat r, quat a, quat b) {
396 for (i = 0; i < 4; ++i) r[i] = a[i] - b[i];
398static inline void quat_mul(quat r, quat p, quat q) {
400 vec3_mul_cross(r, p, q);
401 vec3_scale(w, p, q[3]);
403 vec3_scale(w, q, p[3]);
405 r[3] = p[3] * q[3] - vec3_mul_inner(p, q);
407static inline void quat_scale(quat r, quat v,
float s) {
409 for (i = 0; i < 4; ++i) r[i] = v[i] * s;
411static inline float quat_inner_product(quat a, quat b) {
414 for (i = 0; i < 4; ++i) p += b[i] * a[i];
417static inline void quat_conj(quat r, quat q) {
419 for (i = 0; i < 3; ++i) r[i] = -q[i];
422static inline void quat_rotate(quat r,
float angle, vec3 axis) {
425 vec3_scale(v, axis, sinf(angle / 2));
426 for (i = 0; i < 3; ++i) r[i] = v[i];
427 r[3] = cosf(angle / 2);
429#define quat_norm vec4_norm
430static inline void quat_mul_vec3(vec3 r, quat q, vec3 v) {
436 vec3 t = {q[0], q[1], q[2]};
437 vec3 u = {q[0], q[1], q[2]};
439 vec3_mul_cross(t, t, v);
442 vec3_mul_cross(u, u, t);
443 vec3_scale(t, t, q[3]);
448static inline void mat4x4_from_quat(mat4x4 M, quat q) {
458 M[0][0] = a2 + b2 - c2 - d2;
459 M[0][1] = 2.f * (b * c + a * d);
460 M[0][2] = 2.f * (b * d - a * c);
463 M[1][0] = 2 * (b * c - a * d);
464 M[1][1] = a2 - b2 + c2 - d2;
465 M[1][2] = 2.f * (c * d + a * b);
468 M[2][0] = 2.f * (b * d + a * c);
469 M[2][1] = 2.f * (c * d - a * b);
470 M[2][2] = a2 - b2 - c2 + d2;
473 M[3][0] = M[3][1] = M[3][2] = 0.f;
477static inline void mat4x4o_mul_quat(mat4x4 R, mat4x4 M, quat q) {
480 quat_mul_vec3(R[0], q, M[0]);
481 quat_mul_vec3(R[1], q, M[1]);
482 quat_mul_vec3(R[2], q, M[2]);
484 R[3][0] = R[3][1] = R[3][2] = 0.f;
487static inline void quat_from_mat4x4(quat q, mat4x4 M) {
491 int perm[] = {0, 1, 2, 0, 1};
494 for (i = 0; i < 3; i++) {
501 r = (float)sqrt(1.f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]]);
505 q[1] = q[2] = q[3] = 0.f;
510 q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]]) / (2.f * r);
511 q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]]) / (2.f * r);
512 q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]]) / (2.f * r);