/// @ref gtc_random /// @file glm/gtc/random.inl #include "../geometric.hpp" #include "../exponential.hpp" #include #include #include namespace glm{ namespace detail { template class vecType> struct compute_rand { GLM_FUNC_QUALIFIER static vecType call(); }; template struct compute_rand<1, uint8, P, vec> { GLM_FUNC_QUALIFIER static vec<1, uint8, P> call() { return vec<1, uint8, P>( std::rand() % std::numeric_limits::max()); } }; template struct compute_rand<2, uint8, P, vec> { GLM_FUNC_QUALIFIER static vec<2, uint8, P> call() { return vec<2, uint8, P>( std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max()); } }; template struct compute_rand<3, uint8, P, vec> { GLM_FUNC_QUALIFIER static vec<3, uint8, P> call() { return vec<3, uint8, P>( std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max()); } }; template struct compute_rand<4, uint8, P, vec> { GLM_FUNC_QUALIFIER static vec<4, uint8, P> call() { return vec<4, uint8, P>( std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max(), std::rand() % std::numeric_limits::max(), 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 { 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) { return (vecType(compute_rand::call() % vecType(Max + static_cast(1) - 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 + static_cast(1) - 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() % vecType(Max + static_cast(1) - 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 + static_cast(1) - 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() % vecType(Max + static_cast(1) - 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 + static_cast(1) - 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() % vecType(Max + static_cast(1) - 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 + static_cast(1) - 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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (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()) / static_cast(std::numeric_limits::max()) * (Max - Min) + Min; } }; }//namespace detail template GLM_FUNC_QUALIFIER genType linearRand(genType Min, genType Max) { return detail::compute_linearRand<1, genType, highp, vec>::call( vec<1, genType, highp>(Min), vec<1, genType, highp>(Max)).x; } template class vecType> GLM_FUNC_QUALIFIER vecType linearRand(vecType const & Min, vecType const & Max) { return detail::compute_linearRand::call(Min, Max); } template GLM_FUNC_QUALIFIER genType gaussRand(genType Mean, genType Deviation) { genType w, x1, x2; do { x1 = linearRand(genType(-1), genType(1)); x2 = linearRand(genType(-1), genType(1)); w = x1 * x1 + x2 * x2; } while(w > genType(1)); return x2 * Deviation * Deviation * sqrt((genType(-2) * log(w)) / w) + Mean; } template class vecType> GLM_FUNC_QUALIFIER vecType gaussRand(vecType const & Mean, vecType const & Deviation) { return detail::functor2::call(gaussRand, Mean, Deviation); } template GLM_FUNC_QUALIFIER vec<2, T, defaultp> diskRand(T Radius) { vec<2, T, defaultp> Result(T(0)); T LenRadius(T(0)); do { Result = linearRand( vec<2, T, defaultp>(-Radius), vec<2, T, defaultp>(Radius)); LenRadius = length(Result); } while(LenRadius > Radius); return Result; } template GLM_FUNC_QUALIFIER vec<3, T, defaultp> ballRand(T Radius) { vec<3, T, defaultp> Result(T(0)); T LenRadius(T(0)); do { Result = linearRand( vec<3, T, defaultp>(-Radius), vec<3, T, defaultp>(Radius)); LenRadius = length(Result); } while(LenRadius > Radius); return Result; } template GLM_FUNC_QUALIFIER vec<2, T, defaultp> circularRand(T Radius) { T a = linearRand(T(0), T(6.283185307179586476925286766559f)); return vec<2, T, defaultp>(cos(a), sin(a)) * Radius; } template GLM_FUNC_QUALIFIER vec<3, T, defaultp> sphericalRand(T Radius) { T z = linearRand(T(-1), T(1)); T a = linearRand(T(0), T(6.283185307179586476925286766559f)); T r = sqrt(T(1) - z * z); T x = r * cos(a); T y = r * sin(a); return vec<3, T, defaultp>(x, y, z) * Radius; } }//namespace glm