diff --git a/glm/gtx/component_wise.hpp b/glm/gtx/component_wise.hpp index 4c19f85b..2b8cbf76 100644 --- a/glm/gtx/component_wise.hpp +++ b/glm/gtx/component_wise.hpp @@ -60,6 +60,12 @@ namespace glm template class vecType> GLM_FUNC_DECL vecType compNormalize(vecType const & v); + /// Convert a normalized float vector to an integer vector. + /// If the parameter value type is already a floating precision type, the value is passed through. + /// @see gtx_component_wise + template class vecType> + GLM_FUNC_DECL vecType compScale(vecType const & v) + /// Add all vector components together. /// @see gtx_component_wise template diff --git a/glm/gtx/component_wise.inl b/glm/gtx/component_wise.inl index 99936e37..4c380b7a 100644 --- a/glm/gtx/component_wise.inl +++ b/glm/gtx/component_wise.inl @@ -67,6 +67,39 @@ namespace detail return v; } }; + + template class vecType, bool isInteger, bool signedType> + struct compute_compScale + {}; + + template class vecType> + struct compute_compScale + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & v) + { + floatType const Min = static_cast(std::numeric_limits::min()); + floatType const Max = static_cast(std::numeric_limits::max()); + return (vecType(v) + Min) * (Max - Min) * static_cast(2) - static_cast(1); + } + }; + + template class vecType> + struct compute_compScale + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & v) + { + return vecType(vecType(v) * static_cast(std::numeric_limits::max())); + } + }; + + template class vecType> + struct compute_compScale + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & v) + { + return v; + } + }; }//namespace detail template class vecType> @@ -77,6 +110,14 @@ namespace detail return detail::compute_compNormalize::is_integer, std::numeric_limits::is_signed>::call(v); } + template class vecType> + GLM_FUNC_QUALIFIER vecType compScale(vecType const & v) + { + GLM_STATIC_ASSERT(std::numeric_limits::is_iec559, "'compScale' accepts only floating-point types for 'floatType' template parameter"); + + return detail::compute_compScale::is_integer, std::numeric_limits::is_signed>::call(v); + } + template class vecType> GLM_FUNC_QUALIFIER T compAdd(vecType const & v) { diff --git a/readme.md b/readme.md index e52b6b69..5c9ba555 100644 --- a/readme.md +++ b/readme.md @@ -53,7 +53,7 @@ glm::mat4 camera(float Translate, glm::vec2 const & Rotate) #### [GLM 0.9.7.2](https://github.com/g-truc/glm/releases/latest) - 2015-XX-XX ##### Improvements: -- Added compNormalize function to GTX_component_wise +- Added compNormalize and compScale functions to GTX_component_wise ##### Fixes: - Fixed GTC_round floorMultiple/ceilMultiple #412