matrix_transform.inl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. namespace glm
  2. {
  3. template<typename genType>
  4. GLM_FUNC_QUALIFIER GLM_CONSTEXPR genType identity()
  5. {
  6. return detail::init_gentype<genType, detail::genTypeTrait<genType>::GENTYPE>::identity();
  7. }
  8. template<typename T, qualifier Q>
  9. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> translate(mat<4, 4, T, Q> const& m, vec<3, T, Q> const& v)
  10. {
  11. mat<4, 4, T, Q> Result(m);
  12. Result[3] = m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3];
  13. return Result;
  14. }
  15. template<typename T, qualifier Q>
  16. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> rotate(mat<4, 4, T, Q> const& m, T angle, vec<3, T, Q> const& v)
  17. {
  18. T const a = angle;
  19. T const c = cos(a);
  20. T const s = sin(a);
  21. vec<3, T, Q> axis(normalize(v));
  22. vec<3, T, Q> temp((T(1) - c) * axis);
  23. mat<4, 4, T, Q> Rotate;
  24. Rotate[0][0] = c + temp[0] * axis[0];
  25. Rotate[0][1] = temp[0] * axis[1] + s * axis[2];
  26. Rotate[0][2] = temp[0] * axis[2] - s * axis[1];
  27. Rotate[1][0] = temp[1] * axis[0] - s * axis[2];
  28. Rotate[1][1] = c + temp[1] * axis[1];
  29. Rotate[1][2] = temp[1] * axis[2] + s * axis[0];
  30. Rotate[2][0] = temp[2] * axis[0] + s * axis[1];
  31. Rotate[2][1] = temp[2] * axis[1] - s * axis[0];
  32. Rotate[2][2] = c + temp[2] * axis[2];
  33. mat<4, 4, T, Q> Result;
  34. Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
  35. Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
  36. Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
  37. Result[3] = m[3];
  38. return Result;
  39. }
  40. template<typename T, qualifier Q>
  41. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> rotate_slow(mat<4, 4, T, Q> const& m, T angle, vec<3, T, Q> const& v)
  42. {
  43. T const a = angle;
  44. T const c = cos(a);
  45. T const s = sin(a);
  46. mat<4, 4, T, Q> Result;
  47. vec<3, T, Q> axis = normalize(v);
  48. Result[0][0] = c + (static_cast<T>(1) - c) * axis.x * axis.x;
  49. Result[0][1] = (static_cast<T>(1) - c) * axis.x * axis.y + s * axis.z;
  50. Result[0][2] = (static_cast<T>(1) - c) * axis.x * axis.z - s * axis.y;
  51. Result[0][3] = static_cast<T>(0);
  52. Result[1][0] = (static_cast<T>(1) - c) * axis.y * axis.x - s * axis.z;
  53. Result[1][1] = c + (static_cast<T>(1) - c) * axis.y * axis.y;
  54. Result[1][2] = (static_cast<T>(1) - c) * axis.y * axis.z + s * axis.x;
  55. Result[1][3] = static_cast<T>(0);
  56. Result[2][0] = (static_cast<T>(1) - c) * axis.z * axis.x + s * axis.y;
  57. Result[2][1] = (static_cast<T>(1) - c) * axis.z * axis.y - s * axis.x;
  58. Result[2][2] = c + (static_cast<T>(1) - c) * axis.z * axis.z;
  59. Result[2][3] = static_cast<T>(0);
  60. Result[3] = vec<4, T, Q>(0, 0, 0, 1);
  61. return m * Result;
  62. }
  63. template<typename T, qualifier Q>
  64. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> scale(mat<4, 4, T, Q> const& m, vec<3, T, Q> const& v)
  65. {
  66. mat<4, 4, T, Q> Result;
  67. Result[0] = m[0] * v[0];
  68. Result[1] = m[1] * v[1];
  69. Result[2] = m[2] * v[2];
  70. Result[3] = m[3];
  71. return Result;
  72. }
  73. template<typename T, qualifier Q>
  74. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> scale_slow(mat<4, 4, T, Q> const& m, vec<3, T, Q> const& v)
  75. {
  76. mat<4, 4, T, Q> Result(T(1));
  77. Result[0][0] = v.x;
  78. Result[1][1] = v.y;
  79. Result[2][2] = v.z;
  80. return m * Result;
  81. }
  82. template<typename T, qualifier Q>
  83. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> lookAtRH(vec<3, T, Q> const& eye, vec<3, T, Q> const& center, vec<3, T, Q> const& up)
  84. {
  85. vec<3, T, Q> const f(normalize(center - eye));
  86. vec<3, T, Q> const s(normalize(cross(f, up)));
  87. vec<3, T, Q> const u(cross(s, f));
  88. mat<4, 4, T, Q> Result(1);
  89. Result[0][0] = s.x;
  90. Result[1][0] = s.y;
  91. Result[2][0] = s.z;
  92. Result[0][1] = u.x;
  93. Result[1][1] = u.y;
  94. Result[2][1] = u.z;
  95. Result[0][2] =-f.x;
  96. Result[1][2] =-f.y;
  97. Result[2][2] =-f.z;
  98. Result[3][0] =-dot(s, eye);
  99. Result[3][1] =-dot(u, eye);
  100. Result[3][2] = dot(f, eye);
  101. return Result;
  102. }
  103. template<typename T, qualifier Q>
  104. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> lookAtLH(vec<3, T, Q> const& eye, vec<3, T, Q> const& center, vec<3, T, Q> const& up)
  105. {
  106. vec<3, T, Q> const f(normalize(center - eye));
  107. vec<3, T, Q> const s(normalize(cross(up, f)));
  108. vec<3, T, Q> const u(cross(f, s));
  109. mat<4, 4, T, Q> Result(1);
  110. Result[0][0] = s.x;
  111. Result[1][0] = s.y;
  112. Result[2][0] = s.z;
  113. Result[0][1] = u.x;
  114. Result[1][1] = u.y;
  115. Result[2][1] = u.z;
  116. Result[0][2] = f.x;
  117. Result[1][2] = f.y;
  118. Result[2][2] = f.z;
  119. Result[3][0] = -dot(s, eye);
  120. Result[3][1] = -dot(u, eye);
  121. Result[3][2] = -dot(f, eye);
  122. return Result;
  123. }
  124. template<typename T, qualifier Q>
  125. GLM_FUNC_QUALIFIER mat<4, 4, T, Q> lookAt(vec<3, T, Q> const& eye, vec<3, T, Q> const& center, vec<3, T, Q> const& up)
  126. {
  127. if(GLM_CONFIG_CLIP_CONTROL & GLM_CLIP_CONTROL_LH_BIT)
  128. return lookAtLH(eye, center, up);
  129. else
  130. return lookAtRH(eye, center, up);
  131. }
  132. }//namespace glm