1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2012 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://sam.zoy.org/projects/COPYING.WTFPL 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, float y, float z) |
---|
309 | { |
---|
310 | mat3 ret(1.0f); |
---|
311 | |
---|
312 | ret[0][0] = x; |
---|
313 | ret[1][1] = y; |
---|
314 | ret[2][2] = z; |
---|
315 | |
---|
316 | return ret; |
---|
317 | } |
---|
318 | |
---|
319 | template<> mat3 mat3::scale(vec3 v) |
---|
320 | { |
---|
321 | return scale(v.x, v.y, v.z); |
---|
322 | } |
---|
323 | |
---|
324 | template<> mat4 mat4::translate(float x, float y, float z) |
---|
325 | { |
---|
326 | mat4 ret(1.0f); |
---|
327 | ret[3][0] = x; |
---|
328 | ret[3][1] = y; |
---|
329 | ret[3][2] = z; |
---|
330 | return ret; |
---|
331 | } |
---|
332 | |
---|
333 | template<> mat4 mat4::translate(vec3 v) |
---|
334 | { |
---|
335 | return translate(v.x, v.y, v.z); |
---|
336 | } |
---|
337 | |
---|
338 | template<> mat2 mat2::rotate(float angle) |
---|
339 | { |
---|
340 | angle *= (M_PI / 180.0f); |
---|
341 | |
---|
342 | float st = sin(angle); |
---|
343 | float ct = cos(angle); |
---|
344 | |
---|
345 | mat2 ret; |
---|
346 | |
---|
347 | ret[0][0] = ct; |
---|
348 | ret[0][1] = st; |
---|
349 | |
---|
350 | ret[1][0] = -st; |
---|
351 | ret[1][1] = ct; |
---|
352 | |
---|
353 | return ret; |
---|
354 | } |
---|
355 | |
---|
356 | template<> mat3 mat3::rotate(float angle, float x, float y, float z) |
---|
357 | { |
---|
358 | angle *= (M_PI / 180.0f); |
---|
359 | |
---|
360 | float st = sin(angle); |
---|
361 | float ct = cos(angle); |
---|
362 | |
---|
363 | float len = std::sqrt(x * x + y * y + z * z); |
---|
364 | float invlen = len ? 1.0f / len : 0.0f; |
---|
365 | x *= invlen; |
---|
366 | y *= invlen; |
---|
367 | z *= invlen; |
---|
368 | |
---|
369 | float mtx = (1.0f - ct) * x; |
---|
370 | float mty = (1.0f - ct) * y; |
---|
371 | float mtz = (1.0f - ct) * z; |
---|
372 | |
---|
373 | mat3 ret; |
---|
374 | |
---|
375 | ret[0][0] = x * mtx + ct; |
---|
376 | ret[0][1] = x * mty + st * z; |
---|
377 | ret[0][2] = x * mtz - st * y; |
---|
378 | |
---|
379 | ret[1][0] = y * mtx - st * z; |
---|
380 | ret[1][1] = y * mty + ct; |
---|
381 | ret[1][2] = y * mtz + st * x; |
---|
382 | |
---|
383 | ret[2][0] = z * mtx + st * y; |
---|
384 | ret[2][1] = z * mty - st * x; |
---|
385 | ret[2][2] = z * mtz + ct; |
---|
386 | |
---|
387 | return ret; |
---|
388 | } |
---|
389 | |
---|
390 | template<> mat3 mat3::rotate(float angle, vec3 v) |
---|
391 | { |
---|
392 | return rotate(angle, v.x, v.y, v.z); |
---|
393 | } |
---|
394 | |
---|
395 | template<> mat3::Mat3(quat const &q) |
---|
396 | { |
---|
397 | float n = norm(q); |
---|
398 | |
---|
399 | if (!n) |
---|
400 | { |
---|
401 | for (int j = 0; j < 3; j++) |
---|
402 | for (int i = 0; i < 3; i++) |
---|
403 | (*this)[i][j] = (i == j) ? 1.f : 0.f; |
---|
404 | return; |
---|
405 | } |
---|
406 | |
---|
407 | float s = 2.0f / n; |
---|
408 | |
---|
409 | v0[0] = 1.0f - s * (q.y * q.y + q.z * q.z); |
---|
410 | v0[1] = s * (q.x * q.y + q.z * q.w); |
---|
411 | v0[2] = s * (q.x * q.z - q.y * q.w); |
---|
412 | |
---|
413 | v1[0] = s * (q.x * q.y - q.z * q.w); |
---|
414 | v1[1] = 1.0f - s * (q.z * q.z + q.x * q.x); |
---|
415 | v1[2] = s * (q.y * q.z + q.x * q.w); |
---|
416 | |
---|
417 | v2[0] = s * (q.x * q.z + q.y * q.w); |
---|
418 | v2[1] = s * (q.y * q.z - q.x * q.w); |
---|
419 | v2[2] = 1.0f - s * (q.x * q.x + q.y * q.y); |
---|
420 | } |
---|
421 | |
---|
422 | template<> mat4::Mat4(quat const &q) |
---|
423 | { |
---|
424 | *this = mat4(mat3(q), 1.f); |
---|
425 | } |
---|
426 | |
---|
427 | static inline void MatrixToQuat(quat &that, mat3 const &m) |
---|
428 | { |
---|
429 | /* See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm for a version with no branches */ |
---|
430 | float t = m[0][0] + m[1][1] + m[2][2]; |
---|
431 | if (t > 0) |
---|
432 | { |
---|
433 | that.w = 0.5f * std::sqrt(1.0f + t); |
---|
434 | float s = 0.25f / that.w; |
---|
435 | that.x = s * (m[1][2] - m[2][1]); |
---|
436 | that.y = s * (m[2][0] - m[0][2]); |
---|
437 | that.z = s * (m[0][1] - m[1][0]); |
---|
438 | } |
---|
439 | else if (m[0][0] > m[1][1] && m[0][0] > m[2][2]) |
---|
440 | { |
---|
441 | that.x = 0.5f * std::sqrt(1.0f + m[0][0] - m[1][1] - m[2][2]); |
---|
442 | float s = 0.25f / that.x; |
---|
443 | that.y = s * (m[0][1] + m[1][0]); |
---|
444 | that.z = s * (m[2][0] + m[0][2]); |
---|
445 | that.w = s * (m[1][2] - m[2][1]); |
---|
446 | } |
---|
447 | else if (m[1][1] > m[2][2]) |
---|
448 | { |
---|
449 | that.y = 0.5f * std::sqrt(1.0f - m[0][0] + m[1][1] - m[2][2]); |
---|
450 | float s = 0.25f / that.y; |
---|
451 | that.x = s * (m[0][1] + m[1][0]); |
---|
452 | that.z = s * (m[1][2] + m[2][1]); |
---|
453 | that.w = s * (m[2][0] - m[0][2]); |
---|
454 | } |
---|
455 | else |
---|
456 | { |
---|
457 | that.z = 0.5f * std::sqrt(1.0f - m[0][0] - m[1][1] + m[2][2]); |
---|
458 | float s = 0.25f / that.z; |
---|
459 | that.x = s * (m[2][0] + m[0][2]); |
---|
460 | that.y = s * (m[1][2] + m[2][1]); |
---|
461 | that.w = s * (m[0][1] - m[1][0]); |
---|
462 | } |
---|
463 | } |
---|
464 | |
---|
465 | template<> quat::Quat(mat3 const &m) |
---|
466 | { |
---|
467 | MatrixToQuat(*this, m); |
---|
468 | } |
---|
469 | |
---|
470 | template<> quat::Quat(mat4 const &m) |
---|
471 | { |
---|
472 | MatrixToQuat(*this, mat3(m)); |
---|
473 | } |
---|
474 | |
---|
475 | template<> quat quat::rotate(float angle, vec3 const &v) |
---|
476 | { |
---|
477 | angle *= (M_PI / 360.0f); |
---|
478 | |
---|
479 | vec3 tmp = normalize(v) * sin(angle); |
---|
480 | |
---|
481 | return quat(cos(angle), tmp.x, tmp.y, tmp.z); |
---|
482 | } |
---|
483 | |
---|
484 | template<> quat quat::rotate(float angle, float x, float y, float z) |
---|
485 | { |
---|
486 | return quat::rotate(angle, vec3(x, y, z)); |
---|
487 | } |
---|
488 | |
---|
489 | template<> quat quat::slerp(quat QuatA, quat QuatB, float const &Scalar) |
---|
490 | { |
---|
491 | float magnitude = lol::sqrt(sqlength(QuatA) * sqlength(QuatB)); |
---|
492 | //btAssert(magnitude > btScalar(0)); |
---|
493 | |
---|
494 | float product = lol::dot(QuatA,QuatB) / magnitude; |
---|
495 | if (product > -1.0f && product < 1.0f) |
---|
496 | { |
---|
497 | // Take care of long angle case see http://en.wikipedia.org/wiki/Slerp |
---|
498 | const float sign = (product < 0.0f) ? -1.0f : 1.0f; |
---|
499 | |
---|
500 | const float theta = lol::acos(sign * product); |
---|
501 | const float s1 = lol::sin(sign * Scalar * theta); |
---|
502 | const float d = 1.0f / lol::sin(theta); |
---|
503 | const float s0 = lol::sin((1.0f - Scalar) * theta); |
---|
504 | |
---|
505 | return quat( |
---|
506 | (QuatA.w * s0 + QuatB.w * s1) * d, |
---|
507 | (QuatA.x * s0 + QuatB.x * s1) * d, |
---|
508 | (QuatA.y * s0 + QuatB.y * s1) * d, |
---|
509 | (QuatA.z * s0 + QuatB.z * s1) * d); |
---|
510 | } |
---|
511 | else |
---|
512 | { |
---|
513 | return QuatA; |
---|
514 | } |
---|
515 | } |
---|
516 | |
---|
517 | template<> vec3 vec3::toeuler(quat const &q) |
---|
518 | { |
---|
519 | float n = norm(q); |
---|
520 | |
---|
521 | if (!n) |
---|
522 | return vec3(0.f); |
---|
523 | |
---|
524 | vec3 ret(atan2(2.f * (q.w * q.x + q.y * q.z), |
---|
525 | 1.f - 2.f * (q.x * q.x + q.y * q.y)), |
---|
526 | asin(2.f * (q.w * q.y - q.z * q.x)), |
---|
527 | atan2(2.f * (q.w * q.z + q.y * q.x), |
---|
528 | 1.f - 2.f * (q.z * q.z + q.y * q.y))); |
---|
529 | |
---|
530 | return (180.0f / M_PI / n) * ret; |
---|
531 | } |
---|
532 | |
---|
533 | static inline mat3 mat3_fromeuler_generic(vec3 const &v, int i, int j, int k) |
---|
534 | { |
---|
535 | mat3 ret; |
---|
536 | |
---|
537 | vec3 radians = (M_PI / 180.0f) * v; |
---|
538 | float s0 = sin(radians[0]), c0 = cos(radians[0]); |
---|
539 | float s1 = sin(radians[1]), c1 = cos(radians[1]); |
---|
540 | float s2 = sin(radians[2]), c2 = cos(radians[2]); |
---|
541 | |
---|
542 | if (k == i) |
---|
543 | { |
---|
544 | k = 3 - i - j; |
---|
545 | |
---|
546 | ret[i][i] = c1; |
---|
547 | ret[j][i] = s1 * s2; |
---|
548 | ret[i][j] = s0 * s1; |
---|
549 | ret[j][j] = c0 * c2 - s0 * c1 * s2; |
---|
550 | ret[k][k] = - s0 * s2 + c0 * c1 * c2; |
---|
551 | |
---|
552 | if ((2 + i - j) % 3) |
---|
553 | { |
---|
554 | ret[k][i] = s1 * c2; |
---|
555 | ret[k][j] = - c0 * s2 - s0 * c1 * c2; |
---|
556 | ret[i][k] = - c0 * s1; |
---|
557 | ret[j][k] = s0 * c2 + c0 * c1 * s2; |
---|
558 | } |
---|
559 | else |
---|
560 | { |
---|
561 | ret[k][i] = - s1 * c2; |
---|
562 | ret[k][j] = c0 * s2 + s0 * c1 * c2; |
---|
563 | ret[i][k] = c0 * s1; |
---|
564 | ret[j][k] = - s0 * c2 - c0 * c1 * s2; |
---|
565 | } |
---|
566 | } |
---|
567 | else |
---|
568 | { |
---|
569 | ret[i][i] = c1 * c2; |
---|
570 | ret[k][k] = c0 * c1; |
---|
571 | |
---|
572 | if ((2 + i - j) % 3) |
---|
573 | { |
---|
574 | ret[j][i] = - c1 * s2; |
---|
575 | ret[k][i] = s1; |
---|
576 | |
---|
577 | ret[i][j] = c0 * s2 + s0 * s1 * c2; |
---|
578 | ret[j][j] = c0 * c2 - s0 * s1 * s2; |
---|
579 | ret[k][j] = - s0 * c1; |
---|
580 | |
---|
581 | ret[i][k] = s0 * s2 - c0 * s1 * c2; |
---|
582 | ret[j][k] = s0 * c2 + c0 * s1 * s2; |
---|
583 | } |
---|
584 | else |
---|
585 | { |
---|
586 | ret[j][i] = c1 * s2; |
---|
587 | ret[k][i] = - s1; |
---|
588 | |
---|
589 | ret[i][j] = - c0 * s2 + s0 * s1 * c2; |
---|
590 | ret[j][j] = c0 * c2 + s0 * s1 * s2; |
---|
591 | ret[k][j] = s0 * c1; |
---|
592 | |
---|
593 | ret[i][k] = s0 * s2 + c0 * s1 * c2; |
---|
594 | ret[j][k] = - s0 * c2 + c0 * s1 * s2; |
---|
595 | } |
---|
596 | } |
---|
597 | |
---|
598 | return ret; |
---|
599 | } |
---|
600 | |
---|
601 | static inline quat quat_fromeuler_generic(vec3 const &v, int i, int j, int k) |
---|
602 | { |
---|
603 | vec3 half_angles = (M_PI / 360.0f) * v; |
---|
604 | float s0 = sin(half_angles[0]), c0 = cos(half_angles[0]); |
---|
605 | float s1 = sin(half_angles[1]), c1 = cos(half_angles[1]); |
---|
606 | float s2 = sin(half_angles[2]), c2 = cos(half_angles[2]); |
---|
607 | |
---|
608 | quat ret; |
---|
609 | |
---|
610 | if (k == i) |
---|
611 | { |
---|
612 | k = 3 - i - j; |
---|
613 | |
---|
614 | ret[0] = c1 * (c0 * c2 - s0 * s2); |
---|
615 | ret[1 + i] = c1 * (c0 * s2 + s0 * c2); |
---|
616 | ret[1 + j] = s1 * (c0 * c2 + s0 * s2); |
---|
617 | ret[1 + k] = ((2 + i - j) % 3) ? s1 * (s0 * c2 - c0 * s2) |
---|
618 | : s1 * (c0 * s2 - s0 * c2); |
---|
619 | } |
---|
620 | else |
---|
621 | { |
---|
622 | vec4 v1(c0 * c1 * c2, s0 * c1 * c2, c0 * s1 * c2, c0 * c1 * s2); |
---|
623 | vec4 v2(s0 * s1 * s2, -c0 * s1 * s2, s0 * c1 * s2, -s0 * s1 * c2); |
---|
624 | |
---|
625 | if ((2 + i - j) % 3) |
---|
626 | v1 -= v2; |
---|
627 | else |
---|
628 | v1 += v2; |
---|
629 | |
---|
630 | ret[0] = v1[0]; |
---|
631 | ret[1 + i] = v1[1]; |
---|
632 | ret[1 + j] = v1[2]; |
---|
633 | ret[1 + k] = v1[3]; |
---|
634 | } |
---|
635 | |
---|
636 | return ret; |
---|
637 | } |
---|
638 | |
---|
639 | #define DEFINE_FROMEULER_GENERIC(name, i, j, k) \ |
---|
640 | template<> quat quat::fromeuler_##name(vec3 const &v) \ |
---|
641 | { \ |
---|
642 | return quat_fromeuler_generic(v, i, j, k); \ |
---|
643 | } \ |
---|
644 | \ |
---|
645 | template<> quat quat::fromeuler_##name(float phi, float theta, float psi) \ |
---|
646 | { \ |
---|
647 | return quat::fromeuler_##name(vec3(phi, theta, psi)); \ |
---|
648 | } \ |
---|
649 | \ |
---|
650 | template<> mat3 mat3::fromeuler_##name(vec3 const &v) \ |
---|
651 | { \ |
---|
652 | return mat3_fromeuler_generic(v, i, j, k); \ |
---|
653 | } \ |
---|
654 | \ |
---|
655 | template<> mat3 mat3::fromeuler_##name(float phi, float theta, float psi) \ |
---|
656 | { \ |
---|
657 | return mat3::fromeuler_##name(vec3(phi, theta, psi)); \ |
---|
658 | } \ |
---|
659 | \ |
---|
660 | template<> mat4 mat4::fromeuler_##name(vec3 const &v) \ |
---|
661 | { \ |
---|
662 | return mat4(mat3_fromeuler_generic(v, i, j, k), 1.f); \ |
---|
663 | } \ |
---|
664 | \ |
---|
665 | template<> mat4 mat4::fromeuler_##name(float phi, float theta, float psi) \ |
---|
666 | { \ |
---|
667 | return mat4::fromeuler_##name(vec3(phi, theta, psi)); \ |
---|
668 | } |
---|
669 | |
---|
670 | DEFINE_FROMEULER_GENERIC(xyx, 0, 1, 0) |
---|
671 | DEFINE_FROMEULER_GENERIC(xzx, 0, 2, 0) |
---|
672 | DEFINE_FROMEULER_GENERIC(yxy, 1, 0, 1) |
---|
673 | DEFINE_FROMEULER_GENERIC(yzy, 1, 2, 1) |
---|
674 | DEFINE_FROMEULER_GENERIC(zxz, 2, 0, 2) |
---|
675 | DEFINE_FROMEULER_GENERIC(zyz, 2, 1, 2) |
---|
676 | |
---|
677 | DEFINE_FROMEULER_GENERIC(xyz, 0, 1, 2) |
---|
678 | DEFINE_FROMEULER_GENERIC(xzy, 0, 2, 1) |
---|
679 | DEFINE_FROMEULER_GENERIC(yxz, 1, 0, 2) |
---|
680 | DEFINE_FROMEULER_GENERIC(yzx, 1, 2, 0) |
---|
681 | DEFINE_FROMEULER_GENERIC(zxy, 2, 0, 1) |
---|
682 | DEFINE_FROMEULER_GENERIC(zyx, 2, 1, 0) |
---|
683 | |
---|
684 | #undef DEFINE_FROMEULER_GENERIC |
---|
685 | |
---|
686 | template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up) |
---|
687 | { |
---|
688 | vec3 v3 = normalize(eye - center); |
---|
689 | vec3 v2 = normalize(up); |
---|
690 | vec3 v1 = normalize(cross(v2, v3)); |
---|
691 | v2 = cross(v3, v1); |
---|
692 | |
---|
693 | mat4 orient(1.0f); |
---|
694 | orient[0][0] = v1.x; |
---|
695 | orient[0][1] = v2.x; |
---|
696 | orient[0][2] = v3.x; |
---|
697 | orient[1][0] = v1.y; |
---|
698 | orient[1][1] = v2.y; |
---|
699 | orient[1][2] = v3.y; |
---|
700 | orient[2][0] = v1.z; |
---|
701 | orient[2][1] = v2.z; |
---|
702 | orient[2][2] = v3.z; |
---|
703 | |
---|
704 | return orient * mat4::translate(-eye); |
---|
705 | } |
---|
706 | |
---|
707 | template<> mat4 mat4::ortho(float left, float right, float bottom, |
---|
708 | float top, float near, float far) |
---|
709 | { |
---|
710 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
711 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
712 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
713 | |
---|
714 | mat4 ret(0.0f); |
---|
715 | ret[0][0] = 2.0f * invrl; |
---|
716 | ret[1][1] = 2.0f * invtb; |
---|
717 | ret[2][2] = -2.0f * invfn; |
---|
718 | ret[3][0] = - (right + left) * invrl; |
---|
719 | ret[3][1] = - (top + bottom) * invtb; |
---|
720 | ret[3][2] = - (far + near) * invfn; |
---|
721 | ret[3][3] = 1.0f; |
---|
722 | return ret; |
---|
723 | } |
---|
724 | |
---|
725 | template<> mat4 mat4::ortho(float width, float height, |
---|
726 | float near, float far) |
---|
727 | { |
---|
728 | return mat4::ortho(-0.5f * width, 0.5f * width, |
---|
729 | -0.5f * height, 0.5f * height, near, far); |
---|
730 | } |
---|
731 | |
---|
732 | template<> mat4 mat4::frustum(float left, float right, float bottom, |
---|
733 | float top, float near, float far) |
---|
734 | { |
---|
735 | float invrl = (right != left) ? 1.0f / (right - left) : 0.0f; |
---|
736 | float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f; |
---|
737 | float invfn = (far != near) ? 1.0f / (far - near) : 0.0f; |
---|
738 | |
---|
739 | mat4 ret(0.0f); |
---|
740 | ret[0][0] = 2.0f * near * invrl; |
---|
741 | ret[1][1] = 2.0f * near * invtb; |
---|
742 | ret[2][0] = (right + left) * invrl; |
---|
743 | ret[2][1] = (top + bottom) * invtb; |
---|
744 | ret[2][2] = - (far + near) * invfn; |
---|
745 | ret[2][3] = -1.0f; |
---|
746 | ret[3][2] = -2.0f * far * near * invfn; |
---|
747 | return ret; |
---|
748 | } |
---|
749 | |
---|
750 | template<> mat4 mat4::perspective(float fov_y, float width, |
---|
751 | float height, float near, float far) |
---|
752 | { |
---|
753 | fov_y *= (M_PI / 180.0f); |
---|
754 | |
---|
755 | float t2 = tanf(fov_y * 0.5f); |
---|
756 | float t1 = t2 * width / height; |
---|
757 | |
---|
758 | return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far); |
---|
759 | } |
---|
760 | |
---|
761 | } /* namespace lol */ |
---|
762 | |
---|