Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

matrix.cpp 5.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. namespace lol
  14. {
  15. template<> mat3 mat3::scale(float x, float y, float z)
  16. {
  17. mat3 ret(1.0f);
  18. ret[0][0] = x;
  19. ret[1][1] = y;
  20. ret[2][2] = z;
  21. return ret;
  22. }
  23. template<> mat3 mat3::scale(float x)
  24. {
  25. return scale(x, x, x);
  26. }
  27. template<> mat3 mat3::scale(vec3 v)
  28. {
  29. return scale(v.x, v.y, v.z);
  30. }
  31. template<> mat4 mat4::translate(float x, float y, float z)
  32. {
  33. mat4 ret(1.0f);
  34. ret[3][0] = x;
  35. ret[3][1] = y;
  36. ret[3][2] = z;
  37. return ret;
  38. }
  39. template<> mat4 mat4::translate(vec3 v)
  40. {
  41. return translate(v.x, v.y, v.z);
  42. }
  43. template<> mat2 mat2::rotate(float radians)
  44. {
  45. float st = sin(radians);
  46. float ct = cos(radians);
  47. mat2 ret;
  48. ret[0][0] = ct;
  49. ret[0][1] = st;
  50. ret[1][0] = -st;
  51. ret[1][1] = ct;
  52. return ret;
  53. }
  54. template<> mat3 mat3::rotate(float radians, float x, float y, float z)
  55. {
  56. float st = sin(radians);
  57. float ct = cos(radians);
  58. float len = std::sqrt(x * x + y * y + z * z);
  59. float invlen = len ? 1.0f / len : 0.0f;
  60. x *= invlen;
  61. y *= invlen;
  62. z *= invlen;
  63. float mtx = (1.0f - ct) * x;
  64. float mty = (1.0f - ct) * y;
  65. float mtz = (1.0f - ct) * z;
  66. mat3 ret;
  67. ret[0][0] = x * mtx + ct;
  68. ret[0][1] = x * mty + st * z;
  69. ret[0][2] = x * mtz - st * y;
  70. ret[1][0] = y * mtx - st * z;
  71. ret[1][1] = y * mty + ct;
  72. ret[1][2] = y * mtz + st * x;
  73. ret[2][0] = z * mtx + st * y;
  74. ret[2][1] = z * mty - st * x;
  75. ret[2][2] = z * mtz + ct;
  76. return ret;
  77. }
  78. template<> mat3 mat3::rotate(float radians, vec3 v)
  79. {
  80. return rotate(radians, v.x, v.y, v.z);
  81. }
  82. template<> mat3::mat_t(quat const &q)
  83. {
  84. float n = norm(q);
  85. if (!n)
  86. {
  87. for (int j = 0; j < 3; j++)
  88. for (int i = 0; i < 3; i++)
  89. (*this)[i][j] = (i == j) ? 1.f : 0.f;
  90. return;
  91. }
  92. float s = 2.0f / n;
  93. (*this)[0][0] = 1.0f - s * (q.y * q.y + q.z * q.z);
  94. (*this)[0][1] = s * (q.x * q.y + q.z * q.w);
  95. (*this)[0][2] = s * (q.x * q.z - q.y * q.w);
  96. (*this)[1][0] = s * (q.x * q.y - q.z * q.w);
  97. (*this)[1][1] = 1.0f - s * (q.z * q.z + q.x * q.x);
  98. (*this)[1][2] = s * (q.y * q.z + q.x * q.w);
  99. (*this)[2][0] = s * (q.x * q.z + q.y * q.w);
  100. (*this)[2][1] = s * (q.y * q.z - q.x * q.w);
  101. (*this)[2][2] = 1.0f - s * (q.x * q.x + q.y * q.y);
  102. }
  103. template<> mat4::mat_t(quat const &q)
  104. {
  105. *this = mat4(mat3(q), 1.f);
  106. }
  107. template<> mat4 mat4::lookat(vec3 eye, vec3 center, vec3 up)
  108. {
  109. vec3 v3 = normalize(eye - center);
  110. vec3 v1 = normalize(cross(up, v3));
  111. vec3 v2 = cross(v3, v1);
  112. return mat4(vec4(v1.x, v2.x, v3.x, 0.f),
  113. vec4(v1.y, v2.y, v3.y, 0.f),
  114. vec4(v1.z, v2.z, v3.z, 0.f),
  115. vec4(-dot(eye, v1), -dot(eye, v2), -dot(eye, v3), 1.f));
  116. }
  117. template<> mat4 mat4::ortho(float left, float right, float bottom,
  118. float top, float near, float far)
  119. {
  120. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  121. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  122. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  123. mat4 ret(0.0f);
  124. ret[0][0] = 2.0f * invrl;
  125. ret[1][1] = 2.0f * invtb;
  126. ret[2][2] = -2.0f * invfn;
  127. ret[3][0] = - (right + left) * invrl;
  128. ret[3][1] = - (top + bottom) * invtb;
  129. ret[3][2] = - (far + near) * invfn;
  130. ret[3][3] = 1.0f;
  131. return ret;
  132. }
  133. template<> mat4 mat4::ortho(float width, float height,
  134. float near, float far)
  135. {
  136. return mat4::ortho(-0.5f * width, 0.5f * width,
  137. -0.5f * height, 0.5f * height, near, far);
  138. }
  139. template<> mat4 mat4::frustum(float left, float right, float bottom,
  140. float top, float near, float far)
  141. {
  142. float invrl = (right != left) ? 1.0f / (right - left) : 0.0f;
  143. float invtb = (top != bottom) ? 1.0f / (top - bottom) : 0.0f;
  144. float invfn = (far != near) ? 1.0f / (far - near) : 0.0f;
  145. mat4 ret(0.0f);
  146. ret[0][0] = 2.0f * near * invrl;
  147. ret[1][1] = 2.0f * near * invtb;
  148. ret[2][0] = (right + left) * invrl;
  149. ret[2][1] = (top + bottom) * invtb;
  150. ret[2][2] = - (far + near) * invfn;
  151. ret[2][3] = -1.0f;
  152. ret[3][2] = -2.0f * far * near * invfn;
  153. return ret;
  154. }
  155. /*
  156. * Return a standard perspective matrix
  157. */
  158. template<> mat4 mat4::perspective(float fov_y, float width,
  159. float height, float near, float far)
  160. {
  161. float t2 = lol::tan(fov_y * 0.5f);
  162. float t1 = t2 * width / height;
  163. return frustum(-near * t1, near * t1, -near * t2, near * t2, near, far);
  164. }
  165. /*
  166. * Return a perspective matrix with the camera location shifted to be on
  167. * the near plane
  168. */
  169. template<> mat4 mat4::shifted_perspective(float fov_y, float screen_size,
  170. float screen_ratio_yx,
  171. float near, float far)
  172. {
  173. float tan_y = tanf(fov_y * .5f);
  174. ASSERT(tan_y > 0.000001f);
  175. float dist_scr = (screen_size * screen_ratio_yx * .5f) / tan_y;
  176. return mat4::perspective(fov_y, screen_size, screen_size * screen_ratio_yx,
  177. max(.001f, dist_scr + near),
  178. max(.001f, dist_scr + far)) *
  179. mat4::translate(.0f, .0f, -dist_scr);
  180. }
  181. } /* namespace lol */