DebugRendererSimple.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #ifndef JPH_DEBUG_RENDERER
  6. #error This file should only be included when JPH_DEBUG_RENDERER is defined
  7. #endif // !JPH_DEBUG_RENDERER
  8. #include <Jolt/Renderer/DebugRenderer.h>
  9. JPH_NAMESPACE_BEGIN
  10. /// Inherit from this class to simplify implementing a debug renderer, start with this implementation:
  11. ///
  12. /// class MyDebugRenderer : public JPH::DebugRendererSimple
  13. /// {
  14. /// public:
  15. /// virtual void DrawLine(JPH::RVec3Arg inFrom, JPH::RVec3Arg inTo, JPH::ColorArg inColor) override
  16. /// {
  17. /// // Implement
  18. /// }
  19. ///
  20. /// virtual void DrawTriangle(JPH::RVec3Arg inV1, JPH::RVec3Arg inV2, JPH::RVec3Arg inV3, JPH::ColorArg inColor, ECastShadow inCastShadow) override
  21. /// {
  22. /// // Implement
  23. /// }
  24. ///
  25. /// virtual void DrawText3D(JPH::RVec3Arg inPosition, const string_view &inString, JPH::ColorArg inColor, float inHeight) override
  26. /// {
  27. /// // Implement
  28. /// }
  29. /// };
  30. ///
  31. /// Note that this class is meant to be a quick start for implementing a debug renderer, it is not the most efficient way to implement a debug renderer.
  32. class JPH_DEBUG_RENDERER_EXPORT DebugRendererSimple : public DebugRenderer
  33. {
  34. public:
  35. JPH_OVERRIDE_NEW_DELETE
  36. /// Constructor
  37. DebugRendererSimple();
  38. /// Should be called every frame by the application to provide the camera position.
  39. /// This is used to determine the correct LOD for rendering.
  40. void SetCameraPos(RVec3Arg inCameraPos)
  41. {
  42. mCameraPos = inCameraPos;
  43. mCameraPosSet = true;
  44. }
  45. /// Fallback implementation that uses DrawLine to draw a triangle (override this if you have a version that renders solid triangles)
  46. virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow) override
  47. {
  48. DrawLine(inV1, inV2, inColor);
  49. DrawLine(inV2, inV3, inColor);
  50. DrawLine(inV3, inV1, inColor);
  51. }
  52. protected:
  53. /// Implementation of DebugRenderer interface
  54. virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) override;
  55. virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) override;
  56. virtual void DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) override;
  57. private:
  58. /// Implementation specific batch object
  59. class BatchImpl : public RefTargetVirtual
  60. {
  61. public:
  62. JPH_OVERRIDE_NEW_DELETE
  63. virtual void AddRef() override { ++mRefCount; }
  64. virtual void Release() override { if (--mRefCount == 0) delete this; }
  65. Array<Triangle> mTriangles;
  66. private:
  67. atomic<uint32> mRefCount = 0;
  68. };
  69. /// Last provided camera position
  70. RVec3 mCameraPos;
  71. bool mCameraPosSet = false;
  72. };
  73. JPH_NAMESPACE_END