You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

756 line
19 KiB

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