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