DebugRenderer.h 13 KB

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