[Bf-blender-cvs] [f18c971] hair_system: Moved child hair randomization into the hair render iterator and added hair frame caching.

Lukas Tönne noreply at git.blender.org
Tue Aug 12 21:01:39 CEST 2014


Commit: f18c97180a502149f940d5f60c1e5c193b5e3ca9
Author: Lukas Tönne
Date:   Tue Aug 12 20:49:41 2014 +0200
Branches: hair_system
https://developer.blender.org/rBf18c97180a502149f940d5f60c1e5c193b5e3ca9

Moved child hair randomization into the hair render iterator and added
hair frame caching.

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

M	source/blender/blenkernel/BKE_hair.h
M	source/blender/blenkernel/intern/hair.c
M	source/blender/editors/space_view3d/drawhair.c

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

diff --git a/source/blender/blenkernel/BKE_hair.h b/source/blender/blenkernel/BKE_hair.h
index b734558..2e859bf 100644
--- a/source/blender/blenkernel/BKE_hair.h
+++ b/source/blender/blenkernel/BKE_hair.h
@@ -60,12 +60,19 @@ typedef struct HairPointRenderCache {
 	float frame[3][3];
 } HairPointRenderCache;
 
+typedef struct HairRenderChildData {
+	float u, v;
+} HairRenderChildData;
+
 typedef struct HairRenderIterator {
 	struct HairSystem *hsys;
 	struct HairPointRenderCache *hair_cache; /* array of maxpoints elements to avoid recalculating per child hair */
 	int maxpoints;
 	int steps_per_point;
 	
+	HairRenderChildData *child_data;
+	int maxchildren;
+	
 	/* hair curve data */
 	struct HairCurve *hair;
 	int i;
diff --git a/source/blender/blenkernel/intern/hair.c b/source/blender/blenkernel/intern/hair.c
index 1607d17..627a5a1 100644
--- a/source/blender/blenkernel/intern/hair.c
+++ b/source/blender/blenkernel/intern/hair.c
@@ -34,6 +34,7 @@
 #include "MEM_guardedalloc.h"
 
 #include "BLI_math.h"
+#include "BLI_rand.h"
 #include "BLI_utildefines.h"
 
 #include "DNA_hair_types.h"
@@ -41,6 +42,8 @@
 #include "BKE_hair.h"
 #include "BKE_mesh_sample.h"
 
+#include "HAIR_capi.h"
+
 HairSystem *BKE_hairsys_new(void)
 {
 	HairSystem *hsys = MEM_callocN(sizeof(HairSystem), "hair system");
@@ -232,6 +235,9 @@ void BKE_hair_debug_data_free(HairDebugData *debug_data)
 	}
 }
 
+
+/* ================ Render ================ */
+
 static int hair_maxpoints(HairSystem *hsys)
 {
 	HairCurve *hair;
@@ -244,6 +250,65 @@ static int hair_maxpoints(HairSystem *hsys)
 	return maxpoints;
 }
 
+static HairRenderChildData *hair_gen_child_data(HairParams *params, unsigned int seed)
+{
+	int num_render_hairs = params->num_render_hairs;
+	HairRenderChildData *hair, *data = MEM_mallocN(sizeof(HairRenderChildData) * num_render_hairs, "hair render data");
+	RNG *rng;
+	int i;
+	
+	rng = BLI_rng_new(seed);
+	
+	for (i = 0, hair = data; i < num_render_hairs; ++i, ++hair) {
+		hair->u = BLI_rng_get_float(rng)*2.0f - 1.0f;
+		hair->v = BLI_rng_get_float(rng)*2.0f - 1.0f;
+	}
+	
+	BLI_rng_free(rng);
+	
+	return data;
+}
+
+static void get_hair_root_frame(HairCurve *hair, float frame[3][3])
+{
+	const float up[3] = {0.0f, 0.0f, 1.0f};
+	float normal[3];
+	
+	if (hair->totpoints >= 2) {
+		sub_v3_v3v3(normal, hair->points[1].co, hair->points[0].co);
+		normalize_v3(normal);
+		
+		copy_v3_v3(frame[0], normal);
+		madd_v3_v3v3fl(frame[1], up, normal, -dot_v3v3(up, normal));
+		normalize_v3(frame[1]);
+		cross_v3_v3v3(frame[2], frame[0], frame[1]);
+	}
+	else {
+		unit_m3(frame);
+	}
+}
+
+static void hair_precalc_cache(HairRenderIterator *iter)
+{
+	struct HAIR_FrameIterator *frame_iter = HAIR_frame_iter_new();
+	HairPointRenderCache *cache = iter->hair_cache;
+	float initial_frame[3][3];
+	int i;
+	
+	get_hair_root_frame(iter->hair, initial_frame);
+	
+	for (HAIR_frame_iter_init(frame_iter, iter->hair, iter->hair->avg_rest_length, iter->hsys->params.curl_smoothing, initial_frame);
+	     HAIR_frame_iter_valid(frame_iter);
+	     HAIR_frame_iter_next(iter)) {
+		
+		HAIR_frame_iter_get(frame_iter, cache->frame[0], cache->frame[1], cache->frame[2]);
+		/* matrix is stored row-major, needs to be transposed */
+		transpose_m3(cache->frame);
+		
+		++cache;
+	}
+}
+
 void BKE_hair_render_iter_init(HairRenderIterator *iter, HairSystem *hsys)
 {
 	iter->hsys = hsys;
@@ -251,6 +316,9 @@ void BKE_hair_render_iter_init(HairRenderIterator *iter, HairSystem *hsys)
 	iter->hair_cache = MEM_mallocN(sizeof(HairPointRenderCache) * iter->maxpoints, "hair render cache data");
 	iter->steps_per_point = 1; // XXX TODO!
 	
+	iter->maxchildren = hsys->params.num_render_hairs;
+	iter->child_data = hair_gen_child_data(&hsys->params, 12345); /* TODO handle seeds properly here ... */
+	
 	iter->hair = hsys->curves;
 	iter->i = 0;
 }
@@ -262,12 +330,18 @@ void BKE_hair_render_iter_init_hair(HairRenderIterator *iter)
 	
 	iter->step = 0;
 	iter->totsteps = (iter->hair->totpoints - 1) * iter->steps_per_point + 1;
+	
+	/* fill the hair cache to avoid redundant per-child calculations */
+	hair_precalc_cache(iter);
 }
 
 void BKE_hair_render_iter_end(HairRenderIterator *iter)
 {
 	if (iter->hair_cache)
 		MEM_freeN(iter->hair_cache);
+	
+	if (iter->child_data)
+		MEM_freeN(iter->child_data);
 }
 
 bool BKE_hair_render_iter_valid_hair(HairRenderIterator *iter)
diff --git a/source/blender/editors/space_view3d/drawhair.c b/source/blender/editors/space_view3d/drawhair.c
index e0b0ea5..e6041d6 100644
--- a/source/blender/editors/space_view3d/drawhair.c
+++ b/source/blender/editors/space_view3d/drawhair.c
@@ -39,7 +39,6 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
-#include "BLI_rand.h"
 #include "BLI_utildefines.h"
 
 #include "BKE_global.h"
@@ -91,52 +90,8 @@ static void draw_hair_line(HairSystem *hsys)
 	}
 }
 
-static void get_hair_root_frame(HairCurve *hair, float frame[3][3])
-{
-	const float up[3] = {0.0f, 0.0f, 1.0f};
-	float normal[3];
-	
-	if (hair->totpoints >= 2) {
-		sub_v3_v3v3(normal, hair->points[1].co, hair->points[0].co);
-		normalize_v3(normal);
-		
-		copy_v3_v3(frame[0], normal);
-		madd_v3_v3v3fl(frame[1], up, normal, -dot_v3v3(up, normal));
-		normalize_v3(frame[1]);
-		cross_v3_v3v3(frame[2], frame[0], frame[1]);
-	}
-	else {
-		unit_m3(frame);
-	}
-}
-
-typedef struct HairRenderData {
-	float u, v;
-} HairRenderData;
-
-static HairRenderData *gen_render_hairs(HairParams *params, unsigned int seed)
-{
-	int num_render_hairs = params->num_render_hairs;
-	HairRenderData *hair, *data = MEM_mallocN(sizeof(HairRenderData) * num_render_hairs, "hair render data");
-	RNG *rng;
-	int i;
-	
-	rng = BLI_rng_new(seed);
-	
-	for (i = 0, hair = data; i < num_render_hairs; ++i, ++hair) {
-		hair->u = BLI_rng_get_float(rng)*2.0f - 1.0f;
-		hair->v = BLI_rng_get_float(rng)*2.0f - 1.0f;
-	}
-	
-	BLI_rng_free(rng);
-	
-	return data;
-}
-
 static void draw_hair_render(HairSystem *hsys)
 {
-	HairRenderData *render_data;
-	
 	static unsigned int vertex_glbuf = 0;
 	static unsigned int elem_glbuf = 0;
 	
@@ -156,9 +111,6 @@ static void draw_hair_render(HairSystem *hsys)
 		return;
 	}
 	
-	/* TODO handle seeds properly here ... */
-	render_data = gen_render_hairs(&hsys->params, 12345);
-	
 	glColor3f(0.4f, 0.7f, 1.0f);
 	
 #define USE_BUFFERS
@@ -188,9 +140,6 @@ static void draw_hair_render(HairSystem *hsys)
 		int totelems;
 		unsigned int vertex_offset = 0;
 		unsigned int elem_offset = 0;
-		float initial_frame[3][3];
-		
-		get_hair_root_frame(iter.hair, initial_frame);
 		
 #ifdef USE_BUFFERS
 		vertex_data = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
@@ -249,8 +198,6 @@ static void draw_hair_render(HairSystem *hsys)
 	
 	BKE_hair_render_iter_end(&iter);
 	
-	MEM_freeN(render_data);
-	
 #undef USE_BUFFERS
 }




More information about the Bf-blender-cvs mailing list