[Bf-blender-cvs] [7723dc5485e] temp-fracture-modifier-2.8: got physics visualization operational again

Martin Felke noreply at git.blender.org
Fri Nov 30 17:41:28 CET 2018


Commit: 7723dc5485ebe6182e6a84fb1098d82ddb23fc6c
Author: Martin Felke
Date:   Fri Nov 30 17:40:53 2018 +0100
Branches: temp-fracture-modifier-2.8
https://developer.blender.org/rB7723dc5485ebe6182e6a84fb1098d82ddb23fc6c

got physics visualization operational again

kudos to hypersomniac for pointing me to the debug draw api

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

M	intern/rigidbody/RBI_api.h
M	intern/rigidbody/rb_bullet_api.cpp
M	source/blender/blenkernel/BKE_rigidbody.h
M	source/blender/blenkernel/intern/rigidbody.c
M	source/blender/draw/intern/draw_manager.c

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

diff --git a/intern/rigidbody/RBI_api.h b/intern/rigidbody/RBI_api.h
index c75821dfd83..d497f8d6216 100644
--- a/intern/rigidbody/RBI_api.h
+++ b/intern/rigidbody/RBI_api.h
@@ -82,6 +82,9 @@ typedef struct rbContactPoint {
 //something with OB_DRAWNAME, complicated to set up...
 typedef void (*draw_string)(float loc[3], const char *str, const size_t len, float color[3]);
 
+
+typedef void (*draw_line)(const float v1[3], const float v2[3], const float color[4]);
+
 /* ********************************** */
 /* Dynamics World Methods */
 
@@ -112,7 +115,7 @@ 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, draw_string str_callback);
+void RB_dworld_debug_draw(rbDynamicsWorld *world, draw_string str_callback, draw_line line_callback);
 
 /* Export -------------------------- */
 
diff --git a/intern/rigidbody/rb_bullet_api.cpp b/intern/rigidbody/rb_bullet_api.cpp
index 898d7dbfec0..fd4acbc7fa5 100644
--- a/intern/rigidbody/rb_bullet_api.cpp
+++ b/intern/rigidbody/rb_bullet_api.cpp
@@ -79,6 +79,8 @@ subject to the following restrictions:
 
 #include "../../extern/glew/include/GL/glew.h"
 
+static inline void copy_v3_btvec3(float vec[3], const btVector3 &btvec);
+
 typedef struct rbConstraint
 {
 	btTypedConstraint *con;
@@ -98,38 +100,24 @@ struct	ViewportDebugDraw : public btIDebugDraw
 		DBG_DrawImpulses = 1 << 15,
 	};
 
-//taken from internet, lol
-	struct _LINE {
-		btVector3 from;
-		btVector3 to;
-
-		_LINE(btVector3 f, btVector3 t) {
-			from = f;
-			to = t;
-		}
-	};
-
-	std::vector<_LINE> LINES;
-
-	struct _COLOR {
-		btVector3 col;
-
-		_COLOR(btVector3 c) {
-			col = c;
-		}
-	};
-
-	std::vector<_COLOR> COLORS;
-
-	GLuint vao, vbo[2];
-
-
 	int m_debugMode;
+	//void (DRW_debug_line_v3v3(const float v1[3], const float v2[3], const float color[4])
+	draw_line m_lineCallback;
+
 
 	virtual void	drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
 	{
 		if (m_debugMode >0)
 		{
+			if (m_lineCallback)
+			{
+				float v1[3], v2[3], col[4];
+				copy_v3_btvec3(v1, from);
+				copy_v3_btvec3(v2, to);
+				copy_v3_btvec3(col, color);
+				col[3] = 1.0f; //fully opaque;
+				m_lineCallback(v1, v2, col); // this is DRW_debug_line_v3v3
+			}
 #if 0
 			//draw lines, TODO, crashes on newer OpenGL ? hmmm
 			glBegin(GL_LINES);
@@ -138,8 +126,6 @@ struct	ViewportDebugDraw : public btIDebugDraw
 				glVertex3fv(to);
 			glEnd();
 #endif
-			LINES.push_back(_LINE(from, to));
-			COLORS.push_back(_COLOR(color));
 		}
 	}
 
@@ -168,62 +154,10 @@ struct	ViewportDebugDraw : public btIDebugDraw
 
 	}
 
-	void do_drawing() {
-
-#if 0
-		// Debug drawing
-		///////////////////////////
-		vector<GLfloat> vertices;
-		vector<GLuint> indices;
-		unsigned int indexI = 0;
-
-		for (vector<ViewportDebugDraw::_LINE>::iterator it = LINES.begin(); it != LINES.end(); it++)
-		{
-			ViewportDebugDraw::_LINE l = *it;
-
-			vertices.push_back(l.from.x);
-			vertices.push_back(l.from.y);
-			vertices.push_back(l.from.z);
-
-			vertices.push_back(l.to.x);
-			vertices.push_back(l.to.y);
-			vertices.push_back(l.to.z);
-
-			indices.push_back(indexI);
-			indices.push_back(indexI + 1);
-			indexI += 2;
-		}
-#endif
-		//glDrawElements(GL_LINES, indices.size(), GL_UNSIGNED_INT, (void*)&indices[0]);
-
-
-		glGenVertexArrays(1, &vao);
-		glBindVertexArray(vao);
-
-		glGenBuffers(2, vbo);
-		glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
-		glBufferData(GL_ARRAY_BUFFER, LINES.size() * sizeof(_LINE), &LINES[0], GL_STATIC_DRAW);
-		glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);
-		glEnableVertexAttribArray(0);
-
-		glBindBuffer(GL_ARRAY_BUFFER, vbo[1]);
-		glBufferData(GL_ARRAY_BUFFER, COLORS.size() * sizeof(_COLOR), &COLORS[0], GL_STATIC_DRAW);
-		glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0);
-		glEnableVertexAttribArray(1);
-
-		glDrawArrays(GL_LINES, 0, LINES.size() * 2);
-
-		LINES.clear();
-		COLORS.clear();
-
-		glBindVertexArray(0);
-		glBindBuffer(GL_ARRAY_BUFFER, 0);
-
-
-
-		///////////////////////////
+	void setDrawCallback(draw_line line_callback)
+	{
+		m_lineCallback = line_callback;
 	}
-
 };
 
 
@@ -256,7 +190,7 @@ class CallbackDynamicsWorld : public btDiscreteDynamicsWorld
 		void* m_bscene;
 
 		virtual void debugDrawConstraints(rbConstraint *con, draw_string str_callback, float loc[]);
-		virtual void debugDrawWorld(draw_string str_callback);
+		virtual void debugDrawWorld(draw_string str_callback, draw_line line_callback);
 
 };
 
@@ -310,10 +244,13 @@ rbContactPoint* CallbackDynamicsWorld::make_contact_point(btManifoldPoint& point
 	return cp;
 }
 
-void CallbackDynamicsWorld::debugDrawWorld(draw_string str_callback)
+void CallbackDynamicsWorld::debugDrawWorld(draw_string str_callback, draw_line line_callback)
 {
 	BT_PROFILE("debugDrawWorld");
 
+	ViewportDebugDraw *drawer = (ViewportDebugDraw*)getDebugDrawer();
+	drawer->setDrawCallback(line_callback);
+
 	btCollisionWorld::debugDrawWorld();
 
 	bool drawConstraints = false;
@@ -363,9 +300,6 @@ void CallbackDynamicsWorld::debugDrawWorld(draw_string str_callback)
 			}
 		}
 	}
-
-	ViewportDebugDraw *drawer = (ViewportDebugDraw*)getDebugDrawer();
-	drawer->do_drawing();
 }
 
 static const char* val_to_str(rbConstraint* con, int precision, int *length)
@@ -923,9 +857,9 @@ void RB_dworld_step_simulation(rbDynamicsWorld *world, float timeStep, int maxSu
 	world->dynamicsWorld->stepSimulation(timeStep, maxSubSteps, timeSubStep);
 }
 
-void RB_dworld_debug_draw(rbDynamicsWorld *world, draw_string str_callback)
+void RB_dworld_debug_draw(rbDynamicsWorld *world, draw_string str_callback, draw_line line_callback)
 {
-	world->dynamicsWorld->debugDrawWorld(str_callback);
+	world->dynamicsWorld->debugDrawWorld(str_callback, line_callback);
 }
 
 /* Export -------------------------- */
diff --git a/source/blender/blenkernel/BKE_rigidbody.h b/source/blender/blenkernel/BKE_rigidbody.h
index 947aadd6fb9..8c053456004 100644
--- a/source/blender/blenkernel/BKE_rigidbody.h
+++ b/source/blender/blenkernel/BKE_rigidbody.h
@@ -182,4 +182,7 @@ bool BKE_restoreKinematic(struct RigidBodyWorld *rbw, bool override_bind);
 void BKE_rigidbody_update_simulation(struct Scene *scene, struct RigidBodyWorld *rbw, bool rebuild,
                                      struct Depsgraph *depsgraph);
 
+void BKE_rigidbody_physics_visualize(struct RigidBodyWorld *rbw);
+
+
 #endif /* __BKE_RIGIDBODY_H__ */
diff --git a/source/blender/blenkernel/intern/rigidbody.c b/source/blender/blenkernel/intern/rigidbody.c
index 1ff5ed3c671..e86b526ab14 100644
--- a/source/blender/blenkernel/intern/rigidbody.c
+++ b/source/blender/blenkernel/intern/rigidbody.c
@@ -85,6 +85,8 @@
 #include "DEG_depsgraph_query.h"
 #include "DEG_depsgraph_build.h"
 
+#include "../draw/intern/draw_debug.h"// for DRW_debug_line_v3v3/ bullet physics visualization
+
 /* ************************************** */
 /* Memory Management */
 
@@ -103,6 +105,13 @@ static void RB_constraint_delete(void *UNUSED(con)) {}
 
 #endif
 
+void BKE_rigidbody_physics_visualize(RigidBodyWorld *rbw) {
+	/* visualize physics if wanted */
+	if (rbw && rbw->shared->physics_world && (rbw->flag & RBW_FLAG_VISUALIZE_PHYSICS)) {
+		RB_dworld_debug_draw(rbw->shared->physics_world, NULL, DRW_debug_line_v3v3);
+	}
+}
+
 /* Free rigidbody world */
 void BKE_rigidbody_free_world(Scene *scene)
 {
@@ -2198,11 +2207,6 @@ void BKE_rigidbody_eval_simulation(Depsgraph *depsgraph,
 
 	rbw = scene->rigidbody_world;
 	BKE_rigidbody_do_simulation(depsgraph, scene, ctime);
-
-	/* visualize physics if wanted */
-	if (rbw && rbw->shared->physics_world && (rbw->flag & RBW_FLAG_VISUALIZE_PHYSICS)) {
-		RB_dworld_debug_draw(rbw->shared->physics_world, NULL);
-	}
 }
 
 void BKE_rigidbody_object_sync_transforms(Depsgraph *depsgraph,
diff --git a/source/blender/draw/intern/draw_manager.c b/source/blender/draw/intern/draw_manager.c
index 8846b6f1dc3..4266e232bb9 100644
--- a/source/blender/draw/intern/draw_manager.c
+++ b/source/blender/draw/intern/draw_manager.c
@@ -38,6 +38,7 @@
 #include "BKE_object.h"
 #include "BKE_particle.h"
 #include "BKE_pointcache.h"
+#include "BKE_rigidbody.h"
 #include "BKE_workspace.h"
 
 #include "draw_manager.h"
@@ -1494,6 +1495,8 @@ void DRW_draw_render_loop_ex(
 
 	DRW_state_reset();
 
+	BKE_rigidbody_physics_visualize(scene->rigidbody_world);
+
 	drw_debug_draw();
 
 	glDisable(GL_DEPTH_TEST);



More information about the Bf-blender-cvs mailing list