No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

263 líneas
8.2 KiB

  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<> quat quat::rotate(float radians, vec3 const &v)
  16. {
  17. float half_angle = radians * 0.5f;
  18. vec3 tmp = normalize(v) * sin(half_angle);
  19. return quat(cos(half_angle), tmp.x, tmp.y, tmp.z);
  20. }
  21. template<> quat quat::rotate(float radians, float x, float y, float z)
  22. {
  23. return quat::rotate(radians, vec3(x, y, z));
  24. }
  25. template<> quat quat::rotate(vec3 const &src, vec3 const &dst)
  26. {
  27. /* Algorithm directly taken from Sam Hocevar's article "Quaternion from
  28. * two vectors: the final version".
  29. * http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final */
  30. float magnitude = lol::sqrt(sqlength(src) * sqlength(dst));
  31. float real_part = magnitude + dot(src, dst);
  32. vec3 w;
  33. if (real_part < 1.e-6f * magnitude)
  34. {
  35. /* If src and dst are exactly opposite, rotate 180 degrees
  36. * around an arbitrary orthogonal axis. Axis normalisation
  37. * can happen later, when we normalise the quaternion. */
  38. real_part = 0.0f;
  39. w = abs(src.x) > abs(src.z) ? vec3(-src.y, src.x, 0.f)
  40. : vec3(0.f, -src.z, src.y);
  41. }
  42. else
  43. {
  44. /* Otherwise, build quaternion the standard way. */
  45. w = cross(src, dst);
  46. }
  47. return normalize(quat(real_part, w.x, w.y, w.z));
  48. }
  49. template<> quat slerp(quat const &qa, quat const &qb, float f)
  50. {
  51. float const magnitude = lol::sqrt(sqlength(qa) * sqlength(qb));
  52. float const product = lol::dot(qa, qb) / magnitude;
  53. /* If quaternions are equal or opposite, there is no need
  54. * to slerp anything, just return qa. */
  55. if (std::abs(product) >= 1.0f)
  56. return qa;
  57. float const sign = (product < 0.0f) ? -1.0f : 1.0f;
  58. float const theta = lol::acos(sign * product);
  59. float const s1 = lol::sin(sign * f * theta);
  60. float const s0 = lol::sin((1.0f - f) * theta);
  61. /* This is the same as 1/sin(theta) */
  62. float const d = 1.0f / lol::sqrt(1.f - product * product);
  63. return qa * (s0 * d) + qb * (s1 * d);
  64. }
  65. static inline vec3 quat_toeuler_generic(quat const &q, int i, int j, int k)
  66. {
  67. float n = norm(q);
  68. if (!n)
  69. return vec3::zero;
  70. /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */
  71. float const sign = ((2 + i - j) % 3) ? 1.f : -1.f;
  72. vec3 ret;
  73. /* k == i means X-Y-X style Euler angles; otherwise we’re
  74. * actually handling X-Y-Z style Tait-Bryan angles. */
  75. if (k == i)
  76. {
  77. k = 3 - i - j;
  78. ret[0] = atan2(q[1 + i] * q[1 + j] + sign * (q.w * q[1 + k]),
  79. q.w * q[1 + j] - sign * (q[1 + i] * q[1 + k]));
  80. ret[1] = acos(2.f * (sq(q.w) + sq(q[1 + i])) - 1.f);
  81. ret[2] = atan2(q[1 + i] * q[1 + j] - sign * (q.w * q[1 + k]),
  82. q.w * q[1 + j] + sign * (q[1 + i] * q[1 + k]));
  83. }
  84. else
  85. {
  86. ret[0] = atan2(2.f * (q.w * q[1 + i] - sign * (q[1 + j] * q[1 + k])),
  87. 1.f - 2.f * (sq(q[1 + i]) + sq(q[1 + j])));
  88. ret[1] = asin(2.f * (q.w * q[1 + j] + sign * (q[1 + i] * q[1 + k])));
  89. ret[2] = atan2(2.f * (q.w * q[1 + k] - sign * (q[1 + j] * q[1 + i])),
  90. 1.f - 2.f * (sq(q[1 + k]) + sq(q[1 + j])));
  91. }
  92. return ret / n;
  93. }
  94. static inline mat3 mat3_fromeuler_generic(vec3 const &v, int i, int j, int k)
  95. {
  96. mat3 ret;
  97. float const s0 = sin(v[0]), c0 = cos(v[0]);
  98. float const s1 = sin(v[1]), c1 = cos(v[1]);
  99. float const s2 = sin(v[2]), c2 = cos(v[2]);
  100. /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */
  101. float const sign = ((2 + i - j) % 3) ? 1.f : -1.f;
  102. /* k == i means X-Y-X style Euler angles; otherwise we’re
  103. * actually handling X-Y-Z style Tait-Bryan angles. */
  104. if (k == i)
  105. {
  106. k = 3 - i - j;
  107. ret[i][i] = c1;
  108. ret[i][j] = s0 * s1;
  109. ret[i][k] = - sign * (c0 * s1);
  110. ret[j][i] = s1 * s2;
  111. ret[j][j] = c0 * c2 - s0 * c1 * s2;
  112. ret[j][k] = sign * (s0 * c2 + c0 * c1 * s2);
  113. ret[k][i] = sign * (s1 * c2);
  114. ret[k][j] = - sign * (c0 * s2 + s0 * c1 * c2);
  115. ret[k][k] = - s0 * s2 + c0 * c1 * c2;
  116. }
  117. else
  118. {
  119. ret[i][i] = c1 * c2;
  120. ret[i][j] = sign * (c0 * s2) + s0 * s1 * c2;
  121. ret[i][k] = s0 * s2 - sign * (c0 * s1 * c2);
  122. ret[j][i] = - sign * (c1 * s2);
  123. ret[j][j] = c0 * c2 - sign * (s0 * s1 * s2);
  124. ret[j][k] = sign * (s0 * c2) + c0 * s1 * s2;
  125. ret[k][i] = sign * s1;
  126. ret[k][j] = - sign * (s0 * c1);
  127. ret[k][k] = c0 * c1;
  128. }
  129. return ret;
  130. }
  131. static inline quat quat_fromeuler_generic(vec3 const &v, int i, int j, int k)
  132. {
  133. vec3 const half_angles = v * 0.5f;
  134. float const s0 = sin(half_angles[0]), c0 = cos(half_angles[0]);
  135. float const s1 = sin(half_angles[1]), c1 = cos(half_angles[1]);
  136. float const s2 = sin(half_angles[2]), c2 = cos(half_angles[2]);
  137. quat ret;
  138. /* (2 + i - j) % 3 means x-y-z direct order; otherwise indirect */
  139. float const sign = ((2 + i - j) % 3) ? 1.f : -1.f;
  140. /* k == i means X-Y-X style Euler angles; otherwise we’re
  141. * actually handling X-Y-Z style Tait-Bryan angles. */
  142. if (k == i)
  143. {
  144. k = 3 - i - j;
  145. ret[0] = c1 * (c0 * c2 - s0 * s2);
  146. ret[1 + i] = c1 * (c0 * s2 + s0 * c2);
  147. ret[1 + j] = s1 * (c0 * c2 + s0 * s2);
  148. ret[1 + k] = sign * (s1 * (s0 * c2 - c0 * s2));
  149. }
  150. else
  151. {
  152. ret[0] = c0 * c1 * c2 - sign * (s0 * s1 * s2);
  153. ret[1 + i] = s0 * c1 * c2 + sign * (c0 * s1 * s2);
  154. ret[1 + j] = c0 * s1 * c2 - sign * (s0 * c1 * s2);
  155. ret[1 + k] = c0 * c1 * s2 + sign * (s0 * s1 * c2);
  156. }
  157. return ret;
  158. }
  159. #define DEFINE_GENERIC_EULER_CONVERSIONS(a1, a2, a3) \
  160. DEFINE_GENERIC_EULER_CONVERSIONS_INNER(a1, a2, a3, a1##a2##a3) \
  161. #define DEFINE_GENERIC_EULER_CONVERSIONS_INNER(a1, a2, a3, name) \
  162. /* Create quaternions from Euler angles */ \
  163. template<> quat quat::fromeuler_##name(vec3 const &v) \
  164. { \
  165. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  166. return quat_fromeuler_generic(v, a1, a2, a3); \
  167. } \
  168. \
  169. template<> quat quat::fromeuler_##name(float phi, float theta, float psi) \
  170. { \
  171. return quat::fromeuler_##name(vec3(phi, theta, psi)); \
  172. } \
  173. \
  174. /* Create 3×3 matrices from Euler angles */ \
  175. template<> mat3 mat3::fromeuler_##name(vec3 const &v) \
  176. { \
  177. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  178. return mat3_fromeuler_generic(v, a1, a2, a3); \
  179. } \
  180. \
  181. template<> mat3 mat3::fromeuler_##name(float phi, float theta, float psi) \
  182. { \
  183. return mat3::fromeuler_##name(vec3(phi, theta, psi)); \
  184. } \
  185. \
  186. /* Create 4×4 matrices from Euler angles */ \
  187. template<> mat4 mat4::fromeuler_##name(vec3 const &v) \
  188. { \
  189. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  190. return mat4(mat3_fromeuler_generic(v, a1, a2, a3), 1.f); \
  191. } \
  192. \
  193. template<> mat4 mat4::fromeuler_##name(float phi, float theta, float psi) \
  194. { \
  195. return mat4::fromeuler_##name(vec3(phi, theta, psi)); \
  196. } \
  197. \
  198. /* Retrieve Euler angles from a quaternion */ \
  199. template<> vec3 vec3::toeuler_##name(quat const &q) \
  200. { \
  201. int x = 0, y = 1, z = 2; UNUSED(x, y, z); \
  202. return quat_toeuler_generic(q, a1, a2, a3); \
  203. }
  204. DEFINE_GENERIC_EULER_CONVERSIONS(x, y, x)
  205. DEFINE_GENERIC_EULER_CONVERSIONS(x, z, x)
  206. DEFINE_GENERIC_EULER_CONVERSIONS(y, x, y)
  207. DEFINE_GENERIC_EULER_CONVERSIONS(y, z, y)
  208. DEFINE_GENERIC_EULER_CONVERSIONS(z, x, z)
  209. DEFINE_GENERIC_EULER_CONVERSIONS(z, y, z)
  210. DEFINE_GENERIC_EULER_CONVERSIONS(x, y, z)
  211. DEFINE_GENERIC_EULER_CONVERSIONS(x, z, y)
  212. DEFINE_GENERIC_EULER_CONVERSIONS(y, x, z)
  213. DEFINE_GENERIC_EULER_CONVERSIONS(y, z, x)
  214. DEFINE_GENERIC_EULER_CONVERSIONS(z, x, y)
  215. DEFINE_GENERIC_EULER_CONVERSIONS(z, y, x)
  216. #undef DEFINE_GENERIC_EULER_CONVERSIONS
  217. #undef DEFINE_GENERIC_EULER_CONVERSIONS_INNER
  218. } /* namespace lol */