Browse Source

First pass at DebugRenderer, critical for integrating

Lucien Greathouse 1 year ago
parent
commit
45b0f815ca
5 changed files with 100 additions and 4 deletions
  1. 14 3
      HelloWorld/main.cpp
  2. 16 1
      JoltC/Enums.h
  3. 36 0
      JoltC/Functions.h
  4. 33 0
      JoltC/JoltC.cpp
  5. 1 0
      JoltC/Test.cpp

+ 14 - 3
HelloWorld/main.cpp

@@ -79,6 +79,15 @@ static JPC_ObjectLayerPairFilterFns Hello_OVO = {
 	.ShouldCollide = Hello_OVO_ShouldCollide,
 };
 
+void Hello_Debug_DrawLine(const void *self, JPC_RVec3 inFrom, JPC_RVec3 inTo, JPC_Color inColor) {
+	printf("Draw line from (%f, %f, %f) to (%f, %f, %f) with color (%d, %d, %d)\n",
+		inFrom.x, inFrom.y, inFrom.z, inTo.x, inTo.y, inTo.z, inColor.r, inColor.g, inColor.b);
+}
+
+static JPC_DebugRendererSimpleFns Hello_DebugRenderer = {
+	.DrawLine = Hello_Debug_DrawLine,
+};
+
 int main() {
 	JPC_RegisterDefaultAllocator();
 	JPC_FactoryInit();
@@ -162,12 +171,16 @@ int main() {
 
 	JPC_BodyInterface_SetLinearVelocity(body_interface, sphere_id, JPC_Vec3{0.0, -5.0, 0.0});
 
+	JPC_DebugRendererSimple* debug_renderer = JPC_DebugRendererSimple_new(nullptr, Hello_DebugRenderer);
+	JPC_BodyManager_DrawSettings draw_settings;
+	JPC_BodyManager_DrawSettings_default(&draw_settings);
+	JPC_PhysicsSystem_DrawBodies(physics_system, &draw_settings, debug_renderer, nullptr);
+
 	// TODO: PhysicsSystem::OptimizeBroadPhase
 
 	const float cDeltaTime = 1.0f / 60.0f;
 	const int cCollisionSteps = 1;
 
-	// TODO: Update loop
 	int step = 0;
 	while (JPC_BodyInterface_IsActive(body_interface, sphere_id)) {
 		++step;
@@ -180,8 +193,6 @@ int main() {
 		JPC_PhysicsSystem_Update(physics_system, cDeltaTime, cCollisionSteps, temp_allocator, job_system);
 	}
 
-	// TODO: RemoveBody and DestroyBody
-
 	JPC_BodyInterface_RemoveBody(body_interface, sphere_id);
 	JPC_BodyInterface_DestroyBody(body_interface, sphere_id);
 

+ 16 - 1
JoltC/Enums.h

@@ -283,4 +283,19 @@ typedef enum JPC_Features: uint32_t {
     JPC_FEATURE_PLATFORM_DETERMINISTIC = (1 << 12),
     JPC_FEATURE_FLOATING_POINT_EXCEPTIONS = (1 << 13),
     JPC_FEATURE_DEBUG = (1 << 14),
-} JPC_Features;
+} JPC_Features;
+
+typedef int JPC_ShapeColor;
+const JPC_ShapeColor JPC_SHAPE_COLOR_INSTANCE_COLOR = 0;
+const JPC_ShapeColor JPC_SHAPE_COLOR_SHAPE_TYPE_COLOR = 1;
+const JPC_ShapeColor JPC_SHAPE_COLOR_MOTION_TYPE_COLOR = 2;
+const JPC_ShapeColor JPC_SHAPE_COLOR_SLEEP_COLOR = 3;
+const JPC_ShapeColor JPC_SHAPE_COLOR_ISLAND_COLOR = 4;
+const JPC_ShapeColor JPC_SHAPE_COLOR_MATERIAL_COLOR = 5;
+
+ENSURE_ENUM_EQ(JPC_SHAPE_COLOR_INSTANCE_COLOR, JPH::BodyManager::EShapeColor::InstanceColor)
+ENSURE_ENUM_EQ(JPC_SHAPE_COLOR_SHAPE_TYPE_COLOR, JPH::BodyManager::EShapeColor::ShapeTypeColor)
+ENSURE_ENUM_EQ(JPC_SHAPE_COLOR_MOTION_TYPE_COLOR, JPH::BodyManager::EShapeColor::MotionTypeColor)
+ENSURE_ENUM_EQ(JPC_SHAPE_COLOR_SLEEP_COLOR, JPH::BodyManager::EShapeColor::SleepColor)
+ENSURE_ENUM_EQ(JPC_SHAPE_COLOR_ISLAND_COLOR, JPH::BodyManager::EShapeColor::IslandColor)
+ENSURE_ENUM_EQ(JPC_SHAPE_COLOR_MATERIAL_COLOR, JPH::BodyManager::EShapeColor::MaterialColor)

+ 36 - 0
JoltC/Functions.h

@@ -198,6 +198,36 @@ JPC_API JPC_ObjectLayerPairFilter* JPC_ObjectLayerPairFilter_new(
 
 JPC_API void JPC_ObjectLayerPairFilter_delete(JPC_ObjectLayerPairFilter* object);
 
+////////////////////////////////////////////////////////////////////////////////
+// DrawSettings
+
+typedef struct JPC_BodyManager_DrawSettings {
+	bool mDrawGetSupportFunction;
+	bool mDrawSupportDirection;
+	bool mDrawGetSupportingFace;
+	bool mDrawShape;
+	bool mDrawShapeWireframe;
+	JPC_ShapeColor mDrawShapeColor;
+	bool mDrawBoundingBox;
+	bool mDrawCenterOfMassTransform;
+	bool mDrawWorldTransform;
+	bool mDrawVelocity;
+	bool mDrawMassAndInertia;
+	bool mDrawSleepStats;
+	bool mDrawSoftBodyVertices;
+	bool mDrawSoftBodyVertexVelocities;
+	bool mDrawSoftBodyEdgeConstraints;
+	bool mDrawSoftBodyBendConstraints;
+	bool mDrawSoftBodyVolumeConstraints;
+	bool mDrawSoftBodySkinConstraints;
+	bool mDrawSoftBodyLRAConstraints;
+	bool mDrawSoftBodyPredictedBounds;
+} JPC_BodyManager_DrawSettings;
+
+ENSURE_SIZE_ALIGN(JPC_BodyManager_DrawSettings, JPH::BodyManager::DrawSettings)
+
+JPC_API void JPC_BodyManager_DrawSettings_default(JPC_BodyManager_DrawSettings* object);
+
 ////////////////////////////////////////////////////////////////////////////////
 // DebugRendererSimple
 
@@ -336,6 +366,12 @@ JPC_API JPC_PhysicsUpdateError JPC_PhysicsSystem_Update(
 
 JPC_API JPC_BodyInterface* JPC_PhysicsSystem_GetBodyInterface(JPC_PhysicsSystem* self);
 
+JPC_API void JPC_PhysicsSystem_DrawBodies(
+	JPC_PhysicsSystem* self,
+	JPC_BodyManager_DrawSettings* inSettings,
+	JPC_DebugRendererSimple* inRenderer, // FIXME: un-specialize
+	const void* inBodyFilter); // FIXME: BodyDrawFilter
+
 #ifdef __cplusplus
 }
 #endif

+ 33 - 0
JoltC/JoltC.cpp

@@ -30,6 +30,21 @@
 	static c_type to_jpc(cpp_type in) { return static_cast<c_type>(in); } \
 	static cpp_type to_jph(c_type in) { return static_cast<cpp_type>(in); }
 
+#define LAYOUT_COMPATIBLE(c_type, cpp_type) \
+	static c_type to_jpc(cpp_type in) { \
+		c_type out; \
+		memcpy(&out, &in, sizeof(c_type)); \
+		return out; \
+	} \
+	static cpp_type to_jph(c_type in) { \
+		cpp_type out; \
+		memcpy(&out, &in, sizeof(cpp_type)); \
+		return out; \
+	} \
+	static_assert(sizeof(c_type) == sizeof(cpp_type), "size of " #c_type " did not match size of " #cpp_type); \
+	static_assert(alignof(c_type) == alignof(cpp_type), "align of " #c_type " did not match align of " #cpp_type); \
+	static_assert(!std::is_polymorphic_v<cpp_type>, #cpp_type " is polymorphic and cannot be made layout compatible");
+
 template<typename E>
 constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type 
 {
@@ -71,6 +86,8 @@ DESTRUCTOR(JPC_BoxShapeSettings)
 OPAQUE_WRAPPER(JPC_SphereShapeSettings, JPH::SphereShapeSettings)
 DESTRUCTOR(JPC_SphereShapeSettings)
 
+LAYOUT_COMPATIBLE(JPC_BodyManager_DrawSettings, JPH::BodyManager::DrawSettings)
+
 static auto to_jpc(JPH::BroadPhaseLayer in) { return in.GetValue(); }
 static auto to_jph(JPC_BroadPhaseLayer in) { return JPH::BroadPhaseLayer(in); }
 
@@ -259,6 +276,13 @@ JPC_API JPC_ObjectLayerPairFilter* JPC_ObjectLayerPairFilter_new(
 	return to_jpc(new JPC_ObjectLayerPairFilterBridge(self, fns));
 }
 
+////////////////////////////////////////////////////////////////////////////////
+// BodyManager::DrawSettings
+
+JPC_API void JPC_BodyManager_DrawSettings_default(JPC_BodyManager_DrawSettings* object) {
+	*object = to_jpc(JPH::BodyManager::DrawSettings());
+}
+
 ////////////////////////////////////////////////////////////////////////////////
 // DebugRendererSimple
 
@@ -475,4 +499,13 @@ JPC_API JPC_PhysicsUpdateError JPC_PhysicsSystem_Update(
 		to_jph(inJobSystem));
 
 	return to_integral(res);
+}
+
+JPC_API void JPC_PhysicsSystem_DrawBodies(
+	JPC_PhysicsSystem* self,
+	JPC_BodyManager_DrawSettings* inSettings,
+	JPC_DebugRendererSimple* inRenderer,
+	[[maybe_unused]] const void* inBodyFilter)
+{
+	to_jph(self)->DrawBodies(to_jph(*inSettings), to_jph(inRenderer), nullptr);
 }

+ 1 - 0
JoltC/Test.cpp

@@ -14,6 +14,7 @@
 #include <Jolt/Physics/Collision/Shape/SphereShape.h>
 #include <Jolt/Physics/Body/BodyCreationSettings.h>
 #include <Jolt/Physics/Body/BodyActivationListener.h>
+#include <Jolt/Physics/Body/BodyManager.h>
 
 template<typename E>
 constexpr auto to_integral(E e) -> typename std::underlying_type<E>::type