From 84e6d82051749ead43d2fcb82efca7c4234b1015 Mon Sep 17 00:00:00 2001 From: Sergey Kosarevsky Date: Mon, 11 Dec 2017 18:09:07 +0100 Subject: [PATCH] Fixed #692: sphericalRand() is doing correct distribution on a sphere --- glm/gtc/random.inl | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/glm/gtc/random.inl b/glm/gtc/random.inl index 68f38742..e0e7a93f 100644 --- a/glm/gtc/random.inl +++ b/glm/gtc/random.inl @@ -340,13 +340,12 @@ namespace detail 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 theta = linearRand(T(0), T(6.283185307179586476925286766559f)); + T phi = std::acos(linearRand(T(-1.0f), T(1.0f))); - T r = sqrt(T(1) - z * z); - - T x = r * std::cos(a); - T y = r * std::sin(a); + T x = std::sin(phi) * std::cos(theta); + T y = std::sin(phi) * std::sin(theta); + T z = std::cos(phi); return vec<3, T, defaultp>(x, y, z) * Radius; }