[Bf-blender-cvs] [faf529d] master: BLI_rand: Add BLI_rng_get_float_unit_v3, was static rayshade func

Campbell Barton noreply at git.blender.org
Sun Mar 30 06:04:56 CEST 2014


Commit: faf529d03689c0c472ee5895625cd2902915cfd6
Author: Campbell Barton
Date:   Sun Mar 30 13:03:57 2014 +1100
https://developer.blender.org/rBfaf529d03689c0c472ee5895625cd2902915cfd6

BLI_rand: Add BLI_rng_get_float_unit_v3, was static rayshade func

===================================================================

M	source/blender/blenlib/BLI_rand.h
M	source/blender/blenlib/intern/rand.c
M	source/blender/render/intern/source/rayshade.c

===================================================================

diff --git a/source/blender/blenlib/BLI_rand.h b/source/blender/blenlib/BLI_rand.h
index 378beff..20d5f8d 100644
--- a/source/blender/blenlib/BLI_rand.h
+++ b/source/blender/blenlib/BLI_rand.h
@@ -49,6 +49,7 @@ void        BLI_rng_srandom(struct RNG *rng, unsigned int seed);
 int         BLI_rng_get_int(struct RNG *rng);
 double      BLI_rng_get_double(struct RNG *rng);
 float       BLI_rng_get_float(struct RNG *rng);
+void        BLI_rng_get_float_unit_v3(struct RNG *rng, float v[3]);
 void        BLI_rng_shuffle_array(struct RNG *rng, void *data, int elemSize, int numElems);
 
 /** Note that skipping is as slow as generating n numbers! */
@@ -62,6 +63,7 @@ int     BLI_rand(void);
 
 /** Return a pseudo-random number N where 0.0f<=N<1.0f */
 float   BLI_frand(void);
+void    BLI_frand_unit_v3(float v[3]);
 
 /** Return a pseudo-random (hash) float from an integer value */
 float	BLI_hash_frand(unsigned int seed);
diff --git a/source/blender/blenlib/intern/rand.c b/source/blender/blenlib/intern/rand.c
index c5b58e5..f6f7c6e 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;
diff --git a/source/blender/render/intern/source/rayshade.c b/source/blender/render/intern/source/rayshade.c
index ae5b9ec..ed2c745 100644
--- a/source/blender/render/intern/source/rayshade.c
+++ b/source/blender/render/intern/source/rayshade.c
@@ -1672,18 +1672,6 @@ static void ray_trace_shadow_tra(Isect *is, ShadeInput *origshi, int depth, int
 
 
 /* aolight: function to create random unit sphere vectors for total random sampling */
-static void RandomSpherical(RNG *rng, float v[3])
-{
-	float r;
-	v[2] = 2.f*BLI_rng_get_float(rng)-1.f;
-	if ((r = 1.f - v[2]*v[2])>0.f) {
-		float a = 6.283185307f*BLI_rng_get_float(rng);
-		r = sqrt(r);
-		v[0] = r * cosf(a);
-		v[1] = r * sinf(a);
-	}
-	else v[2] = 1.f;
-}
 
 /* calc distributed spherical energy */
 static void DS_energy(float *sphere, int tot, float vec[3])
@@ -1729,7 +1717,7 @@ void init_ao_sphere(World *wrld)
 	/* init */
 	fp= wrld->aosphere;
 	for (a=0; a<tot; a++, fp+= 3) {
-		RandomSpherical(rng, fp);
+		BLI_rng_get_float_unit_v3(rng, fp);
 	}
 	
 	while (iter--) {
@@ -1780,7 +1768,7 @@ static float *sphere_sampler(int type, int resol, int thread, int xs, int ys, in
 
 		vec= sphere;
 		for (a=0; a<tot; a++, vec+=3) {
-			RandomSpherical(rng, vec);
+			BLI_rng_get_float_unit_v3(rng, vec);
 		}
 
 		BLI_rng_free(rng);




More information about the Bf-blender-cvs mailing list