Skip to content

Commit

Permalink
add wip DebugRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
xpenatan committed Aug 19, 2024
1 parent c27156e commit 8c6b866
Show file tree
Hide file tree
Showing 3 changed files with 270 additions and 2 deletions.
161 changes: 161 additions & 0 deletions jolt/jolt-build/src/main/cpp/custom/JoltCustom.h
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,165 @@ class HeightFieldShapeConstantValues
public:
/// Value used to create gaps in the height field
static constexpr float cNoCollisionValue = HeightFieldShapeConstants::cNoCollisionValue;
};

// DEBUG RENDERER

#include "Jolt/Renderer/DebugRendererSimple.h"

using BodyManagerDrawSettings = BodyManager::DrawSettings;
using DebugRendererVertex = DebugRenderer::Vertex;
using DebugRendererTriangle = DebugRenderer::Triangle;

using ECullMode = DebugRenderer::ECullMode;
constexpr ECullMode ECullMode_CullBackFace = ECullMode::CullBackFace;
constexpr ECullMode ECullMode_CullFrontFace = ECullMode::CullFrontFace;
constexpr ECullMode ECullMode_Off = ECullMode::Off;

using ECastShadow = DebugRenderer::ECastShadow;
constexpr ECastShadow ECastShadow_On = ECastShadow::On;
constexpr ECastShadow ECastShadow_Off = ECastShadow::Off;

using EDrawMode = DebugRenderer::EDrawMode;
constexpr EDrawMode EDrawMode_Solid = EDrawMode::Solid;
constexpr EDrawMode EDrawMode_Wireframe = EDrawMode::Wireframe;

using EShapeColor = BodyManager::EShapeColor;
constexpr EShapeColor EShapeColor_InstanceColor = EShapeColor::InstanceColor;
constexpr EShapeColor EShapeColor_ShapeTypeColor = EShapeColor::ShapeTypeColor;
constexpr EShapeColor EShapeColor_MotionTypeColor = EShapeColor::MotionTypeColor;
constexpr EShapeColor EShapeColor_SleepColor = EShapeColor::SleepColor;
constexpr EShapeColor EShapeColor_IslandColor = EShapeColor::IslandColor;
constexpr EShapeColor EShapeColor_MaterialColor = EShapeColor::MaterialColor;

constexpr ESoftBodyConstraintColor ESoftBodyConstraintColor_ConstraintType = ESoftBodyConstraintColor::ConstraintType;
constexpr ESoftBodyConstraintColor ESoftBodyConstraintColor_ConstraintGroup = ESoftBodyConstraintColor::ConstraintGroup;
constexpr ESoftBodyConstraintColor ESoftBodyConstraintColor_ConstraintOrder = ESoftBodyConstraintColor::ConstraintOrder;

class DebugRendererVertexTraits
{
public:
static constexpr uint mPositionOffset = offsetof(DebugRendererVertex, mPosition);
static constexpr uint mNormalOffset = offsetof(DebugRendererVertex, mNormal);
static constexpr uint mUVOffset = offsetof(DebugRendererVertex, mUV);
static constexpr uint mSize = sizeof(DebugRendererVertex);
};

class DebugRendererTriangleTraits
{
public:
static constexpr uint mVOffset = offsetof(DebugRendererTriangle, mV);
static constexpr uint mSize = sizeof(DebugRendererTriangle);
};

class DebugRendererEm : public JPH::DebugRenderer
{
public:
void Initialize()
{
JPH::DebugRenderer::Initialize();
}

virtual void DrawLine(const RVec3 *inFrom, const RVec3 *inTo, const Color *inColor) = 0;

virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor)
{
DrawLine(&inFrom, &inTo, &inColor);
}

virtual void DrawTriangle(const RVec3 *inV1, const RVec3 *inV2, const RVec3 *inV3, const Color *inColor, ECastShadow inCastShadow = ECastShadow::Off) = 0;

virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::Off)
{
DrawTriangle(&inV1, &inV2, &inV3, &inColor, inCastShadow);
}

virtual void DrawText3D(const RVec3 *inPosition, const void *inString, uint32 inStringLen, const Color *inColor, float inHeight) = 0;

virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor, float inHeight)
{
DrawText3D(&inPosition, (const void*)inString.data(), inString.size(), &inColor, inHeight);
}

virtual uint32 CreateTriangleBatchID(const void *inTriangles, int inTriangleCount) = 0;

virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount)
{
uint32 batch = CreateTriangleBatchID((const void*)inTriangles, inTriangleCount);
return new BatchImpl(batch);
}

virtual uint32 CreateTriangleBatchIDWithIndex(const void *inVertices, int inVertexCount, const void *inIndices, int inIndexCount) = 0;

virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount)
{
uint32 batch = CreateTriangleBatchIDWithIndex((const void*)inVertices, inVertexCount, (const void*)inIndices, inIndexCount);
return new BatchImpl(batch);
}

virtual void DrawGeometryWithID(const RMat44 *inModelMatrix, const AABox *inWorldSpaceBounds, float inLODScaleSq, Color *inModelColor, const uint32 inGeometryID, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) = 0;

virtual void DrawGeometry(RMat44Arg inModelMatrix, const AABox& inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef& inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode)
{
const LOD *lod = inGeometry->mLODs.data();
const BatchImpl *batch = static_cast<const BatchImpl*>(lod->mTriangleBatch.GetPtr());

DrawGeometryWithID(&inModelMatrix, &inWorldSpaceBounds, inLODScaleSq, &inModelColor, batch->mID, inCullMode, inCastShadow, inDrawMode);
}

void DrawBodies(PhysicsSystem *inSystem, BodyManager::DrawSettings *inDrawSettings)
{
inSystem->DrawBodies(*inDrawSettings, this);
}
void DrawBodies(PhysicsSystem *inSystem)
{
inSystem->DrawBodies(BodyManager::DrawSettings(), this);
}

void DrawConstraints(PhysicsSystem *inSystem)
{
inSystem->DrawConstraints(this);
}

void DrawConstraintLimits(PhysicsSystem *inSystem)
{
inSystem->DrawConstraintLimits(this);
}

void DrawConstraintReferenceFrame(PhysicsSystem *inSystem)
{
inSystem->DrawConstraintReferenceFrame(this);
}

void DrawShape(Shape *inShape, const RMat44 *inModelMatrix, const RVec3 *inScale, const Color *inColor, bool inDrawWireFrame)
{
inShape->Draw(this, *inModelMatrix, *inScale, *inColor, false, inDrawWireFrame);
}

void DrawBody(Body *inBody, const Color *inColor, bool inDrawWireFrame)
{
RMat44 com = inBody->GetCenterOfMassTransform();
inBody->GetShape()->Draw(this, com, Vec3::sReplicate(1.0f), *inColor, false, inDrawWireFrame);
}

void DrawConstraint(Constraint *inConstraint)
{
inConstraint->DrawConstraint(this);
}

private:
/// Implementation specific batch object
class BatchImpl : public RefTargetVirtual
{
public:
JPH_OVERRIDE_NEW_DELETE

BatchImpl(uint32 inID) : mID(inID) { }

virtual void AddRef() override { ++mRefCount; }
virtual void Release() override { if (--mRefCount == 0) delete this; }

atomic<uint32> mRefCount = 0;
uint32 mID;
};
};
99 changes: 99 additions & 0 deletions jolt/jolt-build/src/main/cpp/jolt.idl
Original file line number Diff line number Diff line change
@@ -1,3 +1,102 @@
enum ECullMode {
"ECullMode_CullBackFace",
"ECullMode_CullFrontFace",
"ECullMode_Off"
};

enum ECastShadow {
"ECastShadow_On",
"ECastShadow_Off",
};

enum EDrawMode {
"EDrawMode_Solid",
"EDrawMode_Wireframe",
};

enum EShapeColor {
"EShapeColor_InstanceColor",
"EShapeColor_ShapeTypeColor",
"EShapeColor_MotionTypeColor",
"EShapeColor_SleepColor",
"EShapeColor_IslandColor",
"EShapeColor_MaterialColor"
};

enum ESoftBodyConstraintColor {
"ESoftBodyConstraintColor_ConstraintType",
"ESoftBodyConstraintColor_ConstraintGroup",
"ESoftBodyConstraintColor_ConstraintOrder"
};

interface Color {
attribute unsigned long mU32;
};

interface BodyManagerDrawSettings {
void BodyManagerDrawSettings();

attribute boolean mDrawGetSupportFunction;
attribute boolean mDrawSupportDirection;
attribute boolean mDrawGetSupportingFace;
attribute boolean mDrawShape;
attribute boolean mDrawShapeWireframe;
attribute EShapeColor mDrawShapeColor;
attribute boolean mDrawBoundingBox;
attribute boolean mDrawCenterOfMassTransform;
attribute boolean mDrawWorldTransform;
attribute boolean mDrawVelocity;
attribute boolean mDrawMassAndInertia;
attribute boolean mDrawSleepStats;
attribute boolean mDrawSoftBodyVertices;
attribute boolean mDrawSoftBodyVertexVelocities;
attribute boolean mDrawSoftBodyEdgeConstraints;
attribute boolean mDrawSoftBodyBendConstraints;
attribute boolean mDrawSoftBodyVolumeConstraints;
attribute boolean mDrawSoftBodySkinConstraints;
attribute boolean mDrawSoftBodyLRAConstraints;
attribute boolean mDrawSoftBodyPredictedBounds;
attribute ESoftBodyConstraintColor mDrawSoftBodyConstraintColor;
};

interface DebugRendererVertexTraits
{
static readonly attribute unsigned long mPositionOffset;
static readonly attribute unsigned long mNormalOffset;
static readonly attribute unsigned long mUVOffset;
static readonly attribute unsigned long mSize;
};

interface DebugRendererTriangleTraits
{
static readonly attribute unsigned long mVOffset;
static readonly attribute unsigned long mSize;
};

interface DebugRendererEm {
void Initialize();
void DrawBodies(PhysicsSystem system, BodyManagerDrawSettings inDrawSettings);
void DrawBodies(PhysicsSystem system);
void DrawConstraints(PhysicsSystem system);
void DrawConstraintLimits(PhysicsSystem system);
void DrawConstraintReferenceFrame(PhysicsSystem system);
void DrawShape(Shape inShape, [Const] RMat44 inModelMatrix, [Const] RVec3 inScale, [Const] Color inColor, boolean inDrawWireFrame);
void DrawBody(Body body, [Const] Color inColor, boolean inDrawWireFrame);
void DrawConstraint(Constraint inConstraint);
};

[JSImplementation = "DebugRendererEm"]
interface DebugRendererJS : DebugRendererEm {
void DebugRendererJS();
void DrawLine([Const] RVec3 inFrom, [Const] RVec3 inTo, [Const] Color inColor);
void DrawTriangle([Const] RVec3 inV1, [Const] RVec3 inV2, [Const] RVec3 inV3, [Const] Color inColor, ECastShadow inCastShadow);
void DrawText3D([Const] RVec3 inPosition, [Const] any inString, unsigned long inStringLen, [Const] Color inColor, float inHeight);
void DrawGeometryWithID([Const] RMat44 inModelMatrix, [Const] AABox inWorldSpaceBounds, float inLODScaleSq, Color inModelColor, [Const] unsigned long inGeometryID, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode);
unsigned long CreateTriangleBatchID([Const] any inTriangles, long inTriangleCount);
unsigned long CreateTriangleBatchIDWithIndex([Const] any inVertices, long inVertexCount, [Const] any inIndices, long inIndexCount);
};


//interface JPHString {
// void JPHString(DOMString str, long length);
// [Const] DOMString c_str(); // TODO: This is not a nice way to get a string from an interface
Expand Down
12 changes: 10 additions & 2 deletions jolt/jolt-build/src/main/java/Build.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private static BuildMultiTarget getWindowTarget(BuildToolOptions op) {
windowsTarget.isStatic = true;
windowsTarget.headerDirs.add("-I" + libBuildCPPPath + "/src/jolt/");
windowsTarget.cppInclude.add(libBuildCPPPath + "/**/jolt/Jolt/**.cpp");
windowsTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
windowsTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
windowsTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
windowsTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -70,6 +71,7 @@ private static BuildMultiTarget getWindowTarget(BuildToolOptions op) {
linkTarget.headerDirs.add("-I" + libBuildCPPPath + "/src/jolt/");
linkTarget.linkerFlags.add(libBuildCPPPath + "/libs/windows/vc/jolt64_.lib");
linkTarget.cppInclude.add(libBuildCPPPath + "/src/jniglue/JNIGlue.cpp");
linkTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
linkTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
linkTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
linkTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -89,6 +91,7 @@ private static BuildMultiTarget getLinuxTarget(BuildToolOptions op) {
linuxTarget.isStatic = true;
linuxTarget.headerDirs.add("-I" + libBuildCPPPath + "/src/jolt/");
linuxTarget.cppInclude.add(libBuildCPPPath + "/**/jolt/Jolt/**.cpp");
linuxTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
linuxTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
linuxTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
linuxTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -101,6 +104,7 @@ private static BuildMultiTarget getLinuxTarget(BuildToolOptions op) {
linkTarget.headerDirs.add("-I" + libBuildCPPPath + "/src/jolt/");
linkTarget.linkerFlags.add(libBuildCPPPath + "/libs/linux/libjolt64_.a");
linkTarget.cppInclude.add(libBuildCPPPath + "/src/jniglue/JNIGlue.cpp");
linkTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
linkTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
linkTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
linkTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -120,6 +124,7 @@ private static BuildMultiTarget getMacTarget(BuildToolOptions op, boolean isArm)
macTarget.isStatic = true;
macTarget.headerDirs.add("-I" + libBuildCPPPath + "/src/jolt/");
macTarget.cppInclude.add(libBuildCPPPath + "/**/jolt/Jolt/**.cpp");
macTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
macTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
macTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
macTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -137,6 +142,7 @@ private static BuildMultiTarget getMacTarget(BuildToolOptions op, boolean isArm)
linkTarget.linkerFlags.add(libBuildCPPPath + "/libs/mac/libjolt64_.a");
}
linkTarget.cppInclude.add(libBuildCPPPath + "/src/jniglue/JNIGlue.cpp");
linkTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
linkTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
linkTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
linkTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -149,8 +155,7 @@ private static BuildMultiTarget getMacTarget(BuildToolOptions op, boolean isArm)
private static BuildMultiTarget getTeaVMTarget(BuildToolOptions op, IDLReader idlReader) {
String libBuildCPPPath = op.getModuleBuildCPPPath();

EmscriptenTarget.DEBUG_BUILD = true;

EmscriptenTarget.DEBUG_BUILD = false;

BuildMultiTarget multiTarget = new BuildMultiTarget();

Expand All @@ -160,6 +165,7 @@ private static BuildMultiTarget getTeaVMTarget(BuildToolOptions op, IDLReader id
libTarget.compileGlueCode = false;
libTarget.headerDirs.add("-I" + libBuildCPPPath + "/src/jolt");
libTarget.cppInclude.add(libBuildCPPPath + "/**/jolt/Jolt/**.cpp");
libTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
libTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
libTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
libTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -171,6 +177,7 @@ private static BuildMultiTarget getTeaVMTarget(BuildToolOptions op, IDLReader id
linkTarget.headerDirs.add("-I" + libBuildCPPPath + "/src/jolt");
linkTarget.headerDirs.add("-include" + libBuildCPPPath + "/src/jolt/JoltCustom.h");
linkTarget.linkerFlags.add(libBuildCPPPath + "/libs/emscripten/jolt_.a");
linkTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
linkTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
linkTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
linkTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand All @@ -190,6 +197,7 @@ private static BuildMultiTarget getAndroidTarget(BuildToolOptions op) {
androidTarget.headerDirs.add(libBuildCPPPath + "/src/jolt");
androidTarget.cppInclude.add(libBuildCPPPath + "/**/jolt/Jolt/**.cpp");
androidTarget.cppFlags.add("-Wno-error=format-security");
androidTarget.cppFlags.add("-DJPH_DEBUG_RENDERER");
androidTarget.cppFlags.add("-DJPH_DISABLE_CUSTOM_ALLOCATOR");
androidTarget.cppFlags.add("-DJPH_ENABLE_ASSERTS");
androidTarget.cppFlags.add("-DJPH_CROSS_PLATFORM_DETERMINISTIC");
Expand Down

0 comments on commit 8c6b866

Please sign in to comment.