[Bf-blender-cvs] [a083f0a] hair_system: Fixed the smoothing algorithm.

Lukas Tönne noreply at git.blender.org
Sun Jul 27 11:23:16 CEST 2014


Commit: a083f0ac326f0640522665c8cf1e0cd275d23520
Author: Lukas Tönne
Date:   Sun Jul 27 11:22:21 2014 +0200
Branches: hair_system
https://developer.blender.org/rBa083f0ac326f0640522665c8cf1e0cd275d23520

Fixed the smoothing algorithm.

The smoothing factor used for display is now exposed in RNA/modifier UI
too. This is just a debugging feature though, it doesn't really serve
any higher purpose.

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

M	release/scripts/startup/bl_ui/properties_data_modifier.py
M	source/blender/editors/physics/hair_ops.c
M	source/blender/editors/space_view3d/drawhair.c
M	source/blender/hair/HAIR_capi.cpp
M	source/blender/hair/HAIR_capi.h
M	source/blender/hair/intern/HAIR_smoothing.h
M	source/blender/makesdna/DNA_hair_types.h
M	source/blender/makesrna/intern/CMakeLists.txt
M	source/blender/makesrna/intern/makesrna.c
A	source/blender/makesrna/intern/rna_hair.c
M	source/blender/makesrna/intern/rna_internal.h
M	source/blender/makesrna/intern/rna_modifier.c

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

diff --git a/release/scripts/startup/bl_ui/properties_data_modifier.py b/release/scripts/startup/bl_ui/properties_data_modifier.py
index 2097c46..e94fd11 100644
--- a/release/scripts/startup/bl_ui/properties_data_modifier.py
+++ b/release/scripts/startup/bl_ui/properties_data_modifier.py
@@ -1223,7 +1223,13 @@ class DATA_PT_modifiers(ModifierButtonsPanel, Panel):
         col.prop(md, "material_offset", text="Material Offset")
 
     def HAIR(self, layout, ob, md):
+        hsys = md.hair_system
+
         col = layout.column()
+        col.prop(hsys, "smooth")
+
+        col.separator()
+
         col.operator("hair.copy_from_particles")
 
 
diff --git a/source/blender/editors/physics/hair_ops.c b/source/blender/editors/physics/hair_ops.c
index 5a4ab07..db0dd85 100644
--- a/source/blender/editors/physics/hair_ops.c
+++ b/source/blender/editors/physics/hair_ops.c
@@ -128,6 +128,7 @@ static int hair_copy_from_particles_exec(bContext *C, wmOperator *UNUSED(op))
 	}
 	CTX_DATA_END;
 	
+	WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
 	return OPERATOR_FINISHED;
 }
 
diff --git a/source/blender/editors/space_view3d/drawhair.c b/source/blender/editors/space_view3d/drawhair.c
index 302fa51..3dd989d 100644
--- a/source/blender/editors/space_view3d/drawhair.c
+++ b/source/blender/editors/space_view3d/drawhair.c
@@ -48,17 +48,21 @@
 #include "BIF_gl.h"
 #include "BIF_glutil.h"
 
+#include "UI_resources.h"
+
 #include "HAIR_capi.h"
 
 /* ******** Hair Drawing ******** */
 
 /* TODO vertex/index buffers, etc. etc., avoid direct mode ... */
 
-static void draw_hair_curve(HairSystem *UNUSED(hsys), HairCurve *hair)
+static void draw_hair_curve(HairSystem *hsys, HairCurve *hair)
 {
 	HairPoint *point;
 	int k;
 	
+	glColor3f(0.4f, 0.7f, 1.0f);
+	
 	glBegin(GL_LINE_STRIP);
 	for (point = hair->points, k = 0; k < hair->totpoints; ++point, ++k) {
 		glVertex3fv(point->co);
@@ -66,22 +70,37 @@ static void draw_hair_curve(HairSystem *UNUSED(hsys), HairCurve *hair)
 	glEnd();
 	
 	/* smoothed curve */
-	{
+	if (hair->totpoints >= 2) {
 		struct SmoothingIteratorFloat3 *iter;
 		float smooth_co[3];
 		
 		glColor3f(0.5f, 1.0f, 0.1f);
 		
 		glBegin(GL_LINE_STRIP);
-		iter = HAIR_smoothing_iter_new(hair, 0.1f, 4.0f);
-		for (point = hair->points, k = 0; k < hair->totpoints; ++point, ++k) {
-			if (HAIR_smoothing_iter_valid(hair, iter)) {
-				HAIR_smoothing_iter_next(hair, iter, smooth_co);
-				glVertex3fv(smooth_co);
-			}
+		iter = HAIR_smoothing_iter_new(hair, 1.0f / hair->totpoints, hsys->smooth, smooth_co);
+		glVertex3fv(smooth_co);
+		while (HAIR_smoothing_iter_valid(hair, iter)) {
+			HAIR_smoothing_iter_next(hair, iter, smooth_co);
+			glVertex3fv(smooth_co);
+		}
+		HAIR_smoothing_iter_end(hair, iter, smooth_co);
+		glVertex3fv(smooth_co);
+		HAIR_smoothing_iter_free(iter);
+		glEnd();
+		
+		glPointSize(2.5f);
+		glBegin(GL_POINTS);
+		iter = HAIR_smoothing_iter_new(hair, 1.0f / hair->totpoints, hsys->smooth, smooth_co);
+		glVertex3fv(smooth_co);
+		while (HAIR_smoothing_iter_valid(hair, iter)) {
+			HAIR_smoothing_iter_next(hair, iter, smooth_co);
+			glVertex3fv(smooth_co);
 		}
+		HAIR_smoothing_iter_end(hair, iter, smooth_co);
+		glVertex3fv(smooth_co);
 		HAIR_smoothing_iter_free(iter);
 		glEnd();
+		glPointSize(1.0f);
 	}
 }
 
diff --git a/source/blender/hair/HAIR_capi.cpp b/source/blender/hair/HAIR_capi.cpp
index 4eb54e8..93a34b3 100644
--- a/source/blender/hair/HAIR_capi.cpp
+++ b/source/blender/hair/HAIR_capi.cpp
@@ -35,18 +35,26 @@ extern "C" {
 
 using namespace HAIR_NAMESPACE;
 
-struct SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(struct HairCurve *curve, float rest_length, float amount)
+struct SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(HairCurve *curve, float rest_length, float amount, float cval[3])
 {
 	SmoothingIterator<float3> *iter = new SmoothingIterator<float3>(rest_length, amount);
 	
 	if (curve->totpoints >= 2) {
 		float *co0 = curve->points[0].co;
 		float *co1 = curve->points[1].co;
-		iter->begin(float3(co0[0], co0[1], co0[2]), float3(co1[0], co1[1], co1[2]));
+		
+		float3 val = iter->begin(float3(co0[0], co0[1], co0[2]), float3(co1[0], co1[1], co1[2]));
+		cval[0] = val.x;
+		cval[1] = val.y;
+		cval[2] = val.z;
 	}
-	else {
-		iter->num = 1; /* XXX not nice, find a better way to indicate invalid iterator */
+	/* XXX setting iter->num is not nice, find a better way to invalidate the iterator */
+	else if (curve->totpoints >= 1) {
+		copy_v3_v3(cval, curve->points[0].co);
+		iter->num = 2; 
 	}
+	else
+		iter->num = 1;
 	
 	return (struct SmoothingIteratorFloat3 *)iter;
 }
@@ -58,18 +66,29 @@ void HAIR_smoothing_iter_free(struct SmoothingIteratorFloat3 *citer)
 	delete iter;
 }
 
-bool HAIR_smoothing_iter_valid(struct HairCurve *curve, struct SmoothingIteratorFloat3 *citer)
+bool HAIR_smoothing_iter_valid(HairCurve *curve, struct SmoothingIteratorFloat3 *citer)
 {
 	SmoothingIterator<float3> *iter = (SmoothingIterator<float3> *)citer;
 	
-	return iter->num < curve->totpoints - 1;
+	return iter->num < curve->totpoints;
 }
 
-void HAIR_smoothing_iter_next(struct HairCurve *curve, struct SmoothingIteratorFloat3 *citer, float cval[3])
+void HAIR_smoothing_iter_next(HairCurve *curve, struct SmoothingIteratorFloat3 *citer, float cval[3])
 {
 	SmoothingIterator<float3> *iter = (SmoothingIterator<float3> *)citer;
 	
-	float *co = curve->points[iter->num + 1].co;
+	float *co = curve->points[iter->num].co;
+	float3 val = iter->next(float3(co[0], co[1], co[2]));
+	cval[0] = val.x;
+	cval[1] = val.y;
+	cval[2] = val.z;
+}
+
+void HAIR_smoothing_iter_end(HairCurve *curve, struct SmoothingIteratorFloat3 *citer, float cval[3])
+{
+	SmoothingIterator<float3> *iter = (SmoothingIterator<float3> *)citer;
+	
+	float *co = curve->points[iter->num-1].co;
 	float3 val = iter->next(float3(co[0], co[1], co[2]));
 	cval[0] = val.x;
 	cval[1] = val.y;
diff --git a/source/blender/hair/HAIR_capi.h b/source/blender/hair/HAIR_capi.h
index 04c5279..779d22d 100644
--- a/source/blender/hair/HAIR_capi.h
+++ b/source/blender/hair/HAIR_capi.h
@@ -32,10 +32,11 @@ struct HairCurve;
 
 struct SmoothingIteratorFloat3;
 
-struct SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(struct HairCurve *curve, float rest_length, float amount);
+struct SmoothingIteratorFloat3 *HAIR_smoothing_iter_new(struct HairCurve *curve, float rest_length, float amount, float cval[3]);
 void HAIR_smoothing_iter_free(struct SmoothingIteratorFloat3 *iter);
 bool HAIR_smoothing_iter_valid(struct HairCurve *curve, struct SmoothingIteratorFloat3 *iter);
 void HAIR_smoothing_iter_next(struct HairCurve *curve, struct SmoothingIteratorFloat3 *iter, float val[3]);
+void HAIR_smoothing_iter_end(struct HairCurve *curve, struct SmoothingIteratorFloat3 *citer, float cval[3]);
 
 #ifdef __cplusplus
 }
diff --git a/source/blender/hair/intern/HAIR_smoothing.h b/source/blender/hair/intern/HAIR_smoothing.h
index 63aa781..8504c8d 100644
--- a/source/blender/hair/intern/HAIR_smoothing.h
+++ b/source/blender/hair/intern/HAIR_smoothing.h
@@ -50,47 +50,55 @@ struct StdArithmetic {
 };
 #endif
 
-//template <typename T, typename OperationsT = StdArithmetic<T> >
 template <typename T>
+struct NullDefaultCtor {
+	static const T null = T();
+};
+
+//template <typename T, typename OperationsT = StdArithmetic<T> >
+template <typename T, typename NullT = NullDefaultCtor<T> >
 struct SmoothingIterator {
 	SmoothingIterator(float rest_length, float amount) :
-	    beta(min_ff(1.0f, 1.0f - expf(- rest_length / amount))),
+	    beta(min_ff(1.0f, amount > 0.0f ? 1.0f - expf(- rest_length / amount) : 1.0f)),
 	    f1(2.0f*(1.0f-beta)),
-	    f2(-(1.0f-beta)*(1.0f-beta)),
+	    f2((1.0f-beta)*(1.0f-beta)),
 	    f3(beta*beta)
 	{
 	}
 	
-	void begin(const T &val0, const T &val1)
+	T begin(const T &val0, const T &val1)
 	{
-		dval_ppp = dval_pp = dval_p = val1 - val0;
+		dval_pp = dval_p = val1 - val0;
 		
 		res_p = val0;
-		res = val1;
 		
-		num = 1;
+		val = val1;
+		
+		num = 2;
+		
+		return val0;
 	}
 	
-	const T& next(const T &val_)
+	T next(const T &val_n)
 	{
-		dval_ppp = dval_pp;
+		T ndval_p = f1 * dval_p - f2 * dval_pp + f3 * (val_n - val);
+		T nres = res_p + dval_p;
+		
 		dval_pp = dval_p;
-		dval_p = f1 * dval_pp + f2 * dval_ppp + f3 * (val_ - val_p);
+		dval_p = ndval_p;
 		
-		T tmp = res_p;
-		res_p = res;
-		res = tmp + dval_p;
+		res_p = nres;
 		
-		val_p = val_;
+		val = val_n;
 		
 		++num;
 		
-		return res;
+		return nres;
 	}
 	
-	T val_p;
-	T res, res_p;
-	T dval_p, dval_pp, dval_ppp;
+	T val;
+	T res_p;
+	T dval_p, dval_pp;
 	int num;
 	const float beta, f1, f2, f3;
 };
diff --git a/source/blender/makesdna/DNA_hair_types.h b/source/blender/makesdna/DNA_hair_types.h
index b80e533..7312eda 100644
--- a/source/blender/makesdna/DNA_hair_types.h
+++ b/source/blender/makesdna/DNA_hair_types.h
@@ -47,7 +47,9 @@ typedef struct HairCurve {
 typedef struct HairSystem {
 	HairCurve *curves;          /* curve data */
 	int totcurves;              /* number of curves */
-	int pad;
+	
+	/* testing */
+	float smooth;
 } HairSystem;
 
 #endif
diff --git a/source/blender/makesrna/intern/CMakeLists.txt b/source/blender/makesrna/intern/CMakeLists.txt
index 6a0208a..e85aa94 100644
--- a/source/blender/makesrna/intern/CMakeLists.txt
+++ b/source/blender/makesrna/intern/CMakeLists.txt
@@ -52,6 +52,7 @@ set(DEFSRC
 	rna_fluidsim.c
 	rna_gpencil.c
 	rna_group.c
+	rna_hair.c
 	rna_image.c
 	rna_key.c
 	rna_lamp.c
diff --git a/source/blender/makesrna/intern/makesrna.c b/source/blender/makesrna/intern/makesrna.c
index 7f1f04c..92f68f9 100644
--- a/source/blender/makesrna/intern/makesrna.c
+++ b/source/blender/makesrna/intern/makesrna.c
@@ -3290,6 +3290,7 @@ static RNAProcessItem PROCESS_ITEMS[] = {
 	{"rna_movieclip.c", NULL, RNA_def_movieclip},
 	{"rna_tracking.c", NULL, RNA_def_tracking},
 	{"rna_mask.c", NULL, RNA_def_mask},
+    {"rna_hair.c", NULL, RNA_def_hair},
 	{NULL, NULL}
 };
 
diff --git a/source/blender/makesrna/intern/rna_hair.c b/source/blender/makesrna/intern/rna_hair.c
new file mode 100644
index 0000000..e5dfb63
--- /dev/null
+++ b/source/blender/makesrna/intern/rna_hair.c
@@ -0,0 +1,75 @@
+/*
+ * ***** BEGIN GPL LICENSE BLOCK *****
+ *
+ * This program is free software; you can redistribute it

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list