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#ifndef JPH_DEBUG_RENDERER_EXPORT
12 // By default export the debug renderer
13 #define JPH_DEBUG_RENDERER_EXPORT JPH_EXPORT
14#endif // !JPH_DEBUG_RENDERER_EXPORT
15
16#include <Jolt/Core/Color.h>
17#include <Jolt/Core/Reference.h>
21#include <Jolt/Math/Float2.h>
23#include <Jolt/Geometry/AABox.h>
24
26
27class OrientedBox;
28
47{
48public:
50
53 virtual ~DebugRenderer();
54
56 void NextFrame();
57
59 virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) = 0;
60
62 void DrawWireBox(const AABox &inBox, ColorArg inColor);
63 void DrawWireBox(const OrientedBox &inBox, ColorArg inColor);
64 void DrawWireBox(RMat44Arg inMatrix, const AABox &inBox, ColorArg inColor);
65
67 void DrawMarker(RVec3Arg inPosition, ColorArg inColor, float inSize);
68
70 void DrawArrow(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor, float inSize);
71
73 void DrawCoordinateSystem(RMat44Arg inTransform, float inSize = 1.0f);
74
76 void DrawPlane(RVec3Arg inPoint, Vec3Arg inNormal, ColorArg inColor, float inSize);
77
79 void DrawWireTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor);
80
82 template <class VERTEX_ARRAY>
83 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); }
84
86 void DrawWireSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, int inLevel = 3);
87 void DrawWireUnitSphere(RMat44Arg inMatrix, ColorArg inColor, int inLevel = 3);
88
90 enum class ECastShadow
91 {
92 On,
93 Off
94 };
95
97 enum class EDrawMode
98 {
99 Solid,
100 Wireframe,
101 };
102
104 virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::Off) = 0;
105
107 void DrawBox(const AABox &inBox, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
108 void DrawBox(RMat44Arg inMatrix, const AABox &inBox, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
109
111 void DrawSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
112 void DrawUnitSphere(RMat44Arg inMatrix, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
113
116 void DrawCapsule(RMat44Arg inMatrix, float inHalfHeightOfCylinder, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
117
120 void DrawCylinder(RMat44Arg inMatrix, float inHalfHeight, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
121
131 void DrawOpenCone(RVec3Arg inTop, Vec3Arg inAxis, Vec3Arg inPerpendicular, float inHalfAngle, float inLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
132
141 void DrawSwingConeLimits(RMat44Arg inMatrix, float inSwingYHalfAngle, float inSwingZHalfAngle, float inEdgeLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
142
153 void DrawSwingPyramidLimits(RMat44Arg inMatrix, float inMinSwingYAngle, float inMaxSwingYAngle, float inMinSwingZAngle, float inMaxSwingZAngle, float inEdgeLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
154
165 void DrawPie(RVec3Arg inCenter, float inRadius, Vec3Arg inNormal, Vec3Arg inAxis, float inMinAngle, float inMaxAngle, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
166
176 void DrawTaperedCylinder(RMat44Arg inMatrix, float inTop, float inBottom, float inTopRadius, float inBottomRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid);
177
180
182 class Vertex
183 {
184 public:
189 };
190
193 {
194 public:
195 Triangle() = default;
196 Triangle(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, ColorArg inColor);
197 Triangle(Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, ColorArg inColor, Vec3Arg inUVOrigin, Vec3Arg inUVDirection);
198
199 Vertex mV[3];
200 };
201
204
206 class LOD
207 {
208 public:
211 };
212
214 class Geometry : public RefTarget<Geometry>
215 {
216 public:
218
220 Geometry(const AABox &inBounds) : mBounds(inBounds) { }
221 Geometry(const Batch &inBatch, const AABox &inBounds) : mBounds(inBounds) { mLODs.push_back({ inBatch, FLT_MAX }); }
222
228 const LOD & GetLOD(Vec3Arg inCameraPosition, const AABox &inWorldSpaceBounds, float inLODScaleSq) const
229 {
230 float dist_sq = inWorldSpaceBounds.GetSqDistanceTo(inCameraPosition);
231 for (const LOD &lod : mLODs)
232 if (dist_sq <= inLODScaleSq * Square(lod.mDistance))
233 return lod;
234
235 return mLODs.back();
236 }
237
240
243 };
244
247
249 static AABox sCalculateBounds(const Vertex *inVertices, int inVertexCount);
250
252 virtual Batch CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) = 0;
253 virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) = 0;
254 Batch CreateTriangleBatch(const Array<Triangle> &inTriangles) { return CreateTriangleBatch(inTriangles.empty()? nullptr : &inTriangles[0], int(inTriangles.size())); }
255 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())); }
256 Batch CreateTriangleBatch(const VertexList &inVertices, const IndexedTriangleNoMaterialList &inTriangles);
257
259 using SupportFunction = function<Vec3 (Vec3Arg inDirection)>;
260 Batch CreateTriangleBatchForConvex(SupportFunction inGetSupport, int inLevel, AABox *outBounds = nullptr);
261 GeometryRef CreateTriangleGeometryForConvex(SupportFunction inGetSupport);
262
264 enum class ECullMode
265 {
266 CullBackFace,
267 CullFrontFace,
268 Off
269 };
270
280 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;
281 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); }
282
284 virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor = Color::sWhite, float inHeight = 0.5f) = 0;
285
286protected:
288 void Initialize();
289
290private:
292 void DrawWireUnitSphereRecursive(RMat44Arg inMatrix, ColorArg inColor, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, int inLevel);
293
295 void CreateQuad(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, Vec3Arg inV4);
296
298 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);
299 void Create8thSphere(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, const Float2 &inUV, SupportFunction inGetSupport, int inLevel);
300
302 Batch CreateCylinder(float inTop, float inBottom, float inTopRadius, float inBottomRadius, int inLevel);
303
305 Geometry * CreateSwingLimitGeometry(int inNumSegments, const Vec3 *inVertices);
306
307 // Predefined shapes
308 GeometryRef mBox;
309 GeometryRef mSphere;
310 GeometryRef mCapsuleTop;
311 GeometryRef mCapsuleMid;
312 GeometryRef mCapsuleBottom;
313 GeometryRef mOpenCone;
314 GeometryRef mCylinder;
315
316 struct SwingConeLimits
317 {
318 bool operator == (const SwingConeLimits &inRHS) const
319 {
320 return mSwingYHalfAngle == inRHS.mSwingYHalfAngle
321 && mSwingZHalfAngle == inRHS.mSwingZHalfAngle;
322 }
323
324 float mSwingYHalfAngle;
325 float mSwingZHalfAngle;
326 };
327
328 JPH_MAKE_HASH_STRUCT(SwingConeLimits, SwingConeLimitsHasher, t.mSwingYHalfAngle, t.mSwingZHalfAngle)
329
330 using SwingConeBatches = UnorderedMap<SwingConeLimits, GeometryRef, SwingConeLimitsHasher>;
331 SwingConeBatches mSwingConeLimits;
332 SwingConeBatches mPrevSwingConeLimits;
333
334 struct SwingPyramidLimits
335 {
336 bool operator == (const SwingPyramidLimits &inRHS) const
337 {
338 return mMinSwingYAngle == inRHS.mMinSwingYAngle
339 && mMaxSwingYAngle == inRHS.mMaxSwingYAngle
340 && mMinSwingZAngle == inRHS.mMinSwingZAngle
341 && mMaxSwingZAngle == inRHS.mMaxSwingZAngle;
342 }
343
344 float mMinSwingYAngle;
345 float mMaxSwingYAngle;
346 float mMinSwingZAngle;
347 float mMaxSwingZAngle;
348 };
349
350 JPH_MAKE_HASH_STRUCT(SwingPyramidLimits, SwingPyramidLimitsHasher, t.mMinSwingYAngle, t.mMaxSwingYAngle, t.mMinSwingZAngle, t.mMaxSwingZAngle)
351
352 using SwingPyramidBatches = UnorderedMap<SwingPyramidLimits, GeometryRef, SwingPyramidLimitsHasher>;
353 SwingPyramidBatches mSwingPyramidLimits;
354 SwingPyramidBatches mPrevSwingPyramidLimits;
355
356 using PieBatces = UnorderedMap<float, GeometryRef>;
357 PieBatces mPieLimits;
358 PieBatces mPrevPieLimits;
359
360 struct TaperedCylinder
361 {
362 bool operator == (const TaperedCylinder &inRHS) const
363 {
364 return mTop == inRHS.mTop
365 && mBottom == inRHS.mBottom
366 && mTopRadius == inRHS.mTopRadius
367 && mBottomRadius == inRHS.mBottomRadius;
368 }
369
370 float mTop;
371 float mBottom;
372 float mTopRadius;
373 float mBottomRadius;
374 };
375
376 JPH_MAKE_HASH_STRUCT(TaperedCylinder, TaperedCylinderHasher, t.mTop, t.mBottom, t.mTopRadius, t.mBottomRadius)
377
378 using TaperedCylinderBatces = UnorderedMap<TaperedCylinder, GeometryRef, TaperedCylinderHasher>;
379 TaperedCylinderBatces mTaperedCylinders;
380 TaperedCylinderBatces mPrevTaperedCylinders;
381};
382
#define JPH_NAMESPACE_END
Definition: Core.h:378
std::uint32_t uint32
Definition: Core.h:455
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:372
#define JPH_DEBUG_RENDERER_EXPORT
Definition: DebugRenderer.h:13
#define JPH_MAKE_HASH_STRUCT(type, name,...)
Definition: HashCombine.h:76
JPH_INLINE constexpr T Square(T inV)
Square a value.
Definition: Math.h:52
#define JPH_OVERRIDE_NEW_DELETE
Macro to override the new and delete functions.
Definition: Memory.h:31
@ Off
Motor is off.
std::unordered_map< Key, T, Hash, KeyEqual, STLAllocator< pair< const Key, T > > > UnorderedMap
Definition: UnorderedMap.h:13
Axis aligned box.
Definition: AABox.h:16
float GetSqDistanceTo(Vec3Arg inPoint) const
Get the squared distance between inPoint and this box (will be 0 if in Point is inside the box)
Definition: AABox.h:294
Definition: Array.h:36
bool empty() const
Returns true if there are no elements in the array.
Definition: Array.h:312
size_type size() const
Returns amount of elements in the array.
Definition: Array.h:318
Class that holds an RGBA color with 8-bits per component.
Definition: Color.h:16
static const Color sWhite
Definition: Color.h:67
A geometry primitive containing triangle batches for various lods.
Definition: DebugRenderer.h:215
AABox mBounds
Bounding box that encapsulates all LODs.
Definition: DebugRenderer.h:242
const LOD & GetLOD(Vec3Arg inCameraPosition, const AABox &inWorldSpaceBounds, float inLODScaleSq) const
Definition: DebugRenderer.h:228
Array< LOD > mLODs
All level of details for this mesh.
Definition: DebugRenderer.h:239
JPH_OVERRIDE_NEW_DELETE Geometry(const AABox &inBounds)
Constructor.
Definition: DebugRenderer.h:220
Geometry(const Batch &inBatch, const AABox &inBounds)
Definition: DebugRenderer.h:221
A single level of detail.
Definition: DebugRenderer.h:207
Batch mTriangleBatch
Definition: DebugRenderer.h:209
float mDistance
Definition: DebugRenderer.h:210
A single triangle.
Definition: DebugRenderer.h:193
Vertex format used by the triangle renderer.
Definition: DebugRenderer.h:183
Float3 mPosition
Definition: DebugRenderer.h:185
Float2 mUV
Definition: DebugRenderer.h:187
Color mColor
Definition: DebugRenderer.h:188
Float3 mNormal
Definition: DebugRenderer.h:186
Definition: DebugRenderer.h:47
virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor=Color::sWhite, float inHeight=0.5f)=0
Draw text.
ECullMode
Determines which polygons are culled.
Definition: DebugRenderer.h:265
virtual Batch CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount)=0
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
ECastShadow
Enum that determines if a shadow should be cast or not.
Definition: DebugRenderer.h:91
static DebugRenderer * sInstance
Singleton instance.
Definition: DebugRenderer.h:179
Batch CreateTriangleBatch(const Array< Triangle > &inTriangles)
Definition: DebugRenderer.h:254
EDrawMode
Determines how triangles are drawn.
Definition: DebugRenderer.h:98
Batch CreateTriangleBatch(const Array< Vertex > &inVertices, const Array< uint32 > &inIndices)
Definition: DebugRenderer.h:255
void DrawGeometry(RMat44Arg inModelMatrix, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode=ECullMode::CullBackFace, ECastShadow inCastShadow=ECastShadow::On, EDrawMode inDrawMode=EDrawMode::Solid)
Definition: DebugRenderer.h:281
void DrawWirePolygon(RMat44Arg inTransform, const VERTEX_ARRAY &inVertices, ColorArg inColor, float inArrowSize=0.0f)
Draw a wireframe polygon.
Definition: DebugRenderer.h:83
virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor)=0
Draw line.
function< Vec3(Vec3Arg inDirection)> SupportFunction
Create a primitive for a convex shape using its support function.
Definition: DebugRenderer.h:259
virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow=ECastShadow::Off)=0
Draw a single back face culled triangle.
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:148
JPH_INLINE Vec3 GetAxisZ() const
Definition: Mat44.h:150
JPH_INLINE Vec3 GetAxisX() const
Access to the columns.
Definition: Mat44.h:146
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition: NonCopyable.h:11
Oriented box.
Definition: OrientedBox.h:18
Definition: Reference.h:35
Definition: Vec3.h:17
JPH_INLINE float LengthSq() const
Squared length of vector.
Definition: Vec3.inl:665