9
3

scalar_multiplication.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /// @ref gtx
  2. /// @file glm/gtx/scalar_multiplication.hpp
  3. /// @author Joshua Moerman
  4. ///
  5. /// Include <glm/gtx/scalar_multiplication.hpp> to use the features of this extension.
  6. ///
  7. /// Enables scalar multiplication for all types
  8. ///
  9. /// Since GLSL is very strict about types, the following (often used) combinations do not work:
  10. /// double * vec4
  11. /// int * vec4
  12. /// vec4 / int
  13. /// So we'll fix that! Of course "float * vec4" should remain the same (hence the enable_if magic)
  14. #pragma once
  15. #include "../detail/setup.hpp"
  16. #if GLM_MESSAGES == GLM_ENABLE && !defined(GLM_EXT_INCLUDED)
  17. # ifndef GLM_ENABLE_EXPERIMENTAL
  18. # pragma message("GLM: GLM_GTX_scalar_multiplication is an experimental extension and may change in the future. Use #define GLM_ENABLE_EXPERIMENTAL before including it, if you really want to use it.")
  19. # else
  20. # pragma message("GLM: GLM_GTX_scalar_multiplication extension included")
  21. # endif
  22. #endif
  23. #include "../vec2.hpp"
  24. #include "../vec3.hpp"
  25. #include "../vec4.hpp"
  26. #include "../mat2x2.hpp"
  27. #include <type_traits>
  28. namespace glm
  29. {
  30. template<typename T, typename Vec>
  31. using return_type_scalar_multiplication = typename std::enable_if<
  32. !std::is_same<T, float>::value // T may not be a float
  33. && std::is_arithmetic<T>::value, Vec // But it may be an int or double (no vec3 or mat3, ...)
  34. >::type;
  35. #define GLM_IMPLEMENT_SCAL_MULT(Vec) \
  36. template<typename T> \
  37. return_type_scalar_multiplication<T, Vec> \
  38. operator*(T const& s, Vec rh){ \
  39. return rh *= static_cast<float>(s); \
  40. } \
  41. \
  42. template<typename T> \
  43. return_type_scalar_multiplication<T, Vec> \
  44. operator*(Vec lh, T const& s){ \
  45. return lh *= static_cast<float>(s); \
  46. } \
  47. \
  48. template<typename T> \
  49. return_type_scalar_multiplication<T, Vec> \
  50. operator/(Vec lh, T const& s){ \
  51. return lh *= 1.0f / static_cast<float>(s); \
  52. }
  53. GLM_IMPLEMENT_SCAL_MULT(vec2)
  54. GLM_IMPLEMENT_SCAL_MULT(vec3)
  55. GLM_IMPLEMENT_SCAL_MULT(vec4)
  56. GLM_IMPLEMENT_SCAL_MULT(mat2)
  57. GLM_IMPLEMENT_SCAL_MULT(mat2x3)
  58. GLM_IMPLEMENT_SCAL_MULT(mat2x4)
  59. GLM_IMPLEMENT_SCAL_MULT(mat3x2)
  60. GLM_IMPLEMENT_SCAL_MULT(mat3)
  61. GLM_IMPLEMENT_SCAL_MULT(mat3x4)
  62. GLM_IMPLEMENT_SCAL_MULT(mat4x2)
  63. GLM_IMPLEMENT_SCAL_MULT(mat4x3)
  64. GLM_IMPLEMENT_SCAL_MULT(mat4)
  65. #undef GLM_IMPLEMENT_SCAL_MULT
  66. } // namespace glm