DebugRendererPlayback.cpp 4.4 KB

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