You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
4.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2006-11-02
  5. // Updated : 2009-02-19
  6. // Licence : This source is under MIT License
  7. // File : glm/gtx/rotate_vector.inl
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. namespace glm
  10. {
  11. template <typename T>
  12. GLM_FUNC_QUALIFIER detail::tvec2<T> rotate
  13. (
  14. detail::tvec2<T> const & v,
  15. T const & angle
  16. )
  17. {
  18. detail::tvec2<T> Result;
  19. #ifdef GLM_FORCE_RADIANS
  20. T const Cos(cos(angle));
  21. T const Sin(sin(angle));
  22. #else
  23. T const Cos = cos(radians(angle));
  24. T const Sin = sin(radians(angle));
  25. #endif
  26. Result.x = v.x * Cos - v.y * Sin;
  27. Result.y = v.x * Sin + v.y * Cos;
  28. return Result;
  29. }
  30. template <typename T>
  31. GLM_FUNC_QUALIFIER detail::tvec3<T> rotate
  32. (
  33. detail::tvec3<T> const & v,
  34. T const & angle,
  35. detail::tvec3<T> const & normal
  36. )
  37. {
  38. return detail::tmat3x3<T>(glm::rotate(angle, normal)) * v;
  39. }
  40. /*
  41. template <typename T>
  42. GLM_FUNC_QUALIFIER detail::tvec3<T> rotateGTX(
  43. const detail::tvec3<T>& x,
  44. T angle,
  45. const detail::tvec3<T>& normal)
  46. {
  47. const T Cos = cos(radians(angle));
  48. const T Sin = sin(radians(angle));
  49. return x * Cos + ((x * normal) * (T(1) - Cos)) * normal + cross(x, normal) * Sin;
  50. }
  51. */
  52. template <typename T>
  53. GLM_FUNC_QUALIFIER detail::tvec4<T> rotate
  54. (
  55. detail::tvec4<T> const & v,
  56. T const & angle,
  57. detail::tvec3<T> const & normal
  58. )
  59. {
  60. return rotate(angle, normal) * v;
  61. }
  62. template <typename T>
  63. GLM_FUNC_QUALIFIER detail::tvec3<T> rotateX
  64. (
  65. detail::tvec3<T> const & v,
  66. T const & angle
  67. )
  68. {
  69. detail::tvec3<T> Result(v);
  70. #ifdef GLM_FORCE_RADIANS
  71. T const Cos(cos(angle));
  72. T const Sin(sin(angle));
  73. #else
  74. T const Cos = cos(radians(angle));
  75. T const Sin = sin(radians(angle));
  76. #endif
  77. Result.y = v.y * Cos - v.z * Sin;
  78. Result.z = v.y * Sin + v.z * Cos;
  79. return Result;
  80. }
  81. template <typename T>
  82. GLM_FUNC_QUALIFIER detail::tvec3<T> rotateY
  83. (
  84. detail::tvec3<T> const & v,
  85. T const & angle
  86. )
  87. {
  88. detail::tvec3<T> Result = v;
  89. #ifdef GLM_FORCE_RADIANS
  90. T const Cos(cos(angle));
  91. T const Sin(sin(angle));
  92. #else
  93. T const Cos(cos(radians(angle)));
  94. T const Sin(sin(radians(angle)));
  95. #endif
  96. Result.x = v.x * Cos + v.z * Sin;
  97. Result.z = -v.x * Sin + v.z * Cos;
  98. return Result;
  99. }
  100. template <typename T>
  101. GLM_FUNC_QUALIFIER detail::tvec3<T> rotateZ
  102. (
  103. detail::tvec3<T> const & v,
  104. T const & angle
  105. )
  106. {
  107. detail::tvec3<T> Result = v;
  108. #ifdef GLM_FORCE_RADIANS
  109. T const Cos(cos(angle));
  110. T const Sin(sin(angle));
  111. #else
  112. T const Cos(cos(radians(angle)));
  113. T const Sin(sin(radians(angle)));
  114. #endif
  115. Result.x = v.x * Cos - v.y * Sin;
  116. Result.y = v.x * Sin + v.y * Cos;
  117. return Result;
  118. }
  119. template <typename T>
  120. GLM_FUNC_QUALIFIER detail::tvec4<T> rotateX
  121. (
  122. detail::tvec4<T> const & v,
  123. T const & angle
  124. )
  125. {
  126. detail::tvec4<T> Result = v;
  127. #ifdef GLM_FORCE_RADIANS
  128. T const Cos(cos(angle));
  129. T const Sin(sin(angle));
  130. #else
  131. T const Cos(cos(radians(angle)));
  132. T const Sin(sin(radians(angle)));
  133. #endif
  134. Result.y = v.y * Cos - v.z * Sin;
  135. Result.z = v.y * Sin + v.z * Cos;
  136. return Result;
  137. }
  138. template <typename T>
  139. GLM_FUNC_QUALIFIER detail::tvec4<T> rotateY
  140. (
  141. detail::tvec4<T> const & v,
  142. T const & angle
  143. )
  144. {
  145. detail::tvec4<T> Result = v;
  146. #ifdef GLM_FORCE_RADIANS
  147. T const Cos(cos(angle));
  148. T const Sin(sin(angle));
  149. #else
  150. T const Cos(cos(radians(angle)));
  151. T const Sin(sin(radians(angle)));
  152. #endif
  153. Result.x = v.x * Cos + v.z * Sin;
  154. Result.z = -v.x * Sin + v.z * Cos;
  155. return Result;
  156. }
  157. template <typename T>
  158. GLM_FUNC_QUALIFIER detail::tvec4<T> rotateZ
  159. (
  160. detail::tvec4<T> const & v,
  161. T const & angle
  162. )
  163. {
  164. detail::tvec4<T> Result = v;
  165. #ifdef GLM_FORCE_RADIANS
  166. T const Cos(cos(angle));
  167. T const Sin(sin(angle));
  168. #else
  169. T const Cos(cos(radians(angle)));
  170. T const Sin(sin(radians(angle)));
  171. #endif
  172. Result.x = v.x * Cos - v.y * Sin;
  173. Result.y = v.x * Sin + v.y * Cos;
  174. return Result;
  175. }
  176. template <typename T>
  177. GLM_FUNC_QUALIFIER detail::tmat4x4<T> orientation
  178. (
  179. detail::tvec3<T> const & Normal,
  180. detail::tvec3<T> const & Up
  181. )
  182. {
  183. if(all(equal(Normal, Up)))
  184. return detail::tmat4x4<T>(T(1));
  185. detail::tvec3<T> RotationAxis = cross(Up, Normal);
  186. # ifdef GLM_FORCE_RADIANS
  187. T Angle = acos(dot(Normal, Up));
  188. # else
  189. T Angle = degrees(acos(dot(Normal, Up)));
  190. # endif
  191. return rotate(Angle, RotationAxis);
  192. }
  193. }//namespace glm