Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
DebugRenderer.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#ifndef JPH_DEBUG_RENDERER
8 #error This file should only be included when JPH_DEBUG_RENDERER is defined
9#endif // !JPH_DEBUG_RENDERER
10
11#include <Jolt/Core/Color.h>
12#include <Jolt/Core/Reference.h>
15#include <Jolt/Math/Float2.h>
17#include <Jolt/Geometry/AABox.h>
18
20
21class OrientedBox;
22
25{
26public:
28
31 virtual ~DebugRenderer();
32
34 virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) = 0;
35
37 void DrawWireBox(const AABox &inBox, ColorArg inColor);
38 void DrawWireBox(const OrientedBox &inBox, ColorArg inColor);
39 void DrawWireBox(RMat44Arg inMatrix, const AABox &inBox, ColorArg inColor);
40
42 void DrawMarker(RVec3Arg inPosition, ColorArg inColor, float inSize);
43
45 void DrawArrow(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor, float inSize);
46
48 void DrawCoordinateSystem(RMat44Arg inTransform, float inSize = 1.0f);
49
51 void DrawPlane(RVec3Arg inPoint, Vec3Arg inNormal, ColorArg inColor, float inSize);
52
54 void DrawWireTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor);
55
57 template <class VERTEX_ARRAY>
58 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); }
59
61 void DrawWireSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, int inLevel = 3);
62 void DrawWireUnitSphere(RMat44Arg inMatrix, ColorArg inColor, int inLevel = 3);
63
65 enum class ECastShadow
66 {
67 On, // This shape should cast a shadow
68 Off // This shape should not cast a shadow
69 };
70
72 enum class EDrawMode
73 {
74 Solid,
75 Wireframe,
76 };
77
79 virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor) = 0;
80
82 void DrawBox(const AABox &inBox, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
83 void DrawBox(RMat44Arg inMatrix, const AABox &inBox, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
84
86 void DrawSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
87 void DrawUnitSphere(RMat44Arg inMatrix, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
88
91 void DrawCapsule(RMat44Arg inMatrix, float inHalfHeightOfCylinder, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
92
95 void DrawCylinder(RMat44Arg inMatrix, float inHalfHeight, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
96
106 void DrawOpenCone(RVec3Arg inTop, Vec3Arg inAxis, Vec3Arg inPerpendicular, float inHalfAngle, float inLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
107
116 void DrawSwingLimits(RMat44Arg inMatrix, float inSwingYHalfAngle, float inSwingZHalfAngle, float inEdgeLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
117
128 void DrawPie(RVec3Arg inCenter, float inRadius, Vec3Arg inNormal, Vec3Arg inAxis, float inMinAngle, float inMaxAngle, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
129
132
134 class Vertex
135 {
136 public:
141 };
142
145 {
146 public:
147 Triangle() = default;
148 Triangle(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, ColorArg inColor);
149 Triangle(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, ColorArg inColor, Vec3Arg inUVOrigin, Vec3Arg inUVDirection);
150
152 };
153
156
158 class LOD
159 {
160 public:
163 };
164
166 class Geometry : public RefTarget<Geometry>
167 {
168 public:
170
172 Geometry(const AABox &inBounds) : mBounds(inBounds) { }
173 Geometry(const Batch &inBatch, const AABox &inBounds) : mBounds(inBounds) { mLODs.push_back({ inBatch, FLT_MAX }); }
174
177
180 };
181
184
186 static AABox sCalculateBounds(const Vertex *inVertices, int inVertexCount);
187
189 virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) = 0;
190 virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) = 0;
191 Batch CreateTriangleBatch(const Array<Triangle> &inTriangles) { return CreateTriangleBatch(inTriangles.empty()? nullptr : &inTriangles[0], (int)inTriangles.size()); }
192 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()); }
193 Batch CreateTriangleBatch(const VertexList &inVertices, const IndexedTriangleNoMaterialList &inTriangles);
194
196 using SupportFunction = function<Vec3 (Vec3Arg inDirection)>;
197 Batch CreateTriangleBatchForConvex(SupportFunction inGetSupport, int inLevel, AABox *outBounds = nullptr);
199
201 enum class ECullMode
202 {
205 Off
206 };
207
217 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;
218 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); }
219
221 virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor = Color::sWhite, float inHeight = 0.5f) = 0;
222
223protected:
225 void Initialize();
226
227private:
229 void DrawWireUnitSphereRecursive(RMat44Arg inMatrix, ColorArg inColor, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, int inLevel);
230
232 void CreateQuad(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, Vec3Arg inV4);
233
235 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);
236 void Create8thSphere(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, const Float2 &inUV, SupportFunction inGetSupport, int inLevel);
237
238 // Predefined shapes
239 GeometryRef mBox;
240 GeometryRef mSphere;
241 GeometryRef mCapsuleTop;
242 GeometryRef mCapsuleMid;
243 GeometryRef mCapsuleBottom;
244 GeometryRef mOpenCone;
245 GeometryRef mCylinder;
246
247 struct SwingLimits
248 {
249 bool operator == (const SwingLimits &inRHS) const { return mSwingYHalfAngle == inRHS.mSwingYHalfAngle && mSwingZHalfAngle == inRHS.mSwingZHalfAngle; }
250
251 float mSwingYHalfAngle;
252 float mSwingZHalfAngle;
253 };
254
255 JPH_MAKE_HASH_STRUCT(SwingLimits, SwingLimitsHasher, t.mSwingYHalfAngle, t.mSwingZHalfAngle)
256
257 using SwingBatches = UnorderedMap<SwingLimits, GeometryRef, SwingLimitsHasher>;
258 SwingBatches mSwingLimits;
259
260 using PieBatces = UnorderedMap<float, GeometryRef>;
261 PieBatces mPieLimits;
262};
263
uint32_t uint32
Definition: Core.h:312
#define JPH_NAMESPACE_END
Definition: Core.h:240
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:234
Array< Float3 > VertexList
Definition: Float3.h:42
#define JPH_MAKE_HASH_STRUCT(type, name,...)
Definition: HashCombine.h:76
Array< IndexedTriangleNoMaterial > IndexedTriangleNoMaterialList
Definition: IndexedTriangle.h:104
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:29
std::vector< T, STLAllocator< T > > Array
Definition: STLAllocator.h:81
std::unordered_map< Key, T, Hash, KeyEqual, STLAllocator< pair< const Key, T > > > UnorderedMap
Definition: UnorderedMap.h:13
Axis aligned box.
Definition: AABox.h:16
AABox Transformed(Mat44Arg inMatrix) const
Transform bounding box.
Definition: AABox.h:184
Class that holds an RGBA color with 8-bits per component.
Definition: Color.h:16
static const Color sWhite
Definition: Color.h:60
A geometry primitive containing triangle batches for various lods.
Definition: DebugRenderer.h:167
AABox mBounds
Bounding box that encapsulates all LODs.
Definition: DebugRenderer.h:179
Array< LOD > mLODs
All level of details for this mesh.
Definition: DebugRenderer.h:176
JPH_OVERRIDE_NEW_DELETE Geometry(const AABox &inBounds)
Constructor.
Definition: DebugRenderer.h:172
Geometry(const Batch &inBatch, const AABox &inBounds)
Definition: DebugRenderer.h:173
A single level of detail.
Definition: DebugRenderer.h:159
Batch mTriangleBatch
Definition: DebugRenderer.h:161
float mDistance
Definition: DebugRenderer.h:162
A single triangle.
Definition: DebugRenderer.h:145
Vertex mV[3]
Definition: DebugRenderer.h:151
Vertex format used by the triangle renderer.
Definition: DebugRenderer.h:135
Float3 mPosition
Definition: DebugRenderer.h:137
Float2 mUV
Definition: DebugRenderer.h:139
Color mColor
Definition: DebugRenderer.h:140
Float3 mNormal
Definition: DebugRenderer.h:138
Simple triangle renderer for debugging purposes.
Definition: DebugRenderer.h:25
void DrawWireUnitSphere(RMat44Arg inMatrix, ColorArg inColor, int inLevel=3)
Definition: DebugRenderer.cpp:258
Batch CreateTriangleBatchForConvex(SupportFunction inGetSupport, int inLevel, AABox *outBounds=nullptr)
Definition: DebugRenderer.cpp:667
virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor=Color::sWhite, float inHeight=0.5f)=0
Draw text.
static AABox sCalculateBounds(const Vertex *inVertices, int inVertexCount)
Calculate bounding box for a batch of triangles.
Definition: DebugRenderer.cpp:620
GeometryRef CreateTriangleGeometryForConvex(SupportFunction inGetSupport)
Definition: DebugRenderer.cpp:688
void DrawCapsule(RMat44Arg inMatrix, float inHalfHeightOfCylinder, float inRadius, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.cpp:747
Ref< RefTargetVirtual > Batch
Handle for a batch of triangles.
Definition: DebugRenderer.h:155
void DrawMarker(RVec3Arg inPosition, ColorArg inColor, float inSize)
Draw a marker on a position.
Definition: DebugRenderer.cpp:172
void DrawPlane(RVec3Arg inPoint, Vec3Arg inNormal, ColorArg inColor, float inSize)
Draw a plane through inPoint with normal inNormal.
Definition: DebugRenderer.cpp:215
Ref< Geometry > GeometryRef
Handle for a lodded triangle batch.
Definition: DebugRenderer.h:183
void Initialize()
Initialize the system, must be called from the constructor of the DebugRenderer implementation.
Definition: DebugRenderer.cpp:396
virtual ~DebugRenderer()
Definition: DebugRenderer.cpp:79
void DrawBox(const AABox &inBox, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Draw a box.
Definition: DebugRenderer.cpp:713
void DrawWireTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor)
Draw wireframe triangle.
Definition: DebugRenderer.cpp:242
ECullMode
Determines which polygons are culled.
Definition: DebugRenderer.h:202
@ CullBackFace
Don't draw backfacing polygons.
@ CullFrontFace
Don't draw front facing polygons.
void DrawSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Draw a sphere.
Definition: DebugRenderer.cpp:731
JPH_OVERRIDE_NEW_DELETE DebugRenderer()
Constructor.
Definition: DebugRenderer.cpp:72
void DrawCoordinateSystem(RMat44Arg inTransform, float inSize=1.0f)
Draw coordinate system (3 arrows, x = red, y = green, z = blue)
Definition: DebugRenderer.cpp:206
virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount)=0
void DrawSwingLimits(RMat44Arg inMatrix, float inSwingYHalfAngle, float inSwingZHalfAngle, float inEdgeLength, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.cpp:800
virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount)=0
Create a batch of triangles that can be drawn efficiently.
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
void DrawPie(RVec3Arg inCenter, float inRadius, Vec3Arg inNormal, Vec3Arg inAxis, float inMinAngle, float inMaxAngle, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.cpp:930
ECastShadow
Enum that determines if a shadow should be cast or not.
Definition: DebugRenderer.h:66
void DrawWireSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, int inLevel=3)
Draw wireframe sphere.
Definition: DebugRenderer.cpp:251
static DebugRenderer * sInstance
Singleton instance.
Definition: DebugRenderer.h:131
void DrawWireBox(const AABox &inBox, ColorArg inColor)
Draw wireframe box.
Definition: DebugRenderer.cpp:85
Batch CreateTriangleBatch(const Array< Triangle > &inTriangles)
Definition: DebugRenderer.h:191
EDrawMode
Determines how triangles are drawn.
Definition: DebugRenderer.h:73
@ Wireframe
Draw as wireframe.
@ Solid
Draw as a solid shape.
void DrawOpenCone(RVec3Arg inTop, Vec3Arg inAxis, Vec3Arg inPerpendicular, float inHalfAngle, float inLength, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.cpp:781
virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor)=0
Draw a single back face culled triangle without any shadows.
Batch CreateTriangleBatch(const Array< Vertex > &inVertices, const Array< uint32 > &inIndices)
Definition: DebugRenderer.h:192
void DrawGeometry(RMat44Arg inModelMatrix, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode=ECullMode::CullBackFace, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.h:218
void DrawWirePolygon(RMat44Arg inTransform, const VERTEX_ARRAY &inVertices, ColorArg inColor, float inArrowSize=0.0f)
Draw a wireframe polygon.
Definition: DebugRenderer.h:58
virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor)=0
Draw line.
void DrawArrow(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor, float inSize)
Draw an arrow.
Definition: DebugRenderer.cpp:184
void DrawCylinder(RMat44Arg inMatrix, float inHalfHeight, float inRadius, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.cpp:771
function< Vec3(Vec3Arg inDirection)> SupportFunction
Create a primitive for a convex shape using its support function.
Definition: DebugRenderer.h:196
void DrawUnitSphere(RMat44Arg inMatrix, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.cpp:740
Class that holds 2 floats, used as a storage class mainly.
Definition: Float2.h:11
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition: Float3.h:13
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition: Mat44.h:13
JPH_INLINE Vec3 GetAxisY() const
Definition: Mat44.h:144
JPH_INLINE Vec3 GetAxisZ() const
Definition: Mat44.h:146
JPH_INLINE Vec3 GetAxisX() const
Access to the columns.
Definition: Mat44.h:142
Oriented box.
Definition: OrientedBox.h:18
Definition: Reference.h:35
Definition: Vec3.h:16
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:653