blob: d558c40c80a872f1f83a5240c24a56ffb04e3254 [file] [log] [blame]
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -06001/*
Karl Schultzdae791d2016-02-04 10:29:00 -07002 * Copyright (c) 2015-2016 The Khronos Group Inc.
3 * Copyright (c) 2015-2016 Valve Corporation
4 * Copyright (c) 2015-2016 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and/or associated documentation files (the "Materials"), to
8 * deal in the Materials without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Materials, and to permit persons to whom the Materials are
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice(s) and this permission notice shall be included in
14 * all copies or substantial portions of the Materials.
15 *
16 * The Materials are Confidential Information as defined by the Khronos
17 * Membership Agreement until designated non-confidential by Khronos, at which
18 * point this condition clause shall be removed.
19 *
20 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 *
24 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
26 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
27 * USE OR OTHER DEALINGS IN THE MATERIALS.
28 *
29 * Relicensed from the WTFPL (http://www.wtfpl.net/faq/).
30 */
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060031
32#ifndef LINMATH_H
33#define LINMATH_H
34
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060035#include <math.h>
36
37// Converts degrees to radians.
38#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
39
40// Converts radians to degrees.
41#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
42
43typedef float vec3[3];
Karl Schultz481756e2016-02-02 15:37:51 -070044static inline void vec3_add(vec3 r, vec3 const a, vec3 const b) {
45 int i;
46 for (i = 0; i < 3; ++i)
47 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060048}
Karl Schultz481756e2016-02-02 15:37:51 -070049static inline void vec3_sub(vec3 r, vec3 const a, vec3 const b) {
50 int i;
51 for (i = 0; i < 3; ++i)
52 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060053}
Karl Schultz481756e2016-02-02 15:37:51 -070054static inline void vec3_scale(vec3 r, vec3 const v, float const s) {
55 int i;
56 for (i = 0; i < 3; ++i)
57 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060058}
Karl Schultz481756e2016-02-02 15:37:51 -070059static inline float vec3_mul_inner(vec3 const a, vec3 const b) {
60 float p = 0.f;
61 int i;
62 for (i = 0; i < 3; ++i)
63 p += b[i] * a[i];
64 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060065}
Karl Schultz481756e2016-02-02 15:37:51 -070066static inline void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b) {
67 r[0] = a[1] * b[2] - a[2] * b[1];
68 r[1] = a[2] * b[0] - a[0] * b[2];
69 r[2] = a[0] * b[1] - a[1] * b[0];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060070}
Karl Schultz481756e2016-02-02 15:37:51 -070071static inline float vec3_len(vec3 const v) {
72 return sqrtf(vec3_mul_inner(v, v));
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060073}
Karl Schultz481756e2016-02-02 15:37:51 -070074static inline void vec3_norm(vec3 r, vec3 const v) {
75 float k = 1.f / vec3_len(v);
76 vec3_scale(r, v, k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060077}
Karl Schultz481756e2016-02-02 15:37:51 -070078static inline void vec3_reflect(vec3 r, vec3 const v, vec3 const n) {
79 float p = 2.f * vec3_mul_inner(v, n);
80 int i;
81 for (i = 0; i < 3; ++i)
82 r[i] = v[i] - p * n[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060083}
84
85typedef float vec4[4];
Karl Schultz481756e2016-02-02 15:37:51 -070086static inline void vec4_add(vec4 r, vec4 const a, vec4 const b) {
87 int i;
88 for (i = 0; i < 4; ++i)
89 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060090}
Karl Schultz481756e2016-02-02 15:37:51 -070091static inline void vec4_sub(vec4 r, vec4 const a, vec4 const b) {
92 int i;
93 for (i = 0; i < 4; ++i)
94 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060095}
Karl Schultz481756e2016-02-02 15:37:51 -070096static inline void vec4_scale(vec4 r, vec4 v, float s) {
97 int i;
98 for (i = 0; i < 4; ++i)
99 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600100}
Karl Schultz481756e2016-02-02 15:37:51 -0700101static inline float vec4_mul_inner(vec4 a, vec4 b) {
102 float p = 0.f;
103 int i;
104 for (i = 0; i < 4; ++i)
105 p += b[i] * a[i];
106 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600107}
Karl Schultz481756e2016-02-02 15:37:51 -0700108static inline void vec4_mul_cross(vec4 r, vec4 a, vec4 b) {
109 r[0] = a[1] * b[2] - a[2] * b[1];
110 r[1] = a[2] * b[0] - a[0] * b[2];
111 r[2] = a[0] * b[1] - a[1] * b[0];
112 r[3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600113}
Karl Schultz481756e2016-02-02 15:37:51 -0700114static inline float vec4_len(vec4 v) { return sqrtf(vec4_mul_inner(v, v)); }
115static inline void vec4_norm(vec4 r, vec4 v) {
116 float k = 1.f / vec4_len(v);
117 vec4_scale(r, v, k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600118}
Karl Schultz481756e2016-02-02 15:37:51 -0700119static inline void vec4_reflect(vec4 r, vec4 v, vec4 n) {
120 float p = 2.f * vec4_mul_inner(v, n);
121 int i;
122 for (i = 0; i < 4; ++i)
123 r[i] = v[i] - p * n[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600124}
125
126typedef vec4 mat4x4[4];
Karl Schultz481756e2016-02-02 15:37:51 -0700127static inline void mat4x4_identity(mat4x4 M) {
128 int i, j;
129 for (i = 0; i < 4; ++i)
130 for (j = 0; j < 4; ++j)
131 M[i][j] = i == j ? 1.f : 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600132}
Karl Schultz481756e2016-02-02 15:37:51 -0700133static inline void mat4x4_dup(mat4x4 M, mat4x4 N) {
134 int i, j;
135 for (i = 0; i < 4; ++i)
136 for (j = 0; j < 4; ++j)
137 M[i][j] = N[i][j];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600138}
Karl Schultz481756e2016-02-02 15:37:51 -0700139static inline void mat4x4_row(vec4 r, mat4x4 M, int i) {
140 int k;
141 for (k = 0; k < 4; ++k)
142 r[k] = M[k][i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600143}
Karl Schultz481756e2016-02-02 15:37:51 -0700144static inline void mat4x4_col(vec4 r, mat4x4 M, int i) {
145 int k;
146 for (k = 0; k < 4; ++k)
147 r[k] = M[i][k];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600148}
Karl Schultz481756e2016-02-02 15:37:51 -0700149static inline void mat4x4_transpose(mat4x4 M, mat4x4 N) {
150 int i, j;
151 for (j = 0; j < 4; ++j)
152 for (i = 0; i < 4; ++i)
153 M[i][j] = N[j][i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600154}
Karl Schultz481756e2016-02-02 15:37:51 -0700155static inline void mat4x4_add(mat4x4 M, mat4x4 a, mat4x4 b) {
156 int i;
157 for (i = 0; i < 4; ++i)
158 vec4_add(M[i], a[i], b[i]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600159}
Karl Schultz481756e2016-02-02 15:37:51 -0700160static inline void mat4x4_sub(mat4x4 M, mat4x4 a, mat4x4 b) {
161 int i;
162 for (i = 0; i < 4; ++i)
163 vec4_sub(M[i], a[i], b[i]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600164}
Karl Schultz481756e2016-02-02 15:37:51 -0700165static inline void mat4x4_scale(mat4x4 M, mat4x4 a, float k) {
166 int i;
167 for (i = 0; i < 4; ++i)
168 vec4_scale(M[i], a[i], k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600169}
Karl Schultz481756e2016-02-02 15:37:51 -0700170static inline void mat4x4_scale_aniso(mat4x4 M, mat4x4 a, float x, float y,
171 float z) {
172 int i;
173 vec4_scale(M[0], a[0], x);
174 vec4_scale(M[1], a[1], y);
175 vec4_scale(M[2], a[2], z);
176 for (i = 0; i < 4; ++i) {
177 M[3][i] = a[3][i];
178 }
179}
180static inline void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b) {
181 int k, r, c;
182 for (c = 0; c < 4; ++c)
183 for (r = 0; r < 4; ++r) {
184 M[c][r] = 0.f;
185 for (k = 0; k < 4; ++k)
186 M[c][r] += a[k][r] * b[c][k];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600187 }
188}
Karl Schultz481756e2016-02-02 15:37:51 -0700189static inline void mat4x4_mul_vec4(vec4 r, mat4x4 M, vec4 v) {
190 int i, j;
191 for (j = 0; j < 4; ++j) {
192 r[j] = 0.f;
193 for (i = 0; i < 4; ++i)
194 r[j] += M[i][j] * v[i];
195 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600196}
Karl Schultz481756e2016-02-02 15:37:51 -0700197static inline void mat4x4_translate(mat4x4 T, float x, float y, float z) {
198 mat4x4_identity(T);
199 T[3][0] = x;
200 T[3][1] = y;
201 T[3][2] = z;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600202}
Karl Schultz481756e2016-02-02 15:37:51 -0700203static inline void mat4x4_translate_in_place(mat4x4 M, float x, float y,
204 float z) {
205 vec4 t = {x, y, z, 0};
206 vec4 r;
207 int i;
208 for (i = 0; i < 4; ++i) {
209 mat4x4_row(r, M, i);
210 M[3][i] += vec4_mul_inner(r, t);
211 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600212}
Karl Schultz481756e2016-02-02 15:37:51 -0700213static inline void mat4x4_from_vec3_mul_outer(mat4x4 M, vec3 a, vec3 b) {
214 int i, j;
215 for (i = 0; i < 4; ++i)
216 for (j = 0; j < 4; ++j)
217 M[i][j] = i < 3 && j < 3 ? a[i] * b[j] : 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600218}
Karl Schultz481756e2016-02-02 15:37:51 -0700219static inline void mat4x4_rotate(mat4x4 R, mat4x4 M, float x, float y, float z,
220 float angle) {
221 float s = sinf(angle);
222 float c = cosf(angle);
223 vec3 u = {x, y, z};
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600224
Karl Schultz481756e2016-02-02 15:37:51 -0700225 if (vec3_len(u) > 1e-4) {
226 vec3_norm(u, u);
227 mat4x4 T;
228 mat4x4_from_vec3_mul_outer(T, u, u);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600229
Karl Schultz481756e2016-02-02 15:37:51 -0700230 mat4x4 S = {{0, u[2], -u[1], 0},
231 {-u[2], 0, u[0], 0},
232 {u[1], -u[0], 0, 0},
233 {0, 0, 0, 0}};
234 mat4x4_scale(S, S, s);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600235
Karl Schultz481756e2016-02-02 15:37:51 -0700236 mat4x4 C;
237 mat4x4_identity(C);
238 mat4x4_sub(C, C, T);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600239
Karl Schultz481756e2016-02-02 15:37:51 -0700240 mat4x4_scale(C, C, c);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600241
Karl Schultz481756e2016-02-02 15:37:51 -0700242 mat4x4_add(T, T, C);
243 mat4x4_add(T, T, S);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600244
Karl Schultz481756e2016-02-02 15:37:51 -0700245 T[3][3] = 1.;
246 mat4x4_mul(R, M, T);
247 } else {
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600248 mat4x4_dup(R, M);
Karl Schultz481756e2016-02-02 15:37:51 -0700249 }
250}
251static inline void mat4x4_rotate_X(mat4x4 Q, mat4x4 M, float angle) {
252 float s = sinf(angle);
253 float c = cosf(angle);
254 mat4x4 R = {{1.f, 0.f, 0.f, 0.f},
255 {0.f, c, s, 0.f},
256 {0.f, -s, c, 0.f},
257 {0.f, 0.f, 0.f, 1.f}};
258 mat4x4_mul(Q, M, R);
259}
260static inline void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M, float angle) {
261 float s = sinf(angle);
262 float c = cosf(angle);
263 mat4x4 R = {{c, 0.f, s, 0.f},
264 {0.f, 1.f, 0.f, 0.f},
265 {-s, 0.f, c, 0.f},
266 {0.f, 0.f, 0.f, 1.f}};
267 mat4x4_mul(Q, M, R);
268}
269static inline void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle) {
270 float s = sinf(angle);
271 float c = cosf(angle);
272 mat4x4 R = {{c, s, 0.f, 0.f},
273 {-s, c, 0.f, 0.f},
274 {0.f, 0.f, 1.f, 0.f},
275 {0.f, 0.f, 0.f, 1.f}};
276 mat4x4_mul(Q, M, R);
277}
278static inline void mat4x4_invert(mat4x4 T, mat4x4 M) {
279 float s[6];
280 float c[6];
281 s[0] = M[0][0] * M[1][1] - M[1][0] * M[0][1];
282 s[1] = M[0][0] * M[1][2] - M[1][0] * M[0][2];
283 s[2] = M[0][0] * M[1][3] - M[1][0] * M[0][3];
284 s[3] = M[0][1] * M[1][2] - M[1][1] * M[0][2];
285 s[4] = M[0][1] * M[1][3] - M[1][1] * M[0][3];
286 s[5] = M[0][2] * M[1][3] - M[1][2] * M[0][3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600287
Karl Schultz481756e2016-02-02 15:37:51 -0700288 c[0] = M[2][0] * M[3][1] - M[3][0] * M[2][1];
289 c[1] = M[2][0] * M[3][2] - M[3][0] * M[2][2];
290 c[2] = M[2][0] * M[3][3] - M[3][0] * M[2][3];
291 c[3] = M[2][1] * M[3][2] - M[3][1] * M[2][2];
292 c[4] = M[2][1] * M[3][3] - M[3][1] * M[2][3];
293 c[5] = M[2][2] * M[3][3] - M[3][2] * M[2][3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600294
Karl Schultz481756e2016-02-02 15:37:51 -0700295 /* Assumes it is invertible */
296 float idet = 1.0f / (s[0] * c[5] - s[1] * c[4] + s[2] * c[3] + s[3] * c[2] -
297 s[4] * c[1] + s[5] * c[0]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600298
Karl Schultz481756e2016-02-02 15:37:51 -0700299 T[0][0] = (M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
300 T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
301 T[0][2] = (M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
302 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 -0600303
Karl Schultz481756e2016-02-02 15:37:51 -0700304 T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
305 T[1][1] = (M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
306 T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
307 T[1][3] = (M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
308
309 T[2][0] = (M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
310 T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
311 T[2][2] = (M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
312 T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
313
314 T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
315 T[3][1] = (M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
316 T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
317 T[3][3] = (M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
318}
319static inline void mat4x4_orthonormalize(mat4x4 R, mat4x4 M) {
320 mat4x4_dup(R, M);
321 float s = 1.;
322 vec3 h;
323
324 vec3_norm(R[2], R[2]);
325
326 s = vec3_mul_inner(R[1], R[2]);
327 vec3_scale(h, R[2], s);
328 vec3_sub(R[1], R[1], h);
329 vec3_norm(R[2], R[2]);
330
331 s = vec3_mul_inner(R[1], R[2]);
332 vec3_scale(h, R[2], s);
333 vec3_sub(R[1], R[1], h);
334 vec3_norm(R[1], R[1]);
335
336 s = vec3_mul_inner(R[0], R[1]);
337 vec3_scale(h, R[1], s);
338 vec3_sub(R[0], R[0], h);
339 vec3_norm(R[0], R[0]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600340}
341
Karl Schultz481756e2016-02-02 15:37:51 -0700342static inline void mat4x4_frustum(mat4x4 M, float l, float r, float b, float t,
343 float n, float f) {
344 M[0][0] = 2.f * n / (r - l);
345 M[0][1] = M[0][2] = M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600346
Karl Schultz481756e2016-02-02 15:37:51 -0700347 M[1][1] = 2.f * n / (t - b);
348 M[1][0] = M[1][2] = M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600349
Karl Schultz481756e2016-02-02 15:37:51 -0700350 M[2][0] = (r + l) / (r - l);
351 M[2][1] = (t + b) / (t - b);
352 M[2][2] = -(f + n) / (f - n);
353 M[2][3] = -1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600354
Karl Schultz481756e2016-02-02 15:37:51 -0700355 M[3][2] = -2.f * (f * n) / (f - n);
356 M[3][0] = M[3][1] = M[3][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600357}
Karl Schultz481756e2016-02-02 15:37:51 -0700358static inline void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t,
359 float n, float f) {
360 M[0][0] = 2.f / (r - l);
361 M[0][1] = M[0][2] = M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600362
Karl Schultz481756e2016-02-02 15:37:51 -0700363 M[1][1] = 2.f / (t - b);
364 M[1][0] = M[1][2] = M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600365
Karl Schultz481756e2016-02-02 15:37:51 -0700366 M[2][2] = -2.f / (f - n);
367 M[2][0] = M[2][1] = M[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600368
Karl Schultz481756e2016-02-02 15:37:51 -0700369 M[3][0] = -(r + l) / (r - l);
370 M[3][1] = -(t + b) / (t - b);
371 M[3][2] = -(f + n) / (f - n);
372 M[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600373}
Karl Schultz481756e2016-02-02 15:37:51 -0700374static inline void mat4x4_perspective(mat4x4 m, float y_fov, float aspect,
375 float n, float f) {
376 /* NOTE: Degrees are an unhandy unit to work with.
377 * linmath.h uses radians for everything! */
378 float const a = (float)(1.f / tan(y_fov / 2.f));
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600379
Karl Schultz481756e2016-02-02 15:37:51 -0700380 m[0][0] = a / aspect;
381 m[0][1] = 0.f;
382 m[0][2] = 0.f;
383 m[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600384
Karl Schultz481756e2016-02-02 15:37:51 -0700385 m[1][0] = 0.f;
386 m[1][1] = a;
387 m[1][2] = 0.f;
388 m[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600389
Karl Schultz481756e2016-02-02 15:37:51 -0700390 m[2][0] = 0.f;
391 m[2][1] = 0.f;
392 m[2][2] = -((f + n) / (f - n));
393 m[2][3] = -1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600394
Karl Schultz481756e2016-02-02 15:37:51 -0700395 m[3][0] = 0.f;
396 m[3][1] = 0.f;
397 m[3][2] = -((2.f * f * n) / (f - n));
398 m[3][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600399}
Karl Schultz481756e2016-02-02 15:37:51 -0700400static inline void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up) {
401 /* Adapted from Android's OpenGL Matrix.java. */
402 /* See the OpenGL GLUT documentation for gluLookAt for a description */
403 /* of the algorithm. We implement it in a straightforward way: */
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600404
Karl Schultz481756e2016-02-02 15:37:51 -0700405 /* TODO: The negation of of can be spared by swapping the order of
406 * operands in the following cross products in the right way. */
407 vec3 f;
408 vec3_sub(f, center, eye);
409 vec3_norm(f, f);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600410
Karl Schultz481756e2016-02-02 15:37:51 -0700411 vec3 s;
412 vec3_mul_cross(s, f, up);
413 vec3_norm(s, s);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600414
Karl Schultz481756e2016-02-02 15:37:51 -0700415 vec3 t;
416 vec3_mul_cross(t, s, f);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600417
Karl Schultz481756e2016-02-02 15:37:51 -0700418 m[0][0] = s[0];
419 m[0][1] = t[0];
420 m[0][2] = -f[0];
421 m[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600422
Karl Schultz481756e2016-02-02 15:37:51 -0700423 m[1][0] = s[1];
424 m[1][1] = t[1];
425 m[1][2] = -f[1];
426 m[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600427
Karl Schultz481756e2016-02-02 15:37:51 -0700428 m[2][0] = s[2];
429 m[2][1] = t[2];
430 m[2][2] = -f[2];
431 m[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600432
Karl Schultz481756e2016-02-02 15:37:51 -0700433 m[3][0] = 0.f;
434 m[3][1] = 0.f;
435 m[3][2] = 0.f;
436 m[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600437
Karl Schultz481756e2016-02-02 15:37:51 -0700438 mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600439}
440
441typedef float quat[4];
Karl Schultz481756e2016-02-02 15:37:51 -0700442static inline void quat_identity(quat q) {
443 q[0] = q[1] = q[2] = 0.f;
444 q[3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600445}
Karl Schultz481756e2016-02-02 15:37:51 -0700446static inline void quat_add(quat r, quat a, quat b) {
447 int i;
448 for (i = 0; i < 4; ++i)
449 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600450}
Karl Schultz481756e2016-02-02 15:37:51 -0700451static inline void quat_sub(quat r, quat a, quat b) {
452 int i;
453 for (i = 0; i < 4; ++i)
454 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600455}
Karl Schultz481756e2016-02-02 15:37:51 -0700456static inline void quat_mul(quat r, quat p, quat q) {
457 vec3 w;
458 vec3_mul_cross(r, p, q);
459 vec3_scale(w, p, q[3]);
460 vec3_add(r, r, w);
461 vec3_scale(w, q, p[3]);
462 vec3_add(r, r, w);
463 r[3] = p[3] * q[3] - vec3_mul_inner(p, q);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600464}
Karl Schultz481756e2016-02-02 15:37:51 -0700465static inline void quat_scale(quat r, quat v, float s) {
466 int i;
467 for (i = 0; i < 4; ++i)
468 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600469}
Karl Schultz481756e2016-02-02 15:37:51 -0700470static inline float quat_inner_product(quat a, quat b) {
471 float p = 0.f;
472 int i;
473 for (i = 0; i < 4; ++i)
474 p += b[i] * a[i];
475 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600476}
Karl Schultz481756e2016-02-02 15:37:51 -0700477static inline void quat_conj(quat r, quat q) {
478 int i;
479 for (i = 0; i < 3; ++i)
480 r[i] = -q[i];
481 r[3] = q[3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600482}
483#define quat_norm vec4_norm
Karl Schultz481756e2016-02-02 15:37:51 -0700484static inline void quat_mul_vec3(vec3 r, quat q, vec3 v) {
485 quat v_ = {v[0], v[1], v[2], 0.f};
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600486
Karl Schultz481756e2016-02-02 15:37:51 -0700487 quat_conj(r, q);
488 quat_norm(r, r);
489 quat_mul(r, v_, r);
490 quat_mul(r, q, r);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600491}
Karl Schultz481756e2016-02-02 15:37:51 -0700492static inline void mat4x4_from_quat(mat4x4 M, quat q) {
493 float a = q[3];
494 float b = q[0];
495 float c = q[1];
496 float d = q[2];
497 float a2 = a * a;
498 float b2 = b * b;
499 float c2 = c * c;
500 float d2 = d * d;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600501
Karl Schultz481756e2016-02-02 15:37:51 -0700502 M[0][0] = a2 + b2 - c2 - d2;
503 M[0][1] = 2.f * (b * c + a * d);
504 M[0][2] = 2.f * (b * d - a * c);
505 M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600506
Karl Schultz481756e2016-02-02 15:37:51 -0700507 M[1][0] = 2 * (b * c - a * d);
508 M[1][1] = a2 - b2 + c2 - d2;
509 M[1][2] = 2.f * (c * d + a * b);
510 M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600511
Karl Schultz481756e2016-02-02 15:37:51 -0700512 M[2][0] = 2.f * (b * d + a * c);
513 M[2][1] = 2.f * (c * d - a * b);
514 M[2][2] = a2 - b2 - c2 + d2;
515 M[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600516
Karl Schultz481756e2016-02-02 15:37:51 -0700517 M[3][0] = M[3][1] = M[3][2] = 0.f;
518 M[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600519}
520
Karl Schultz481756e2016-02-02 15:37:51 -0700521static inline void mat4x4o_mul_quat(mat4x4 R, mat4x4 M, quat q) {
522 /* XXX: The way this is written only works for othogonal matrices. */
523 /* TODO: Take care of non-orthogonal case. */
524 quat_mul_vec3(R[0], q, M[0]);
525 quat_mul_vec3(R[1], q, M[1]);
526 quat_mul_vec3(R[2], q, M[2]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600527
Karl Schultz481756e2016-02-02 15:37:51 -0700528 R[3][0] = R[3][1] = R[3][2] = 0.f;
529 R[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600530}
Karl Schultz481756e2016-02-02 15:37:51 -0700531static inline void quat_from_mat4x4(quat q, mat4x4 M) {
532 float r = 0.f;
533 int i;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600534
Karl Schultz481756e2016-02-02 15:37:51 -0700535 int perm[] = {0, 1, 2, 0, 1};
536 int *p = perm;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600537
Karl Schultz481756e2016-02-02 15:37:51 -0700538 for (i = 0; i < 3; i++) {
539 float m = M[i][i];
540 if (m < r)
541 continue;
542 m = r;
543 p = &perm[i];
544 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600545
Karl Schultz481756e2016-02-02 15:37:51 -0700546 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 -0600547
Karl Schultz481756e2016-02-02 15:37:51 -0700548 if (r < 1e-6) {
549 q[0] = 1.f;
550 q[1] = q[2] = q[3] = 0.f;
551 return;
552 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600553
Karl Schultz481756e2016-02-02 15:37:51 -0700554 q[0] = r / 2.f;
555 q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]]) / (2.f * r);
556 q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]]) / (2.f * r);
557 q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]]) / (2.f * r);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600558}
559
560#endif