0.2.0-dev Download ZIP
80 lines ·2.4 KB Raw
| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2024 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <Jolt/Jolt.h> |
| 6 | |
| 7 | #ifdef JPH_DEBUG_RENDERER |
| 8 | |
| 9 | #include <Jolt/Renderer/DebugRendererSimple.h> |
| 10 | |
| 11 | JPH_NAMESPACE_BEGIN |
| 12 | |
| 13 | DebugRendererSimple::DebugRendererSimple() |
| 14 | { |
| 15 | Initialize(); |
| 16 | } |
| 17 | |
| 18 | DebugRenderer::Batch DebugRendererSimple::CreateTriangleBatch(const Triangle *inTriangles, int inTriangleCount) |
| 19 | { |
| 20 | BatchImpl *batch = new BatchImpl; |
| 21 | if (inTriangles == nullptr || inTriangleCount == 0) |
| 22 | return batch; |
| 23 | |
| 24 | batch->mTriangles.assign(inTriangles, inTriangles + inTriangleCount); |
| 25 | return batch; |
| 26 | } |
| 27 | |
| 28 | DebugRenderer::Batch DebugRendererSimple::CreateTriangleBatch(const Vertex *inVertices, int inVertexCount, const uint32 *inIndices, int inIndexCount) |
| 29 | { |
| 30 | BatchImpl *batch = new BatchImpl; |
| 31 | if (inVertices == nullptr || inVertexCount == 0 || inIndices == nullptr || inIndexCount == 0) |
| 32 | return batch; |
| 33 | |
| 34 | // Convert indexed triangle list to triangle list |
| 35 | batch->mTriangles.resize(inIndexCount / 3); |
| 36 | for (size_t t = 0; t < batch->mTriangles.size(); ++t) |
| 37 | { |
| 38 | Triangle &triangle = batch->mTriangles[t]; |
| 39 | triangle.mV[0] = inVertices[inIndices[t * 3 + 0]]; |
| 40 | triangle.mV[1] = inVertices[inIndices[t * 3 + 1]]; |
| 41 | triangle.mV[2] = inVertices[inIndices[t * 3 + 2]]; |
| 42 | } |
| 43 | |
| 44 | return batch; |
| 45 | } |
| 46 | |
| 47 | void DebugRendererSimple::DrawGeometry(RMat44Arg inModelMatrix, const AABox &inWorldSpaceBounds, float inLODScaleSq, ColorArg inModelColor, const GeometryRef &inGeometry, ECullMode inCullMode, ECastShadow inCastShadow, EDrawMode inDrawMode) |
| 48 | { |
| 49 | // Figure out which LOD to use |
| 50 | const LOD *lod = inGeometry->mLODs.data(); |
| 51 | if (mCameraPosSet) |
| 52 | lod = &inGeometry->GetLOD(Vec3(mCameraPos), inWorldSpaceBounds, inLODScaleSq); |
| 53 | |
| 54 | // Draw the batch |
| 55 | const BatchImpl *batch = static_cast<const BatchImpl *>(lod->mTriangleBatch.GetPtr()); |
| 56 | for (const Triangle &triangle : batch->mTriangles) |
| 57 | { |
| 58 | RVec3 v0 = inModelMatrix * Vec3(triangle.mV[0].mPosition); |
| 59 | RVec3 v1 = inModelMatrix * Vec3(triangle.mV[1].mPosition); |
| 60 | RVec3 v2 = inModelMatrix * Vec3(triangle.mV[2].mPosition); |
| 61 | Color color = inModelColor * triangle.mV[0].mColor; |
| 62 | |
| 63 | switch (inDrawMode) |
| 64 | { |
| 65 | case EDrawMode::Wireframe: |
| 66 | DrawLine(v0, v1, color); |
| 67 | DrawLine(v1, v2, color); |
| 68 | DrawLine(v2, v0, color); |
| 69 | break; |
| 70 | |
| 71 | case EDrawMode::Solid: |
| 72 | DrawTriangle(v0, v1, v2, color, inCastShadow); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | JPH_NAMESPACE_END |
| 79 | |
| 80 | #endif // JPH_DEBUG_RENDERER |