Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 

225 rindas
5.6 KiB

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