[Bf-blender-cvs] [8dd28ee] hair_system: Set a flag in the hair modifier whenever the hair data in the solver becomes invalid.

Lukas Tönne noreply at git.blender.org
Wed Jul 30 18:03:01 CEST 2014


Commit: 8dd28ee32c4acfbdc59ee40c42a0b679bb363ce2
Author: Lukas Tönne
Date:   Wed Jul 30 18:01:29 2014 +0200
Branches: hair_system
https://developer.blender.org/rB8dd28ee32c4acfbdc59ee40c42a0b679bb363ce2

Set a flag in the hair modifier whenever the hair data in the solver
becomes invalid.

This is needed when the number of hairs or points changes, but also
in case on sudden positional changes etc. (like resetting to rest
position), because the solver would otherwise calculate a large delta
and generate a lot of fake energy.

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

M	source/blender/editors/physics/hair_ops.c
M	source/blender/hair/HAIR_capi.cpp
M	source/blender/hair/HAIR_capi.h
M	source/blender/hair/intern/HAIR_solver.cpp
M	source/blender/hair/intern/HAIR_solver.h
M	source/blender/makesdna/DNA_modifier_types.h
M	source/blender/modifiers/intern/MOD_hair.c

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

diff --git a/source/blender/editors/physics/hair_ops.c b/source/blender/editors/physics/hair_ops.c
index 19103c5..f52d56c 100644
--- a/source/blender/editors/physics/hair_ops.c
+++ b/source/blender/editors/physics/hair_ops.c
@@ -61,7 +61,7 @@
 
 #include "physics_intern.h" // own include
 
-static bool ED_hair_get(bContext *C, Object **r_ob, HairSystem **r_hsys)
+static bool ED_hair_get(bContext *C, Object **r_ob, HairSystem **r_hsys, HairModifierData **r_hmd)
 {
 	Object *ob;
 	HairModifierData *hmd;
@@ -76,12 +76,13 @@ static bool ED_hair_get(bContext *C, Object **r_ob, HairSystem **r_hsys)
 	
 	if (r_ob) *r_ob = ob;
 	if (r_hsys) *r_hsys = hmd->hairsys;
+	if (r_hmd) *r_hmd = hmd;
 	return true;
 }
 
 static int ED_hair_active_poll(bContext *C)
 {
-	return ED_hair_get(C, NULL, NULL);
+	return ED_hair_get(C, NULL, NULL, NULL);
 }
 
 /************************ reset hair to rest position *********************/
@@ -90,11 +91,13 @@ static int hair_reset_to_rest_location_exec(bContext *C, wmOperator *UNUSED(op))
 {
 	Object *ob;
 	HairSystem *hsys;
+	HairModifierData *hmd;
 	int i, k;
-	ED_hair_get(C, &ob, &hsys);
+	ED_hair_get(C, &ob, &hsys, &hmd);
 	
 	for (i = 0; i < hsys->totcurves; ++i) {
 		HairCurve *hair = &hsys->curves[i];
+		
 		for (k = 0; k < hair->totpoints; ++k) {
 			HairPoint *point = &hair->points[k];
 			
@@ -103,6 +106,8 @@ static int hair_reset_to_rest_location_exec(bContext *C, wmOperator *UNUSED(op))
 		}
 	}
 	
+	hmd->flag &= ~MOD_HAIR_SOLVER_DATA_VALID;
+	
 	WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
 	return OPERATOR_FINISHED;
 }
@@ -224,7 +229,8 @@ static int hair_copy_from_particles_exec(bContext *C, wmOperator *op)
 	Object *ob;
 	ParticleSystem *psys;
 	HairSystem *hsys;
-	ED_hair_get(C, &ob, &hsys);
+	HairModifierData *hmd;
+	ED_hair_get(C, &ob, &hsys, &hmd);
 	
 	for (psys = ob->particlesystem.first; psys; psys = psys->next) {
 		ParticleSystemModifierData *psmd;
@@ -238,9 +244,14 @@ static int hair_copy_from_particles_exec(bContext *C, wmOperator *op)
 		}
 		
 		psmd = psys_get_modifier(ob, psys);
-		BLI_assert(psmd != NULL);
+		if (!psmd || !psmd->dm) {
+			BKE_reportf(op->reports, RPT_ERROR, "Skipping particle system %s: Invalid data", psys->name);
+			continue;
+		}
 		
 		hair_copy_from_particles_psys(ob, hsys, psys, psmd->dm);
+		
+		hmd->flag &= ~MOD_HAIR_SOLVER_DATA_VALID;
 	}
 	
 	WM_event_add_notifier(C, NC_OBJECT|ND_DRAW, ob);
diff --git a/source/blender/hair/HAIR_capi.cpp b/source/blender/hair/HAIR_capi.cpp
index eb8a283..edbd935 100644
--- a/source/blender/hair/HAIR_capi.cpp
+++ b/source/blender/hair/HAIR_capi.cpp
@@ -43,9 +43,9 @@ extern "C" {
 
 using namespace HAIR_NAMESPACE;
 
-struct HAIR_Solver *HAIR_solver_new(const struct HairParams *params)
+struct HAIR_Solver *HAIR_solver_new(void)
 {
-	Solver *solver = new Solver(*params);
+	Solver *solver = new Solver();
 	
 	return (HAIR_Solver *)solver;
 }
@@ -57,7 +57,14 @@ void HAIR_solver_free(struct HAIR_Solver *csolver)
 	delete solver;
 }
 
-void HAIR_solver_init(struct HAIR_Solver *csolver, Scene *scene, Object *ob, DerivedMesh *dm, HairSystem *hsys, float time)
+void HAIR_solver_set_params(struct HAIR_Solver *csolver, const struct HairParams *params)
+{
+	Solver *solver = (Solver *)csolver;
+	
+	solver->params(*params);
+}
+
+void HAIR_solver_build_data(struct HAIR_Solver *csolver, Scene *scene, Object *ob, DerivedMesh *dm, HairSystem *hsys, float time)
 {
 	Solver *solver = (Solver *)csolver;
 	
diff --git a/source/blender/hair/HAIR_capi.h b/source/blender/hair/HAIR_capi.h
index f9a915e..6fc7dce 100644
--- a/source/blender/hair/HAIR_capi.h
+++ b/source/blender/hair/HAIR_capi.h
@@ -38,9 +38,10 @@ struct HAIR_Solver;
 struct HAIR_SmoothingIteratorFloat3;
 struct HAIR_FrameIterator;
 
-struct HAIR_Solver *HAIR_solver_new(const struct HairParams *params);
+struct HAIR_Solver *HAIR_solver_new(void);
 void HAIR_solver_free(struct HAIR_Solver *solver);
-void HAIR_solver_init(struct HAIR_Solver *solver, struct Scene *scene, struct Object *ob, struct DerivedMesh *dm, struct HairSystem *hsys, float time);
+void HAIR_solver_set_params(struct HAIR_Solver *solver, const struct HairParams *params);
+void HAIR_solver_build_data(struct HAIR_Solver *solver, struct Scene *scene, struct Object *ob, struct DerivedMesh *dm, struct HairSystem *hsys, float time);
 void HAIR_solver_update_externals(struct HAIR_Solver *solver, struct Scene *scene, struct Object *ob, struct DerivedMesh *dm, struct HairSystem *hsys, float time);
 void HAIR_solver_step(struct HAIR_Solver *solver, float time, float timestep);
 void HAIR_solver_apply(struct HAIR_Solver *solver, struct Scene *scene, struct Object *ob, struct HairSystem *hsys);
diff --git a/source/blender/hair/intern/HAIR_solver.cpp b/source/blender/hair/intern/HAIR_solver.cpp
index f56bfa7..57fa06b 100644
--- a/source/blender/hair/intern/HAIR_solver.cpp
+++ b/source/blender/hair/intern/HAIR_solver.cpp
@@ -109,8 +109,7 @@ SolverForces::SolverForces()
 }
 
 
-Solver::Solver(const HairParams &params) :
-    m_params(params),
+Solver::Solver() :
     m_data(NULL)
 {
 }
@@ -253,6 +252,9 @@ void Solver::step(float time, float timestep)
 			
 			prev_stretch = stretch;
 			prev_bend = bend;
+			
+			if (len_v3(point->next.co) > 100.0f)
+				printf("badbadbad\n");
 		}
 	}
 	
diff --git a/source/blender/hair/intern/HAIR_solver.h b/source/blender/hair/intern/HAIR_solver.h
index f4d444a..282b859 100644
--- a/source/blender/hair/intern/HAIR_solver.h
+++ b/source/blender/hair/intern/HAIR_solver.h
@@ -63,9 +63,10 @@ struct SolverForces {
 class Solver
 {
 public:
-	Solver(const HairParams &params);
+	Solver();
 	~Solver();
 	
+	void params(const HairParams &params) { m_params = params; }
 	const HairParams &params() const { return m_params; }
 	
 	SolverForces &forces() { return m_forces; }
diff --git a/source/blender/makesdna/DNA_modifier_types.h b/source/blender/makesdna/DNA_modifier_types.h
index 5a8f772..6771b22 100644
--- a/source/blender/makesdna/DNA_modifier_types.h
+++ b/source/blender/makesdna/DNA_modifier_types.h
@@ -1374,7 +1374,14 @@ typedef struct HairModifierData {
 	struct HAIR_Solver *solver;     /* runtime instance */
 	float prev_cfra;
 	int steps_per_second;
+	
+	int flag;
+	int pad;
 } HairModifierData;
 
+enum {
+	MOD_HAIR_SOLVER_DATA_VALID  = (1 << 0),
+};
+
 
 #endif  /* __DNA_MODIFIER_TYPES_H__ */
diff --git a/source/blender/modifiers/intern/MOD_hair.c b/source/blender/modifiers/intern/MOD_hair.c
index fc60b13..ce04401 100644
--- a/source/blender/modifiers/intern/MOD_hair.c
+++ b/source/blender/modifiers/intern/MOD_hair.c
@@ -101,8 +101,15 @@ static DerivedMesh *applyModifier(ModifierData *md, Object *ob,
 		float time = floorf(steps) * dt;
 		
 		if (!hmd->solver) {
-			hmd->solver = HAIR_solver_new(&hsys->params);
-			HAIR_solver_init(hmd->solver, scene, ob, dm, hsys, time);
+			hmd->solver = HAIR_solver_new();
+			hmd->flag &= ~MOD_HAIR_SOLVER_DATA_VALID;
+		}
+		
+		HAIR_solver_set_params(hmd->solver, &hsys->params);
+		
+		if (!hmd->flag & MOD_HAIR_SOLVER_DATA_VALID) {
+			HAIR_solver_build_data(hmd->solver, scene, ob, dm, hsys, time);
+			hmd->flag |= MOD_HAIR_SOLVER_DATA_VALID;
 		}
 		
 		HAIR_solver_update_externals(hmd->solver, scene, ob, dm, hsys, time);




More information about the Bf-blender-cvs mailing list