735 lines
18 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<> vec3 vec3::toeuler(quat const &q)
  407. {
  408. float n = norm(q);
  409. if (!n)
  410. return vec3(0.f);
  411. vec3 ret(atan2(2.f * (q.w * q.x + q.y * q.z),
  412. 1.f - 2.f * (q.x * q.x + q.y * q.y)),
  413. asin(2.f * (q.w * q.y - q.z * q.x)),
  414. atan2(2.f * (q.w * q.z + q.y * q.x),
  415. 1.f - 2.f * (q.z * q.z + q.y * q.y)));
  416. return (180.0f / M_PI / n) * ret;
  417. }
  418. static inline mat3 mat3_fromeuler_generic(vec3 const &v, int i, int j, int k)
  419. {
  420. mat3 ret;
  421. vec3 radians = (M_PI / 180.0f) * v;
  422. float s0 = sin(radians[0]), c0 = cos(radians[0]);
  423. float s1 = sin(radians[1]), c1 = cos(radians[1]);
  424. float s2 = sin(radians[2]), c2 = cos(radians[2]);
  425. if (k == i)
  426. {
  427. k = 3 - i - j;
  428. ret[i][i] = c1;
  429. ret[j][i] = s1 * s2;
  430. ret[i][j] = s0 * s1;
  431. ret[j][j] = c0 * c2 - s0 * c1 * s2;
  432. ret[k][k] = - s0 * s2 + c0 * c1 * c2;
  433. if ((2 + i - j) % 3)
  434. {
  435. ret[k][i] = s1 * c2;
  436. ret[k][j] = - c0 * s2 - s0 * c1 * c2;
  437. ret[i][k] = - c0 * s1;
  438. ret[j][k] = s0 * c2 + c0 * c1 * s2;
  439. }
  440. else
  441. {
  442. ret[k][i] = - s1 * c2;
  443. ret[k][j] = c0 * s2 + s0 * c1 * c2;
  444. ret[i][k] = c0 * s1;
  445. ret[j][k] = - s0 * c2 - c0 * c1 * s2;
  446. }
  447. }
  448. else
  449. {
  450. ret[i][i] = c1 * c2;
  451. ret[k][k] = c0 * c1;
  452. if ((2 + i - j) % 3)
  453. {
  454. ret[j][i] = - c1 * s2;
  455. ret[k][i] = s1;
  456. ret[i][j] = c0 * s2 + s0 * s1 * c2;
  457. ret[j][j] = c0 * c2 - s0 * s1 * s2;
  458. ret[k][j] = - s0 * c1;
  459. ret[i][k] = s0 * s2 - c0 * s1 * c2;
  460. ret[j][k] = s0 * c2 + c0 * s1 * s2;
  461. }
  462. else
  463. {
  464. ret[j][i] = c1 * s2;
  465. ret[k][i] = - s1;
  466. ret[i][j] = - c0 * s2 + s0 * s1 * c2;
  467. ret[j][j] = c0 * c2 + s0 * s1 * s2;
  468. ret[k][j] = s0 * c1;
  469. ret[i][k] = s0 * s2 + c0 * s1 * c2;
  470. ret[j][k] = - s0 * c2 + c0 * s1 * s2;
  471. }
  472. }
  473. return ret;
  474. }
  475. static inline quat quat_fromeuler_generic(vec3 const &v, int i, int j, int k)
  476. {
  477. vec3 half_angles = (M_PI / 360.0f) * v;
  478. float s0 = sin(half_angles[0]), c0 = cos(half_angles[0]);
  479. float s1 = sin(half_angles[1]), c1 = cos(half_angles[1]);
  480. float s2 = sin(half_angles[2]), c2 = cos(half_angles[2]);
  481. quat ret;
  482. if (k == i)
  483. {
  484. k = 3 - i - j;
  485. ret[0] = c1 * (c0 * c2 - s0 * s2);
  486. ret[1 + i] = c1 * (c0 * s2 + s0 * c2);
  487. ret[1 + j] = s1 * (c0 * c2 + s0 * s2);
  488. ret[1 + k] = ((2 + i - j) % 3) ? s1 * (s0 * c2 - c0 * s2)
  489. : s1 * (c0 * s2 - s0 * c2);
  490. }
  491. else
  492. {
  493. vec4 v1(c0 * c1 * c2, s0 * c1 * c2, c0 * s1 * c2, c0 * c1 * s2);
  494. vec4 v2(s0 * s1 * s2, -c0 * s1 * s2, s0 * c1 * s2, -s0 * s1 * c2);
  495. if ((2 + i - j) % 3)
  496. v1 -= v2;
  497. else
  498. v1 += v2;
  499. ret[0] = v1[0];
  500. ret[1 + i] = v1[1];
  501. ret[1 + j] = v1[2];
  502. ret[1 + k] = v1[3];
  503. }
  504. return ret;
  505. }
  506. #define DEFINE_FROMEULER_GENERIC(name, i, j, k) \
  507. template<> quat quat::fromeuler_##name(vec3 const &v) \
  508. { \
  509. return quat_fromeuler_generic(v, i, j, k); \
  510. } \
  511. \
  512. template<> quat quat::fromeuler_##name(float phi, float theta, float psi) \
  513. { \
  514. return quat::fromeuler_##name(vec3(phi, theta, psi)); \
  515. } \
  516. \
  517. template<> mat3 mat3::fromeuler_##name(vec3 const &v) \
  518. { \
  519. return mat3_fromeuler_generic(v, i, j, k); \
  520. } \
  521. \
  522. template<> mat3 mat3::fromeuler_##name(float phi, float theta, float psi) \
  523. { \
  524. return mat3::fromeuler_##name(vec3(phi, theta, psi)); \
  525. } \
  526. \
  527. template<> mat4 mat4::fromeuler_##name(vec3 const &v) \
  528. { \
  529. return mat4(mat3_fromeuler_generic(v, i, j, k), 1.f); \
  530. } \
  531. \
  532. template<> mat4 mat4::fromeuler_##name(float phi, float theta, float psi) \
  533. { \
  534. return mat4::fromeuler_##name(vec3(phi, theta, psi)); \
  535. }
  536. DEFINE_FROMEULER_GENERIC(xyx, 0, 1, 0)
  537. DEFINE_FROMEULER_GENERIC(xzx, 0, 2, 0)
  538. DEFINE_FROMEULER_GENERIC(yxy, 1, 0, 1)
  539. DEFINE_FROMEULER_GENERIC(yzy, 1, 2, 1)
  540. DEFINE_FROMEULER_GENERIC(zxz, 2, 0, 2)
  541. DEFINE_FROMEULER_GENERIC(zyz, 2, 1, 2)
  542. DEFINE_FROMEULER_GENERIC(xyz, 0, 1, 2)
  543. DEFINE_FROMEULER_GENERIC(xzy, 0, 2, 1)
  544. DEFINE_FROMEULER_GENERIC(yxz, 1, 0, 2)
  545. DEFINE_FROMEULER_GENERIC(yzx, 1, 2, 0)
  546. DEFINE_FROMEULER_GENERIC(zxy, 2, 0, 1)
  547. DEFINE_FROMEULER_GENERIC(zyx, 2, 1, 0)
  548. #undef DEFINE_FROMEULER_GENERIC
  549. template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up)
  550. {
  551. vec3 v3 = normalize(eye - center);
  552. vec3 v2 = normalize(up);
  553. vec3 v1 = normalize(cross(v2, v3));
  554. v2 = cross(v3, v1);
  555. mat4 orient(1.0f);
  556. orient[0][0] = v1.x;
  557. orient[0][1] = v2.x;
  558. orient[0][2] = v3.x;
  559. orient[1][0] = v1.y;
  560. orient[1][1] = v2.y;
  561. orient[1][2] = v3.y;
  562. orient[2][0] = v1.z;
  563. orient[2][1] = v2.z;
  564. orient[2][2] = v3.z;
  565. return orient * mat4::translate(-eye);
  566. }
  567. template<> mat4 mat4::ortho(float left, float right, float bottom,
  568. float top, float near, float far)
  569. {
  570. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  571. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  572. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  573. mat4 ret(0.0f);
  574. ret[0][0] = 2.0f * invrl;
  575. ret[1][1] = 2.0f * invtb;
  576. ret[2][2] = -2.0f * invfn;
  577. ret[3][0] = - (right + left) * invrl;
  578. ret[3][1] = - (top + bottom) * invtb;
  579. ret[3][2] = - (far + near) * invfn;
  580. ret[3][3] = 1.0f;
  581. return ret;
  582. }
  583. template<> mat4 mat4::ortho(float width, float height,
  584. float near, float far)
  585. {
  586. return mat4::ortho(-0.5f * width, 0.5f * width,
  587. -0.5f * height, 0.5f * height, near, far);
  588. }
  589. template<> mat4 mat4::frustum(float left, float right, float bottom,
  590. float top, float near, float far)
  591. {
  592. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  593. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  594. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  595. mat4 ret(0.0f);
  596. ret[0][0] = 2.0f * near * invrl;
  597. ret[1][1] = 2.0f * near * invtb;
  598. ret[2][0] = (right + left) * invrl;
  599. ret[2][1] = (top + bottom) * invtb;
  600. ret[2][2] = - (far + near) * invfn;
  601. ret[2][3] = -1.0f;
  602. ret[3][2] = -2.0f * far * near * invfn;
  603. return ret;
  604. }
  605. template<> mat4 mat4::perspective(float fov_y, float width,
  606. float height, float near, float far)
  607. {
  608. fov_y *= (M_PI / 180.0f);
  609. float t2 = tanf(fov_y * 0.5f);
  610. float t1 = t2 * width / height;
  611. return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
  612. }
  613. } /* namespace lol */