blob: a36b1885bfcba32fd1b922c6f25be60481470530 [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 *
Jon Ashburn43b53e82016-04-19 11:30:31 -06006 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
Karl Schultzdae791d2016-02-04 10:29:00 -07009 *
Jon Ashburn43b53e82016-04-19 11:30:31 -060010 * http://www.apache.org/licenses/LICENSE-2.0
Karl Schultzdae791d2016-02-04 10:29:00 -070011 *
Jon Ashburn43b53e82016-04-19 11:30:31 -060012 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
Karl Schultzdae791d2016-02-04 10:29:00 -070017 *
18 * Relicensed from the WTFPL (http://www.wtfpl.net/faq/).
19 */
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060020
21#ifndef LINMATH_H
22#define LINMATH_H
23
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060024#include <math.h>
25
26// Converts degrees to radians.
27#define degreesToRadians(angleDegrees) (angleDegrees * M_PI / 180.0)
28
29// Converts radians to degrees.
30#define radiansToDegrees(angleRadians) (angleRadians * 180.0 / M_PI)
31
32typedef float vec3[3];
Karl Schultz481756e2016-02-02 15:37:51 -070033static inline void vec3_add(vec3 r, vec3 const a, vec3 const b) {
34 int i;
35 for (i = 0; i < 3; ++i)
36 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060037}
Karl Schultz481756e2016-02-02 15:37:51 -070038static inline void vec3_sub(vec3 r, vec3 const a, vec3 const b) {
39 int i;
40 for (i = 0; i < 3; ++i)
41 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060042}
Karl Schultz481756e2016-02-02 15:37:51 -070043static inline void vec3_scale(vec3 r, vec3 const v, float const s) {
44 int i;
45 for (i = 0; i < 3; ++i)
46 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060047}
Karl Schultz481756e2016-02-02 15:37:51 -070048static inline float vec3_mul_inner(vec3 const a, vec3 const b) {
49 float p = 0.f;
50 int i;
51 for (i = 0; i < 3; ++i)
52 p += b[i] * a[i];
53 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060054}
Karl Schultz481756e2016-02-02 15:37:51 -070055static inline void vec3_mul_cross(vec3 r, vec3 const a, vec3 const b) {
56 r[0] = a[1] * b[2] - a[2] * b[1];
57 r[1] = a[2] * b[0] - a[0] * b[2];
58 r[2] = a[0] * b[1] - a[1] * b[0];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060059}
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -070060static inline float vec3_len(vec3 const v) { return sqrtf(vec3_mul_inner(v, v)); }
Karl Schultz481756e2016-02-02 15:37:51 -070061static inline void vec3_norm(vec3 r, vec3 const v) {
62 float k = 1.f / vec3_len(v);
63 vec3_scale(r, v, k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060064}
Karl Schultz481756e2016-02-02 15:37:51 -070065static inline void vec3_reflect(vec3 r, vec3 const v, vec3 const n) {
66 float p = 2.f * vec3_mul_inner(v, n);
67 int i;
68 for (i = 0; i < 3; ++i)
69 r[i] = v[i] - p * n[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060070}
71
72typedef float vec4[4];
Karl Schultz481756e2016-02-02 15:37:51 -070073static inline void vec4_add(vec4 r, vec4 const a, vec4 const b) {
74 int i;
75 for (i = 0; i < 4; ++i)
76 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060077}
Karl Schultz481756e2016-02-02 15:37:51 -070078static inline void vec4_sub(vec4 r, vec4 const a, vec4 const b) {
79 int i;
80 for (i = 0; i < 4; ++i)
81 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060082}
Karl Schultz481756e2016-02-02 15:37:51 -070083static inline void vec4_scale(vec4 r, vec4 v, float s) {
84 int i;
85 for (i = 0; i < 4; ++i)
86 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060087}
Karl Schultz481756e2016-02-02 15:37:51 -070088static inline float vec4_mul_inner(vec4 a, vec4 b) {
89 float p = 0.f;
90 int i;
91 for (i = 0; i < 4; ++i)
92 p += b[i] * a[i];
93 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -060094}
Karl Schultz481756e2016-02-02 15:37:51 -070095static inline void vec4_mul_cross(vec4 r, vec4 a, vec4 b) {
96 r[0] = a[1] * b[2] - a[2] * b[1];
97 r[1] = a[2] * b[0] - a[0] * b[2];
98 r[2] = a[0] * b[1] - a[1] * b[0];
99 r[3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600100}
Karl Schultz481756e2016-02-02 15:37:51 -0700101static inline float vec4_len(vec4 v) { return sqrtf(vec4_mul_inner(v, v)); }
102static inline void vec4_norm(vec4 r, vec4 v) {
103 float k = 1.f / vec4_len(v);
104 vec4_scale(r, v, k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600105}
Karl Schultz481756e2016-02-02 15:37:51 -0700106static inline void vec4_reflect(vec4 r, vec4 v, vec4 n) {
107 float p = 2.f * vec4_mul_inner(v, n);
108 int i;
109 for (i = 0; i < 4; ++i)
110 r[i] = v[i] - p * n[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600111}
112
113typedef vec4 mat4x4[4];
Karl Schultz481756e2016-02-02 15:37:51 -0700114static inline void mat4x4_identity(mat4x4 M) {
115 int i, j;
116 for (i = 0; i < 4; ++i)
117 for (j = 0; j < 4; ++j)
118 M[i][j] = i == j ? 1.f : 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600119}
Karl Schultz481756e2016-02-02 15:37:51 -0700120static inline void mat4x4_dup(mat4x4 M, mat4x4 N) {
121 int i, j;
122 for (i = 0; i < 4; ++i)
123 for (j = 0; j < 4; ++j)
124 M[i][j] = N[i][j];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600125}
Karl Schultz481756e2016-02-02 15:37:51 -0700126static inline void mat4x4_row(vec4 r, mat4x4 M, int i) {
127 int k;
128 for (k = 0; k < 4; ++k)
129 r[k] = M[k][i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600130}
Karl Schultz481756e2016-02-02 15:37:51 -0700131static inline void mat4x4_col(vec4 r, mat4x4 M, int i) {
132 int k;
133 for (k = 0; k < 4; ++k)
134 r[k] = M[i][k];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600135}
Karl Schultz481756e2016-02-02 15:37:51 -0700136static inline void mat4x4_transpose(mat4x4 M, mat4x4 N) {
137 int i, j;
138 for (j = 0; j < 4; ++j)
139 for (i = 0; i < 4; ++i)
140 M[i][j] = N[j][i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600141}
Karl Schultz481756e2016-02-02 15:37:51 -0700142static inline void mat4x4_add(mat4x4 M, mat4x4 a, mat4x4 b) {
143 int i;
144 for (i = 0; i < 4; ++i)
145 vec4_add(M[i], a[i], b[i]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600146}
Karl Schultz481756e2016-02-02 15:37:51 -0700147static inline void mat4x4_sub(mat4x4 M, mat4x4 a, mat4x4 b) {
148 int i;
149 for (i = 0; i < 4; ++i)
150 vec4_sub(M[i], a[i], b[i]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600151}
Karl Schultz481756e2016-02-02 15:37:51 -0700152static inline void mat4x4_scale(mat4x4 M, mat4x4 a, float k) {
153 int i;
154 for (i = 0; i < 4; ++i)
155 vec4_scale(M[i], a[i], k);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600156}
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700157static inline void mat4x4_scale_aniso(mat4x4 M, mat4x4 a, float x, float y, float z) {
Karl Schultz481756e2016-02-02 15:37:51 -0700158 int i;
159 vec4_scale(M[0], a[0], x);
160 vec4_scale(M[1], a[1], y);
161 vec4_scale(M[2], a[2], z);
162 for (i = 0; i < 4; ++i) {
163 M[3][i] = a[3][i];
164 }
165}
166static inline void mat4x4_mul(mat4x4 M, mat4x4 a, mat4x4 b) {
167 int k, r, c;
168 for (c = 0; c < 4; ++c)
169 for (r = 0; r < 4; ++r) {
170 M[c][r] = 0.f;
171 for (k = 0; k < 4; ++k)
172 M[c][r] += a[k][r] * b[c][k];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600173 }
174}
Karl Schultz481756e2016-02-02 15:37:51 -0700175static inline void mat4x4_mul_vec4(vec4 r, mat4x4 M, vec4 v) {
176 int i, j;
177 for (j = 0; j < 4; ++j) {
178 r[j] = 0.f;
179 for (i = 0; i < 4; ++i)
180 r[j] += M[i][j] * v[i];
181 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600182}
Karl Schultz481756e2016-02-02 15:37:51 -0700183static inline void mat4x4_translate(mat4x4 T, float x, float y, float z) {
184 mat4x4_identity(T);
185 T[3][0] = x;
186 T[3][1] = y;
187 T[3][2] = z;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600188}
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700189static inline void mat4x4_translate_in_place(mat4x4 M, float x, float y, float z) {
Karl Schultz481756e2016-02-02 15:37:51 -0700190 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}
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700204static inline void mat4x4_rotate(mat4x4 R, mat4x4 M, float x, float y, float z, float angle) {
Karl Schultz481756e2016-02-02 15:37:51 -0700205 float s = sinf(angle);
206 float c = cosf(angle);
207 vec3 u = {x, y, z};
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600208
Karl Schultz481756e2016-02-02 15:37:51 -0700209 if (vec3_len(u) > 1e-4) {
210 vec3_norm(u, u);
211 mat4x4 T;
212 mat4x4_from_vec3_mul_outer(T, u, u);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600213
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700214 mat4x4 S = {{0, u[2], -u[1], 0}, {-u[2], 0, u[0], 0}, {u[1], -u[0], 0, 0}, {0, 0, 0, 0}};
Karl Schultz481756e2016-02-02 15:37:51 -0700215 mat4x4_scale(S, S, s);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600216
Karl Schultz481756e2016-02-02 15:37:51 -0700217 mat4x4 C;
218 mat4x4_identity(C);
219 mat4x4_sub(C, C, T);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600220
Karl Schultz481756e2016-02-02 15:37:51 -0700221 mat4x4_scale(C, C, c);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600222
Karl Schultz481756e2016-02-02 15:37:51 -0700223 mat4x4_add(T, T, C);
224 mat4x4_add(T, T, S);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600225
Karl Schultz481756e2016-02-02 15:37:51 -0700226 T[3][3] = 1.;
227 mat4x4_mul(R, M, T);
228 } else {
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600229 mat4x4_dup(R, M);
Karl Schultz481756e2016-02-02 15:37:51 -0700230 }
231}
232static inline void mat4x4_rotate_X(mat4x4 Q, mat4x4 M, float angle) {
233 float s = sinf(angle);
234 float c = cosf(angle);
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700235 mat4x4 R = {{1.f, 0.f, 0.f, 0.f}, {0.f, c, s, 0.f}, {0.f, -s, c, 0.f}, {0.f, 0.f, 0.f, 1.f}};
Karl Schultz481756e2016-02-02 15:37:51 -0700236 mat4x4_mul(Q, M, R);
237}
238static inline void mat4x4_rotate_Y(mat4x4 Q, mat4x4 M, float angle) {
239 float s = sinf(angle);
240 float c = cosf(angle);
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700241 mat4x4 R = {{c, 0.f, s, 0.f}, {0.f, 1.f, 0.f, 0.f}, {-s, 0.f, c, 0.f}, {0.f, 0.f, 0.f, 1.f}};
Karl Schultz481756e2016-02-02 15:37:51 -0700242 mat4x4_mul(Q, M, R);
243}
244static inline void mat4x4_rotate_Z(mat4x4 Q, mat4x4 M, float angle) {
245 float s = sinf(angle);
246 float c = cosf(angle);
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700247 mat4x4 R = {{c, s, 0.f, 0.f}, {-s, c, 0.f, 0.f}, {0.f, 0.f, 1.f, 0.f}, {0.f, 0.f, 0.f, 1.f}};
Karl Schultz481756e2016-02-02 15:37:51 -0700248 mat4x4_mul(Q, M, R);
249}
250static inline void mat4x4_invert(mat4x4 T, mat4x4 M) {
251 float s[6];
252 float c[6];
253 s[0] = M[0][0] * M[1][1] - M[1][0] * M[0][1];
254 s[1] = M[0][0] * M[1][2] - M[1][0] * M[0][2];
255 s[2] = M[0][0] * M[1][3] - M[1][0] * M[0][3];
256 s[3] = M[0][1] * M[1][2] - M[1][1] * M[0][2];
257 s[4] = M[0][1] * M[1][3] - M[1][1] * M[0][3];
258 s[5] = M[0][2] * M[1][3] - M[1][2] * M[0][3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600259
Karl Schultz481756e2016-02-02 15:37:51 -0700260 c[0] = M[2][0] * M[3][1] - M[3][0] * M[2][1];
261 c[1] = M[2][0] * M[3][2] - M[3][0] * M[2][2];
262 c[2] = M[2][0] * M[3][3] - M[3][0] * M[2][3];
263 c[3] = M[2][1] * M[3][2] - M[3][1] * M[2][2];
264 c[4] = M[2][1] * M[3][3] - M[3][1] * M[2][3];
265 c[5] = M[2][2] * M[3][3] - M[3][2] * M[2][3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600266
Karl Schultz481756e2016-02-02 15:37:51 -0700267 /* Assumes it is invertible */
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700268 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]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600269
Karl Schultz481756e2016-02-02 15:37:51 -0700270 T[0][0] = (M[1][1] * c[5] - M[1][2] * c[4] + M[1][3] * c[3]) * idet;
271 T[0][1] = (-M[0][1] * c[5] + M[0][2] * c[4] - M[0][3] * c[3]) * idet;
272 T[0][2] = (M[3][1] * s[5] - M[3][2] * s[4] + M[3][3] * s[3]) * idet;
273 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 -0600274
Karl Schultz481756e2016-02-02 15:37:51 -0700275 T[1][0] = (-M[1][0] * c[5] + M[1][2] * c[2] - M[1][3] * c[1]) * idet;
276 T[1][1] = (M[0][0] * c[5] - M[0][2] * c[2] + M[0][3] * c[1]) * idet;
277 T[1][2] = (-M[3][0] * s[5] + M[3][2] * s[2] - M[3][3] * s[1]) * idet;
278 T[1][3] = (M[2][0] * s[5] - M[2][2] * s[2] + M[2][3] * s[1]) * idet;
279
280 T[2][0] = (M[1][0] * c[4] - M[1][1] * c[2] + M[1][3] * c[0]) * idet;
281 T[2][1] = (-M[0][0] * c[4] + M[0][1] * c[2] - M[0][3] * c[0]) * idet;
282 T[2][2] = (M[3][0] * s[4] - M[3][1] * s[2] + M[3][3] * s[0]) * idet;
283 T[2][3] = (-M[2][0] * s[4] + M[2][1] * s[2] - M[2][3] * s[0]) * idet;
284
285 T[3][0] = (-M[1][0] * c[3] + M[1][1] * c[1] - M[1][2] * c[0]) * idet;
286 T[3][1] = (M[0][0] * c[3] - M[0][1] * c[1] + M[0][2] * c[0]) * idet;
287 T[3][2] = (-M[3][0] * s[3] + M[3][1] * s[1] - M[3][2] * s[0]) * idet;
288 T[3][3] = (M[2][0] * s[3] - M[2][1] * s[1] + M[2][2] * s[0]) * idet;
289}
290static inline void mat4x4_orthonormalize(mat4x4 R, mat4x4 M) {
291 mat4x4_dup(R, M);
292 float s = 1.;
293 vec3 h;
294
295 vec3_norm(R[2], R[2]);
296
297 s = vec3_mul_inner(R[1], R[2]);
298 vec3_scale(h, R[2], s);
299 vec3_sub(R[1], R[1], h);
300 vec3_norm(R[2], R[2]);
301
302 s = vec3_mul_inner(R[1], R[2]);
303 vec3_scale(h, R[2], s);
304 vec3_sub(R[1], R[1], h);
305 vec3_norm(R[1], R[1]);
306
307 s = vec3_mul_inner(R[0], R[1]);
308 vec3_scale(h, R[1], s);
309 vec3_sub(R[0], R[0], h);
310 vec3_norm(R[0], R[0]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600311}
312
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700313static inline void mat4x4_frustum(mat4x4 M, float l, float r, float b, float t, float n, float f) {
Karl Schultz481756e2016-02-02 15:37:51 -0700314 M[0][0] = 2.f * n / (r - l);
315 M[0][1] = M[0][2] = M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600316
Karl Schultz481756e2016-02-02 15:37:51 -0700317 M[1][1] = 2.f * n / (t - b);
318 M[1][0] = M[1][2] = M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600319
Karl Schultz481756e2016-02-02 15:37:51 -0700320 M[2][0] = (r + l) / (r - l);
321 M[2][1] = (t + b) / (t - b);
322 M[2][2] = -(f + n) / (f - n);
323 M[2][3] = -1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600324
Karl Schultz481756e2016-02-02 15:37:51 -0700325 M[3][2] = -2.f * (f * n) / (f - n);
326 M[3][0] = M[3][1] = M[3][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600327}
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700328static inline void mat4x4_ortho(mat4x4 M, float l, float r, float b, float t, float n, float f) {
Karl Schultz481756e2016-02-02 15:37:51 -0700329 M[0][0] = 2.f / (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 / (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][2] = -2.f / (f - n);
336 M[2][0] = M[2][1] = M[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600337
Karl Schultz481756e2016-02-02 15:37:51 -0700338 M[3][0] = -(r + l) / (r - l);
339 M[3][1] = -(t + b) / (t - b);
340 M[3][2] = -(f + n) / (f - n);
341 M[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600342}
Mark Lobodzinskibc9caa52017-01-26 12:16:30 -0700343static inline void mat4x4_perspective(mat4x4 m, float y_fov, float aspect, float n, float f) {
Karl Schultz481756e2016-02-02 15:37:51 -0700344 /* NOTE: Degrees are an unhandy unit to work with.
345 * linmath.h uses radians for everything! */
346 float const a = (float)(1.f / tan(y_fov / 2.f));
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600347
Karl Schultz481756e2016-02-02 15:37:51 -0700348 m[0][0] = a / aspect;
349 m[0][1] = 0.f;
350 m[0][2] = 0.f;
351 m[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600352
Karl Schultz481756e2016-02-02 15:37:51 -0700353 m[1][0] = 0.f;
354 m[1][1] = a;
355 m[1][2] = 0.f;
356 m[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600357
Karl Schultz481756e2016-02-02 15:37:51 -0700358 m[2][0] = 0.f;
359 m[2][1] = 0.f;
360 m[2][2] = -((f + n) / (f - n));
361 m[2][3] = -1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600362
Karl Schultz481756e2016-02-02 15:37:51 -0700363 m[3][0] = 0.f;
364 m[3][1] = 0.f;
365 m[3][2] = -((2.f * f * n) / (f - n));
366 m[3][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600367}
Karl Schultz481756e2016-02-02 15:37:51 -0700368static inline void mat4x4_look_at(mat4x4 m, vec3 eye, vec3 center, vec3 up) {
369 /* Adapted from Android's OpenGL Matrix.java. */
370 /* See the OpenGL GLUT documentation for gluLookAt for a description */
371 /* of the algorithm. We implement it in a straightforward way: */
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600372
Karl Schultz481756e2016-02-02 15:37:51 -0700373 /* TODO: The negation of of can be spared by swapping the order of
374 * operands in the following cross products in the right way. */
375 vec3 f;
376 vec3_sub(f, center, eye);
377 vec3_norm(f, f);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600378
Karl Schultz481756e2016-02-02 15:37:51 -0700379 vec3 s;
380 vec3_mul_cross(s, f, up);
381 vec3_norm(s, s);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600382
Karl Schultz481756e2016-02-02 15:37:51 -0700383 vec3 t;
384 vec3_mul_cross(t, s, f);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600385
Karl Schultz481756e2016-02-02 15:37:51 -0700386 m[0][0] = s[0];
387 m[0][1] = t[0];
388 m[0][2] = -f[0];
389 m[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600390
Karl Schultz481756e2016-02-02 15:37:51 -0700391 m[1][0] = s[1];
392 m[1][1] = t[1];
393 m[1][2] = -f[1];
394 m[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600395
Karl Schultz481756e2016-02-02 15:37:51 -0700396 m[2][0] = s[2];
397 m[2][1] = t[2];
398 m[2][2] = -f[2];
399 m[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600400
Karl Schultz481756e2016-02-02 15:37:51 -0700401 m[3][0] = 0.f;
402 m[3][1] = 0.f;
403 m[3][2] = 0.f;
404 m[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600405
Karl Schultz481756e2016-02-02 15:37:51 -0700406 mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600407}
408
409typedef float quat[4];
Karl Schultz481756e2016-02-02 15:37:51 -0700410static inline void quat_identity(quat q) {
411 q[0] = q[1] = q[2] = 0.f;
412 q[3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600413}
Karl Schultz481756e2016-02-02 15:37:51 -0700414static inline void quat_add(quat r, quat a, quat b) {
415 int i;
416 for (i = 0; i < 4; ++i)
417 r[i] = a[i] + b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600418}
Karl Schultz481756e2016-02-02 15:37:51 -0700419static inline void quat_sub(quat r, quat a, quat b) {
420 int i;
421 for (i = 0; i < 4; ++i)
422 r[i] = a[i] - b[i];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600423}
Karl Schultz481756e2016-02-02 15:37:51 -0700424static inline void quat_mul(quat r, quat p, quat q) {
425 vec3 w;
426 vec3_mul_cross(r, p, q);
427 vec3_scale(w, p, q[3]);
428 vec3_add(r, r, w);
429 vec3_scale(w, q, p[3]);
430 vec3_add(r, r, w);
431 r[3] = p[3] * q[3] - vec3_mul_inner(p, q);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600432}
Karl Schultz481756e2016-02-02 15:37:51 -0700433static inline void quat_scale(quat r, quat v, float s) {
434 int i;
435 for (i = 0; i < 4; ++i)
436 r[i] = v[i] * s;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600437}
Karl Schultz481756e2016-02-02 15:37:51 -0700438static inline float quat_inner_product(quat a, quat b) {
439 float p = 0.f;
440 int i;
441 for (i = 0; i < 4; ++i)
442 p += b[i] * a[i];
443 return p;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600444}
Karl Schultz481756e2016-02-02 15:37:51 -0700445static inline void quat_conj(quat r, quat q) {
446 int i;
447 for (i = 0; i < 3; ++i)
448 r[i] = -q[i];
449 r[3] = q[3];
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600450}
451#define quat_norm vec4_norm
Karl Schultz481756e2016-02-02 15:37:51 -0700452static inline void quat_mul_vec3(vec3 r, quat q, vec3 v) {
453 quat v_ = {v[0], v[1], v[2], 0.f};
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600454
Karl Schultz481756e2016-02-02 15:37:51 -0700455 quat_conj(r, q);
456 quat_norm(r, r);
457 quat_mul(r, v_, r);
458 quat_mul(r, q, r);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600459}
Karl Schultz481756e2016-02-02 15:37:51 -0700460static inline void mat4x4_from_quat(mat4x4 M, quat q) {
461 float a = q[3];
462 float b = q[0];
463 float c = q[1];
464 float d = q[2];
465 float a2 = a * a;
466 float b2 = b * b;
467 float c2 = c * c;
468 float d2 = d * d;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600469
Karl Schultz481756e2016-02-02 15:37:51 -0700470 M[0][0] = a2 + b2 - c2 - d2;
471 M[0][1] = 2.f * (b * c + a * d);
472 M[0][2] = 2.f * (b * d - a * c);
473 M[0][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600474
Karl Schultz481756e2016-02-02 15:37:51 -0700475 M[1][0] = 2 * (b * c - a * d);
476 M[1][1] = a2 - b2 + c2 - d2;
477 M[1][2] = 2.f * (c * d + a * b);
478 M[1][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600479
Karl Schultz481756e2016-02-02 15:37:51 -0700480 M[2][0] = 2.f * (b * d + a * c);
481 M[2][1] = 2.f * (c * d - a * b);
482 M[2][2] = a2 - b2 - c2 + d2;
483 M[2][3] = 0.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600484
Karl Schultz481756e2016-02-02 15:37:51 -0700485 M[3][0] = M[3][1] = M[3][2] = 0.f;
486 M[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600487}
488
Karl Schultz481756e2016-02-02 15:37:51 -0700489static inline void mat4x4o_mul_quat(mat4x4 R, mat4x4 M, quat q) {
490 /* XXX: The way this is written only works for othogonal matrices. */
491 /* TODO: Take care of non-orthogonal case. */
492 quat_mul_vec3(R[0], q, M[0]);
493 quat_mul_vec3(R[1], q, M[1]);
494 quat_mul_vec3(R[2], q, M[2]);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600495
Karl Schultz481756e2016-02-02 15:37:51 -0700496 R[3][0] = R[3][1] = R[3][2] = 0.f;
497 R[3][3] = 1.f;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600498}
Karl Schultz481756e2016-02-02 15:37:51 -0700499static inline void quat_from_mat4x4(quat q, mat4x4 M) {
500 float r = 0.f;
501 int i;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600502
Karl Schultz481756e2016-02-02 15:37:51 -0700503 int perm[] = {0, 1, 2, 0, 1};
504 int *p = perm;
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600505
Karl Schultz481756e2016-02-02 15:37:51 -0700506 for (i = 0; i < 3; i++) {
507 float m = M[i][i];
508 if (m < r)
509 continue;
510 m = r;
511 p = &perm[i];
512 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600513
Karl Schultz481756e2016-02-02 15:37:51 -0700514 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 -0600515
Karl Schultz481756e2016-02-02 15:37:51 -0700516 if (r < 1e-6) {
517 q[0] = 1.f;
518 q[1] = q[2] = q[3] = 0.f;
519 return;
520 }
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600521
Karl Schultz481756e2016-02-02 15:37:51 -0700522 q[0] = r / 2.f;
523 q[1] = (M[p[0]][p[1]] - M[p[1]][p[0]]) / (2.f * r);
524 q[2] = (M[p[2]][p[0]] - M[p[0]][p[2]]) / (2.f * r);
525 q[3] = (M[p[2]][p[1]] - M[p[1]][p[2]]) / (2.f * r);
Courtney Goeltzenleuchter4825f6a2014-10-28 10:27:47 -0600526}
527
528#endif