2
0

DebugRenderer.h 13 KB

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