DebugRendererPlayback.cpp 4.5 KB

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