scalar_multiplication.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef GLM_ENABLE_EXPERIMENTAL
  17. # error "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."
  18. #endif
  19. #if !GLM_HAS_TEMPLATE_ALIASES && !(GLM_COMPILER & GLM_COMPILER_GCC)
  20. # error "GLM_GTX_scalar_multiplication requires C++11 support or alias templates and if not support for GCC"
  21. #endif
  22. #include "../vec2.hpp"
  23. #include "../vec3.hpp"
  24. #include "../vec4.hpp"
  25. #include "../mat2x2.hpp"
  26. #include <type_traits>
  27. namespace glm
  28. {
  29. template<typename T, typename Vec>
  30. using return_type_scalar_multiplication = typename std::enable_if<
  31. !std::is_same<T, float>::value // T may not be a float
  32. && std::is_arithmetic<T>::value, Vec // But it may be an int or double (no vec3 or mat3, ...)
  33. >::type;
  34. #define GLM_IMPLEMENT_SCAL_MULT(Vec) \
  35. template<typename T> \
  36. return_type_scalar_multiplication<T, Vec> \
  37. operator*(T const& s, Vec rh){ \
  38. return rh *= static_cast<float>(s); \
  39. } \
  40. \
  41. template<typename T> \
  42. return_type_scalar_multiplication<T, Vec> \
  43. operator*(Vec lh, T const& s){ \
  44. return lh *= static_cast<float>(s); \
  45. } \
  46. \
  47. template<typename T> \
  48. return_type_scalar_multiplication<T, Vec> \
  49. operator/(Vec lh, T const& s){ \
  50. return lh *= 1.0f / s; \
  51. }
  52. GLM_IMPLEMENT_SCAL_MULT(vec2)
  53. GLM_IMPLEMENT_SCAL_MULT(vec3)
  54. GLM_IMPLEMENT_SCAL_MULT(vec4)
  55. GLM_IMPLEMENT_SCAL_MULT(mat2)
  56. GLM_IMPLEMENT_SCAL_MULT(mat2x3)
  57. GLM_IMPLEMENT_SCAL_MULT(mat2x4)
  58. GLM_IMPLEMENT_SCAL_MULT(mat3x2)
  59. GLM_IMPLEMENT_SCAL_MULT(mat3)
  60. GLM_IMPLEMENT_SCAL_MULT(mat3x4)
  61. GLM_IMPLEMENT_SCAL_MULT(mat4x2)
  62. GLM_IMPLEMENT_SCAL_MULT(mat4x3)
  63. GLM_IMPLEMENT_SCAL_MULT(mat4)
  64. #undef GLM_IMPLEMENT_SCAL_MULT
  65. } // namespace glm