767 rivejä
19 KiB

  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. #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)
  262. {
  263. mat3 ret(1.0f);
  264. ret[0][0] = x;
  265. ret[1][1] = x;
  266. ret[2][2] = x;
  267. return ret;
  268. }
  269. template<> mat3 mat3::scale(float x, float y, float z)
  270. {
  271. mat3 ret(1.0f);
  272. ret[0][0] = x;
  273. ret[1][1] = y;
  274. ret[2][2] = z;
  275. return ret;
  276. }
  277. template<> mat3 mat3::scale(vec3 v)
  278. {
  279. return scale(v.x, v.y, v.z);
  280. }
  281. template<> mat4 mat4::translate(float x, float y, float z)
  282. {
  283. mat4 ret(1.0f);
  284. ret[3][0] = x;
  285. ret[3][1] = y;
  286. ret[3][2] = z;
  287. return ret;
  288. }
  289. template<> mat4 mat4::translate(vec3 v)
  290. {
  291. return translate(v.x, v.y, v.z);
  292. }
  293. template<> mat2 mat2::rotate(float angle)
  294. {
  295. angle *= (M_PI / 180.0f);
  296. float st = sin(angle);
  297. float ct = cos(angle);
  298. mat2 ret;
  299. ret[0][0] = ct;
  300. ret[0][1] = st;
  301. ret[1][0] = -st;
  302. ret[1][1] = ct;
  303. return ret;
  304. }
  305. template<> mat3 mat3::rotate(float angle, float x, float y, float z)
  306. {
  307. angle *= (M_PI / 180.0f);
  308. float st = sin(angle);
  309. float ct = cos(angle);
  310. float len = std::sqrt(x * x + y * y + z * z);
  311. float invlen = len ? 1.0f / len : 0.0f;
  312. x *= invlen;
  313. y *= invlen;
  314. z *= invlen;
  315. float mtx = (1.0f - ct) * x;
  316. float mty = (1.0f - ct) * y;
  317. float mtz = (1.0f - ct) * z;
  318. mat3 ret;
  319. ret[0][0] = x * mtx + ct;
  320. ret[0][1] = x * mty + st * z;
  321. ret[0][2] = x * mtz - st * y;
  322. ret[1][0] = y * mtx - st * z;
  323. ret[1][1] = y * mty + ct;
  324. ret[1][2] = y * mtz + st * x;
  325. ret[2][0] = z * mtx + st * y;
  326. ret[2][1] = z * mty - st * x;
  327. ret[2][2] = z * mtz + ct;
  328. return ret;
  329. }
  330. template<> mat3 mat3::rotate(float angle, vec3 v)
  331. {
  332. return rotate(angle, v.x, v.y, v.z);
  333. }
  334. template<> mat3::Mat3(quat const &q)
  335. {
  336. float n = norm(q);
  337. if (!n)
  338. {
  339. for (int j = 0; j < 3; j++)
  340. for (int i = 0; i < 3; i++)
  341. (*this)[i][j] = (i == j) ? 1.f : 0.f;
  342. return;
  343. }
  344. float s = 2.0f / n;
  345. v0[0] = 1.0f - s * (q.y * q.y + q.z * q.z);
  346. v0[1] = s * (q.x * q.y + q.z * q.w);
  347. v0[2] = s * (q.x * q.z - q.y * q.w);
  348. v1[0] = s * (q.x * q.y - q.z * q.w);
  349. v1[1] = 1.0f - s * (q.z * q.z + q.x * q.x);
  350. v1[2] = s * (q.y * q.z + q.x * q.w);
  351. v2[0] = s * (q.x * q.z + q.y * q.w);
  352. v2[1] = s * (q.y * q.z - q.x * q.w);
  353. v2[2] = 1.0f - s * (q.x * q.x + q.y * q.y);
  354. }
  355. template<> mat4::Mat4(quat const &q)
  356. {
  357. *this = mat4(mat3(q), 1.f);
  358. }
  359. static inline void MatrixToQuat(quat &that, mat3 const &m)
  360. {
  361. /* See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm for a version with no branches */
  362. float t = m[0][0] + m[1][1] + m[2][2];
  363. if (t > 0)
  364. {
  365. that.w = 0.5f * std::sqrt(1.0f + t);
  366. float s = 0.25f / that.w;
  367. that.x = s * (m[1][2] - m[2][1]);
  368. that.y = s * (m[2][0] - m[0][2]);
  369. that.z = s * (m[0][1] - m[1][0]);
  370. }
  371. else if (m[0][0] > m[1][1] && m[0][0] > m[2][2])
  372. {
  373. that.x = 0.5f * std::sqrt(1.0f + m[0][0] - m[1][1] - m[2][2]);
  374. float s = 0.25f / that.x;
  375. that.y = s * (m[0][1] + m[1][0]);
  376. that.z = s * (m[2][0] + m[0][2]);
  377. that.w = s * (m[1][2] - m[2][1]);
  378. }
  379. else if (m[1][1] > m[2][2])
  380. {
  381. that.y = 0.5f * std::sqrt(1.0f - m[0][0] + m[1][1] - m[2][2]);
  382. float s = 0.25f / that.y;
  383. that.x = s * (m[0][1] + m[1][0]);
  384. that.z = s * (m[1][2] + m[2][1]);
  385. that.w = s * (m[2][0] - m[0][2]);
  386. }
  387. else
  388. {
  389. that.z = 0.5f * std::sqrt(1.0f - m[0][0] - m[1][1] + m[2][2]);
  390. float s = 0.25f / that.z;
  391. that.x = s * (m[2][0] + m[0][2]);
  392. that.y = s * (m[1][2] + m[2][1]);
  393. that.w = s * (m[0][1] - m[1][0]);
  394. }
  395. }
  396. template<> quat::Quat(mat3 const &m)
  397. {
  398. MatrixToQuat(*this, m);
  399. }
  400. template<> quat::Quat(mat4 const &m)
  401. {
  402. MatrixToQuat(*this, mat3(m));
  403. }
  404. template<> quat quat::rotate(float angle, vec3 const &v)
  405. {
  406. angle *= (M_PI / 360.0f);
  407. vec3 tmp = normalize(v) * sin(angle);
  408. return quat(cos(angle), tmp.x, tmp.y, tmp.z);
  409. }
  410. template<> quat quat::rotate(float angle, float x, float y, float z)
  411. {
  412. return quat::rotate(angle, vec3(x, y, z));
  413. }
  414. template<> quat slerp(quat const &qa, quat const &qb, float f)
  415. {
  416. float const magnitude = lol::sqrt(sqlength(qa) * sqlength(qb));
  417. float const product = lol::dot(qa, qb) / magnitude;
  418. /* If quaternions are equal or opposite, there is no need
  419. * to slerp anything, just return qa. */
  420. if (std::abs(product) >= 1.0f)
  421. return qa;
  422. float const sign = (product < 0.0f) ? -1.0f : 1.0f;
  423. float const theta = lol::acos(sign * product);
  424. float const s1 = lol::sin(sign * f * theta);
  425. float const s0 = lol::sin((1.0f - f) * theta);
  426. /* This is the same as 1/sin(theta) */
  427. float const d = 1.0f / lol::sqrt(1.f - product * product);
  428. return qa * (s0 * d) + qb * (s1 * d);
  429. }
  430. template<> vec3 vec3::toeuler(quat const &q)
  431. {
  432. float n = norm(q);
  433. if (!n)
  434. return vec3(0.f);
  435. vec3 ret(atan2(2.f * (q.w * q.x + q.y * q.z),
  436. 1.f - 2.f * (q.x * q.x + q.y * q.y)),
  437. asin(2.f * (q.w * q.y - q.z * q.x)),
  438. atan2(2.f * (q.w * q.z + q.y * q.x),
  439. 1.f - 2.f * (q.z * q.z + q.y * q.y)));
  440. return (180.0f / M_PI / n) * ret;
  441. }
  442. static inline mat3 mat3_fromeuler_generic(vec3 const &v, int i, int j, int k)
  443. {
  444. mat3 ret;
  445. vec3 radians = (M_PI / 180.0f) * v;
  446. float s0 = sin(radians[0]), c0 = cos(radians[0]);
  447. float s1 = sin(radians[1]), c1 = cos(radians[1]);
  448. float s2 = sin(radians[2]), c2 = cos(radians[2]);
  449. if (k == i)
  450. {
  451. k = 3 - i - j;
  452. ret[i][i] = c1;
  453. ret[j][i] = s1 * s2;
  454. ret[i][j] = s0 * s1;
  455. ret[j][j] = c0 * c2 - s0 * c1 * s2;
  456. ret[k][k] = - s0 * s2 + c0 * c1 * c2;
  457. if ((2 + i - j) % 3)
  458. {
  459. ret[k][i] = s1 * c2;
  460. ret[k][j] = - c0 * s2 - s0 * c1 * c2;
  461. ret[i][k] = - c0 * s1;
  462. ret[j][k] = s0 * c2 + c0 * c1 * s2;
  463. }
  464. else
  465. {
  466. ret[k][i] = - s1 * c2;
  467. ret[k][j] = c0 * s2 + s0 * c1 * c2;
  468. ret[i][k] = c0 * s1;
  469. ret[j][k] = - s0 * c2 - c0 * c1 * s2;
  470. }
  471. }
  472. else
  473. {
  474. ret[i][i] = c1 * c2;
  475. ret[k][k] = c0 * c1;
  476. if ((2 + i - j) % 3)
  477. {
  478. ret[j][i] = - c1 * s2;
  479. ret[k][i] = s1;
  480. ret[i][j] = c0 * s2 + s0 * s1 * c2;
  481. ret[j][j] = c0 * c2 - s0 * s1 * s2;
  482. ret[k][j] = - s0 * c1;
  483. ret[i][k] = s0 * s2 - c0 * s1 * c2;
  484. ret[j][k] = s0 * c2 + c0 * s1 * s2;
  485. }
  486. else
  487. {
  488. ret[j][i] = c1 * s2;
  489. ret[k][i] = - s1;
  490. ret[i][j] = - c0 * s2 + s0 * s1 * c2;
  491. ret[j][j] = c0 * c2 + s0 * s1 * s2;
  492. ret[k][j] = s0 * c1;
  493. ret[i][k] = s0 * s2 + c0 * s1 * c2;
  494. ret[j][k] = - s0 * c2 + c0 * s1 * s2;
  495. }
  496. }
  497. return ret;
  498. }
  499. static inline quat quat_fromeuler_generic(vec3 const &v, int i, int j, int k)
  500. {
  501. vec3 half_angles = (M_PI / 360.0f) * v;
  502. float s0 = sin(half_angles[0]), c0 = cos(half_angles[0]);
  503. float s1 = sin(half_angles[1]), c1 = cos(half_angles[1]);
  504. float s2 = sin(half_angles[2]), c2 = cos(half_angles[2]);
  505. quat ret;
  506. if (k == i)
  507. {
  508. k = 3 - i - j;
  509. ret[0] = c1 * (c0 * c2 - s0 * s2);
  510. ret[1 + i] = c1 * (c0 * s2 + s0 * c2);
  511. ret[1 + j] = s1 * (c0 * c2 + s0 * s2);
  512. ret[1 + k] = ((2 + i - j) % 3) ? s1 * (s0 * c2 - c0 * s2)
  513. : s1 * (c0 * s2 - s0 * c2);
  514. }
  515. else
  516. {
  517. vec4 v1(c0 * c1 * c2, s0 * c1 * c2, c0 * s1 * c2, c0 * c1 * s2);
  518. vec4 v2(s0 * s1 * s2, -c0 * s1 * s2, s0 * c1 * s2, -s0 * s1 * c2);
  519. if ((2 + i - j) % 3)
  520. v1 -= v2;
  521. else
  522. v1 += v2;
  523. ret[0] = v1[0];
  524. ret[1 + i] = v1[1];
  525. ret[1 + j] = v1[2];
  526. ret[1 + k] = v1[3];
  527. }
  528. return ret;
  529. }
  530. #define DEFINE_FROMEULER_GENERIC(name, i, j, k) \
  531. template<> quat quat::fromeuler_##name(vec3 const &v) \
  532. { \
  533. return quat_fromeuler_generic(v, i, j, k); \
  534. } \
  535. \
  536. template<> quat quat::fromeuler_##name(float phi, float theta, float psi) \
  537. { \
  538. return quat::fromeuler_##name(vec3(phi, theta, psi)); \
  539. } \
  540. \
  541. template<> mat3 mat3::fromeuler_##name(vec3 const &v) \
  542. { \
  543. return mat3_fromeuler_generic(v, i, j, k); \
  544. } \
  545. \
  546. template<> mat3 mat3::fromeuler_##name(float phi, float theta, float psi) \
  547. { \
  548. return mat3::fromeuler_##name(vec3(phi, theta, psi)); \
  549. } \
  550. \
  551. template<> mat4 mat4::fromeuler_##name(vec3 const &v) \
  552. { \
  553. return mat4(mat3_fromeuler_generic(v, i, j, k), 1.f); \
  554. } \
  555. \
  556. template<> mat4 mat4::fromeuler_##name(float phi, float theta, float psi) \
  557. { \
  558. return mat4::fromeuler_##name(vec3(phi, theta, psi)); \
  559. }
  560. DEFINE_FROMEULER_GENERIC(xyx, 0, 1, 0)
  561. DEFINE_FROMEULER_GENERIC(xzx, 0, 2, 0)
  562. DEFINE_FROMEULER_GENERIC(yxy, 1, 0, 1)
  563. DEFINE_FROMEULER_GENERIC(yzy, 1, 2, 1)
  564. DEFINE_FROMEULER_GENERIC(zxz, 2, 0, 2)
  565. DEFINE_FROMEULER_GENERIC(zyz, 2, 1, 2)
  566. DEFINE_FROMEULER_GENERIC(xyz, 0, 1, 2)
  567. DEFINE_FROMEULER_GENERIC(xzy, 0, 2, 1)
  568. DEFINE_FROMEULER_GENERIC(yxz, 1, 0, 2)
  569. DEFINE_FROMEULER_GENERIC(yzx, 1, 2, 0)
  570. DEFINE_FROMEULER_GENERIC(zxy, 2, 0, 1)
  571. DEFINE_FROMEULER_GENERIC(zyx, 2, 1, 0)
  572. #undef DEFINE_FROMEULER_GENERIC
  573. template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up)
  574. {
  575. vec3 v3 = normalize(eye - center);
  576. vec3 v2 = normalize(up);
  577. vec3 v1 = normalize(cross(v2, v3));
  578. v2 = cross(v3, v1);
  579. mat4 orient(1.0f);
  580. orient[0][0] = v1.x;
  581. orient[0][1] = v2.x;
  582. orient[0][2] = v3.x;
  583. orient[1][0] = v1.y;
  584. orient[1][1] = v2.y;
  585. orient[1][2] = v3.y;
  586. orient[2][0] = v1.z;
  587. orient[2][1] = v2.z;
  588. orient[2][2] = v3.z;
  589. return orient * mat4::translate(-eye);
  590. }
  591. template<> mat4 mat4::ortho(float left, float right, float bottom,
  592. float top, float near, float far)
  593. {
  594. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  595. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  596. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  597. mat4 ret(0.0f);
  598. ret[0][0] = 2.0f * invrl;
  599. ret[1][1] = 2.0f * invtb;
  600. ret[2][2] = -2.0f * invfn;
  601. ret[3][0] = - (right + left) * invrl;
  602. ret[3][1] = - (top + bottom) * invtb;
  603. ret[3][2] = - (far + near) * invfn;
  604. ret[3][3] = 1.0f;
  605. return ret;
  606. }
  607. template<> mat4 mat4::ortho(float width, float height,
  608. float near, float far)
  609. {
  610. return mat4::ortho(-0.5f * width, 0.5f * width,
  611. -0.5f * height, 0.5f * height, near, far);
  612. }
  613. template<> mat4 mat4::frustum(float left, float right, float bottom,
  614. float top, float near, float far)
  615. {
  616. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  617. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  618. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  619. mat4 ret(0.0f);
  620. ret[0][0] = 2.0f * near * invrl;
  621. ret[1][1] = 2.0f * near * invtb;
  622. ret[2][0] = (right + left) * invrl;
  623. ret[2][1] = (top + bottom) * invtb;
  624. ret[2][2] = - (far + near) * invfn;
  625. ret[2][3] = -1.0f;
  626. ret[3][2] = -2.0f * far * near * invfn;
  627. return ret;
  628. }
  629. template<> mat4 mat4::perspective(float fov_y, float width,
  630. float height, float near, float far)
  631. {
  632. fov_y *= (M_PI / 180.0f);
  633. float t2 = tanf(fov_y * 0.5f);
  634. float t1 = t2 * width / height;
  635. return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
  636. }
  637. } /* namespace lol */