DebugRenderer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 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. #ifndef JPH_DEBUG_RENDERER_EXPORT
  9. // By default export the debug renderer
  10. #define JPH_DEBUG_RENDERER_EXPORT JPH_EXPORT
  11. #endif // !JPH_DEBUG_RENDERER_EXPORT
  12. #include <Jolt/Core/Color.h>
  13. #include <Jolt/Core/Reference.h>
  14. #include <Jolt/Core/HashCombine.h>
  15. #include <Jolt/Core/UnorderedMap.h>
  16. #include <Jolt/Math/Float2.h>
  17. #include <Jolt/Geometry/IndexedTriangle.h>
  18. #include <Jolt/Geometry/AABox.h>
  19. JPH_NAMESPACE_BEGIN
  20. class OrientedBox;
  21. /// Simple triangle renderer for debugging purposes.
  22. class JPH_DEBUG_RENDERER_EXPORT DebugRenderer
  23. {
  24. public:
  25. JPH_OVERRIDE_NEW_DELETE
  26. /// Constructor
  27. DebugRenderer();
  28. virtual ~DebugRenderer();
  29. /// Draw line
  30. virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) = 0;
  31. /// Draw wireframe box
  32. void DrawWireBox(const AABox &inBox, ColorArg inColor);
  33. void DrawWireBox(const OrientedBox &inBox, ColorArg inColor);
  34. void DrawWireBox(RMat44Arg inMatrix, const AABox &inBox, ColorArg inColor);
  35. /// Draw a marker on a position
  36. void DrawMarker(RVec3Arg inPosition, ColorArg inColor, float inSize);
  37. /// Draw an arrow
  38. void DrawArrow(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor, float inSize);
  39. /// Draw coordinate system (3 arrows, x = red, y = green, z = blue)
  40. void DrawCoordinateSystem(RMat44Arg inTransform, float inSize = 1.0f);
  41. /// Draw a plane through inPoint with normal inNormal
  42. void DrawPlane(RVec3Arg inPoint, Vec3Arg inNormal, ColorArg inColor, float inSize);
  43. /// Draw wireframe triangle
  44. void DrawWireTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor);
  45. /// Draw a wireframe polygon
  46. template <class VERTEX_ARRAY>
  47. void DrawWirePolygon(RMat44Arg inTransform, const VERTEX_ARRAY &inVertices, ColorArg inColor, float inArrowSize = 0.0f) { for (typename VERTEX_ARRAY::size_type i = 0; i < inVertices.size(); ++i) DrawArrow(inTransform * inVertices[i], inTransform * inVertices[(i + 1) % inVertices.size()], inColor, inArrowSize); }
  48. /// Draw wireframe sphere
  49. void DrawWireSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, int inLevel = 3);
  50. void DrawWireUnitSphere(RMat44Arg inMatrix, ColorArg inColor, int inLevel = 3);
  51. /// Enum that determines if a shadow should be cast or not
  52. enum class ECastShadow
  53. {
  54. On, // This shape should cast a shadow
  55. Off // This shape should not cast a shadow
  56. };
  57. /// Determines how triangles are drawn
  58. enum class EDrawMode
  59. {
  60. Solid, ///< Draw as a solid shape
  61. Wireframe, ///< Draw as wireframe
  62. };
  63. /// Draw a single back face culled triangle
  64. virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::Off) = 0;
  65. /// Draw a box
  66. void DrawBox(const AABox &inBox, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  67. void DrawBox(RMat44Arg inMatrix, const AABox &inBox, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  68. /// Draw a sphere
  69. void DrawSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  70. void DrawUnitSphere(RMat44Arg inMatrix, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  71. /// Draw a capsule with one half sphere at (0, -inHalfHeightOfCylinder, 0) and the other half sphere at (0, inHalfHeightOfCylinder, 0) and radius inRadius.
  72. /// The capsule will be transformed by inMatrix.
  73. void DrawCapsule(RMat44Arg inMatrix, float inHalfHeightOfCylinder, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  74. /// Draw a cylinder with top (0, inHalfHeight, 0) and bottom (0, -inHalfHeight, 0) and radius inRadius.
  75. /// The cylinder will be transformed by inMatrix
  76. void DrawCylinder(RMat44Arg inMatrix, float inHalfHeight, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  77. /// Draw a bottomless cone.
  78. /// @param inTop Top of cone, center of base is at inTop + inAxis.
  79. /// @param inAxis Height and direction of cone
  80. /// @param inPerpendicular Perpendicular vector to inAxis.
  81. /// @param inHalfAngle Specifies the cone angle in radians (angle measured between inAxis and cone surface).
  82. /// @param inLength The length of the cone.
  83. /// @param inColor Color to use for drawing the cone.
  84. /// @param inCastShadow determins if this geometry should cast a shadow or not.
  85. /// @param inDrawMode determines if we draw the geometry solid or in wireframe.
  86. void DrawOpenCone(RVec3Arg inTop, Vec3Arg inAxis, Vec3Arg inPerpendicular, float inHalfAngle, float inLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  87. /// Draws rotation limits as used by the SwingTwistConstraintPart.
  88. /// @param inMatrix Matrix that transforms from constraint space to world space
  89. /// @param inSwingYHalfAngle See SwingTwistConstraintPart
  90. /// @param inSwingZHalfAngle See SwingTwistConstraintPart
  91. /// @param inEdgeLength Size of the edge of the cone shape
  92. /// @param inColor Color to use for drawing the cone.
  93. /// @param inCastShadow determins if this geometry should cast a shadow or not.
  94. /// @param inDrawMode determines if we draw the geometry solid or in wireframe.
  95. void DrawSwingLimits(RMat44Arg inMatrix, float inSwingYHalfAngle, float inSwingZHalfAngle, float inEdgeLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  96. /// Draw a pie (part of a circle).
  97. /// @param inCenter The center of the circle.
  98. /// @param inRadius Radius of the circle.
  99. /// @param inNormal The plane normal in which the pie resides.
  100. /// @param inAxis The axis that defines an angle of 0 radians.
  101. /// @param inMinAngle The pie will be drawn between [inMinAngle, inMaxAngle] (in radians).
  102. /// @param inMaxAngle The pie will be drawn between [inMinAngle, inMaxAngle] (in radians).
  103. /// @param inColor Color to use for drawing the pie.
  104. /// @param inCastShadow determins if this geometry should cast a shadow or not.
  105. /// @param inDrawMode determines if we draw the geometry solid or in wireframe.
  106. void DrawPie(RVec3Arg inCenter, float inRadius, Vec3Arg inNormal, Vec3Arg inAxis, float inMinAngle, float inMaxAngle, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
  107. /// Singleton instance
  108. static DebugRenderer * sInstance;
  109. /// Vertex format used by the triangle renderer
  110. class Vertex
  111. {
  112. public:
  113. Float3 mPosition;
  114. Float3 mNormal;
  115. Float2 mUV;
  116. Color mColor;
  117. };
  118. /// A single triangle
  119. class JPH_DEBUG_RENDERER_EXPORT Triangle
  120. {
  121. public:
  122. Triangle() = default;
  123. Triangle(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, ColorArg inColor);
  124. Triangle(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, ColorArg inColor, Vec3Arg inUVOrigin, Vec3Arg inUVDirection);
  125. Vertex mV[3];
  126. };
  127. /// Handle for a batch of triangles
  128. using Batch = Ref<RefTargetVirtual>;
  129. /// A single level of detail
  130. class LOD
  131. {
  132. public:
  133. Batch mTriangleBatch;
  134. float mDistance;
  135. };
  136. /// A geometry primitive containing triangle batches for various lods
  137. class Geometry : public RefTarget<Geometry>
  138. {
  139. public:
  140. JPH_OVERRIDE_NEW_DELETE
  141. /// Constructor
  142. Geometry(const AABox &inBounds) : mBounds(inBounds) { }
  143. Geometry(const Batch &inBatch, const AABox &inBounds) : mBounds(inBounds) { mLODs.push_back({ inBatch, FLT_MAX }); }
  144. /// All level of details for this mesh
  145. Array<LOD> mLODs;
  146. /// Bounding box that encapsulates all LODs
  147. AABox mBounds;
  148. };
  149. /// Handle for a lodded triangle batch
  150. using GeometryRef = Ref<Geometry>;
  151. /// Calculate bounding box for a batch of triangles
  152. static AABox sCalculateBounds(const Vertex *inVertices, int inVertexCount);
  153. /// Create a batch of triangles that can be drawn efficiently
  154. virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) = 0;
  155. virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) = 0;
  156. Batch CreateTriangleBatch(const Array<Triangle> &inTriangles) { return CreateTriangleBatch(inTriangles.empty()? nullptr : &inTriangles[0], int(inTriangles.size())); }
  157. Batch CreateTriangleBatch(const Array<Vertex> &inVertices, const Array<uint32> &inIndices) { return CreateTriangleBatch(inVertices.empty()? nullptr : &inVertices[0], int(inVertices.size()), inIndices.empty()? nullptr : &inIndices[0], int(inIndices.size())); }
  158. Batch CreateTriangleBatch(const VertexList &inVertices, const IndexedTriangleNoMaterialList &inTriangles);
  159. /// Create a primitive for a convex shape using its support function
  160. using SupportFunction = function<Vec3 (Vec3Arg inDirection)>;
  161. Batch CreateTriangleBatchForConvex(SupportFunction inGetSupport, int inLevel, AABox *outBounds = nullptr);
  162. GeometryRef CreateTriangleGeometryForConvex(SupportFunction inGetSupport);
  163. /// Determines which polygons are culled
  164. enum class ECullMode
  165. {
  166. CullBackFace, ///< Don't draw backfacing polygons
  167. CullFrontFace, ///< Don't draw front facing polygons
  168. Off ///< Don't do culling and draw both sides
  169. };
  170. /// Draw some geometry
  171. /// @param inModelMatrix is the matrix that transforms the geometry to world space.
  172. /// @param inWorldSpaceBounds is the bounding box of the geometry after transforming it into world space.
  173. /// @param inLODScaleSq is the squared scale of the model matrix, it is multiplied with the LOD distances in inGeometry to calculate the real LOD distance (so a number > 1 will force a higher LOD).
  174. /// @param inModelColor is the color with which to multiply the vertex colors in inGeometry.
  175. /// @param inGeometry The geometry to draw.
  176. /// @param inCullMode determines which polygons are culled.
  177. /// @param inCastShadow determines if this geometry should cast a shadow or not.
  178. /// @param inDrawMode determines if we draw the geometry solid or in wireframe.
  179. virtual void DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode = ECullMode::CullBackFace, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid) = 0;
  180. void DrawGeometry(RMat44Arg inModelMatrix, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode = ECullMode::CullBackFace, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid) { DrawGeometry(inModelMatrix, inGeometry->mBounds.Transformed(inModelMatrix), max(max(inModelMatrix.GetAxisX().LengthSq(), inModelMatrix.GetAxisY().LengthSq()), inModelMatrix.GetAxisZ().LengthSq()), inModelColor, inGeometry, inCullMode, inCastShadow, inDrawMode); }
  181. /// Draw text
  182. virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor = Color::sWhite, float inHeight = 0.5f) = 0;
  183. protected:
  184. /// Initialize the system, must be called from the constructor of the DebugRenderer implementation
  185. void Initialize();
  186. private:
  187. /// Recursive helper function for DrawWireUnitSphere
  188. void DrawWireUnitSphereRecursive(RMat44Arg inMatrix, ColorArg inColor, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, int inLevel);
  189. /// Helper functions to create a box
  190. void CreateQuad(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, Vec3Arg inV4);
  191. /// Helper functions to create a vertex and index buffer for a sphere
  192. void Create8thSphereRecursive(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inDir1, uint32 &ioIdx1, Vec3Arg inDir2, uint32 &ioIdx2, Vec3Arg inDir3, uint32 &ioIdx3, const Float2 &inUV, SupportFunction inGetSupport, int inLevel);
  193. void Create8thSphere(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, const Float2 &inUV, SupportFunction inGetSupport, int inLevel);
  194. // Predefined shapes
  195. GeometryRef mBox;
  196. GeometryRef mSphere;
  197. GeometryRef mCapsuleTop;
  198. GeometryRef mCapsuleMid;
  199. GeometryRef mCapsuleBottom;
  200. GeometryRef mOpenCone;
  201. GeometryRef mCylinder;
  202. struct SwingLimits
  203. {
  204. bool operator == (const SwingLimits &inRHS) const { return mSwingYHalfAngle == inRHS.mSwingYHalfAngle && mSwingZHalfAngle == inRHS.mSwingZHalfAngle; }
  205. float mSwingYHalfAngle;
  206. float mSwingZHalfAngle;
  207. };
  208. JPH_MAKE_HASH_STRUCT(SwingLimits, SwingLimitsHasher, t.mSwingYHalfAngle, t.mSwingZHalfAngle)
  209. using SwingBatches = UnorderedMap<SwingLimits, GeometryRef, SwingLimitsHasher>;
  210. SwingBatches mSwingLimits;
  211. using PieBatces = UnorderedMap<float, GeometryRef>;
  212. PieBatces mPieLimits;
  213. };
  214. JPH_NAMESPACE_END