blob: 5de34e9337d2c02e7b3226d9bafd6a7e3c669c05 [file] [log] [blame]
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -06001/*
2 DO WHAT THE **** YOU WANT TO PUBLIC LICENSE
3 Version 2, December 2004
4
5 Copyright (C) 2013 Wolfgang 'datenwolf' Draxinger <code@datenwolf.net>
6
7 Everyone is permitted to copy and distribute verbatim or modified
8 copies of this license document, and changing it is allowed as long
9 as the name is changed.
10
11 DO WHAT THE **** YOU WANT TO PUBLIC LICENSE
12 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
13
14 0. You just DO WHAT THE FUCK YOU WANT TO.
15*/
16
17#ifndef LINMATH_H
18#define LINMATH_H
19
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060020#include <math.h>
21
22// Converts degrees to radians.
23#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
24
25// Converts radians to degrees.
26#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
27
28typedef float vec3[3];
29static inline void vec3_add(vec3 r, vec3 const a, vec3 const b)
30{
31 int i;
32 for(i=0; i<3; ++i)
33 r[i] = a[i] + b[i];
34}
35static inline void vec3_sub(vec3 r, vec3 const a, vec3 const b)
36{
37 int i;
38 for(i=0; i<3; ++i)
39 r[i] = a[i] - b[i];
40}
41static inline void vec3_scale(vec3 r, vec3 const v, float const s)
42{
43 int i;
44 for(i=0; i<3; ++i)
45 r[i] = v[i] * s;
46}
47static inline float vec3_mul_inner(vec3 const a, vec3 const b)
48{
49 float p = 0.f;
50 int i;
51 for(i=0; i<3; ++i)
52 p += b[i]*a[i];
53 return p;
54}
55static inline void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b)
56{
57 r[0] = a[1]*b[2] - a[2]*b[1];
58 r[1] = a[2]*b[0] - a[0]*b[2];
59 r[2] = a[0]*b[1] - a[1]*b[0];
60}
61static inline float vec3_len(vec3 const v)
62{
63 return sqrtf(vec3_mul_inner(v, v));
64}
65static inline void vec3_norm(vec3 r, vec3 const v)
66{
67 float k = 1.f / vec3_len(v);
68 vec3_scale(r, v, k);
69}
70static inline void vec3_reflect(vec3 r, vec3 const v, vec3 const n)
71{
72 float p = 2.f*vec3_mul_inner(v, n);
73 int i;
74 for(i=0;i<3;++i)
75 r[i] = v[i] - p*n[i];
76}
77
78typedef float vec4[4];
79static inline void vec4_add(vec4 r, vec4 const a, vec4 const b)
80{
81 int i;
82 for(i=0; i<4; ++i)
83 r[i] = a[i] + b[i];
84}
85static inline void vec4_sub(vec4 r, vec4 const a, vec4 const b)
86{
87 int i;
88 for(i=0; i<4; ++i)
89 r[i] = a[i] - b[i];
90}
91static inline void vec4_scale(vec4 r, vec4 v, float s)
92{
93 int i;
94 for(i=0; i<4; ++i)
95 r[i] = v[i] * s;
96}
97static inline float vec4_mul_inner(vec4 a, vec4 b)
98{
99 float p = 0.f;
100 int i;
101 for(i=0; i<4; ++i)
102 p += b[i]*a[i];
103 return p;
104}
105static inline void vec4_mul_cross(vec4 r, vec4 a, vec4 b)
106{
107 r[0] = a[1]*b[2] - a[2]*b[1];
108 r[1] = a[2]*b[0] - a[0]*b[2];
109 r[2] = a[0]*b[1] - a[1]*b[0];
110 r[3] = 1.f;
111}
112static inline float vec4_len(vec4 v)
113{
114 return sqrtf(vec4_mul_inner(v, v));
115}
116static inline void vec4_norm(vec4 r, vec4 v)
117{
118 float k = 1.f / vec4_len(v);
119 vec4_scale(r, v, k);
120}
121static inline void vec4_reflect(vec4 r, vec4 v, vec4 n)
122{
123 float p = 2.f*vec4_mul_inner(v, n);
124 int i;
125 for(i=0;i<4;++i)
126 r[i] = v[i] - p*n[i];
127}
128
129typedef vec4 mat4x4[4];
130static inline void mat4x4_identity(mat4x4 M)
131{
132 int i, j;
133 for(i=0; i<4; ++i)
134 for(j=0; j<4; ++j)
135 M[i][j] = i==j ? 1.f : 0.f;
136}
137static inline void mat4x4_dup(mat4x4 M, mat4x4 N)
138{
139 int i, j;
140 for(i=0; i<4; ++i)
141 for(j=0; j<4; ++j)
142 M[i][j] = N[i][j];
143}
144static inline void mat4x4_row(vec4 r, mat4x4 M, int i)
145{
146 int k;
147 for(k=0; k<4; ++k)
148 r[k] = M[k][i];
149}
150static inline void mat4x4_col(vec4 r, mat4x4 M, int i)
151{
152 int k;
153 for(k=0; k<4; ++k)
154 r[k] = M[i][k];
155}
156static inline void mat4x4_transpose(mat4x4 M, mat4x4 N)
157{
158 int i, j;
159 for(j=0; j<4; ++j)
160 for(i=0; i<4; ++i)
161 M[i][j] = N[j][i];
162}
163static inline void mat4x4_add(mat4x4 M, mat4x4 a, mat4x4 b)
164{
165 int i;
166 for(i=0; i<4; ++i)
167 vec4_add(M[i], a[i], b[i]);
168}
169static inline void mat4x4_sub(mat4x4 M, mat4x4 a, mat4x4 b)
170{
171 int i;
172 for(i=0; i<4; ++i)
173 vec4_sub(M[i], a[i], b[i]);
174}
175static inline void mat4x4_scale(mat4x4 M, mat4x4 a, float k)
176{
177 int i;
178 for(i=0; i<4; ++i)
179 vec4_scale(M[i], a[i], k);
180}
181static inline void mat4x4_scale_aniso(mat4x4 M, mat4x4 a, float x, float y, float z)
182{
183 int i;
184 vec4_scale(M[0], a[0], x);
185 vec4_scale(M[1], a[1], y);
186 vec4_scale(M[2], a[2], z);
187 for(i = 0; i < 4; ++i) {
188 M[3][i] = a[3][i];
189 }
190}
191static inline void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b)
192{
193 int k, r, c;
194 for(c=0; c<4; ++c) for(r=0; r<4; ++r) {
195 M[c][r] = 0.f;
196 for(k=0; k<4; ++k)
197 M[c][r] += a[k][r] * b[c][k];
198 }
199}
200static inline void mat4x4_mul_vec4(vec4 r, mat4x4 M, vec4 v)
201{
202 int i, j;
203 for(j=0; j<4; ++j) {
204 r[j] = 0.f;
205 for(i=0; i<4; ++i)
206 r[j] += M[i][j] * v[i];
207 }
208}
209static inline void mat4x4_translate(mat4x4 T, float x, float y, float z)
210{
211 mat4x4_identity(T);
212 T[3][0] = x;
213 T[3][1] = y;
214 T[3][2] = z;
215}
216static inline void mat4x4_translate_in_place(mat4x4 M, float x, float y, float z)
217{
218 vec4 t = {x, y, z, 0};
219 vec4 r;
220 int i;
221 for (i = 0; i < 4; ++i) {
222 mat4x4_row(r, M, i);
223 M[3][i] += vec4_mul_inner(r, t);
224 }
225}
226static inline void mat4x4_from_vec3_mul_outer(mat4x4 M, vec3 a, vec3 b)
227{
228 int i, j;
229 for(i=0; i<4; ++i) for(j=0; j<4; ++j)
230 M[i][j] = i<3 && j<3 ? a[i] * b[j] : 0.f;
231}
232static inline void mat4x4_rotate(mat4x4 R, mat4x4 M, float x, float y, float z, float angle)
233{
234 float s = sinf(angle);
235 float c = cosf(angle);
236 vec3 u = {x, y, z};
237
238 if(vec3_len(u) > 1e-4) {
239 vec3_norm(u, u);
240 mat4x4 T;
241 mat4x4_from_vec3_mul_outer(T, u, u);
242
243 mat4x4 S = {
244 { 0, u[2], -u[1], 0},
245 {-u[2], 0, u[0], 0},
246 { u[1], -u[0], 0, 0},
247 { 0, 0, 0, 0}
248 };
249 mat4x4_scale(S, S, s);
250
251 mat4x4 C;
252 mat4x4_identity(C);
253 mat4x4_sub(C, C, T);
254
255 mat4x4_scale(C, C, c);
256
257 mat4x4_add(T, T, C);
258 mat4x4_add(T, T, S);
259
260 T[3][3] = 1.;
261 mat4x4_mul(R, M, T);
262 } else {
263 mat4x4_dup(R, M);
264 }
265}
266static inline void mat4x4_rotate_X(mat4x4 Q, mat4x4 M, float angle)
267{
268 float s = sinf(angle);
269 float c = cosf(angle);
270 mat4x4 R = {
271 {1.f, 0.f, 0.f, 0.f},
272 {0.f, c, s, 0.f},
273 {0.f, -s, c, 0.f},
274 {0.f, 0.f, 0.f, 1.f}
275 };
276 mat4x4_mul(Q, M, R);
277}
278static inline void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M, float angle)
279{
280 float s = sinf(angle);
281 float c = cosf(angle);
282 mat4x4 R = {
283 { c, 0.f, s, 0.f},
284 { 0.f, 1.f, 0.f, 0.f},
285 { -s, 0.f, c, 0.f},
286 { 0.f, 0.f, 0.f, 1.f}
287 };
288 mat4x4_mul(Q, M, R);
289}
290static inline void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle)
291{
292 float s = sinf(angle);
293 float c = cosf(angle);
294 mat4x4 R = {
295 { c, s, 0.f, 0.f},
296 { -s, c, 0.f, 0.f},
297 { 0.f, 0.f, 1.f, 0.f},
298 { 0.f, 0.f, 0.f, 1.f}
299 };
300 mat4x4_mul(Q, M, R);
301}
302static inline void mat4x4_invert(mat4x4 T, mat4x4 M)
303{
304 float s[6];
305 float c[6];
306 s[0] = M[0][0]*M[1][1] - M[1][0]*M[0][1];
307 s[1] = M[0][0]*M[1][2] - M[1][0]*M[0][2];
308 s[2] = M[0][0]*M[1][3] - M[1][0]*M[0][3];
309 s[3] = M[0][1]*M[1][2] - M[1][1]*M[0][2];
310 s[4] = M[0][1]*M[1][3] - M[1][1]*M[0][3];
311 s[5] = M[0][2]*M[1][3] - M[1][2]*M[0][3];
312
313 c[0] = M[2][0]*M[3][1] - M[3][0]*M[2][1];
314 c[1] = M[2][0]*M[3][2] - M[3][0]*M[2][2];
315 c[2] = M[2][0]*M[3][3] - M[3][0]*M[2][3];
316 c[3] = M[2][1]*M[3][2] - M[3][1]*M[2][2];
317 c[4] = M[2][1]*M[3][3] - M[3][1]*M[2][3];
318 c[5] = M[2][2]*M[3][3] - M[3][2]*M[2][3];
319
320 /* Assumes it is invertible */
321 float 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] );
322
323 T[0][0] = ( M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
324 T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
325 T[0][2] = ( M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
326 T[0][3] = (-M[2][1] * s[5] + M[2][2] * s[4] - M[2][3] * s[3]) * idet;
327
328 T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
329 T[1][1] = ( M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
330 T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
331 T[1][3] = ( M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
332
333 T[2][0] = ( M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
334 T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
335 T[2][2] = ( M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
336 T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
337
338 T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
339 T[3][1] = ( M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
340 T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
341 T[3][3] = ( M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
342}
343static inline void mat4x4_orthonormalize(mat4x4 R, mat4x4 M)
344{
345 mat4x4_dup(R, M);
346 float s = 1.;
347 vec3 h;
348
349 vec3_norm(R[2], R[2]);
350
351 s = vec3_mul_inner(R[1], R[2]);
352 vec3_scale(h, R[2], s);
353 vec3_sub(R[1], R[1], h);
354 vec3_norm(R[2], R[2]);
355
356 s = vec3_mul_inner(R[1], R[2]);
357 vec3_scale(h, R[2], s);
358 vec3_sub(R[1], R[1], h);
359 vec3_norm(R[1], R[1]);
360
361 s = vec3_mul_inner(R[0], R[1]);
362 vec3_scale(h, R[1], s);
363 vec3_sub(R[0], R[0], h);
364 vec3_norm(R[0], R[0]);
365}
366
367static inline void mat4x4_frustum(mat4x4 M, float l, float r, float b, float t, float n, float f)
368{
369 M[0][0] = 2.f*n/(r-l);
370 M[0][1] = M[0][2] = M[0][3] = 0.f;
371
Piers Daniell886be472015-02-23 16:23:13 -0700372 M[1][1] = 2.f*n/(t-b);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600373 M[1][0] = M[1][2] = M[1][3] = 0.f;
374
375 M[2][0] = (r+l)/(r-l);
376 M[2][1] = (t+b)/(t-b);
377 M[2][2] = -(f+n)/(f-n);
378 M[2][3] = -1.f;
379
380 M[3][2] = -2.f*(f*n)/(f-n);
381 M[3][0] = M[3][1] = M[3][3] = 0.f;
382}
383static inline void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f)
384{
385 M[0][0] = 2.f/(r-l);
386 M[0][1] = M[0][2] = M[0][3] = 0.f;
387
388 M[1][1] = 2.f/(t-b);
389 M[1][0] = M[1][2] = M[1][3] = 0.f;
390
391 M[2][2] = -2.f/(f-n);
392 M[2][0] = M[2][1] = M[2][3] = 0.f;
393
394 M[3][0] = -(r+l)/(r-l);
395 M[3][1] = -(t+b)/(t-b);
396 M[3][2] = -(f+n)/(f-n);
397 M[3][3] = 1.f;
398}
399static inline void mat4x4_perspective(mat4x4 m, float y_fov, float aspect, float n, float f)
400{
401 /* NOTE: Degrees are an unhandy unit to work with.
402 * linmath.h uses radians for everything! */
Piers Daniell886be472015-02-23 16:23:13 -0700403 float const a = (float)(1.f / tan(y_fov / 2.f));
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600404
405 m[0][0] = a / aspect;
406 m[0][1] = 0.f;
407 m[0][2] = 0.f;
408 m[0][3] = 0.f;
409
410 m[1][0] = 0.f;
411 m[1][1] = a;
412 m[1][2] = 0.f;
413 m[1][3] = 0.f;
414
415 m[2][0] = 0.f;
416 m[2][1] = 0.f;
417 m[2][2] = -((f + n) / (f - n));
418 m[2][3] = -1.f;
419
420 m[3][0] = 0.f;
421 m[3][1] = 0.f;
422 m[3][2] = -((2.f * f * n) / (f - n));
423 m[3][3] = 0.f;
424}
425static inline void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up)
426{
427 /* Adapted from Android's OpenGL Matrix.java. */
428 /* See the OpenGL GLUT documentation for gluLookAt for a description */
429 /* of the algorithm. We implement it in a straightforward way: */
430
431 /* TODO: The negation of of can be spared by swapping the order of
432 * operands in the following cross products in the right way. */
433 vec3 f;
434 vec3_sub(f, center, eye);
435 vec3_norm(f, f);
436
437 vec3 s;
438 vec3_mul_cross(s, f, up);
439 vec3_norm(s, s);
440
441 vec3 t;
442 vec3_mul_cross(t, s, f);
443
444 m[0][0] = s[0];
445 m[0][1] = t[0];
446 m[0][2] = -f[0];
447 m[0][3] = 0.f;
448
449 m[1][0] = s[1];
450 m[1][1] = t[1];
451 m[1][2] = -f[1];
452 m[1][3] = 0.f;
453
454 m[2][0] = s[2];
455 m[2][1] = t[2];
456 m[2][2] = -f[2];
457 m[2][3] = 0.f;
458
459 m[3][0] = 0.f;
460 m[3][1] = 0.f;
461 m[3][2] = 0.f;
462 m[3][3] = 1.f;
463
464 mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]);
465}
466
467typedef float quat[4];
468static inline void quat_identity(quat q)
469{
470 q[0] = q[1] = q[2] = 0.f;
471 q[3] = 1.f;
472}
473static inline void quat_add(quat r, quat a, quat b)
474{
475 int i;
476 for(i=0; i<4; ++i)
477 r[i] = a[i] + b[i];
478}
479static inline void quat_sub(quat r, quat a, quat b)
480{
481 int i;
482 for(i=0; i<4; ++i)
483 r[i] = a[i] - b[i];
484}
485static inline void quat_mul(quat r, quat p, quat q)
486{
487 vec3 w;
488 vec3_mul_cross(r, p, q);
489 vec3_scale(w, p, q[3]);
490 vec3_add(r, r, w);
491 vec3_scale(w, q, p[3]);
492 vec3_add(r, r, w);
493 r[3] = p[3]*q[3] - vec3_mul_inner(p, q);
494}
495static inline void quat_scale(quat r, quat v, float s)
496{
497 int i;
498 for(i=0; i<4; ++i)
499 r[i] = v[i] * s;
500}
501static inline float quat_inner_product(quat a, quat b)
502{
503 float p = 0.f;
504 int i;
505 for(i=0; i<4; ++i)
506 p += b[i]*a[i];
507 return p;
508}
509static inline void quat_conj(quat r, quat q)
510{
511 int i;
512 for(i=0; i<3; ++i)
513 r[i] = -q[i];
514 r[3] = q[3];
515}
516#define quat_norm vec4_norm
517static inline void quat_mul_vec3(vec3 r, quat q, vec3 v)
518{
519 quat v_ = {v[0], v[1], v[2], 0.f};
520
521 quat_conj(r, q);
522 quat_norm(r, r);
523 quat_mul(r, v_, r);
524 quat_mul(r, q, r);
525}
526static inline void mat4x4_from_quat(mat4x4 M, quat q)
527{
528 float a = q[3];
529 float b = q[0];
530 float c = q[1];
531 float d = q[2];
532 float a2 = a*a;
533 float b2 = b*b;
534 float c2 = c*c;
535 float d2 = d*d;
536
537 M[0][0] = a2 + b2 - c2 - d2;
538 M[0][1] = 2.f*(b*c + a*d);
539 M[0][2] = 2.f*(b*d - a*c);
540 M[0][3] = 0.f;
541
542 M[1][0] = 2*(b*c - a*d);
543 M[1][1] = a2 - b2 + c2 - d2;
544 M[1][2] = 2.f*(c*d + a*b);
545 M[1][3] = 0.f;
546
547 M[2][0] = 2.f*(b*d + a*c);
548 M[2][1] = 2.f*(c*d - a*b);
549 M[2][2] = a2 - b2 - c2 + d2;
550 M[2][3] = 0.f;
551
552 M[3][0] = M[3][1] = M[3][2] = 0.f;
553 M[3][3] = 1.f;
554}
555
556static inline void mat4x4o_mul_quat(mat4x4 R, mat4x4 M, quat q)
557{
558/* XXX: The way this is written only works for othogonal matrices. */
559/* TODO: Take care of non-orthogonal case. */
560 quat_mul_vec3(R[0], q, M[0]);
561 quat_mul_vec3(R[1], q, M[1]);
562 quat_mul_vec3(R[2], q, M[2]);
563
564 R[3][0] = R[3][1] = R[3][2] = 0.f;
565 R[3][3] = 1.f;
566}
567static inline void quat_from_mat4x4(quat q, mat4x4 M)
568{
569 float r=0.f;
570 int i;
571
572 int perm[] = { 0, 1, 2, 0, 1 };
573 int *p = perm;
574
575 for(i = 0; i<3; i++) {
576 float m = M[i][i];
577 if( m < r )
578 continue;
579 m = r;
580 p = &perm[i];
581 }
582
583 r = sqrtf(1.f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]] );
584
585 if(r < 1e-6) {
586 q[0] = 1.f;
587 q[1] = q[2] = q[3] = 0.f;
588 return;
589 }
590
591 q[0] = r/2.f;
592 q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]])/(2.f*r);
593 q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]])/(2.f*r);
594 q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]])/(2.f*r);
595}
596
597#endif