[Bf-blender-cvs] [a1dd14c] hair_system: Usable random distribution algorithm.

Lukas Tönne noreply at git.blender.org
Sun Oct 5 14:56:23 CEST 2014


Commit: a1dd14c7644d4085f1c3855a07d5eee510a90609
Author: Lukas Tönne
Date:   Mon Mar 3 12:36:02 2014 +0100
Branches: hair_system
https://developer.blender.org/rBa1dd14c7644d4085f1c3855a07d5eee510a90609

Usable random distribution algorithm.

Does not include area weighting yet.

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

M	source/blender/blenkernel/BKE_mesh_sample.h
M	source/blender/blenkernel/intern/mesh_sample.c

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

diff --git a/source/blender/blenkernel/BKE_mesh_sample.h b/source/blender/blenkernel/BKE_mesh_sample.h
index a58e892..084f477 100644
--- a/source/blender/blenkernel/BKE_mesh_sample.h
+++ b/source/blender/blenkernel/BKE_mesh_sample.h
@@ -57,17 +57,20 @@ void BKE_mesh_sample_surface_array_begin(MSurfaceSampleArrayIterator *iter, MSur
 
 /* Sampling */
 
-typedef enum eMSurfaceSampleRNG {
-	MSS_RNG_UNIFORM
-} eMSurfaceSampleRNG;
+typedef enum eMSurfaceSampleAlgorithm {
+	MSS_RANDOM
+} eMSurfaceSampleAlgorithm;
 
 typedef struct MSurfaceSampleInfo {
+	eMSurfaceSampleAlgorithm algorithm;
 	struct DerivedMesh *dm;
 	
-	eMSurfaceSampleRNG rng;
-	unsigned int seed;
+	struct RNG *rng;
 } MSurfaceSampleInfo;
 
+void BKE_mesh_sample_info_random(struct MSurfaceSampleInfo *info, struct DerivedMesh *dm, unsigned int seed);
+void BKE_mesh_sample_info_release(struct MSurfaceSampleInfo *info);
+
 void BKE_mesh_sample_surface_array(const struct MSurfaceSampleInfo *info, struct MSurfaceSample *samples, int totsample);
 
 #endif  /* __BKE_MESH_SAMPLE_H__ */
diff --git a/source/blender/blenkernel/intern/mesh_sample.c b/source/blender/blenkernel/intern/mesh_sample.c
index ddb35e2..53b9231 100644
--- a/source/blender/blenkernel/intern/mesh_sample.c
+++ b/source/blender/blenkernel/intern/mesh_sample.c
@@ -72,14 +72,60 @@ void BKE_mesh_sample_surface_array_begin(MSurfaceSampleArrayIterator *iter, MSur
 
 /* Sampling */
 
-static mesh_sample_surface_uniform(const MSurfaceSampleInfo *info, MSurfaceSample *sample, RNG *rng)
+void BKE_mesh_sample_info_random(MSurfaceSampleInfo *info, DerivedMesh *dm, unsigned int seed)
 {
-	sample->orig_face = BLI_rng_get_int(rng) % info->dm->getNumTessFaces(info->dm);
-	sample->orig_weights = 
+	info->algorithm = MSS_RANDOM;
+	info->dm = dm;
+	
+	info->rng = BLI_rng_new(seed);
+}
+
+void BKE_mesh_sample_info_release(MSurfaceSampleInfo *info)
+{
+	if (info->rng) {
+		BLI_rng_free(info->rng);
+		info->rng = NULL;
+	}
+}
+
+
+static void mesh_sample_surface_random(const MSurfaceSampleInfo *info, MSurfaceSample *sample)
+{
+	MFace *mfaces = info->dm->getTessFaceArray(info->dm);
+	int totfaces = info->dm->getNumTessFaces(info->dm);
+	MFace *mface;
+	float sum, inv_sum;
+	
+	sample->orig_face = BLI_rng_get_int(info->rng) % totfaces;
+	
+	mface = &mfaces[sample->orig_face];
+	sample->orig_weights[0] = BLI_rng_get_float(info->rng);
+	sample->orig_weights[1] = BLI_rng_get_float(info->rng);
+	sample->orig_weights[2] = BLI_rng_get_float(info->rng);
+	sample->orig_weights[3] = mface->v4 ? BLI_rng_get_float(info->rng) : 0.0f;
+	
+	sum = sample->orig_weights[0] +
+	      sample->orig_weights[1] +
+	      sample->orig_weights[2] +
+	      sample->orig_weights[3];
+	inv_sum = sum > 0.0f ? 1.0f/sum : 0.0f;
+	sample->orig_weights[0] *= inv_sum;
+	sample->orig_weights[1] *= inv_sum;
+	sample->orig_weights[2] *= inv_sum;
+	sample->orig_weights[3] *= inv_sum;
 }
 
 void BKE_mesh_sample_surface_array(const MSurfaceSampleInfo *info, MSurfaceSample *samples, int totsample)
 {
-	RNG *rng = BLI_rng_new(info->seed);
+	MSurfaceSample *sample;
+	int i;
 	
+	switch (info->algorithm) {
+		case MSS_RANDOM: {
+			DM_ensure_tessface(info->dm);
+			for (sample = samples, i = 0; i < totsample; ++sample, ++i)
+				mesh_sample_surface_random(info, sample);
+			break;
+		}
+	}
 }




More information about the Bf-blender-cvs mailing list