392 Zeilen
10 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 <cmath> /* for M_PI */
  26. #include <cstdlib> /* free() */
  27. #include <cstring> /* strdup() */
  28. #include "core.h"
  29. using namespace std;
  30. namespace lol
  31. {
  32. static inline float det3(float a, float b, float c,
  33. float d, float e, float f,
  34. float g, float h, float i)
  35. {
  36. return a * (e * i - h * f)
  37. + b * (f * g - i * d)
  38. + c * (d * h - g * e);
  39. }
  40. static inline float cofact3(mat4 const &mat, int i, int j)
  41. {
  42. return det3(mat[(i + 1) & 3][(j + 1) & 3],
  43. mat[(i + 2) & 3][(j + 1) & 3],
  44. mat[(i + 3) & 3][(j + 1) & 3],
  45. mat[(i + 1) & 3][(j + 2) & 3],
  46. mat[(i + 2) & 3][(j + 2) & 3],
  47. mat[(i + 3) & 3][(j + 2) & 3],
  48. mat[(i + 1) & 3][(j + 3) & 3],
  49. mat[(i + 2) & 3][(j + 3) & 3],
  50. mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f);
  51. }
  52. template<> float mat4::det() const
  53. {
  54. float ret = 0;
  55. for (int n = 0; n < 4; n++)
  56. ret += (*this)[n][0] * cofact3(*this, n, 0);
  57. return ret;
  58. }
  59. template<> mat4 mat4::invert() const
  60. {
  61. mat4 ret;
  62. float d = det();
  63. if (d)
  64. {
  65. d = 1.0f / d;
  66. for (int j = 0; j < 4; j++)
  67. for (int i = 0; i < 4; i++)
  68. ret[j][i] = cofact3(*this, i, j) * d;
  69. }
  70. return ret;
  71. }
  72. template<> void vec2::printf() const
  73. {
  74. Log::Debug("[ %6.6f %6.6f ]\n", x, y);
  75. }
  76. template<> void ivec2::printf() const
  77. {
  78. Log::Debug("[ %i %i ]\n", x, y);
  79. }
  80. template<> void cmplx::printf() const
  81. {
  82. Log::Debug("[ %6.6f %6.6f ]\n", x, y);
  83. }
  84. template<> void vec3::printf() const
  85. {
  86. Log::Debug("[ %6.6f %6.6f %6.6f ]\n", x, y, z);
  87. }
  88. template<> void ivec3::printf() const
  89. {
  90. Log::Debug("[ %i %i %i ]\n", x, y, z);
  91. }
  92. template<> void vec4::printf() const
  93. {
  94. Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w);
  95. }
  96. template<> void ivec4::printf() const
  97. {
  98. Log::Debug("[ %i %i %i %i ]\n", x, y, z, w);
  99. }
  100. template<> void quat::printf() const
  101. {
  102. Log::Debug("[ %6.6f %6.6f %6.6f %6.6f ]\n", x, y, z, w);
  103. }
  104. template<> void mat4::printf() const
  105. {
  106. mat4 const &p = *this;
  107. Log::Debug("[ %6.6f %6.6f %6.6f %6.6f\n",
  108. p[0][0], p[1][0], p[2][0], p[3][0]);
  109. Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n",
  110. p[0][1], p[1][1], p[2][1], p[3][1]);
  111. Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n",
  112. p[0][2], p[1][2], p[2][2], p[3][2]);
  113. Log::Debug(" %6.6f %6.6f %6.6f %6.6f ]\n",
  114. p[0][3], p[1][3], p[2][3], p[3][3]);
  115. }
  116. #if !defined __ANDROID__
  117. template<> std::ostream &operator<<(std::ostream &stream, ivec2 const &v)
  118. {
  119. return stream << "(" << v.x << ", " << v.y << ")";
  120. }
  121. template<> std::ostream &operator<<(std::ostream &stream, icmplx const &v)
  122. {
  123. return stream << "(" << v.x << ", " << v.y << ")";
  124. }
  125. template<> std::ostream &operator<<(std::ostream &stream, ivec3 const &v)
  126. {
  127. return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
  128. }
  129. template<> std::ostream &operator<<(std::ostream &stream, ivec4 const &v)
  130. {
  131. return stream << "(" << v.x << ", " << v.y << ", "
  132. << v.z << ", " << v.w << ")";
  133. }
  134. template<> std::ostream &operator<<(std::ostream &stream, iquat const &v)
  135. {
  136. return stream << "(" << v.x << ", " << v.y << ", "
  137. << v.z << ", " << v.w << ")";
  138. }
  139. template<> std::ostream &operator<<(std::ostream &stream, vec2 const &v)
  140. {
  141. return stream << "(" << v.x << ", " << v.y << ")";
  142. }
  143. template<> std::ostream &operator<<(std::ostream &stream, cmplx const &v)
  144. {
  145. return stream << "(" << v.x << ", " << v.y << ")";
  146. }
  147. template<> std::ostream &operator<<(std::ostream &stream, vec3 const &v)
  148. {
  149. return stream << "(" << v.x << ", " << v.y << ", " << v.z << ")";
  150. }
  151. template<> std::ostream &operator<<(std::ostream &stream, vec4 const &v)
  152. {
  153. return stream << "(" << v.x << ", " << v.y << ", "
  154. << v.z << ", " << v.w << ")";
  155. }
  156. template<> std::ostream &operator<<(std::ostream &stream, quat const &v)
  157. {
  158. return stream << "(" << v.x << ", " << v.y << ", "
  159. << v.z << ", " << v.w << ")";
  160. }
  161. template<> std::ostream &operator<<(std::ostream &stream, mat4 const &m)
  162. {
  163. stream << "((" << m[0][0] << ", " << m[1][0]
  164. << ", " << m[2][0] << ", " << m[3][0] << "), ";
  165. stream << "(" << m[0][1] << ", " << m[1][1]
  166. << ", " << m[2][1] << ", " << m[3][1] << "), ";
  167. stream << "(" << m[0][2] << ", " << m[1][2]
  168. << ", " << m[2][2] << ", " << m[3][2] << "), ";
  169. stream << "(" << m[0][3] << ", " << m[1][3]
  170. << ", " << m[2][3] << ", " << m[3][3] << "))";
  171. return stream;
  172. }
  173. #endif
  174. template<> mat4 mat4::translate(float x, float y, float z)
  175. {
  176. mat4 ret(1.0f);
  177. ret[3][0] = x;
  178. ret[3][1] = y;
  179. ret[3][2] = z;
  180. return ret;
  181. }
  182. template<> mat4 mat4::translate(vec3 v)
  183. {
  184. return translate(v.x, v.y, v.z);
  185. }
  186. template<> mat4 mat4::rotate(float angle, float x, float y, float z)
  187. {
  188. angle *= (M_PI / 180.0f);
  189. float st = sinf(angle);
  190. float ct = cosf(angle);
  191. float len = sqrtf(x * x + y * y + z * z);
  192. float invlen = len ? 1.0f / len : 0.0f;
  193. x *= invlen;
  194. y *= invlen;
  195. z *= invlen;
  196. float mtx = (1.0f - ct) * x;
  197. float mty = (1.0f - ct) * y;
  198. float mtz = (1.0f - ct) * z;
  199. mat4 ret(1.0f);
  200. ret[0][0] = x * mtx + ct;
  201. ret[0][1] = x * mty + st * z;
  202. ret[0][2] = x * mtz - st * y;
  203. ret[1][0] = y * mtx - st * z;
  204. ret[1][1] = y * mty + ct;
  205. ret[1][2] = y * mtz + st * x;
  206. ret[2][0] = z * mtx + st * y;
  207. ret[2][1] = z * mty - st * x;
  208. ret[2][2] = z * mtz + ct;
  209. return ret;
  210. }
  211. template<> mat4 mat4::rotate(float angle, vec3 v)
  212. {
  213. return rotate(angle, v.x, v.y, v.z);
  214. }
  215. template<> mat4 mat4::rotate(quat q)
  216. {
  217. mat4 ret(1.0f);
  218. float n = norm(q);
  219. if (!n)
  220. return ret;
  221. float s = 2.0f / n;
  222. ret[0][0] = 1.0f - s * (q.y * q.y + q.z * q.z);
  223. ret[0][1] = s * (q.x * q.y - q.z * q.w);
  224. ret[0][2] = s * (q.x * q.z + q.y * q.w);
  225. ret[1][0] = s * (q.x * q.y + q.z * q.w);
  226. ret[1][1] = 1.0f - s * (q.z * q.z + q.x * q.x);
  227. ret[1][2] = s * (q.y * q.z - q.x * q.w);
  228. ret[2][0] = s * (q.x * q.z - q.y * q.w);
  229. ret[2][1] = s * (q.y * q.z + q.x * q.w);
  230. ret[2][2] = 1.0f - s * (q.x * q.x + q.y * q.y);
  231. return ret;
  232. }
  233. template<> quat::Quat(mat4 const &m)
  234. {
  235. /* See http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/christian.htm for a version with no branches */
  236. float t = m[0][0] + m[1][1] + m[2][2];
  237. if (t > 0)
  238. {
  239. w = 0.5f * sqrtf(1.0f + t);
  240. float s = 0.25f / w;
  241. x = s * (m[2][1] - m[1][2]);
  242. y = s * (m[0][2] - m[2][0]);
  243. z = s * (m[1][0] - m[0][1]);
  244. }
  245. else if (m[0][0] > m[1][1] && m[0][0] > m[2][2])
  246. {
  247. x = 0.5f * sqrtf(1.0f + m[0][0] - m[1][1] - m[2][2]);
  248. float s = 0.25f / x;
  249. y = s * (m[1][0] + m[0][1]);
  250. z = s * (m[0][2] + m[2][0]);
  251. w = s * (m[2][1] - m[1][2]);
  252. }
  253. else if (m[1][1] > m[2][2])
  254. {
  255. y = 0.5f * sqrtf(1.0f - m[0][0] + m[1][1] - m[2][2]);
  256. float s = 0.25f / y;
  257. x = s * (m[1][0] + m[0][1]);
  258. z = s * (m[2][1] + m[1][2]);
  259. w = s * (m[0][2] - m[2][0]);
  260. }
  261. else
  262. {
  263. z = 0.5f * sqrtf(1.0f - m[0][0] - m[1][1] + m[2][2]);
  264. float s = 0.25f / z;
  265. x = s * (m[0][2] + m[2][0]);
  266. y = s * (m[2][1] + m[1][2]);
  267. w = s * (m[1][0] - m[0][1]);
  268. }
  269. }
  270. template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up)
  271. {
  272. vec3 v3 = normalize(eye - center);
  273. vec3 v2 = normalize(up);
  274. vec3 v1 = normalize(cross(v2, v3));
  275. v2 = cross(v3, v1);
  276. mat4 orient(1.0f);
  277. orient[0][0] = v1.x;
  278. orient[0][1] = v2.x;
  279. orient[0][2] = v3.x;
  280. orient[1][0] = v1.y;
  281. orient[1][1] = v2.y;
  282. orient[1][2] = v3.y;
  283. orient[2][0] = v1.z;
  284. orient[2][1] = v2.z;
  285. orient[2][2] = v3.z;
  286. return orient * mat4::translate(-eye);
  287. }
  288. template<> mat4 mat4::ortho(float left, float right, float bottom,
  289. float top, float near, float far)
  290. {
  291. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  292. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  293. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  294. mat4 ret(0.0f);
  295. ret[0][0] = 2.0f * invrl;
  296. ret[1][1] = 2.0f * invtb;
  297. ret[2][2] = -2.0f * invfn;
  298. ret[3][0] = - (right + left) * invrl;
  299. ret[3][1] = - (top + bottom) * invtb;
  300. ret[3][2] = - (far + near) * invfn;
  301. ret[3][3] = 1.0f;
  302. return ret;
  303. }
  304. template<> mat4 mat4::frustum(float left, float right, float bottom,
  305. float top, float near, float far)
  306. {
  307. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  308. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  309. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  310. mat4 ret(0.0f);
  311. ret[0][0] = 2.0f * near * invrl;
  312. ret[1][1] = 2.0f * near * invtb;
  313. ret[2][0] = (right + left) * invrl;
  314. ret[2][1] = (top + bottom) * invtb;
  315. ret[2][2] = - (far + near) * invfn;
  316. ret[2][3] = -1.0f;
  317. ret[3][2] = -2.0f * far * near * invfn;
  318. return ret;
  319. }
  320. template<> mat4 mat4::perspective(float fov_y, float width,
  321. float height, float near, float far)
  322. {
  323. fov_y *= (M_PI / 180.0f);
  324. float t2 = tanf(fov_y * 0.5f);
  325. float t1 = t2 * width / height;
  326. return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
  327. }
  328. } /* namespace lol */