diff options
author | Campbell Barton <ideasman42@gmail.com> | 2014-03-30 06:03:57 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2014-03-30 08:04:20 +0400 |
commit | faf529d03689c0c472ee5895625cd2902915cfd6 (patch) | |
tree | 04416880217ef8cf4eec0fcd4a1a6b5e699d680d /source/blender/blenlib/intern/rand.c | |
parent | a6e8137983d74ebff785ee134f635e1a08f58237 (diff) |
BLI_rand: Add BLI_rng_get_float_unit_v3, was static rayshade func
Diffstat (limited to 'source/blender/blenlib/intern/rand.c')
-rw-r--r-- | source/blender/blenlib/intern/rand.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c index c5b58e5a61b..f6f7c6e2486 100644 --- a/source/blender/blenlib/intern/rand.c +++ b/source/blender/blenlib/intern/rand.c @@ -32,6 +32,7 @@ #include <stdlib.h> #include <string.h> +#include <math.h> #include "MEM_guardedalloc.h" @@ -117,6 +118,21 @@ float BLI_rng_get_float(RNG *rng) return (float) BLI_rng_get_int(rng) / 0x80000000; } +void BLI_rng_get_float_unit_v3(RNG *rng, float v[3]) +{ + float r; + v[2] = (2.0f * BLI_rng_get_float(rng)) - 1.0f; + if ((r = 1.0f - (v[2] * v[2])) > 0.0f) { + float a = (float)(M_PI * 2.0) * BLI_rng_get_float(rng); + r = sqrtf(r); + v[0] = r * cosf(a); + v[1] = r * sinf(a); + } + else { + v[2] = 1.0f; + } +} + void BLI_rng_shuffle_array(RNG *rng, void *data, int elemSize, int numElems) { int i = numElems; @@ -173,6 +189,11 @@ float BLI_frand(void) return BLI_rng_get_float(&theBLI_rng); } +void BLI_frand_unit_v3(float v[3]) +{ + return BLI_rng_get_float_unit_v3(&theBLI_rng, v); +} + float BLI_hash_frand(unsigned int seed) { RNG rng; |