[Bf-blender-cvs] [b1e4964] fracture_modifier: added basic physics visualization for viewport rigidbodies like in game engine (its optional and can be turned off)

Martin Felke noreply at git.blender.org
Tue Feb 2 14:02:08 CET 2016


Commit: b1e4964edee66a5e63075ce961b090735082913c
Author: Martin Felke
Date:   Tue Feb 2 14:01:46 2016 +0100
Branches: fracture_modifier
https://developer.blender.org/rBb1e4964edee66a5e63075ce961b090735082913c

added basic physics visualization for viewport rigidbodies like in game engine (its optional and can be turned off)

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

M	intern/rigidbody/RBI_api.h
M	intern/rigidbody/rb_bullet_api.cpp
M	release/scripts/startup/bl_ui/properties_scene.py
M	source/blender/blenkernel/intern/fracture.c
M	source/blender/blenkernel/intern/rigidbody.c
M	source/blender/editors/object/object_modifier.c
M	source/blender/editors/space_view3d/drawobject.c
M	source/blender/editors/space_view3d/view3d_draw.c
M	source/blender/makesdna/DNA_rigidbody_types.h
M	source/blender/makesrna/intern/rna_rigidbody.c

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

diff --git a/intern/rigidbody/RBI_api.h b/intern/rigidbody/RBI_api.h
index f4c2706..2acc792 100644
--- a/intern/rigidbody/RBI_api.h
+++ b/intern/rigidbody/RBI_api.h
@@ -111,6 +111,8 @@ void RB_dworld_set_split_impulse(rbDynamicsWorld *world, int split_impulse);
 /* Step the simulation by the desired amount (in seconds) with extra controls on substep sizes and maximum substeps */
 void RB_dworld_step_simulation(rbDynamicsWorld *world, float timeStep, int maxSubSteps, float timeSubStep);
 
+void RB_dworld_debug_draw(rbDynamicsWorld *world);
+
 /* Export -------------------------- */
 
 /* Exports the dynamics world to physics simulator's serialisation format */
diff --git a/intern/rigidbody/rb_bullet_api.cpp b/intern/rigidbody/rb_bullet_api.cpp
index 4786ba6..00252b8 100644
--- a/intern/rigidbody/rb_bullet_api.cpp
+++ b/intern/rigidbody/rb_bullet_api.cpp
@@ -77,6 +77,59 @@ subject to the following restrictions:
 #include "BulletDynamics/Dynamics/btFractureBody.h"
 #include "BulletDynamics/Dynamics/btFractureDynamicsWorld.h"
 
+#include "../glew-mx/glew-mx.h"
+
+
+struct	ViewportDebugDraw : public btIDebugDraw
+{
+	ViewportDebugDraw () :
+		m_debugMode(0)
+	{
+	}
+
+	int m_debugMode;
+
+	virtual void	drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
+	{
+		if (m_debugMode >0)
+		{
+			//draw lines
+			glBegin(GL_LINES);
+				glColor4f(color[0], color[1], color[2], 1.0f);
+				glVertex3fv(from);
+				glVertex3fv(to);
+			glEnd();
+		}
+	}
+
+	virtual void	reportErrorWarning(const char* warningString)
+	{
+
+	}
+
+	virtual void	drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,float distance,int lifeTime,const btVector3& color)
+	{
+		drawLine(PointOnB, PointOnB + normalOnB, color);
+		drawSphere(PointOnB, 0.1, color);
+	}
+
+	virtual void	setDebugMode(int debugMode)
+	{
+		m_debugMode = debugMode;
+	}
+	virtual int		getDebugMode() const
+	{
+		return m_debugMode;
+	}
+	///todo: find out if Blender can do this
+	virtual void	draw3dText(const btVector3& location,const char* textString)
+	{
+
+	}
+
+};
+
+
 struct rbRigidBody {
 	btRigidBody *body;
 	int col_groups;
@@ -458,6 +511,14 @@ rbDynamicsWorld *RB_dworld_new(const float gravity[3], void* blenderWorld, void*
 
 	RB_dworld_set_gravity(world, gravity);
 
+
+	/*debug drawer*/
+	world->dynamicsWorld->setDebugDrawer(new ViewportDebugDraw());
+	world->dynamicsWorld->getDebugDrawer()->setDebugMode(
+	            btIDebugDraw::DBG_DrawWireframe|btIDebugDraw::DBG_DrawAabb|
+	            btIDebugDraw::DBG_DrawContactPoints|btIDebugDraw::DBG_DrawText|
+	            btIDebugDraw::DBG_DrawConstraints/*|btIDebugDraw::DBG_DrawConstraintLimits*/);
+
 	/*contact callback */
 /*
 	if (contactCallback)
@@ -516,6 +577,11 @@ void RB_dworld_step_simulation(rbDynamicsWorld *world, float timeStep, int maxSu
 	world->dynamicsWorld->stepSimulation(timeStep, maxSubSteps, timeSubStep);
 }
 
+void RB_dworld_debug_draw(rbDynamicsWorld *world)
+{
+	world->dynamicsWorld->debugDrawWorld();
+}
+
 /* Export -------------------------- */
 
 /**
diff --git a/release/scripts/startup/bl_ui/properties_scene.py b/release/scripts/startup/bl_ui/properties_scene.py
index bbf11ab..6d9b1e4 100644
--- a/release/scripts/startup/bl_ui/properties_scene.py
+++ b/release/scripts/startup/bl_ui/properties_scene.py
@@ -343,6 +343,8 @@ class SCENE_PT_rigid_body_world(SceneButtonsPanel, Panel):
             col.prop(rbw, "steps_per_second", text="Steps Per Second")
             col.prop(rbw, "solver_iterations", text="Solver Iterations")
 
+            layout.prop(rbw, "visualize_physics")
+
 
 class SCENE_PT_rigid_body_cache(SceneButtonsPanel, Panel):
     bl_label = "Rigid Body Cache"
diff --git a/source/blender/blenkernel/intern/fracture.c b/source/blender/blenkernel/intern/fracture.c
index 83809b1..c99a66c 100644
--- a/source/blender/blenkernel/intern/fracture.c
+++ b/source/blender/blenkernel/intern/fracture.c
@@ -2197,7 +2197,7 @@ static Shard* fracture_object_to_shard( Object *own, Object* target)
 	MPoly* mpoly;
 	MLoop* mloop;
 	SpaceTransform trans;
-	float mat[4][4], size[3]; //, inv_size[3];
+	float mat[4][4], size[3];
 
 	int totvert, totpoly, totloop, v;
 	bool do_free = false;
@@ -2214,8 +2214,6 @@ static Shard* fracture_object_to_shard( Object *own, Object* target)
 	//BLI_SPACE_TRANSFORM_SETUP(&trans, target, own);
 	mat4_to_size(size, target->obmat);
 
-	//ABUSE raw_centroid here for size, its not used in this mode !!!;
-
 	mvert = dm->getVertArray(dm);
 	mpoly = dm->getPolyArray(dm);
 	mloop = dm->getLoopArray(dm);
@@ -2226,21 +2224,24 @@ static Shard* fracture_object_to_shard( Object *own, Object* target)
 	// create temp shard -> that necessary at all ?
 	s = BKE_create_fracture_shard(mvert, mpoly, mloop, totvert, totpoly, totloop, true);
 
-#if 0
-	//ABUSE raw_centroid here for size, its not used in this mode !!!;
-	//needed to compensate scaling (smaller physicsmesh, but original mesh size or vice versa, lets test)
-	inv_size[0] = 1.0f / size[0];
-	inv_size[1] = 1.0f / size[1];
-	inv_size[2] = 1.0f / size[2];
-#endif
+	//use this as size holder, and rawcentroid is the old ob location
+	copy_v3_v3(s->impact_size, size);
 
-	copy_v3_v3(s->raw_centroid, size);
+	//compare centroid in worldspace with location
+	mul_v3_m4v3(s->raw_centroid, target->obmat, s->centroid);
 
 	for (v = 0, mv = s->mvert; v < s->totvert; v++, mv++)
 	{
+		mul_v3_v3(mv->co, size);
+
+		//shrink the shard ? (and take centroid diff into account here, too)
 		BLI_space_transform_apply(&trans, mv->co);
+
+		//add_v3_v3(mv->co, target->loc);
+		//sub_v3_v3(mv->co, s->raw_centroid);
 	}
 
+	//BLI_space_transform_apply(&trans, s->raw_centroid);
 	BLI_space_transform_apply(&trans, s->centroid);
 
 	s = BKE_custom_data_to_shard(s, dm);
@@ -2392,18 +2393,21 @@ int BKE_fracture_update_visual_mesh(FractureModifierData *fmd, Object *ob, bool
 	//update existing island's vert refs, if any...should have used indexes instead :S
 	for (mi = fmd->meshIslands.first; mi; mi = mi->next)
 	{
-		MVert *pvert = mi->physics_mesh->getVertArray(mi->physics_mesh);
+		//MVert *pvert = mi->physics_mesh->getVertArray(mi->physics_mesh);
+		float inv_size[3] = {1.0f, 1.0f, 1.0f};
 		Shard *s = BLI_findlink(&fmd->frac_mesh->shard_map, mi->id);
+		if (!s)
+			continue;
+
+		inv_size[0] = 1.0f / s->impact_size[0];
+		inv_size[1] = 1.0f / s->impact_size[1];
+		inv_size[2] = 1.0f / s->impact_size[2];
 
 		for (i = 0; i < mi->vertex_count; i++)
 		{
 			//just update pointers, dont need to reallocate something
 			MVert *v = NULL;
 			int index;
-			float mat[4][4], imat[4][4];
-			float size[3] = {1.0f, 1.0f, 1.0f};
-			float loc[3] = {0.0f, 0.0f, 0.0f};
-			MVert *pv = NULL;
 
 			//also correct indexes
 			if (mi->vertex_indices[i] >= totvert)
@@ -2417,26 +2421,26 @@ int BKE_fracture_update_visual_mesh(FractureModifierData *fmd, Object *ob, bool
 			v = mv + index;
 			mi->vertices_cached[i] = v;
 
-#if 0
-			//transform vertex properly ?
-			if (s)
-				loc_quat_size_to_mat4(mat, loc , mi->rot, s->raw_centroid);
-			else //fallback, so no crash, but visual difference to get notified of problem
-				loc_quat_size_to_mat4(mat, loc , mi->rot, size);
+			//transform vertex properly ? compensate for shrunken shard ?
+			//sub_v3_v3v3(loc, mi->centroid, s->raw_centroid);
+			//loc_quat_size_to_mat4(mat, loc , rot, s->impact_size);
 
-			invert_m4_m4(imat, mat);
+			//invert_m4_m4(imat, mat);
+#if 0
 			pv = pvert + i;
 			mul_m4_v3(imat, pv->co);
+#endif
 
-			loc_quat_size_to_mat4(mat, loc , mi->rot, size);
-			invert_m4_m4(imat, mat);
+			//eliminate shrink but take also difference in centroids into account here
+			//sub_v3_v3(v->co, s->centroid);
+			//mul_m4_v3(imat, v->co);
+			//add_v3_v3(v->co, s->centroid);
 
-			sub_v3_v3(v->co, mi->centroid);
-			mul_m4_v3(imat, v->co);
-			add_v3_v3(v->co, mi->centroid);
+//			sub_v3_v3(v->co, s->centroid);
+//			mul_v3_v3(v->co, inv_size);
+//			add_v3_v3(v->co, s->centroid);
 
 			//printf("%d %d\n", index, dm->getNumVerts(dm));
-#endif
 
 			//hrm perhaps we need to update rest coordinates, too...
 			mi->vertco[3*i] = v->co[0];
@@ -2447,12 +2451,6 @@ int BKE_fracture_update_visual_mesh(FractureModifierData *fmd, Object *ob, bool
 			mi->vertno[3*i+1] = v->no[1];
 			mi->vertno[3*i+2] = v->no[2];
 
-#if 0
-			sub_v3_v3(v->co, mi->centroid);
-			mul_m4_v3(mat, v->co);
-			add_v3_v3(v->co, mi->centroid);
-#endif
-
 			if (ob && dvert)
 			{
 				int l;
@@ -2569,6 +2567,7 @@ MeshIsland* BKE_fracture_mesh_island_add(FractureModifierData *fmd, Object* own,
 	Shard *s;
 	int vertstart = 0;
 	short totcol = 0, totdef = 0;
+	float loc[3], quat[4];
 
 	if (fmd->fracture_mode != MOD_FRACTURE_EXTERNAL || own->type != OB_MESH || !own->data)
 		return NULL;
@@ -2593,13 +2592,15 @@ MeshIsland* BKE_fracture_mesh_island_add(FractureModifierData *fmd, Object* own,
 	mi = fracture_shard_to_island(fmd, s, vertstart);
 
 	//lets see whether we need to add loc here too XXX TODO
+	mat4_to_loc_quat(loc, quat, target->obmat);
+
+	copy_qt_qt(mi->rot, quat);
+	copy_v3_v3(mi->centroid, loc);
 
 	mi->rigidbody = BKE_rigidbody_create_shard(fmd->modifier.scene, own, target, mi);
 	if (mi->rigidbody)
 	{
 		mi->rigidbody->meshisland_index = mi->id;
-		copy_qt_qt(mi->rot, mi->rigidbody->orn);
-		copy_v3_v3(mi->centroid, mi->rigidbody->pos);
 	}
 
 	BLI_strncpy(mi->name, target->id.name + 2, MAX_ID_NAME - 2);
diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index f0c9ae2..dcb1c1e 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -142,7 +142,7 @@ void BKE_rigidbody_start_dist_angle(RigidBodyShardCon *con, bool exact)
 	float dist, angle;
 	calc_dist_angle(con, &dist, &angle, exact);
 
-//	printf("Start Values(dist, angle) %f %f %f %f\n", con->start_dist, con->start_angle, dist, angle);
+	//printf("Start Values(dist, angle) %f %f %f %f\n", con->start_dist, con->start_angle, dist, angle);
 	con->start_dist = dist;
 	con->start_angle = angle;
 }
@@ -2272,7 +2272,7 @@ RigidBodyOb *BKE_rigidbody_create_shard(Scene *scene, Object *ob, Object *target
 	if (target && target->rigidbody_object)
 	{
 		rbo = BKE_rigidbody_copy_obje

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list