rotate_normalized_axis.inl 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /// @ref gtx_rotate_normalized_axis
  2. namespace glm
  3. {
  4. template<typename T, qualifier Q>
  5. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> rotateNormalizedAxis
  6. (
  7. mat<4, 4, T, Q> const& m,
  8. T const& angle,
  9. vec<3, T, Q> const& v
  10. )
  11. {
  12. T const a = angle;
  13. T const c = cos(a);
  14. T const s = sin(a);
  15. vec<3, T, Q> const axis(v);
  16. vec<3, T, Q> const temp((static_cast<T>(1) - c) * axis);
  17. mat<4, 4, T, Q> Rotate;
  18. Rotate[0][0] = c + temp[0] * axis[0];
  19. Rotate[0][1] = 0 + temp[0] * axis[1] + s * axis[2];
  20. Rotate[0][2] = 0 + temp[0] * axis[2] - s * axis[1];
  21. Rotate[1][0] = 0 + temp[1] * axis[0] - s * axis[2];
  22. Rotate[1][1] = c + temp[1] * axis[1];
  23. Rotate[1][2] = 0 + temp[1] * axis[2] + s * axis[0];
  24. Rotate[2][0] = 0 + temp[2] * axis[0] + s * axis[1];
  25. Rotate[2][1] = 0 + temp[2] * axis[1] - s * axis[0];
  26. Rotate[2][2] = c + temp[2] * axis[2];
  27. mat<4, 4, T, Q> Result;
  28. Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
  29. Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
  30. Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
  31. Result[3] = m[3];
  32. return Result;
  33. }
  34. template<typename T, qualifier Q>
  35. GLM_FUNC_QUALIFIER qua<T, Q> rotateNormalizedAxis
  36. (
  37. qua<T, Q> const& q,
  38. T const& angle,
  39. vec<3, T, Q> const& v
  40. )
  41. {
  42. vec<3, T, Q> const Tmp(v);
  43. T const AngleRad(angle);
  44. T const Sin = sin(AngleRad * T(0.5));
  45. return q * qua<T, Q>(cos(AngleRad * static_cast<T>(0.5)), Tmp.x * Sin, Tmp.y * Sin, Tmp.z * Sin);
  46. //return gtc::quaternion::cross(q, tquat<T, Q>(cos(AngleRad * T(0.5)), Tmp.x * fSin, Tmp.y * fSin, Tmp.z * fSin));
  47. }
  48. }//namespace glm