選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

171 行
4.5 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 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. #include <cstdlib> /* free() */
  14. #include <cstring> /* strdup() */
  15. #include "core.h"
  16. using namespace std;
  17. namespace lol
  18. {
  19. static inline float det3(float a, float b, float c,
  20. float d, float e, float f,
  21. float g, float h, float i)
  22. {
  23. return a * (e * i - h * f)
  24. + b * (f * g - i * d)
  25. + c * (d * h - g * e);
  26. }
  27. static inline float cofact3(mat4 const &mat, int i, int j)
  28. {
  29. return det3(mat[(i + 1) & 3][(j + 1) & 3],
  30. mat[(i + 2) & 3][(j + 1) & 3],
  31. mat[(i + 3) & 3][(j + 1) & 3],
  32. mat[(i + 1) & 3][(j + 2) & 3],
  33. mat[(i + 2) & 3][(j + 2) & 3],
  34. mat[(i + 3) & 3][(j + 2) & 3],
  35. mat[(i + 1) & 3][(j + 3) & 3],
  36. mat[(i + 2) & 3][(j + 3) & 3],
  37. mat[(i + 3) & 3][(j + 3) & 3]) * (((i + j) & 1) ? -1.0f : 1.0f);
  38. }
  39. template<> float mat4::det() const
  40. {
  41. float ret = 0;
  42. for (int n = 0; n < 4; n++)
  43. ret += (*this)[n][0] * cofact3(*this, n, 0);
  44. return ret;
  45. }
  46. template<> mat4 mat4::invert() const
  47. {
  48. mat4 ret;
  49. float d = det();
  50. if (d)
  51. {
  52. d = 1.0f / d;
  53. for (int j = 0; j < 4; j++)
  54. for (int i = 0; i < 4; i++)
  55. ret[j][i] = cofact3(*this, i, j) * d;
  56. }
  57. return ret;
  58. }
  59. template<> void mat4::printf() const
  60. {
  61. mat4 const &p = *this;
  62. Log::Debug("[ %6.6f %6.6f %6.6f %6.6f\n",
  63. p[0][0], p[1][0], p[2][0], p[3][0]);
  64. Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n",
  65. p[0][1], p[1][1], p[2][1], p[3][1]);
  66. Log::Debug(" %6.6f %6.6f %6.6f %6.6f\n",
  67. p[0][2], p[1][2], p[2][2], p[3][2]);
  68. Log::Debug(" %6.6f %6.6f %6.6f %6.6f ]\n",
  69. p[0][3], p[1][3], p[2][3], p[3][3]);
  70. }
  71. template<> mat4 mat4::ortho(float left, float right, float bottom,
  72. float top, float near, float far)
  73. {
  74. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  75. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  76. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  77. mat4 ret(0.0f);
  78. ret[0][0] = 2.0f * invrl;
  79. ret[1][1] = 2.0f * invtb;
  80. ret[2][2] = -2.0f * invfn;
  81. ret[3][0] = - (right + left) * invrl;
  82. ret[3][1] = - (top + bottom) * invtb;
  83. ret[3][2] = - (far + near) * invfn;
  84. ret[3][3] = 1.0f;
  85. return ret;
  86. }
  87. template<> mat4 mat4::frustum(float left, float right, float bottom,
  88. float top, float near, float far)
  89. {
  90. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  91. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  92. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  93. mat4 ret(0.0f);
  94. ret[0][0] = 2.0f * near * invrl;
  95. ret[1][1] = 2.0f * near * invtb;
  96. ret[2][0] = (right + left) * invrl;
  97. ret[2][1] = (top + bottom) * invtb;
  98. ret[2][2] = - (far + near) * invfn;
  99. ret[2][3] = -1.0f;
  100. ret[3][2] = -2.0f * far * near * invfn;
  101. return ret;
  102. }
  103. template<> mat4 mat4::perspective(float theta, float width,
  104. float height, float near, float far)
  105. {
  106. float t1 = tanf(theta / 2.0f);
  107. float t2 = t1 * height / width;
  108. return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
  109. }
  110. template<> mat4 mat4::translate(float x, float y, float z)
  111. {
  112. mat4 ret(1.0f);
  113. ret[3][0] = x;
  114. ret[3][1] = y;
  115. ret[3][2] = z;
  116. return ret;
  117. }
  118. template<> mat4 mat4::rotate(float theta, float x, float y, float z)
  119. {
  120. float st = sinf(theta);
  121. float ct = cosf(theta);
  122. float len = sqrtf(x * x + y * y + z * z);
  123. float invlen = len ? 1.0f / len : 0.0f;
  124. x *= invlen;
  125. y *= invlen;
  126. z *= invlen;
  127. float mtx = (1.0f - ct) * x;
  128. float mty = (1.0f - ct) * y;
  129. float mtz = (1.0f - ct) * z;
  130. mat4 ret(1.0f);
  131. ret[0][0] = x * mtx + ct;
  132. ret[0][1] = x * mty + st * z;
  133. ret[0][2] = x * mtz - st * y;
  134. ret[1][0] = y * mtx - st * z;
  135. ret[1][1] = y * mty + ct;
  136. ret[1][2] = y * mtz + st * x;
  137. ret[2][0] = z * mtx + st * y;
  138. ret[2][1] = z * mty - st * x;
  139. ret[2][2] = z * mtz + ct;
  140. return ret;
  141. }
  142. } /* namespace lol */