Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 1 | /* |
| 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 Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 14 | 0. You just DO WHAT THE **** YOU WANT TO. |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #ifndef LINMATH_H |
| 18 | #define LINMATH_H |
| 19 | |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 20 | #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 | |
| 28 | typedef float vec3[3]; |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 29 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 33 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 34 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 38 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 39 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 43 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 44 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 50 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 51 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 55 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 56 | static inline float vec3_len(vec3 const v) { |
| 57 | return sqrtf(vec3_mul_inner(v, v)); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 58 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 59 | static inline void vec3_norm(vec3 r, vec3 const v) { |
| 60 | float k = 1.f / vec3_len(v); |
| 61 | vec3_scale(r, v, k); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 62 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 63 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | typedef float vec4[4]; |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 71 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 75 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 76 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 80 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 81 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 85 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 86 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 92 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 93 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 98 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 99 | static inline float vec4_len(vec4 v) { return sqrtf(vec4_mul_inner(v, v)); } |
| 100 | static inline void vec4_norm(vec4 r, vec4 v) { |
| 101 | float k = 1.f / vec4_len(v); |
| 102 | vec4_scale(r, v, k); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 103 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 104 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | typedef vec4 mat4x4[4]; |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 112 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 117 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 118 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 123 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 124 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 128 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 129 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 133 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 134 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 139 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 140 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 144 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 145 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 149 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 150 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 154 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 155 | static 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 | } |
| 165 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 172 | } |
| 173 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 174 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 181 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 182 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 187 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 188 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 197 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 198 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 203 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 204 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 209 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 210 | if (vec3_len(u) > 1e-4) { |
| 211 | vec3_norm(u, u); |
| 212 | mat4x4 T; |
| 213 | mat4x4_from_vec3_mul_outer(T, u, u); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 214 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 215 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 220 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 221 | mat4x4 C; |
| 222 | mat4x4_identity(C); |
| 223 | mat4x4_sub(C, C, T); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 224 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 225 | mat4x4_scale(C, C, c); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 226 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 227 | mat4x4_add(T, T, C); |
| 228 | mat4x4_add(T, T, S); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 229 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 230 | T[3][3] = 1.; |
| 231 | mat4x4_mul(R, M, T); |
| 232 | } else { |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 233 | mat4x4_dup(R, M); |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 234 | } |
| 235 | } |
| 236 | static 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 | } |
| 245 | static 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 | } |
| 254 | static 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 | } |
| 263 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 272 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 273 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 279 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 280 | /* 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 283 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 284 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 288 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 289 | 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 | } |
| 304 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 325 | } |
| 326 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 327 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 331 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 332 | M[1][1] = 2.f * n / (t - b); |
| 333 | M[1][0] = M[1][2] = M[1][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 334 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 335 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 339 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 340 | M[3][2] = -2.f * (f * n) / (f - n); |
| 341 | M[3][0] = M[3][1] = M[3][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 342 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 343 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 347 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 348 | M[1][1] = 2.f / (t - b); |
| 349 | M[1][0] = M[1][2] = M[1][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 350 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 351 | M[2][2] = -2.f / (f - n); |
| 352 | M[2][0] = M[2][1] = M[2][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 353 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 354 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 358 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 359 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 364 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 365 | m[0][0] = a / aspect; |
| 366 | m[0][1] = 0.f; |
| 367 | m[0][2] = 0.f; |
| 368 | m[0][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 369 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 370 | m[1][0] = 0.f; |
| 371 | m[1][1] = a; |
| 372 | m[1][2] = 0.f; |
| 373 | m[1][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 374 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 375 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 379 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 380 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 384 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 385 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 389 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 390 | /* 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 395 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 396 | vec3 s; |
| 397 | vec3_mul_cross(s, f, up); |
| 398 | vec3_norm(s, s); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 399 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 400 | vec3 t; |
| 401 | vec3_mul_cross(t, s, f); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 402 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 403 | m[0][0] = s[0]; |
| 404 | m[0][1] = t[0]; |
| 405 | m[0][2] = -f[0]; |
| 406 | m[0][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 407 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 408 | m[1][0] = s[1]; |
| 409 | m[1][1] = t[1]; |
| 410 | m[1][2] = -f[1]; |
| 411 | m[1][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 412 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 413 | m[2][0] = s[2]; |
| 414 | m[2][1] = t[2]; |
| 415 | m[2][2] = -f[2]; |
| 416 | m[2][3] = 0.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 417 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 418 | m[3][0] = 0.f; |
| 419 | m[3][1] = 0.f; |
| 420 | m[3][2] = 0.f; |
| 421 | m[3][3] = 1.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 422 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 423 | mat4x4_translate_in_place(m, -eye[0], -eye[1], -eye[2]); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | typedef float quat[4]; |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 427 | static inline void quat_identity(quat q) { |
| 428 | q[0] = q[1] = q[2] = 0.f; |
| 429 | q[3] = 1.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 430 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 431 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 435 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 436 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 440 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 441 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 449 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 450 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 454 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 455 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 461 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 462 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 467 | } |
| 468 | #define quat_norm vec4_norm |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 469 | static inline void quat_mul_vec3(vec3 r, quat q, vec3 v) { |
| 470 | quat v_ = {v[0], v[1], v[2], 0.f}; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 471 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 472 | quat_conj(r, q); |
| 473 | quat_norm(r, r); |
| 474 | quat_mul(r, v_, r); |
| 475 | quat_mul(r, q, r); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 476 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 477 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 486 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 487 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 491 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 492 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 496 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 497 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 501 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 502 | M[3][0] = M[3][1] = M[3][2] = 0.f; |
| 503 | M[3][3] = 1.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 504 | } |
| 505 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 506 | static 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 512 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 513 | R[3][0] = R[3][1] = R[3][2] = 0.f; |
| 514 | R[3][3] = 1.f; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 515 | } |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 516 | static inline void quat_from_mat4x4(quat q, mat4x4 M) { |
| 517 | float r = 0.f; |
| 518 | int i; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 519 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 520 | int perm[] = {0, 1, 2, 0, 1}; |
| 521 | int *p = perm; |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 522 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 523 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 530 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 531 | r = sqrtf(1.f + M[p[0]][p[0]] - M[p[1]][p[1]] - M[p[2]][p[2]]); |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 532 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 533 | if (r < 1e-6) { |
| 534 | q[0] = 1.f; |
| 535 | q[1] = q[2] = q[3] = 0.f; |
| 536 | return; |
| 537 | } |
Courtney Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 538 | |
Karl Schultz | 481756e | 2016-02-02 15:37:51 -0700 | [diff] [blame^] | 539 | 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 Goeltzenleuchter | 4825f6a | 2014-10-28 10:27:47 -0600 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | #endif |