scalar_multiplication.hpp 2.3 KB

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