| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
| 10 |
|
| 11 |
#ifndef JPH_DEBUG_RENDERER_EXPORT |
| 12 |
|
| 13 |
#define JPH_DEBUG_RENDERER_EXPORT JPH_EXPORT |
| 14 |
#endif |
| 15 |
|
| 16 |
#include <Jolt/Core/Color.h> |
| 17 |
#include <Jolt/Core/Reference.h> |
| 18 |
#include <Jolt/Core/HashCombine.h> |
| 19 |
#include <Jolt/Core/UnorderedMap.h> |
| 20 |
#include <Jolt/Core/NonCopyable.h> |
| 21 |
#include <Jolt/Math/Float2.h> |
| 22 |
#include <Jolt/Geometry/IndexedTriangle.h> |
| 23 |
#include <Jolt/Geometry/AABox.h> |
| 24 |
|
| 25 |
JPH_NAMESPACE_BEGIN |
| 26 |
|
| 27 |
class OrientedBox; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class JPH_DEBUG_RENDERER_EXPORT DebugRenderer : public NonCopyable |
| 47 |
{ |
| 48 |
public: |
| 49 |
JPH_OVERRIDE_NEW_DELETE |
| 50 |
|
| 51 |
|
| 52 |
DebugRenderer(); |
| 53 |
virtual ~DebugRenderer(); |
| 54 |
|
| 55 |
|
| 56 |
void NextFrame(); |
| 57 |
|
| 58 |
|
| 59 |
virtual void DrawLine(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor) = 0; |
| 60 |
|
| 61 |
|
| 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 |
|
| 66 |
|
| 67 |
void DrawMarker(RVec3Arg inPosition, ColorArg inColor, float inSize); |
| 68 |
|
| 69 |
|
| 70 |
void DrawArrow(RVec3Arg inFrom, RVec3Arg inTo, ColorArg inColor, float inSize); |
| 71 |
|
| 72 |
|
| 73 |
void DrawCoordinateSystem(RMat44Arg inTransform, float inSize = 1.0f); |
| 74 |
|
| 75 |
|
| 76 |
void DrawPlane(RVec3Arg inPoint, Vec3Arg inNormal, ColorArg inColor, float inSize); |
| 77 |
|
| 78 |
|
| 79 |
void DrawWireTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor); |
| 80 |
|
| 81 |
|
| 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 |
|
| 85 |
|
| 86 |
void DrawWireSphere(RVec3Arg inCenter, float inRadius, ColorArg inColor, int inLevel = 3); |
| 87 |
void DrawWireUnitSphere(RMat44Arg inMatrix, ColorArg inColor, int inLevel = 3); |
| 88 |
|
| 89 |
|
| 90 |
enum class ECastShadow |
| 91 |
{ |
| 92 |
On, |
| 93 |
Off |
| 94 |
}; |
| 95 |
|
| 96 |
|
| 97 |
enum class EDrawMode |
| 98 |
{ |
| 99 |
Solid, |
| 100 |
Wireframe, |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
virtual void DrawTriangle(RVec3Arg inV1, RVec3Arg inV2, RVec3Arg inV3, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::Off) = 0; |
| 105 |
|
| 106 |
|
| 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 |
|
| 110 |
|
| 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 |
|
| 114 |
|
| 115 |
|
| 116 |
void DrawCapsule(RMat44Arg inMatrix, float inHalfHeightOfCylinder, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
void DrawCylinder(RMat44Arg inMatrix, float inHalfHeight, float inRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
void DrawOpenCone(RVec3Arg inTop, Vec3Arg inAxis, Vec3Arg inPerpendicular, float inHalfAngle, float inLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid); |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
void DrawSwingConeLimits(RMat44Arg inMatrix, float inSwingYHalfAngle, float inSwingZHalfAngle, float inEdgeLength, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid); |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 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 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
void DrawTaperedCylinder(RMat44Arg inMatrix, float inTop, float inBottom, float inTopRadius, float inBottomRadius, ColorArg inColor, ECastShadow inCastShadow = ECastShadow::On, EDrawMode inDrawMode = EDrawMode::Solid); |
| 177 |
|
| 178 |
|
| 179 |
static DebugRenderer * sInstance; |
| 180 |
|
| 181 |
|
| 182 |
class Vertex |
| 183 |
{ |
| 184 |
public: |
| 185 |
Float3 mPosition; |
| 186 |
Float3 mNormal; |
| 187 |
Float2 mUV; |
| 188 |
Color mColor; |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
class JPH_DEBUG_RENDERER_EXPORT Triangle |
| 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 |
|
| 202 |
|
| 203 |
using Batch = Ref<RefTargetVirtual>; |
| 204 |
|
| 205 |
|
| 206 |
class LOD |
| 207 |
{ |
| 208 |
public: |
| 209 |
Batch mTriangleBatch; |
| 210 |
float mDistance; |
| 211 |
}; |
| 212 |
|
| 213 |
|
| 214 |
class Geometry : public RefTarget<Geometry> |
| 215 |
{ |
| 216 |
public: |
| 217 |
JPH_OVERRIDE_NEW_DELETE |
| 218 |
|
| 219 |
|
| 220 |
Geometry(const AABox &inBounds) : mBounds(inBounds) { } |
| 221 |
Geometry(const Batch &inBatch, const AABox &inBounds) : mBounds(inBounds) { mLODs.push_back({ inBatch, cLargeFloat }); } |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 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 |
|
| 238 |
|
| 239 |
Array<LOD> mLODs; |
| 240 |
|
| 241 |
|
| 242 |
AABox mBounds; |
| 243 |
}; |
| 244 |
|
| 245 |
|
| 246 |
using GeometryRef = Ref<Geometry>; |
| 247 |
|
| 248 |
|
| 249 |
static AABox sCalculateBounds(const Vertex *inVertices, int inVertexCount); |
| 250 |
|
| 251 |
|
| 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 |
|
| 258 |
|
| 259 |
using SupportFunction = function<Vec3 (Vec3Arg inDirection)>; |
| 260 |
Batch CreateTriangleBatchForConvex(SupportFunction inGetSupport, int inLevel, AABox *outBounds = nullptr); |
| 261 |
GeometryRef CreateTriangleGeometryForConvex(SupportFunction inGetSupport); |
| 262 |
|
| 263 |
|
| 264 |
enum class ECullMode |
| 265 |
{ |
| 266 |
CullBackFace, |
| 267 |
CullFrontFace, |
| 268 |
Off |
| 269 |
}; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 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 |
|
| 283 |
|
| 284 |
virtual void DrawText3D(RVec3Arg inPosition, const string_view &inString, ColorArg inColor = Color::sWhite, float inHeight = 0.5f) = 0; |
| 285 |
|
| 286 |
protected: |
| 287 |
|
| 288 |
void Initialize(); |
| 289 |
|
| 290 |
private: |
| 291 |
|
| 292 |
void DrawWireUnitSphereRecursive(RMat44Arg inMatrix, ColorArg inColor, Vec3Arg inDir1, Vec3Arg inDir2, Vec3Arg inDir3, int inLevel); |
| 293 |
|
| 294 |
|
| 295 |
void CreateQuad(Array<uint32> &ioIndices, Array<Vertex> &ioVertices, Vec3Arg inV1, Vec3Arg inV2, Vec3Arg inV3, Vec3Arg inV4); |
| 296 |
|
| 297 |
|
| 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 |
|
| 301 |
|
| 302 |
Batch CreateCylinder(float inTop, float inBottom, float inTopRadius, float inBottomRadius, int inLevel); |
| 303 |
|
| 304 |
|
| 305 |
Geometry * CreateSwingLimitGeometry(int inNumSegments, const Vec3 *inVertices); |
| 306 |
|
| 307 |
|
| 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 |
|
| 383 |
JPH_NAMESPACE_END |