From 5f7862ebeca6f399b43f73eed9622a44d52cd998 Mon Sep 17 00:00:00 2001 From: Christophe Riccio Date: Sat, 6 Sep 2014 21:52:51 +0200 Subject: [PATCH] Improved linearRand: support precision and integers (#230) --- glm/detail/func_common.inl | 11 ++ glm/gtc/random.hpp | 13 +- glm/gtc/random.inl | 286 +++++++++++++++++++++++++++++++++---- readme.txt | 1 + test/gtc/gtc_random.cpp | 19 +++ 5 files changed, 301 insertions(+), 29 deletions(-) diff --git a/glm/detail/func_common.inl b/glm/detail/func_common.inl index f43541f9..c6f78332 100644 --- a/glm/detail/func_common.inl +++ b/glm/detail/func_common.inl @@ -311,6 +311,17 @@ namespace detail return std::modf(x, &i); } + template + GLM_FUNC_QUALIFIER detail::tvec1 modf + ( + detail::tvec1 const & x, + detail::tvec1 & i + ) + { + return detail::tvec1( + modf(x.x, i.x)); + } + template GLM_FUNC_QUALIFIER detail::tvec2 modf ( diff --git a/glm/gtc/random.hpp b/glm/gtc/random.hpp index 395bf6a4..7087d205 100644 --- a/glm/gtc/random.hpp +++ b/glm/gtc/random.hpp @@ -58,10 +58,15 @@ namespace glm /// @param Max /// @tparam genType Value type. Currently supported: half (not recommanded), float or double scalars and vectors. /// @see gtc_random - template - GLM_FUNC_DECL genType linearRand( - genType const & Min, - genType const & Max); + template + GLM_FUNC_DECL genTYpe linearRand( + genTYpe const & Min, + genTYpe const & Max); + + template class vecType> + GLM_FUNC_DECL vecType linearRand( + vecType const & Min, + vecType const & Max); /// Generate random numbers in the interval [Min, Max], according a gaussian distribution /// diff --git a/glm/gtc/random.inl b/glm/gtc/random.inl index 0b8c6595..dff1789c 100644 --- a/glm/gtc/random.inl +++ b/glm/gtc/random.inl @@ -35,53 +35,289 @@ namespace glm{ namespace detail { + template class vecType> + struct compute_rand + { + GLM_FUNC_QUALIFIER static vecType call(); + }; + + template + struct compute_rand + { + GLM_FUNC_QUALIFIER static detail::tvec1 call() + { + return detail::tvec1( + std::rand()) % std::numeric_limits::max(); + } + }; + + template + struct compute_rand + { + GLM_FUNC_QUALIFIER static detail::tvec2 call() + { + return detail::tvec2( + std::rand(), + std::rand()) % std::numeric_limits::max(); + } + }; + + template + struct compute_rand + { + GLM_FUNC_QUALIFIER static detail::tvec3 call() + { + return detail::tvec3( + std::rand(), + std::rand(), + std::rand()) % std::numeric_limits::max(); + } + }; + + template + struct compute_rand + { + GLM_FUNC_QUALIFIER static detail::tvec4 call() + { + return detail::tvec4( + std::rand(), + std::rand(), + std::rand(), + std::rand()) % std::numeric_limits::max(); + } + }; + + template class vecType> + struct compute_rand + { + GLM_FUNC_QUALIFIER static vecType call() + { + return + (vecType(compute_rand::call()) << static_cast(8)) | + (vecType(compute_rand::call()) << static_cast(0)); + } + }; + + template class vecType> + struct compute_rand + { + GLM_FUNC_QUALIFIER static vecType call() + { + return + (vecType(compute_rand::call()) << static_cast(16)) | + (vecType(compute_rand::call()) << static_cast(0)); + } + }; + + template class vecType> + struct compute_rand + { + GLM_FUNC_QUALIFIER static vecType call() + { + return + (vecType(compute_rand::call()) << static_cast(32)) | + (vecType(compute_rand::call()) << static_cast(0)); + } + }; + + template class vecType> struct compute_linearRand { - template - GLM_FUNC_QUALIFIER T operator() (T const & Min, T const & Max) const; -/* + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max); + }; + + template class vecType> + struct compute_linearRand + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) { - GLM_STATIC_ASSERT(0, "'linearRand' invalid template parameter type. GLM_GTC_random only supports floating-point template types."); - return Min; + return (vecType(compute_rand::call()) % (Max - Min)) + Min; } -*/ }; - template <> - GLM_FUNC_QUALIFIER float compute_linearRand::operator() (float const & Min, float const & Max) const + template class vecType> + struct compute_linearRand { - return float(std::rand()) / float(RAND_MAX) * (Max - Min) + Min; - } + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) + { + return (compute_rand::call() % (Max - Min)) + Min; + } + }; - template <> - GLM_FUNC_QUALIFIER double compute_linearRand::operator() (double const & Min, double const & Max) const + template class vecType> + struct compute_linearRand + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) + { + return (vecType(compute_rand::call()) % (Max - Min)) + Min; + } + }; + + template class vecType> + struct compute_linearRand + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) + { + return (compute_rand::call() % (Max - Min)) + Min; + } + }; + + template class vecType> + struct compute_linearRand + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) + { + return (vecType(compute_rand::call()) % (Max - Min)) + Min; + } + }; + + template class vecType> + struct compute_linearRand + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) + { + return (compute_rand::call() % (Max - Min)) + Min; + } + }; + + template class vecType> + struct compute_linearRand + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) + { + return (vecType(compute_rand::call()) % (Max - Min)) + Min; + } + }; + + template class vecType> + struct compute_linearRand + { + GLM_FUNC_QUALIFIER static vecType call(vecType const & Min, vecType const & Max) + { + return (compute_rand::call() % (Max - Min)) + Min; + } + }; + + template