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