refactor: extract DebugLinesRenderer

This commit is contained in:
MihailRis
2025-08-03 02:03:20 +03:00
parent a78931205f
commit 3aac6ecbcb
5 changed files with 104 additions and 97 deletions
@@ -1,15 +1,49 @@
#include "GuidesRenderer.hpp" #include "DebugLinesRenderer.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include "graphics/core/Shader.hpp" #include "graphics/core/Shader.hpp"
#include "window/Camera.hpp"
#include "graphics/core/LineBatch.hpp" #include "graphics/core/LineBatch.hpp"
#include "graphics/core/DrawContext.hpp" #include "graphics/core/DrawContext.hpp"
#include "graphics/render/LinesRenderer.hpp"
#include "world/Level.hpp"
#include "voxels/Chunk.hpp"
#include "voxels/Pathfinding.hpp"
#include "maths/voxmaths.hpp" #include "maths/voxmaths.hpp"
#include "window/Camera.hpp"
#include "constants.hpp"
void GuidesRenderer::drawBorders( static void draw_route(
LinesRenderer& lines, const voxels::Agent& agent
) {
const auto& route = agent.route;
if (!route.found)
return;
for (int i = 1; i < route.nodes.size(); i++) {
const auto& a = route.nodes.at(i - 1);
const auto& b = route.nodes.at(i);
if (i == 1) {
lines.pushLine(
glm::vec3(a.pos) + glm::vec3(0.5f),
glm::vec3(a.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
glm::vec4(1, 1, 1, 1)
);
}
lines.pushLine(
glm::vec3(a.pos) + glm::vec3(0.5f),
glm::vec3(b.pos) + glm::vec3(0.5f),
glm::vec4(1, 0, 1, 1)
);
lines.pushLine(
glm::vec3(b.pos) + glm::vec3(0.5f),
glm::vec3(b.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
glm::vec4(1, 1, 1, 1)
);
}
}
void DebugLinesRenderer::drawBorders(
LineBatch& batch, int sx, int sy, int sz, int ex, int ey, int ez LineBatch& batch, int sx, int sy, int sz, int ex, int ey, int ez
) { ) {
int ww = ex - sx; int ww = ex - sx;
@@ -37,7 +71,7 @@ void GuidesRenderer::drawBorders(
batch.flush(); batch.flush();
} }
void GuidesRenderer::drawCoordSystem( void DebugLinesRenderer::drawCoordSystem(
LineBatch& batch, const DrawContext& pctx, float length LineBatch& batch, const DrawContext& pctx, float length
) { ) {
auto ctx = pctx.sub(); auto ctx = pctx.sub();
@@ -55,14 +89,20 @@ void GuidesRenderer::drawCoordSystem(
batch.line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 1.f); batch.line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 1.f);
} }
void GuidesRenderer::renderDebugLines(
const DrawContext& pctx, void DebugLinesRenderer::render(
DrawContext& pctx,
const Camera& camera, const Camera& camera,
LineBatch& batch, LinesRenderer& renderer,
LineBatch& linesBatch,
Shader& linesShader, Shader& linesShader,
bool showChunkBorders bool showChunkBorders
) { ) {
DrawContext ctx = pctx.sub(&batch); // In-world lines
for (const auto& [_, agent] : level.pathfinding->getAgents()) {
draw_route(renderer, agent);
}
DrawContext ctx = pctx.sub(&linesBatch);
const auto& viewport = ctx.getViewport(); const auto& viewport = ctx.getViewport();
ctx.setDepthTest(true); ctx.setDepthTest(true);
@@ -78,7 +118,7 @@ void GuidesRenderer::renderDebugLines(
int cz = floordiv(static_cast<int>(coord.z), CHUNK_D); int cz = floordiv(static_cast<int>(coord.z), CHUNK_D);
drawBorders( drawBorders(
batch, linesBatch,
cx * CHUNK_W, cx * CHUNK_W,
0, 0,
cz * CHUNK_D, cz * CHUNK_D,
@@ -103,5 +143,5 @@ void GuidesRenderer::renderDebugLines(
) * model * ) * model *
glm::inverse(camera.rotation) glm::inverse(camera.rotation)
); );
drawCoordSystem(batch, ctx, length); drawCoordSystem(linesBatch, ctx, length);
} }
@@ -0,0 +1,39 @@
#pragma once
class DrawContext;
class Camera;
class LineBatch;
class LinesRenderer;
class Shader;
class Level;
class DebugLinesRenderer {
public:
DebugLinesRenderer(const Level& level)
: level(level) {};
/// @brief Render debug lines in the world
/// @param ctx Draw context
/// @param camera Camera used for rendering
/// @param renderer Lines renderer used for rendering lines
/// @param linesShader Shader used for rendering lines
/// @param showChunkBorders Whether to show chunk borders
void render(
DrawContext& ctx,
const Camera& camera,
LinesRenderer& renderer,
LineBatch& linesBatch,
Shader& linesShader,
bool showChunkBorders
);
private:
const Level& level;
void drawBorders(
LineBatch& batch, int sx, int sy, int sz, int ex, int ey, int ez
);
void drawCoordSystem(
LineBatch& batch, const DrawContext& pctx, float length
);
};
-28
View File
@@ -1,28 +0,0 @@
#pragma once
class LineBatch;
class DrawContext;
class Camera;
class Shader;
class GuidesRenderer {
public:
void drawBorders(
LineBatch& batch, int sx, int sy, int sz, int ex, int ey, int ez
);
void drawCoordSystem(
LineBatch& batch, const DrawContext& pctx, float length
);
/// @brief Render all debug lines (chunks borders, coord system guides)
/// @param context graphics context
/// @param camera active camera
/// @param linesShader shader used
void renderDebugLines(
const DrawContext& context,
const Camera& camera,
LineBatch& batch,
Shader& linesShader,
bool showChunkBorders
);
};
+10 -54
View File
@@ -52,8 +52,8 @@
#include "NamedSkeletons.hpp" #include "NamedSkeletons.hpp"
#include "TextsRenderer.hpp" #include "TextsRenderer.hpp"
#include "ChunksRenderer.hpp" #include "ChunksRenderer.hpp"
#include "GuidesRenderer.hpp"
#include "LinesRenderer.hpp" #include "LinesRenderer.hpp"
#include "DebugLinesRenderer.hpp"
#include "ModelBatch.hpp" #include "ModelBatch.hpp"
#include "Skybox.hpp" #include "Skybox.hpp"
#include "Emitter.hpp" #include "Emitter.hpp"
@@ -80,7 +80,6 @@ WorldRenderer::WorldRenderer(
modelBatch(std::make_unique<ModelBatch>( modelBatch(std::make_unique<ModelBatch>(
MODEL_BATCH_CAPACITY, assets, *player.chunks, engine.getSettings() MODEL_BATCH_CAPACITY, assets, *player.chunks, engine.getSettings()
)), )),
guides(std::make_unique<GuidesRenderer>()),
chunksRenderer(std::make_unique<ChunksRenderer>( chunksRenderer(std::make_unique<ChunksRenderer>(
&level, &level,
*player.chunks, *player.chunks,
@@ -120,6 +119,7 @@ WorldRenderer::WorldRenderer(
); );
lines = std::make_unique<LinesRenderer>(); lines = std::make_unique<LinesRenderer>();
shadowMapping = std::make_unique<Shadows>(level); shadowMapping = std::make_unique<Shadows>(level);
debugLines = std::make_unique<DebugLinesRenderer>(level);
} }
WorldRenderer::~WorldRenderer() = default; WorldRenderer::~WorldRenderer() = default;
@@ -274,39 +274,6 @@ void WorldRenderer::renderLines(
} }
} }
static void draw_route(
LinesRenderer& lines, const voxels::Agent& agent
) {
const auto& route = agent.route;
if (!route.found)
return;
for (int i = 1; i < route.nodes.size(); i++) {
const auto& a = route.nodes.at(i - 1);
const auto& b = route.nodes.at(i);
if (i == 1) {
lines.pushLine(
glm::vec3(a.pos) + glm::vec3(0.5f),
glm::vec3(a.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
glm::vec4(1, 1, 1, 1)
);
}
lines.pushLine(
glm::vec3(a.pos) + glm::vec3(0.5f),
glm::vec3(b.pos) + glm::vec3(0.5f),
glm::vec4(1, 0, 1, 1)
);
lines.pushLine(
glm::vec3(b.pos) + glm::vec3(0.5f),
glm::vec3(b.pos) + glm::vec3(0.5f, 1.0f, 0.5f),
glm::vec4(1, 1, 1, 1)
);
}
}
void WorldRenderer::renderFrame( void WorldRenderer::renderFrame(
const DrawContext& pctx, const DrawContext& pctx,
Camera& camera, Camera& camera,
@@ -316,6 +283,9 @@ void WorldRenderer::renderFrame(
PostProcessing& postProcessing PostProcessing& postProcessing
) { ) {
// TODO: REFACTOR WHOLE RENDER ENGINE // TODO: REFACTOR WHOLE RENDER ENGINE
auto projView = camera.getProjView();
float delta = uiDelta * !pause; float delta = uiDelta * !pause;
timer += delta; timer += delta;
weather.update(delta); weather.update(delta);
@@ -373,9 +343,6 @@ void WorldRenderer::renderFrame(
setupWorldShader(shader, shadowCamera, engine.getSettings(), 0.0f); setupWorldShader(shader, shadowCamera, engine.getSettings(), 0.0f);
chunksRenderer->drawShadowsPass(shadowCamera, shader, camera); chunksRenderer->drawShadowsPass(shadowCamera, shader, camera);
}); });
auto& linesShader = assets.require<Shader>("lines");
{ {
DrawContext wctx = pctx.sub(); DrawContext wctx = pctx.sub();
postProcessing.use(wctx, gbufferPipeline); postProcessing.use(wctx, gbufferPipeline);
@@ -387,14 +354,6 @@ void WorldRenderer::renderFrame(
ctx.setDepthTest(true); ctx.setDepthTest(true);
ctx.setCullFace(true); ctx.setCullFace(true);
renderOpaque(ctx, camera, settings, uiDelta, pause, hudVisible); renderOpaque(ctx, camera, settings, uiDelta, pause, hudVisible);
// Debug lines
if (hudVisible) {
if (debug) {
guides->renderDebugLines(
ctx, camera, *lineBatch, linesShader, showChunkBorders
);
}
}
} }
texts->render(pctx, camera, settings, hudVisible, true); texts->render(pctx, camera, settings, hudVisible, true);
} }
@@ -419,15 +378,12 @@ void WorldRenderer::renderFrame(
// Background sky plane // Background sky plane
skybox->draw(ctx, camera, assets, worldInfo.daytime, clouds); skybox->draw(ctx, camera, assets, worldInfo.daytime, clouds);
// In-world lines auto& linesShader = assets.require<Shader>("lines");
if (debug) {
for (const auto& [_, agent] : level.pathfinding->getAgents()) {
draw_route(*lines, agent);
}
}
linesShader.use(); linesShader.use();
linesShader.uniformMatrix("u_projview", camera.getProjView()); debugLines->render(
ctx, camera, *lines, *lineBatch, linesShader, showChunkBorders
);
linesShader.uniformMatrix("u_projview", projView);
lines->draw(*lineBatch); lines->draw(*lineBatch);
lineBatch->flush(); lineBatch->flush();
+2 -2
View File
@@ -22,7 +22,6 @@ class BlockWrapsRenderer;
class PrecipitationRenderer; class PrecipitationRenderer;
class HandsRenderer; class HandsRenderer;
class NamedSkeletons; class NamedSkeletons;
class GuidesRenderer;
class LinesRenderer; class LinesRenderer;
class TextsRenderer; class TextsRenderer;
class Shader; class Shader;
@@ -36,6 +35,7 @@ class ModelBatch;
class Assets; class Assets;
class Shadows; class Shadows;
class GBuffer; class GBuffer;
class DebugLinesRenderer;
struct EngineSettings; struct EngineSettings;
struct CompileTimeShaderSettings { struct CompileTimeShaderSettings {
@@ -53,11 +53,11 @@ class WorldRenderer {
std::unique_ptr<LineBatch> lineBatch; std::unique_ptr<LineBatch> lineBatch;
std::unique_ptr<Batch3D> batch3d; std::unique_ptr<Batch3D> batch3d;
std::unique_ptr<ModelBatch> modelBatch; std::unique_ptr<ModelBatch> modelBatch;
std::unique_ptr<GuidesRenderer> guides;
std::unique_ptr<ChunksRenderer> chunksRenderer; std::unique_ptr<ChunksRenderer> chunksRenderer;
std::unique_ptr<HandsRenderer> hands; std::unique_ptr<HandsRenderer> hands;
std::unique_ptr<Skybox> skybox; std::unique_ptr<Skybox> skybox;
std::unique_ptr<Shadows> shadowMapping; std::unique_ptr<Shadows> shadowMapping;
std::unique_ptr<DebugLinesRenderer> debugLines;
Weather weather {}; Weather weather {};
float timer = 0.0f; float timer = 0.0f;