blob: b60da859adb1bc30cde28aa919223f157b5ead87 [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
Karl Schultz481756e2016-02-02 15:37:51 -070014 0. You just DO WHAT THE **** YOU WANT TO.
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060015*/
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];
Karl Schultz481756e2016-02-02 15:37:51 -070029static inline void vec3_add(vec3 r, vec3 const a, vec3 const b) {
30 int i;
31 for (i = 0; i < 3; ++i)
32 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060033}
Karl Schultz481756e2016-02-02 15:37:51 -070034static inline void vec3_sub(vec3 r, vec3 const a, vec3 const b) {
35 int i;
36 for (i = 0; i < 3; ++i)
37 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060038}
Karl Schultz481756e2016-02-02 15:37:51 -070039static inline void vec3_scale(vec3 r, vec3 const v, float const s) {
40 int i;
41 for (i = 0; i < 3; ++i)
42 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060043}
Karl Schultz481756e2016-02-02 15:37:51 -070044static inline float vec3_mul_inner(vec3 const a, vec3 const b) {
45 float p = 0.f;
46 int i;
47 for (i = 0; i < 3; ++i)
48 p += b[i] * a[i];
49 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060050}
Karl Schultz481756e2016-02-02 15:37:51 -070051static inline void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b) {
52 r[0] = a[1] * b[2] - a[2] * b[1];
53 r[1] = a[2] * b[0] - a[0] * b[2];
54 r[2] = a[0] * b[1] - a[1] * b[0];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060055}
Karl Schultz481756e2016-02-02 15:37:51 -070056static inline float vec3_len(vec3 const v) {
57 return sqrtf(vec3_mul_inner(v, v));
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060058}
Karl Schultz481756e2016-02-02 15:37:51 -070059static inline void vec3_norm(vec3 r, vec3 const v) {
60 float k = 1.f / vec3_len(v);
61 vec3_scale(r, v, k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060062}
Karl Schultz481756e2016-02-02 15:37:51 -070063static inline void vec3_reflect(vec3 r, vec3 const v, vec3 const n) {
64 float p = 2.f * vec3_mul_inner(v, n);
65 int i;
66 for (i = 0; i < 3; ++i)
67 r[i] = v[i] - p * n[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060068}
69
70typedef float vec4[4];
Karl Schultz481756e2016-02-02 15:37:51 -070071static inline void vec4_add(vec4 r, vec4 const a, vec4 const b) {
72 int i;
73 for (i = 0; i < 4; ++i)
74 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060075}
Karl Schultz481756e2016-02-02 15:37:51 -070076static inline void vec4_sub(vec4 r, vec4 const a, vec4 const b) {
77 int i;
78 for (i = 0; i < 4; ++i)
79 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060080}
Karl Schultz481756e2016-02-02 15:37:51 -070081static inline void vec4_scale(vec4 r, vec4 v, float s) {
82 int i;
83 for (i = 0; i < 4; ++i)
84 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060085}
Karl Schultz481756e2016-02-02 15:37:51 -070086static inline float vec4_mul_inner(vec4 a, vec4 b) {
87 float p = 0.f;
88 int i;
89 for (i = 0; i < 4; ++i)
90 p += b[i] * a[i];
91 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060092}
Karl Schultz481756e2016-02-02 15:37:51 -070093static inline void vec4_mul_cross(vec4 r, vec4 a, vec4 b) {
94 r[0] = a[1] * b[2] - a[2] * b[1];
95 r[1] = a[2] * b[0] - a[0] * b[2];
96 r[2] = a[0] * b[1] - a[1] * b[0];
97 r[3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060098}
Karl Schultz481756e2016-02-02 15:37:51 -070099static inline float vec4_len(vec4 v) { return sqrtf(vec4_mul_inner(v, v)); }
100static inline void vec4_norm(vec4 r, vec4 v) {
101 float k = 1.f / vec4_len(v);
102 vec4_scale(r, v, k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600103}
Karl Schultz481756e2016-02-02 15:37:51 -0700104static inline void vec4_reflect(vec4 r, vec4 v, vec4 n) {
105 float p = 2.f * vec4_mul_inner(v, n);
106 int i;
107 for (i = 0; i < 4; ++i)
108 r[i] = v[i] - p * n[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600109}
110
111typedef vec4 mat4x4[4];
Karl Schultz481756e2016-02-02 15:37:51 -0700112static inline void mat4x4_identity(mat4x4 M) {
113 int i, j;
114 for (i = 0; i < 4; ++i)
115 for (j = 0; j < 4; ++j)
116 M[i][j] = i == j ? 1.f : 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600117}
Karl Schultz481756e2016-02-02 15:37:51 -0700118static inline void mat4x4_dup(mat4x4 M, mat4x4 N) {
119 int i, j;
120 for (i = 0; i < 4; ++i)
121 for (j = 0; j < 4; ++j)
122 M[i][j] = N[i][j];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600123}
Karl Schultz481756e2016-02-02 15:37:51 -0700124static inline void mat4x4_row(vec4 r, mat4x4 M, int i) {
125 int k;
126 for (k = 0; k < 4; ++k)
127 r[k] = M[k][i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600128}
Karl Schultz481756e2016-02-02 15:37:51 -0700129static inline void mat4x4_col(vec4 r, mat4x4 M, int i) {
130 int k;
131 for (k = 0; k < 4; ++k)
132 r[k] = M[i][k];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600133}
Karl Schultz481756e2016-02-02 15:37:51 -0700134static inline void mat4x4_transpose(mat4x4 M, mat4x4 N) {
135 int i, j;
136 for (j = 0; j < 4; ++j)
137 for (i = 0; i < 4; ++i)
138 M[i][j] = N[j][i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600139}
Karl Schultz481756e2016-02-02 15:37:51 -0700140static inline void mat4x4_add(mat4x4 M, mat4x4 a, mat4x4 b) {
141 int i;
142 for (i = 0; i < 4; ++i)
143 vec4_add(M[i], a[i], b[i]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600144}
Karl Schultz481756e2016-02-02 15:37:51 -0700145static inline void mat4x4_sub(mat4x4 M, mat4x4 a, mat4x4 b) {
146 int i;
147 for (i = 0; i < 4; ++i)
148 vec4_sub(M[i], a[i], b[i]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600149}
Karl Schultz481756e2016-02-02 15:37:51 -0700150static inline void mat4x4_scale(mat4x4 M, mat4x4 a, float k) {
151 int i;
152 for (i = 0; i < 4; ++i)
153 vec4_scale(M[i], a[i], k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600154}
Karl Schultz481756e2016-02-02 15:37:51 -0700155static inline void mat4x4_scale_aniso(mat4x4 M, mat4x4 a, float x, float y,
156 float z) {
157 int i;
158 vec4_scale(M[0], a[0], x);
159 vec4_scale(M[1], a[1], y);
160 vec4_scale(M[2], a[2], z);
161 for (i = 0; i < 4; ++i) {
162 M[3][i] = a[3][i];
163 }
164}
165static inline void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b) {
166 int k, r, c;
167 for (c = 0; c < 4; ++c)
168 for (r = 0; r < 4; ++r) {
169 M[c][r] = 0.f;
170 for (k = 0; k < 4; ++k)
171 M[c][r] += a[k][r] * b[c][k];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600172 }
173}
Karl Schultz481756e2016-02-02 15:37:51 -0700174static inline void mat4x4_mul_vec4(vec4 r, mat4x4 M, vec4 v) {
175 int i, j;
176 for (j = 0; j < 4; ++j) {
177 r[j] = 0.f;
178 for (i = 0; i < 4; ++i)
179 r[j] += M[i][j] * v[i];
180 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600181}
Karl Schultz481756e2016-02-02 15:37:51 -0700182static inline void mat4x4_translate(mat4x4 T, float x, float y, float z) {
183 mat4x4_identity(T);
184 T[3][0] = x;
185 T[3][1] = y;
186 T[3][2] = z;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600187}
Karl Schultz481756e2016-02-02 15:37:51 -0700188static inline void mat4x4_translate_in_place(mat4x4 M, float x, float y,
189 float z) {
190 vec4 t = {x, y, z, 0};
191 vec4 r;
192 int i;
193 for (i = 0; i < 4; ++i) {
194 mat4x4_row(r, M, i);
195 M[3][i] += vec4_mul_inner(r, t);
196 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600197}
Karl Schultz481756e2016-02-02 15:37:51 -0700198static inline void mat4x4_from_vec3_mul_outer(mat4x4 M, vec3 a, vec3 b) {
199 int i, j;
200 for (i = 0; i < 4; ++i)
201 for (j = 0; j < 4; ++j)
202 M[i][j] = i < 3 && j < 3 ? a[i] * b[j] : 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600203}
Karl Schultz481756e2016-02-02 15:37:51 -0700204static inline void mat4x4_rotate(mat4x4 R, mat4x4 M, float x, float y, float z,
205 float angle) {
206 float s = sinf(angle);
207 float c = cosf(angle);
208 vec3 u = {x, y, z};
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600209
Karl Schultz481756e2016-02-02 15:37:51 -0700210 if (vec3_len(u) > 1e-4) {
211 vec3_norm(u, u);
212 mat4x4 T;
213 mat4x4_from_vec3_mul_outer(T, u, u);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600214
Karl Schultz481756e2016-02-02 15:37:51 -0700215 mat4x4 S = {{0, u[2], -u[1], 0},
216 {-u[2], 0, u[0], 0},
217 {u[1], -u[0], 0, 0},
218 {0, 0, 0, 0}};
219 mat4x4_scale(S, S, s);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600220
Karl Schultz481756e2016-02-02 15:37:51 -0700221 mat4x4 C;
222 mat4x4_identity(C);
223 mat4x4_sub(C, C, T);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600224
Karl Schultz481756e2016-02-02 15:37:51 -0700225 mat4x4_scale(C, C, c);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600226
Karl Schultz481756e2016-02-02 15:37:51 -0700227 mat4x4_add(T, T, C);
228 mat4x4_add(T, T, S);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600229
Karl Schultz481756e2016-02-02 15:37:51 -0700230 T[3][3] = 1.;
231 mat4x4_mul(R, M, T);
232 } else {
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600233 mat4x4_dup(R, M);
Karl Schultz481756e2016-02-02 15:37:51 -0700234 }
235}
236static inline void mat4x4_rotate_X(mat4x4 Q, mat4x4 M, float angle) {
237 float s = sinf(angle);
238 float c = cosf(angle);
239 mat4x4 R = {{1.f, 0.f, 0.f, 0.f},
240 {0.f, c, s, 0.f},
241 {0.f, -s, c, 0.f},
242 {0.f, 0.f, 0.f, 1.f}};
243 mat4x4_mul(Q, M, R);
244}
245static inline void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M, float angle) {
246 float s = sinf(angle);
247 float c = cosf(angle);
248 mat4x4 R = {{c, 0.f, s, 0.f},
249 {0.f, 1.f, 0.f, 0.f},
250 {-s, 0.f, c, 0.f},
251 {0.f, 0.f, 0.f, 1.f}};
252 mat4x4_mul(Q, M, R);
253}
254static inline void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle) {
255 float s = sinf(angle);
256 float c = cosf(angle);
257 mat4x4 R = {{c, s, 0.f, 0.f},
258 {-s, c, 0.f, 0.f},
259 {0.f, 0.f, 1.f, 0.f},
260 {0.f, 0.f, 0.f, 1.f}};
261 mat4x4_mul(Q, M, R);
262}
263static inline void mat4x4_invert(mat4x4 T, mat4x4 M) {
264 float s[6];
265 float c[6];
266 s[0] = M[0][0] * M[1][1] - M[1][0] * M[0][1];
267 s[1] = M[0][0] * M[1][2] - M[1][0] * M[0][2];
268 s[2] = M[0][0] * M[1][3] - M[1][0] * M[0][3];
269 s[3] = M[0][1] * M[1][2] - M[1][1] * M[0][2];
270 s[4] = M[0][1] * M[1][3] - M[1][1] * M[0][3];
271 s[5] = M[0][2] * M[1][3] - M[1][2] * M[0][3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600272
Karl Schultz481756e2016-02-02 15:37:51 -0700273 c[0] = M[2][0] * M[3][1] - M[3][0] * M[2][1];
274 c[1] = M[2][0] * M[3][2] - M[3][0] * M[2][2];
275 c[2] = M[2][0] * M[3][3] - M[3][0] * M[2][3];
276 c[3] = M[2][1] * M[3][2] - M[3][1] * M[2][2];
277 c[4] = M[2][1] * M[3][3] - M[3][1] * M[2][3];
278 c[5] = M[2][2] * M[3][3] - M[3][2] * M[2][3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600279
Karl Schultz481756e2016-02-02 15:37:51 -0700280 /* Assumes it is invertible */
281 float idet = 1.0f / (s[0] * c[5] - s[1] * c[4] + s[2] * c[3] + s[3] * c[2] -
282 s[4] * c[1] + s[5] * c[0]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600283
Karl Schultz481756e2016-02-02 15:37:51 -0700284 T[0][0] = (M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
285 T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
286 T[0][2] = (M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
287 T[0][3] = (-M[2][1] * s[5] + M[2][2] * s[4] - M[2][3] * s[3]) * idet;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600288
Karl Schultz481756e2016-02-02 15:37:51 -0700289 T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
290 T[1][1] = (M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
291 T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
292 T[1][3] = (M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
293
294 T[2][0] = (M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
295 T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
296 T[2][2] = (M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
297 T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
298
299 T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
300 T[3][1] = (M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
301 T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
302 T[3][3] = (M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
303}
304static inline void mat4x4_orthonormalize(mat4x4 R, mat4x4 M) {
305 mat4x4_dup(R, M);
306 float s = 1.;
307 vec3 h;
308
309 vec3_norm(R[2], R[2]);
310
311 s = vec3_mul_inner(R[1], R[2]);
312 vec3_scale(h, R[2], s);
313 vec3_sub(R[1], R[1], h);
314 vec3_norm(R[2], R[2]);
315
316 s = vec3_mul_inner(R[1], R[2]);
317 vec3_scale(h, R[2], s);
318 vec3_sub(R[1], R[1], h);
319 vec3_norm(R[1], R[1]);
320
321 s = vec3_mul_inner(R[0], R[1]);
322 vec3_scale(h, R[1], s);
323 vec3_sub(R[0], R[0], h);
324 vec3_norm(R[0], R[0]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600325}
326
Karl Schultz481756e2016-02-02 15:37:51 -0700327static inline void mat4x4_frustum(mat4x4 M, float l, float r, float b, float t,
328 float n, float f) {
329 M[0][0] = 2.f * n / (r - l);
330 M[0][1] = M[0][2] = M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600331
Karl Schultz481756e2016-02-02 15:37:51 -0700332 M[1][1] = 2.f * n / (t - b);
333 M[1][0] = M[1][2] = M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600334
Karl Schultz481756e2016-02-02 15:37:51 -0700335 M[2][0] = (r + l) / (r - l);
336 M[2][1] = (t + b) / (t - b);
337 M[2][2] = -(f + n) / (f - n);
338 M[2][3] = -1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600339
Karl Schultz481756e2016-02-02 15:37:51 -0700340 M[3][2] = -2.f * (f * n) / (f - n);
341 M[3][0] = M[3][1] = M[3][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600342}
Karl Schultz481756e2016-02-02 15:37:51 -0700343static inline void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t,
344 float n, float f) {
345 M[0][0] = 2.f / (r - l);
346 M[0][1] = M[0][2] = M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600347
Karl Schultz481756e2016-02-02 15:37:51 -0700348 M[1][1] = 2.f / (t - b);
349 M[1][0] = M[1][2] = M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600350
Karl Schultz481756e2016-02-02 15:37:51 -0700351 M[2][2] = -2.f / (f - n);
352 M[2][0] = M[2][1] = M[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600353
Karl Schultz481756e2016-02-02 15:37:51 -0700354 M[3][0] = -(r + l) / (r - l);
355 M[3][1] = -(t + b) / (t - b);
356 M[3][2] = -(f + n) / (f - n);
357 M[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600358}
Karl Schultz481756e2016-02-02 15:37:51 -0700359static inline void mat4x4_perspective(mat4x4 m, float y_fov, float aspect,
360 float n, float f) {
361 /* NOTE: Degrees are an unhandy unit to work with.
362 * linmath.h uses radians for everything! */
363 float const a = (float)(1.f / tan(y_fov / 2.f));
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600364
Karl Schultz481756e2016-02-02 15:37:51 -0700365 m[0][0] = a / aspect;
366 m[0][1] = 0.f;
367 m[0][2] = 0.f;
368 m[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600369
Karl Schultz481756e2016-02-02 15:37:51 -0700370 m[1][0] = 0.f;
371 m[1][1] = a;
372 m[1][2] = 0.f;
373 m[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600374
Karl Schultz481756e2016-02-02 15:37:51 -0700375 m[2][0] = 0.f;
376 m[2][1] = 0.f;
377 m[2][2] = -((f + n) / (f - n));
378 m[2][3] = -1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600379
Karl Schultz481756e2016-02-02 15:37:51 -0700380 m[3][0] = 0.f;
381 m[3][1] = 0.f;
382 m[3][2] = -((2.f * f * n) / (f - n));
383 m[3][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600384}
Karl Schultz481756e2016-02-02 15:37:51 -0700385static inline void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up) {
386 /* Adapted from Android's OpenGL Matrix.java. */
387 /* See the OpenGL GLUT documentation for gluLookAt for a description */
388 /* of the algorithm. We implement it in a straightforward way: */
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600389
Karl Schultz481756e2016-02-02 15:37:51 -0700390 /* TODO: The negation of of can be spared by swapping the order of
391 * operands in the following cross products in the right way. */
392 vec3 f;
393 vec3_sub(f, center, eye);
394 vec3_norm(f, f);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600395
Karl Schultz481756e2016-02-02 15:37:51 -0700396 vec3 s;
397 vec3_mul_cross(s, f, up);
398 vec3_norm(s, s);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600399
Karl Schultz481756e2016-02-02 15:37:51 -0700400 vec3 t;
401 vec3_mul_cross(t, s, f);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600402
Karl Schultz481756e2016-02-02 15:37:51 -0700403 m[0][0] = s[0];
404 m[0][1] = t[0];
405 m[0][2] = -f[0];
406 m[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600407
Karl Schultz481756e2016-02-02 15:37:51 -0700408 m[1][0] = s[1];
409 m[1][1] = t[1];
410 m[1][2] = -f[1];
411 m[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600412
Karl Schultz481756e2016-02-02 15:37:51 -0700413 m[2][0] = s[2];
414 m[2][1] = t[2];
415 m[2][2] = -f[2];
416 m[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600417
Karl Schultz481756e2016-02-02 15:37:51 -0700418 m[3][0] = 0.f;
419 m[3][1] = 0.f;
420 m[3][2] = 0.f;
421 m[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600422
Karl Schultz481756e2016-02-02 15:37:51 -0700423 mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600424}
425
426typedef float quat[4];
Karl Schultz481756e2016-02-02 15:37:51 -0700427static inline void quat_identity(quat q) {
428 q[0] = q[1] = q[2] = 0.f;
429 q[3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600430}
Karl Schultz481756e2016-02-02 15:37:51 -0700431static inline void quat_add(quat r, quat a, quat b) {
432 int i;
433 for (i = 0; i < 4; ++i)
434 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600435}
Karl Schultz481756e2016-02-02 15:37:51 -0700436static inline void quat_sub(quat r, quat a, quat b) {
437 int i;
438 for (i = 0; i < 4; ++i)
439 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600440}
Karl Schultz481756e2016-02-02 15:37:51 -0700441static inline void quat_mul(quat r, quat p, quat q) {
442 vec3 w;
443 vec3_mul_cross(r, p, q);
444 vec3_scale(w, p, q[3]);
445 vec3_add(r, r, w);
446 vec3_scale(w, q, p[3]);
447 vec3_add(r, r, w);
448 r[3] = p[3] * q[3] - vec3_mul_inner(p, q);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600449}
Karl Schultz481756e2016-02-02 15:37:51 -0700450static inline void quat_scale(quat r, quat v, float s) {
451 int i;
452 for (i = 0; i < 4; ++i)
453 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600454}
Karl Schultz481756e2016-02-02 15:37:51 -0700455static inline float quat_inner_product(quat a, quat b) {
456 float p = 0.f;
457 int i;
458 for (i = 0; i < 4; ++i)
459 p += b[i] * a[i];
460 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600461}
Karl Schultz481756e2016-02-02 15:37:51 -0700462static inline void quat_conj(quat r, quat q) {
463 int i;
464 for (i = 0; i < 3; ++i)
465 r[i] = -q[i];
466 r[3] = q[3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600467}
468#define quat_norm vec4_norm
Karl Schultz481756e2016-02-02 15:37:51 -0700469static inline void quat_mul_vec3(vec3 r, quat q, vec3 v) {
470 quat v_ = {v[0], v[1], v[2], 0.f};
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600471
Karl Schultz481756e2016-02-02 15:37:51 -0700472 quat_conj(r, q);
473 quat_norm(r, r);
474 quat_mul(r, v_, r);
475 quat_mul(r, q, r);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600476}
Karl Schultz481756e2016-02-02 15:37:51 -0700477static inline void mat4x4_from_quat(mat4x4 M, quat q) {
478 float a = q[3];
479 float b = q[0];
480 float c = q[1];
481 float d = q[2];
482 float a2 = a * a;
483 float b2 = b * b;
484 float c2 = c * c;
485 float d2 = d * d;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600486
Karl Schultz481756e2016-02-02 15:37:51 -0700487 M[0][0] = a2 + b2 - c2 - d2;
488 M[0][1] = 2.f * (b * c + a * d);
489 M[0][2] = 2.f * (b * d - a * c);
490 M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600491
Karl Schultz481756e2016-02-02 15:37:51 -0700492 M[1][0] = 2 * (b * c - a * d);
493 M[1][1] = a2 - b2 + c2 - d2;
494 M[1][2] = 2.f * (c * d + a * b);
495 M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600496
Karl Schultz481756e2016-02-02 15:37:51 -0700497 M[2][0] = 2.f * (b * d + a * c);
498 M[2][1] = 2.f * (c * d - a * b);
499 M[2][2] = a2 - b2 - c2 + d2;
500 M[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600501
Karl Schultz481756e2016-02-02 15:37:51 -0700502 M[3][0] = M[3][1] = M[3][2] = 0.f;
503 M[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600504}
505
Karl Schultz481756e2016-02-02 15:37:51 -0700506static inline void mat4x4o_mul_quat(mat4x4 R, mat4x4 M, quat q) {
507 /* XXX: The way this is written only works for othogonal matrices. */
508 /* TODO: Take care of non-orthogonal case. */
509 quat_mul_vec3(R[0], q, M[0]);
510 quat_mul_vec3(R[1], q, M[1]);
511 quat_mul_vec3(R[2], q, M[2]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600512
Karl Schultz481756e2016-02-02 15:37:51 -0700513 R[3][0] = R[3][1] = R[3][2] = 0.f;
514 R[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600515}
Karl Schultz481756e2016-02-02 15:37:51 -0700516static inline void quat_from_mat4x4(quat q, mat4x4 M) {
517 float r = 0.f;
518 int i;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600519
Karl Schultz481756e2016-02-02 15:37:51 -0700520 int perm[] = {0, 1, 2, 0, 1};
521 int *p = perm;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600522
Karl Schultz481756e2016-02-02 15:37:51 -0700523 for (i = 0; i < 3; i++) {
524 float m = M[i][i];
525 if (m < r)
526 continue;
527 m = r;
528 p = &perm[i];
529 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600530
Karl Schultz481756e2016-02-02 15:37:51 -0700531 r = sqrtf(1.f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600532
Karl Schultz481756e2016-02-02 15:37:51 -0700533 if (r < 1e-6) {
534 q[0] = 1.f;
535 q[1] = q[2] = q[3] = 0.f;
536 return;
537 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600538
Karl Schultz481756e2016-02-02 15:37:51 -0700539 q[0] = r / 2.f;
540 q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]]) / (2.f * r);
541 q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]]) / (2.f * r);
542 q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]]) / (2.f * r);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600543}
544
545#endif