1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://www.wtfpl.net/ for more details. |
---|
9 | // |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #if defined _XBOX |
---|
16 | # define _USE_MATH_DEFINES /* for M_PI */ |
---|
17 | # include <xtl.h> |
---|
18 | # undef near /* Fuck Microsoft */ |
---|
19 | # undef far /* Fuck Microsoft again */ |
---|
20 | #elif defined _WIN32 |
---|
21 | # define _USE_MATH_DEFINES /* for M_PI */ |
---|
22 | # define WIN32_LEAN_AND_MEAN |
---|
23 | # include <windows.h> |
---|
24 | # undef near /* Fuck Microsoft */ |
---|
25 | # undef far /* Fuck Microsoft again */ |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <cstdlib> /* free() */ |
---|
29 | #include <cstring> /* strdup() */ |
---|
30 | |
---|
31 | #include <ostream> /* std::ostream */ |
---|
32 | |
---|
33 | #include "core.h" |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | namespace lol |
---|
38 | { |
---|
39 | |
---|
40 | static inline float det2(float a, float b, |
---|
41 | float c, float d) |
---|
42 | { |
---|
43 | return a * d - b * c; |
---|
44 | } |
---|
45 | |
---|
46 | static inline float det3(float a, float b, float c, |
---|
47 | float d, float e, float f, |
---|
48 | float g, float h, float i) |
---|
49 | { |
---|
50 | return a * (e * i - h * f) |
---|
51 | + b * (f * g - i * d) |
---|
52 | + c * (d * h - g * e); |
---|
53 | } |
---|
54 | |
---|
55 | static inline float cofact(mat2 const &mat, int i, int j) |
---|
56 | { |
---|
57 | return mat[(i + 1) & 1][(j + 1) & 1] * (((i + j) & 1) ? -1.0f : 1.0f); |
---|
58 | } |
---|
59 | |
---|
60 | static inline float cofact(mat3 const &mat, int i, int j) |
---|
61 | { |
---|
62 | return det2(mat[(i + 1) % 3][(j + 1) % 3], |
---|
63 | mat[(i + 2) % 3][(j + 1) % 3], |
---|
64 | mat[(i + 1) % 3][(j + 2) % 3], |
---|
65 | mat[(i + 2) % 3][(j + 2) % 3]); |
---|
66 | } |
---|
67 | |
---|
68 | static inline float cofact(mat4 const &mat, int i, int j) |
---|
69 | { |
---|
70 | return det3(mat[(i + 1) & 3][(j + 1) & 3], |
---|
71 | mat[(i + 2) & 3][(j + 1) & 3], |
---|
72 | mat[(i + 3) & 3][(j + 1) & 3], |
---|
73 | mat[(i + 1) & 3][(j + 2) & 3], |
---|
74 | mat[(i + 2) & 3][(j + 2) & 3], |
---|
75 | mat[(i + 3) & 3][(j + 2) & 3], |
---|
76 | mat[(i + 1) & 3][(j + 3) & 3], |
---|
77 | mat[(i + 2) & 3][(j + 3) & 3], |
---|
78 | mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f); |
---|
79 | } |
---|
80 | |
---|
81 | template<> float determinant(mat2 const &mat) |
---|
82 | { |
---|
83 | return mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0]; |
---|
84 | } |
---|
85 | |
---|
86 | template<> mat2 transpose(mat2 const &mat) |
---|
87 | { |
---|
88 | mat2 ret; |
---|
89 | for (int j = 0; j < 2; j++) |
---|
90 | for (int i = 0; i < 2; i++) |
---|
91 | ret[j][i] = mat[i][j]; |
---|
92 | return ret; |
---|
93 | } |
---|
94 | |
---|
95 | template<> mat2 inverse(mat2 const &mat) |
---|
96 | { |
---|
97 | mat2 ret; |
---|
98 | float d = determinant(mat); |
---|
99 | if (d) |
---|
100 | { |
---|
101 | d = 1.0f / d; |
---|
102 | for (int j = 0; j < 2; j++) |
---|
103 | for (int i = 0; i < 2; i++) |
---|
104 | ret[j][i] = cofact(mat, i, j) * d; |
---|
105 | } |
---|
106 | return ret; |
---|
107 | } |
---|
108 | |
---|
109 | template<> float determinant(mat3 const &mat) |
---|
110 | { |
---|
111 | return det3(mat[0][0], mat[0][1], mat[0][2], |
---|
112 | mat[1][0], mat[1][1], mat[1][2], |
---|
113 | mat[2][0], mat[2][1], mat[2][2]); |
---|
114 | } |
---|
115 | |
---|
116 | template<> mat3 transpose(mat3 const &mat) |
---|
117 | { |
---|
118 | mat3 ret; |
---|
119 | for (int j = 0; j < 3; j++) |
---|
120 | for (int i = 0; i < 3; i++) |
---|
121 | ret[j][i] = mat[i][j]; |
---|
122 | return ret; |
---|
123 | } |
---|
124 | |
---|
125 | template<> mat3 inverse(mat3 const &mat) |
---|
126 | { |
---|
127 | mat3 ret; |
---|
128 | float d = determinant(mat); |
---|
129 | if (d) |
---|
130 | { |
---|
131 | d = 1.0f / d; |
---|
132 | for (int j = 0; j < 3; j++) |
---|
133 | for (int i = 0; i < 3; i++) |
---|
134 | ret[j][i] = cofact(mat, i, j) * d; |
---|
135 | } |
---|
136 | return ret; |
---|
137 | } |
---|
138 | |
---|
139 | template<> float determinant(mat4 const &mat) |
---|
140 | { |
---|
141 | float ret = 0; |
---|
142 | for (int n = 0; n < 4; n++) |
---|
143 | ret += mat[n][0] * cofact(mat, n, 0); |
---|
144 | return ret; |
---|
145 | } |
---|
146 | |
---|
147 | template<> mat4 transpose(mat4 const &mat) |
---|
148 | { |
---|
149 | mat4 ret; |
---|
150 | for (int j = 0; j < 4; j++) |
---|
151 | for (int i = 0; i < 4; i++) |
---|
152 | ret[j][i] = mat[i][j]; |
---|
153 | return ret; |
---|
154 | } |
---|
155 | |
---|
156 | template<> mat4 inverse(mat4 const &mat) |
---|
157 | { |
---|
158 | mat4 ret; |
---|
159 | float d = determinant(mat); |
---|
160 | if (d) |
---|
161 | { |
---|
162 | d = 1.0f / d; |
---|
163 | for (int j = 0; j < 4; j++) |
---|
164 | for (int i = 0; i < 4; i++) |
---|
165 | ret[j][i] = cofact(mat, i, j) * d; |
---|
166 | } |
---|
167 | return ret; |
---|
168 | } |
---|
169 | |
---|
170 | template<> void vec2::printf() const |
---|
171 | { |
---|
172 | Log::Debug("[ %6.6f %6.6f ]\n", x, y); |
---|
173 | } |
---|
174 | |
---|
175 | template<> void ivec2::printf() const |
---|
176 | { |
---|
177 | Log::Debug("[ %i %i ]\n", x, y); |
---|
178 | } |
---|
179 | |
---|
180 | template<> void cmplx::printf() const |
---|
181 | { |
---|
182 | Log::Debug("[ %6.6f %6.6f ]\n", x, y); |
---|
183 | } |
---|
184 | |
---|
185 | template<> void vec3::printf() const |
---|
186 | { |
---|
187 | Log::Debug("[ %6.6f %6.6f %6.6f ]\n", x, y, z); |
---|
188 | } |
---|
189 | |
---|
190 | template<> void ivec3::printf() const |
---|
191 | { |
---|
192 | Log::Debug("[ %i %i %i ]\n", x, y, z); |
---|
193 | } |
---|
194 | |
---|
195 | template<> void vec4::printf() const |
---|
196 | { |
---|
197 | Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w); |
---|
198 | } |
---|
199 | |
---|
200 | template<> void ivec4::printf() const |
---|
201 | { |
---|
202 | Log::Debug("[ %i %i %i %i ]\n", x, y, z, w); |
---|
203 | } |
---|
204 | |
---|
205 | template<> void quat::printf() const |
---|
206 | { |
---|
207 | Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", w, x, y, z); |
---|
208 | } |
---|
209 | |
---|
210 | template<> void mat2::printf() const |
---|
211 | { |
---|
212 | mat2 const &p = *this; |
---|
213 | |
---|
214 | Log::Debug("[ %6.6f %6.6f\n", p[0][0], p[1][0]); |
---|
215 | Log::Debug(" %6.6f %6.6f ]\n", p[0][1], p[1][1]); |
---|
216 | } |
---|
217 | |
---|
218 | template<> void mat3::printf() const |
---|
219 | { |
---|
220 | mat3 const &p = *this; |
---|
221 | |
---|
222 | Log::Debug("[ %6.6f %6.6f %6.6f\n", p[0][0], p[1][0], p[2][0]); |
---|
223 | Log::Debug(" %6.6f %6.6f %6.6f\n", p[0][1], p[1][1], p[2][1]); |
---|
224 | Log::Debug(" %6.6f %6.6f %6.6f ]\n", p[0][2], p[1][2], p[2][2]); |
---|
225 | } |
---|
226 | |
---|
227 | template<> void mat4::printf() const |
---|
228 | { |
---|
229 | mat4 const &p = *this; |
---|
230 | |
---|
231 | Log::Debug("[ %6.6f %6.6f %6.6f %6.6f\n", |
---|
232 | p[0][0], p[1][0], p[2][0], p[3][0]); |
---|
233 | Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n", |
---|
234 | p[0][1], p[1][1], p[2][1], p[3][1]); |
---|
235 | Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n", |
---|
236 | p[0][2], p[1][2], p[2][2], p[3][2]); |
---|
237 | Log::Debug(" %6.6f %6.6f %6.6f %6.6f ]\n", |
---|
238 | p[0][3], p[1][3], p[2][3], p[3][3]); |
---|
239 | } |
---|
240 | |
---|
241 | template<> std::ostream &operator<<(std::ostream &stream, ivec2 const &v) |
---|
242 | { |
---|
243 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
244 | } |
---|
245 | |
---|
246 | template<> std::ostream &operator<<(std::ostream &stream, icmplx const &v) |
---|
247 | { |
---|
248 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
249 | } |
---|
250 | |
---|
251 | template<> std::ostream &operator<<(std::ostream &stream, ivec3 const &v) |
---|
252 | { |
---|
253 | return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")"; |
---|
254 | } |
---|
255 | |
---|
256 | template<> std::ostream &operator<<(std::ostream &stream, ivec4 const &v) |
---|
257 | { |
---|
258 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
259 | << v.z << ", " << v.w << ")"; |
---|
260 | } |
---|
261 | |
---|
262 | template<> std::ostream &operator<<(std::ostream &stream, iquat const &v) |
---|
263 | { |
---|
264 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
265 | << v.z << ", " << v.w << ")"; |
---|
266 | } |
---|
267 | |
---|
268 | template<> std::ostream &operator<<(std::ostream &stream, vec2 const &v) |
---|
269 | { |
---|
270 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
271 | } |
---|
272 | |
---|
273 | template<> std::ostream &operator<<(std::ostream &stream, cmplx const &v) |
---|
274 | { |
---|
275 | return stream << "(" << v.x << ", " << v.y << ")"; |
---|
276 | } |
---|
277 | |
---|
278 | template<> std::ostream &operator<<(std::ostream &stream, vec3 const &v) |
---|
279 | { |
---|
280 | return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")"; |
---|
281 | } |
---|
282 | |
---|
283 | template<> std::ostream &operator<<(std::ostream &stream, vec4 const &v) |
---|
284 | { |
---|
285 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
286 | << v.z << ", " << v.w << ")"; |
---|
287 | } |
---|
288 | |
---|
289 | template<> std::ostream &operator<<(std::ostream &stream, quat const &v) |
---|
290 | { |
---|
291 | return stream << "(" << v.x << ", " << v.y << ", " |
---|
292 | << v.z << ", " << v.w << ")"; |
---|
293 | } |
---|
294 | |
---|
295 | template<> std::ostream &operator<<(std::ostream &stream, mat4 const &m) |
---|
296 | { |
---|
297 | stream << "((" << m[0][0] << ", " << m[1][0] |
---|
298 | << ", " << m[2][0] << ", " << m[3][0] << "), "; |
---|
299 | stream << "(" << m[0][1] << ", " << m[1][1] |
---|
300 | << ", " << m[2][1] << ", " << m[3][1] << "), "; |
---|
301 | stream << "(" << m[0][2] << ", " << m[1][2] |
---|
302 | << ", " << m[2][2] << ", " << m[3][2] << "), "; |
---|
303 | stream << "(" << m[0][3] << ", " << m[1][3] |
---|
304 | << ", " << m[2][3] << ", " << m[3][3] << "))"; |
---|
305 | return stream; |
---|
306 | } |
---|
307 | |
---|
308 | template<> mat3 mat3::scale(float x) |
---|
309 | { |
---|
310 | mat3 ret(1.0f); |
---|
311 | |
---|
312 | ret[0][0] = x; |
---|
313 | ret[1][1] = x; |
---|
314 | ret[2][2] = x; |
---|
315 | |
---|
316 | return ret; |
---|
317 | } |
---|
318 | |
---|
319 | template<> mat3 mat3::scale(float x, float y, float z) |
---|
320 | { |
---|
321 | mat3 ret(1.0f); |
---|
322 | |
---|
323 | ret[0][0] = x; |
---|
324 | ret[1][1] = y; |
---|
325 | ret[2][2] = z; |
---|
326 | |
---|
327 | return ret; |
---|
328 | } |
---|
329 | |
---|
330 | template<> mat3 mat3::scale(vec3 v) |
---|
331 | { |
---|
332 | return scale(v.x, v.y, v.z); |
---|
333 | } |
---|
334 | |
---|
335 | template<> mat4 mat4::translate(float x, float y, float z) |
---|
336 | { |
---|
337 | mat4 ret(1.0f); |
---|
338 | ret[3][0] = x; |
---|
339 | ret[3][1] = y; |
---|
340 | ret[3][2] = z; |
---|
341 | return ret; |
---|
342 | } |
---|
343 | |
---|
344 | template<> mat4 mat4::translate(vec3 v) |
---|
345 | { |
---|
346 | return translate(v.x, v.y, v.z); |
---|
347 | } |
---|
348 | |
---|
349 | template<> mat2 mat2::rotate(float angle) |
---|
350 | { |
---|
351 | angle *= (M_PI / 180.0f); |
---|
352 | |
---|
353 | float st = sin(angle); |
---|
354 | float ct = cos(angle); |
---|
355 | |
---|
356 | mat2 ret; |
---|
357 | |
---|
358 | ret[0][0] = ct; |
---|
359 | ret[0][1] = st; |
---|
360 | |
---|
361 | ret[1][0] = -st; |
---|
362 | ret[1][1] = ct; |
---|
363 | |
---|
364 | return ret; |
---|
365 | } |
---|
366 | |
---|
367 | template<> mat3 mat3::rotate(float angle, float x, float y, float z) |
---|
368 | { |
---|
369 | angle *= (M_PI / 180.0f); |
---|
370 | |
---|
371 | float st = sin(angle); |
---|
372 | float ct = cos(angle); |
---|
373 | |
---|
374 | float len = std::sqrt(x * x + y * y + z * z); |
---|
375 | float invlen = len ? 1.0f / len : 0.0f; |
---|
376 | x *= invlen; |
---|
377 | y *= invlen; |
---|
378 | z *= invlen; |
---|
379 | |
---|
380 | float mtx = (1.0f - ct) * x; |
---|
381 | float mty = (1.0f - ct) * y; |
---|
382 | float mtz = (1.0f - ct) * z; |
---|
383 | |
---|
384 | mat3 ret; |
---|
385 | |
---|
386 | ret[0][0] = x * mtx + ct; |
---|
387 | ret[0][1] = x * mty + st * z; |
---|
388 | ret[0][2] = x * mtz - st * y; |
---|
389 | |
---|
390 | ret[1][0] = y * mtx - st * z; |
---|
391 | ret[1][1] = y * mty + ct; |
---|
392 | ret[1][2] = y * mtz + st * x; |
---|
393 | |
---|
394 | ret[2][0] = z * mtx + st * y; |
---|
395 | ret[2][1] = z * mty - st * x; |
---|
396 | ret[2][2] = z * mtz + ct; |
---|
397 | |
---|
398 | return ret; |
---|
399 | } |
---|
400 | |
---|
401 | template<> mat3 mat3::rotate(float angle, vec3 v) |
---|
402 | { |
---|
403 | return rotate(angle, v.x, v.y, v.z); |
---|
404 | } |
---|
405 | |
---|
406 | template<> mat3::Mat3(quat const &q) |
---|
407 | { |
---|
408 | float n = norm(q); |
---|
409 | |
---|
410 | if (!n) |
---|
411 | { |
---|
412 | for (int j = 0; j < 3; j++) |
---|
413 | for (int i = 0; i < 3; i++) |
---|
414 | (*this)[i][j] = (i == j) ? 1.f : 0.f; |
---|
415 | return; |
---|
416 | } |
---|
417 | |
---|
418 | float s = 2.0f / n; |
---|
419 | |
---|
420 | v0[0] = 1.0f - s * (q.y * q.y + q.z * q.z); |
---|
421 | v0[1] = s * (q.x * q.y + q.z * q.w); |
---|
422 | v0[2] = s * (q.x * q.z - q.y * q.w); |
---|
423 | |
---|
424 | v1[0] = s * (q.x * q.y - q.z * q.w); |
---|
425 | v1[1] = 1.0f - s * (q.z * q.z + q.x * q.x); |
---|
426 | v1[2] = s * (q.y * q.z + q.x * q.w); |
---|
427 | |
---|
428 | v2[0] = s * (q.x * q.z + q.y * q.w); |
---|
429 | v2[1] = s * (q.y * q.z - q.x * q.w); |
---|
430 | v2[2] = 1.0f - s * (q.x * q.x + q.y * q.y); |
---|
431 | } |
---|
432 | |
---|
433 | template<> mat4::Mat4(quat const &q) |
---|
434 | { |
---|
435 | *this = mat4(mat3(q), 1.f); |
---|
436 | } |
---|
437 | |
---|
438 | static inline void MatrixToQuat(quat &that, mat3 const &m) |
---|
439 | { |
---|
440 | /* See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm for a version with no branches */ |
---|
441 | float t = m[0][0] + m[1][1] + m[2][2]; |
---|
442 | if (t > 0) |
---|
443 | { |
---|
444 | that.w = 0.5f * std::sqrt(1.0f + t); |
---|
445 | float s = 0.25f / that.w; |
---|
446 | that.x = s * (m[1][2] - m[2][1]); |
---|
447 | that.y = s * (m[2][0] - m[0][2]); |
---|
448 | that.z = s * (m[0][1] - m[1][0]); |
---|
449 | } |
---|
450 | else if (m[0][0] > m[1][1] && m[0][0] > m[2][2]) |
---|
451 | { |
---|
452 | that.x = 0.5f * std::sqrt(1.0f + m[0][0] - m[1][1] - m[2][2]); |
---|
453 | float s = 0.25f / that.x; |
---|
454 | that.y = s * (m[0][1] + m[1][0]); |
---|
455 | that.z = s * (m[2][0] + m[0][2]); |
---|
456 | that.w = s * (m[1][2] - m[2][1]); |
---|
457 | } |
---|
458 | else if (m[1][1] > m[2][2]) |
---|
459 | { |
---|
460 | that.y = 0.5f * std::sqrt(1.0f - m[0][0] + m[1][1] - m[2][2]); |
---|
461 | float s = 0.25f / that.y; |
---|
462 | that.x = s * (m[0][1] + m[1][0]); |
---|
463 | that.z = s * (m[1][2] + m[2][1]); |
---|
464 | that.w = s * (m[2][0] - m[0][2]); |
---|
465 | } |
---|
466 | else |
---|
467 | { |
---|
468 | that.z = 0.5f * std::sqrt(1.0f - m[0][0] - m[1][1] + m[2][2]); |
---|
469 | float s = 0.25f / that.z; |
---|
470 | that.x = s * (m[2][0] + m[0][2]); |
---|
471 | that.y = s * (m[1][2] + m[2][1]); |
---|
472 | that.w = s * (m[0][1] - m[1][0]); |
---|
473 | } |
---|
474 | } |
---|
475 | |
---|
476 | template<> quat::Quat(mat3 const &m) |
---|
477 | { |
---|
478 | MatrixToQuat(*this, m); |
---|
479 | } |
---|
480 | |
---|
481 | template<> quat::Quat(mat4 const &m) |
---|
482 | { |
---|
483 | MatrixToQuat(*this, mat3(m)); |
---|
484 | } |
---|
485 | |
---|
486 | template<> quat quat::rotate(float angle, vec3 const &v) |
---|
487 | { |
---|
488 | angle *= (M_PI / 360.0f); |
---|
489 | |
---|
490 | vec3 tmp = normalize(v) * sin(angle); |
---|
491 | |
---|
492 | return quat(cos(angle), tmp.x, tmp.y, tmp.z); |
---|
493 | } |
---|
494 | |
---|
495 | template<> quat quat::rotate(float angle, float x, float y, float z) |
---|
496 | { |
---|
497 | return quat::rotate(angle, vec3(x, y, z)); |
---|
498 | } |
---|
499 | |
---|
500 | template<> quat slerp(quat const &qa, quat const &qb, float f) |
---|
501 | { |
---|
502 | float const magnitude = lol::sqrt(sqlength(qa) * sqlength(qb)); |
---|
503 | float const product = lol::dot(qa, qb) / magnitude; |
---|
504 | |
---|
505 | /* If quaternions are equal or opposite, there is no need |
---|
506 | * to slerp anything, just return qa. */ |
---|
507 | if (std::abs(product) >= 1.0f) |
---|
508 | return qa; |
---|
509 | |
---|
510 | float const sign = (product < 0.0f) ? -1.0f : 1.0f; |
---|
511 | float const theta = lol::acos(sign * product); |
---|
512 | float const s1 = lol::sin(sign * f * theta); |
---|
513 | float const s0 = lol::sin((1.0f - f) * theta); |
---|
514 | |
---|
515 | /* This is the same as 1/sin(theta) */ |
---|
516 | float const d = 1.0f / lol::sqrt(1.f - product * product); |
---|
517 | |
---|
518 | return qa * (s0 * d) + qb * (s1 * d); |
---|
519 | } |
---|
520 | |
---|
521 | template<> vec3 vec3::toeuler(quat const &q) |
---|
522 | { |
---|
523 | float n = norm(q); |
---|
524 | |
---|
525 | if (!n) |
---|
526 | return vec3(0.f); |
---|
527 | |
---|
528 | vec3 ret(atan2(2.f * (q.w * q.x + q.y * q.z), |
---|
529 | 1.f - 2.f * (q.x * q.x + q.y * q.y)), |
---|
530 | asin(2.f * (q.w * q.y - q.z * q.x)), |
---|
531 | atan2(2.f * (q.w * q.z + q.y * q.x), |
---|
532 | 1.f - 2.f * (q.z * q.z + q.y * q.y))); |
---|
533 | |
---|
534 | return (float)(180.0f / M_PI / n) * ret; |
---|
535 | } |
---|
536 | |
---|
537 | static inline mat3 mat3_fromeuler_generic(vec3 const &v, int i, int j, int k) |
---|
538 | { |
---|
539 | mat3 ret; |
---|
540 | |
---|
541 | vec3 radians = (float)(M_PI / 180.0f) * v; |
---|
542 | float s0 = sin(radians[0]), c0 = cos(radians[0]); |
---|
543 | float s1 = sin(radians[1]), c1 = cos(radians[1]); |
---|
544 | float s2 = sin(radians[2]), c2 = cos(radians[2]); |
---|
545 | |
---|
546 | if (k == i) |
---|
547 | { |
---|
548 | k = 3 - i - j; |
---|
549 | |
---|
550 | ret[i][i] = c1; |
---|
551 | ret[j][i] = s1 * s2; |
---|
552 | ret[i][j] = s0 * s1; |
---|
553 | ret[j][j] = c0 * c2 - s0 * c1 * s2; |
---|
554 | ret[k][k] = - s0 * s2 + c0 * c1 * c2; |
---|
555 | |
---|
556 | if ((2 + i - j) % 3) |
---|
557 | { |
---|
558 | ret[k][i] = s1 * c2; |
---|
559 | ret[k][j] = - c0 * s2 - s0 * c1 * c2; |
---|
560 | ret[i][k] = - c0 * s1; |
---|
561 | ret[j][k] = s0 * c2 + c0 * c1 * s2; |
---|
562 | } |
---|
563 | else |
---|
564 | { |
---|
565 | ret[k][i] = - s1 * c2; |
---|
566 | ret[k][j] = c0 * s2 + s0 * c1 * c2; |
---|
567 | ret[i][k] = c0 * s1; |
---|
568 | ret[j][k] = - s0 * c2 - c0 * c1 * s2; |
---|
569 | } |
---|
570 | } |
---|
571 | else |
---|
572 | { |
---|
573 | ret[i][i] = c1 * c2; |
---|
574 | ret[k][k] = c0 * c1; |
---|
575 | |
---|
576 | if ((2 + i - j) % 3) |
---|
577 | { |
---|
578 | ret[j][i] = - c1 * s2; |
---|
579 | ret[k][i] = s1; |
---|
580 | |
---|
581 | ret[i][j] = c0 * s2 + s0 * s1 * c2; |
---|
582 | ret[j][j] = c0 * c2 - s0 * s1 * s2; |
---|
583 | ret[k][j] = - s0 * c1; |
---|
584 | |
---|
585 | ret[i][k] = s0 * s2 - c0 * s1 * c2; |
---|
586 | ret[j][k] = s0 * c2 + c0 * s1 * s2; |
---|
587 | } |
---|
588 | else |
---|
589 | { |
---|
590 | ret[j][i] = c1 * s2; |
---|
591 | ret[k][i] = - s1; |
---|
592 | |
---|
593 | ret[i][j] = - c0 * s2 + s0 * s1 * c2; |
---|
594 | ret[j][j] = c0 * c2 + s0 * s1 * s2; |
---|
595 | ret[k][j] = s0 * c1; |
---|
596 | |
---|
597 | ret[i][k] = s0 * s2 + c0 * s1 * c2; |
---|
598 | ret[j][k] = - s0 * c2 + c0 * s1 * s2; |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | return ret; |
---|
603 | } |
---|
604 | |
---|
605 | static inline quat quat_fromeuler_generic(vec3 const &v, int i, int j, int k) |
---|
606 | { |
---|
607 | vec3 half_angles = (float)(M_PI / 360.0f) * v; |
---|
608 | float s0 = sin(half_angles[0]), c0 = cos(half_angles[0]); |
---|
609 | float s1 = sin(half_angles[1]), c1 = cos(half_angles[1]); |
---|
610 | float s2 = sin(half_angles[2]), c2 = cos(half_angles[2]); |
---|
611 | |
---|
612 | quat ret; |
---|
613 | |
---|
614 | if (k == i) |
---|
615 | { |
---|
616 | k = 3 - i - j; |
---|
617 | |
---|
618 | ret[0] = c1 * (c0 * c2 - s0 * s2); |
---|
619 | ret[1 + i] = c1 * (c0 * s2 + s0 * c2); |
---|
620 | ret[1 + j] = s1 * (c0 * c2 + s0 * s2); |
---|
621 | ret[1 + k] = ((2 + i - j) % 3) ? s1 * (s0 * c2 - c0 * s2) |
---|
622 | : s1 * (c0 * s2 - s0 * c2); |
---|
623 | } |
---|
624 | else |
---|
625 | { |
---|
626 | vec4 v1(c0 * c1 * c2, s0 * c1 * c2, c0 * s1 * c2, c0 * c1 * s2); |
---|
627 | vec4 v2(s0 * s1 * s2, -c0 * s1 * s2, s0 * c1 * s2, -s0 * s1 * c2); |
---|
628 | |
---|
629 | if ((2 + i - j) % 3) |
---|
630 | v1 -= v2; |
---|
631 | else |
---|
632 | v1 += v2; |
---|
633 | |
---|
634 | ret[0] = v1[0]; |
---|
635 | ret[1 + i] = v1[1]; |
---|
636 | ret[1 + j] = v1[2]; |
---|
637 | ret[1 + k] = v1[3]; |
---|
638 | } |
---|
639 | |
---|
640 | return ret; |
---|
641 | } |
---|
642 | |
---|
643 | #define DEFINE_FROMEULER_GENERIC(name, i, j, k) \ |
---|
644 | template<> quat quat::fromeuler_##name(vec3 const &v) \ |
---|
645 | { \ |
---|
646 | return quat_fromeuler_generic(v, i, j, k); \ |
---|
647 | } \ |
---|
648 | \ |
---|
649 | template<> quat quat::fromeuler_##name(float phi, float theta, float psi) \ |
---|
650 | { \ |
---|
651 | return quat::fromeuler_##name(vec3(phi, theta, psi)); \ |
---|
652 | } \ |
---|
653 | \ |
---|
654 | template<> mat3 mat3::fromeuler_##name(vec3 const &v) \ |
---|
655 | { \ |
---|
656 | return mat3_fromeuler_generic(v, i, j, k); \ |
---|
657 | } \ |
---|
658 | \ |
---|
659 | template<> mat3 mat3::fromeuler_##name(float phi, float theta, float psi) \ |
---|
660 | { \ |
---|
661 | return mat3::fromeuler_##name(vec3(phi, theta, psi)); \ |
---|
662 | } \ |
---|
663 | \ |
---|
664 | template<> mat4 mat4::fromeuler_##name(vec3 const &v) \ |
---|
665 | { \ |
---|
666 | return mat4(mat3_fromeuler_generic(v, i, j, k), 1.f); \ |
---|
667 | } \ |
---|
668 | \ |
---|
669 | template<> mat4 mat4::fromeuler_##name(float phi, float theta, float psi) \ |
---|
670 | { \ |
---|
671 | return mat4::fromeuler_##name(vec3(phi, theta, psi)); \ |
---|
672 | } |
---|
673 | |
---|
674 | DEFINE_FROMEULER_GENERIC(xyx, 0, 1, 0) |
---|
675 | DEFINE_FROMEULER_GENERIC(xzx, 0, 2, 0) |
---|
676 | DEFINE_FROMEULER_GENERIC(yxy, 1, 0, 1) |
---|
677 | DEFINE_FROMEULER_GENERIC(yzy, 1, 2, 1) |
---|
678 | DEFINE_FROMEULER_GENERIC(zxz, 2, 0, 2) |
---|
679 | DEFINE_FROMEULER_GENERIC(zyz, 2, 1, 2) |
---|
680 | |
---|
681 | DEFINE_FROMEULER_GENERIC(xyz, 0, 1, 2) |
---|
682 | DEFINE_FROMEULER_GENERIC(xzy, 0, 2, 1) |
---|
683 | DEFINE_FROMEULER_GENERIC(yxz, 1, 0, 2) |
---|
684 | DEFINE_FROMEULER_GENERIC(yzx, 1, 2, 0) |
---|
685 | DEFINE_FROMEULER_GENERIC(zxy, 2, 0, 1) |
---|
686 | DEFINE_FROMEULER_GENERIC(zyx, 2, 1, 0) |
---|
687 | |
---|
688 | #undef DEFINE_FROMEULER_GENERIC |
---|
689 | |
---|
690 | template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up) |
---|
691 | { |
---|
692 | vec3 v3 = normalize(eye - center); |
---|
693 | vec3 v2 = normalize(up); |
---|
694 | vec3 v1 = normalize(cross(v2, v3)); |
---|
695 | v2 = cross(v3, v1); |
---|
696 | |
---|
697 | mat4 orient(1.0f); |
---|
698 | orient[0][0] = v1.x; |
---|
699 | orient[0][1] = v2.x; |
---|
700 | orient[0][2] = v3.x; |
---|
701 | orient[1][0] = v1.y; |
---|
702 | orient[1][1] = v2.y; |
---|
703 | orient[1][2] = v3.y; |
---|
704 | orient[2][0] = v1.z; |
---|
705 | orient[2][1] = v2.z; |
---|
706 | orient[2][2] = v3.z; |
---|
707 | |
---|
708 | return orient * mat4::translate(-eye); |
---|
709 | } |
---|
710 | |
---|
711 | template<> mat4 mat4::ortho(float left, float right, float bottom, |
---|
712 | float top, float near, float far) |
---|
713 | { |
---|
714 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
715 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
716 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
717 | |
---|
718 | mat4 ret(0.0f); |
---|
719 | ret[0][0] = 2.0f * invrl; |
---|
720 | ret[1][1] = 2.0f * invtb; |
---|
721 | ret[2][2] = -2.0f * invfn; |
---|
722 | ret[3][0] = - (right + left) * invrl; |
---|
723 | ret[3][1] = - (top + bottom) * invtb; |
---|
724 | ret[3][2] = - (far + near) * invfn; |
---|
725 | ret[3][3] = 1.0f; |
---|
726 | return ret; |
---|
727 | } |
---|
728 | |
---|
729 | template<> mat4 mat4::ortho(float width, float height, |
---|
730 | float near, float far) |
---|
731 | { |
---|
732 | return mat4::ortho(-0.5f * width, 0.5f * width, |
---|
733 | -0.5f * height, 0.5f * height, near, far); |
---|
734 | } |
---|
735 | |
---|
736 | template<> mat4 mat4::frustum(float left, float right, float bottom, |
---|
737 | float top, float near, float far) |
---|
738 | { |
---|
739 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
740 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
741 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
742 | |
---|
743 | mat4 ret(0.0f); |
---|
744 | ret[0][0] = 2.0f * near * invrl; |
---|
745 | ret[1][1] = 2.0f * near * invtb; |
---|
746 | ret[2][0] = (right + left) * invrl; |
---|
747 | ret[2][1] = (top + bottom) * invtb; |
---|
748 | ret[2][2] = - (far + near) * invfn; |
---|
749 | ret[2][3] = -1.0f; |
---|
750 | ret[3][2] = -2.0f * far * near * invfn; |
---|
751 | return ret; |
---|
752 | } |
---|
753 | |
---|
754 | template<> mat4 mat4::perspective(float fov_y, float width, |
---|
755 | float height, float near, float far) |
---|
756 | { |
---|
757 | fov_y *= (M_PI / 180.0f); |
---|
758 | |
---|
759 | float t2 = tanf(fov_y * 0.5f); |
---|
760 | float t1 = t2 * width / height; |
---|
761 | |
---|
762 | return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far); |
---|
763 | } |
---|
764 | |
---|
765 | } /* namespace lol */ |
---|
766 | |
---|