| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <Jolt/Jolt.h> |
| 6 | |
| 7 | #ifdef JPH_DEBUG_RENDERER |
| 8 | |
| 9 | #include <Jolt/Renderer/DebugRendererPlayback.h> |
| 10 | |
| 11 | JPH_NAMESPACE_BEGIN |
| 12 | |
| 13 | void DebugRendererPlayback::Parse(StreamIn &inStream) |
| 14 | { |
| 15 | using ECommand = DebugRendererRecorder::ECommand; |
| 16 | |
| 17 | for (;;) |
| 18 | { |
| 19 | // Read the next command |
| 20 | ECommand command; |
| 21 | inStream.Read(command); |
| 22 | |
| 23 | if (inStream.IsEOF() || inStream.IsFailed()) |
| 24 | return; |
| 25 | |
| 26 | if (command == ECommand::CreateBatch) |
| 27 | { |
| 28 | uint32 id; |
| 29 | inStream.Read(id); |
| 30 | |
| 31 | uint32 triangle_count; |
| 32 | inStream.Read(triangle_count); |
| 33 | |
| 34 | DebugRenderer::Triangle *triangles = new DebugRenderer::Triangle [triangle_count]; |
| 35 | inStream.ReadBytes(triangles, triangle_count * sizeof(DebugRenderer::Triangle)); |
| 36 | |
| 37 | mBatches.insert({ id, mRenderer.CreateTriangleBatch(triangles, triangle_count) }); |
| 38 | |
| 39 | delete [] triangles; |
| 40 | } |
| 41 | else if (command == ECommand::CreateBatchIndexed) |
| 42 | { |
| 43 | uint32 id; |
| 44 | inStream.Read(id); |
| 45 | |
| 46 | uint32 vertex_count; |
| 47 | inStream.Read(vertex_count); |
| 48 | |
| 49 | DebugRenderer::Vertex *vertices = new DebugRenderer::Vertex [vertex_count]; |
| 50 | inStream.ReadBytes(vertices, vertex_count * sizeof(DebugRenderer::Vertex)); |
| 51 | |
| 52 | uint32 index_count; |
| 53 | inStream.Read(index_count); |
| 54 | |
| 55 | uint32 *indices = new uint32 [index_count]; |
| 56 | inStream.ReadBytes(indices, index_count * sizeof(uint32)); |
| 57 | |
| 58 | mBatches.insert({ id, mRenderer.CreateTriangleBatch(vertices, vertex_count, indices, index_count) }); |
| 59 | |
| 60 | delete [] indices; |
| 61 | delete [] vertices; |
| 62 | } |
| 63 | else if (command == ECommand::CreateGeometry) |
| 64 | { |
| 65 | uint32 geometry_id; |
| 66 | inStream.Read(geometry_id); |
| 67 | |
| 68 | AABox bounds; |
| 69 | inStream.Read(bounds.mMin); |
| 70 | inStream.Read(bounds.mMax); |
| 71 | |
| 72 | DebugRenderer::GeometryRef geometry = new DebugRenderer::Geometry(bounds); |
| 73 | mGeometries[geometry_id] = geometry; |
| 74 | |
| 75 | uint32 num_lods; |
| 76 | inStream.Read(num_lods); |
| 77 | for (uint32 l = 0; l < num_lods; ++l) |
| 78 | { |
| 79 | DebugRenderer::LOD lod; |
| 80 | inStream.Read(lod.mDistance); |
| 81 | |
| 82 | uint32 batch_id; |
| 83 | inStream.Read(batch_id); |
| 84 | lod.mTriangleBatch = mBatches.find(batch_id)->second; |
| 85 | |
| 86 | geometry->mLODs.push_back(lod); |
| 87 | } |
| 88 | } |
| 89 | else if (command == ECommand::EndFrame) |
| 90 | { |
| 91 | mFrames.push_back({}); |
| 92 | Frame &frame = mFrames.back(); |
| 93 | |
| 94 | // Read all lines |
| 95 | uint32 num_lines = 0; |
| 96 | inStream.Read(num_lines); |
| 97 | frame.mLines.resize(num_lines); |
| 98 | for (DebugRendererRecorder::LineBlob &line : frame.mLines) |
| 99 | { |
| 100 | inStream.Read(line.mFrom); |
| 101 | inStream.Read(line.mTo); |
| 102 | inStream.Read(line.mColor); |
| 103 | } |
| 104 | |
| 105 | // Read all triangles |
| 106 | uint32 num_triangles = 0; |
| 107 | inStream.Read(num_triangles); |
| 108 | frame.mTriangles.resize(num_triangles); |
| 109 | for (DebugRendererRecorder::TriangleBlob &triangle : frame.mTriangles) |
| 110 | { |
| 111 | inStream.Read(triangle.mV1); |
| 112 | inStream.Read(triangle.mV2); |
| 113 | inStream.Read(triangle.mV3); |
| 114 | inStream.Read(triangle.mColor); |
| 115 | inStream.Read(triangle.mCastShadow); |
| 116 | } |
| 117 | |
| 118 | // Read all texts |
| 119 | uint32 num_texts = 0; |
| 120 | inStream.Read(num_texts); |
| 121 | frame.mTexts.resize(num_texts); |
| 122 | for (DebugRendererRecorder::TextBlob &text : frame.mTexts) |
| 123 | { |
| 124 | inStream.Read(text.mPosition); |
| 125 | inStream.Read(text.mString); |
| 126 | inStream.Read(text.mColor); |
| 127 | inStream.Read(text.mHeight); |
| 128 | } |
| 129 | |
| 130 | // Read all geometries |
| 131 | uint32 num_geometries = 0; |
| 132 | inStream.Read(num_geometries); |
| 133 | frame.mGeometries.resize(num_geometries); |
| 134 | for (DebugRendererRecorder::GeometryBlob &geom : frame.mGeometries) |
| 135 | { |
| 136 | inStream.Read(geom.mModelMatrix); |
| 137 | inStream.Read(geom.mModelColor); |
| 138 | inStream.Read(geom.mGeometryID); |
| 139 | inStream.Read(geom.mCullMode); |
| 140 | inStream.Read(geom.mCastShadow); |
| 141 | inStream.Read(geom.mDrawMode); |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | JPH_ASSERT(false); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | void DebugRendererPlayback::DrawFrame(uint inFrameNumber) const |
| 150 | { |
| 151 | const Frame &frame = mFrames[inFrameNumber]; |
| 152 | |
| 153 | for (const DebugRendererRecorder::LineBlob &line : frame.mLines) |
| 154 | mRenderer.DrawLine(line.mFrom, line.mTo, line.mColor); |
| 155 | |
| 156 | for (const DebugRendererRecorder::TriangleBlob &triangle : frame.mTriangles) |
| 157 | mRenderer.DrawTriangle(triangle.mV1, triangle.mV2, triangle.mV3, triangle.mColor, triangle.mCastShadow); |
| 158 | |
| 159 | for (const DebugRendererRecorder::TextBlob &text : frame.mTexts) |
| 160 | mRenderer.DrawText3D(text.mPosition, text.mString, text.mColor, text.mHeight); |
| 161 | |
| 162 | for (const DebugRendererRecorder::GeometryBlob &geom : frame.mGeometries) |
| 163 | mRenderer.DrawGeometry(geom.mModelMatrix, geom.mModelColor, mGeometries.find(geom.mGeometryID)->second, geom.mCullMode, geom.mCastShadow, geom.mDrawMode); |
| 164 | } |
| 165 | |
| 166 | JPH_NAMESPACE_END |
| 167 | |
| 168 | #endif // JPH_DEBUG_RENDERER |