Skip to content

Commit

Permalink
refactor WorldRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Nov 14, 2024
1 parent 42b2670 commit f9f4d20
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 110 deletions.
109 changes: 109 additions & 0 deletions src/graphics/render/GuidesRenderer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "GuidesRenderer.hpp"

#include <glm/gtc/matrix_transform.hpp>

#include "graphics/core/Shader.hpp"
#include "graphics/core/LineBatch.hpp"
#include "graphics/core/DrawContext.hpp"
#include "maths/voxmaths.hpp"
#include "window/Camera.hpp"
#include "constants.hpp"

void GuidesRenderer::drawBorders(
LineBatch& batch, int sx, int sy, int sz, int ex, int ey, int ez
) {
int ww = ex - sx;
int dd = ez - sz;
/*corner*/ {
batch.line(sx, sy, sz, sx, ey, sz, 0.8f, 0, 0.8f, 1);
batch.line(sx, sy, ez, sx, ey, ez, 0.8f, 0, 0.8f, 1);
batch.line(ex, sy, sz, ex, ey, sz, 0.8f, 0, 0.8f, 1);
batch.line(ex, sy, ez, ex, ey, ez, 0.8f, 0, 0.8f, 1);
}
for (int i = 2; i < ww; i += 2) {
batch.line(sx + i, sy, sz, sx + i, ey, sz, 0, 0, 0.8f, 1);
batch.line(sx + i, sy, ez, sx + i, ey, ez, 0, 0, 0.8f, 1);
}
for (int i = 2; i < dd; i += 2) {
batch.line(sx, sy, sz + i, sx, ey, sz + i, 0.8f, 0, 0, 1);
batch.line(ex, sy, sz + i, ex, ey, sz + i, 0.8f, 0, 0, 1);
}
for (int i = sy; i < ey; i += 2) {
batch.line(sx, i, sz, sx, i, ez, 0, 0.8f, 0, 1);
batch.line(sx, i, ez, ex, i, ez, 0, 0.8f, 0, 1);
batch.line(ex, i, ez, ex, i, sz, 0, 0.8f, 0, 1);
batch.line(ex, i, sz, sx, i, sz, 0, 0.8f, 0, 1);
}
batch.flush();
}

void GuidesRenderer::drawCoordSystem(
LineBatch& batch, const DrawContext& pctx, float length
) {
auto ctx = pctx.sub();
ctx.setDepthTest(false);
batch.lineWidth(4.0f);
batch.line(0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f);
batch.line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 1.f);
batch.line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 1.f);
batch.flush();

ctx.setDepthTest(true);
batch.lineWidth(2.0f);
batch.line(0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f);
batch.line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.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,
const Camera& camera,
LineBatch& batch,
Shader& linesShader,
bool showChunkBorders
) {
DrawContext ctx = pctx.sub(&batch);
const auto& viewport = ctx.getViewport();
uint displayWidth = viewport.getWidth();
uint displayHeight = viewport.getHeight();

ctx.setDepthTest(true);

linesShader.use();

if (showChunkBorders) {
linesShader.uniformMatrix("u_projview", camera.getProjView());
glm::vec3 coord = camera.position;
if (coord.x < 0) coord.x--;
if (coord.z < 0) coord.z--;
int cx = floordiv(static_cast<int>(coord.x), CHUNK_W);
int cz = floordiv(static_cast<int>(coord.z), CHUNK_D);

drawBorders(
batch,
cx * CHUNK_W,
0,
cz * CHUNK_D,
(cx + 1) * CHUNK_W,
CHUNK_H,
(cz + 1) * CHUNK_D
);
}

float length = 40.f;
glm::vec3 tsl(displayWidth / 2, displayHeight / 2, 0.f);
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
linesShader.uniformMatrix(
"u_projview",
glm::ortho(
0.f,
static_cast<float>(displayWidth),
0.f,
static_cast<float>(displayHeight),
-length,
length
) * model *
glm::inverse(camera.rotation)
);
drawCoordSystem(batch, ctx, length);
}
28 changes: 28 additions & 0 deletions src/graphics/render/GuidesRenderer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#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
);
};
2 changes: 1 addition & 1 deletion src/graphics/render/ParticlesRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ size_t ParticlesRenderer::aliveEmitters = 0;
ParticlesRenderer::ParticlesRenderer(
const Assets& assets, const Level& level, const GraphicsSettings* settings
)
: batch(std::make_unique<MainBatch>(1024)),
: batch(std::make_unique<MainBatch>(4096)),
level(level),
assets(assets),
settings(settings) {}
Expand Down
108 changes: 10 additions & 98 deletions src/graphics/render/WorldRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "ParticlesRenderer.hpp"
#include "TextsRenderer.hpp"
#include "ChunksRenderer.hpp"
#include "GuidesRenderer.hpp"
#include "ModelBatch.hpp"
#include "Skybox.hpp"
#include "Emitter.hpp"
Expand Down Expand Up @@ -72,7 +73,8 @@ WorldRenderer::WorldRenderer(
*frontend->getLevel(),
&engine->getSettings().graphics
)),
texts(std::make_unique<TextsRenderer>(frustumCulling.get())) {
texts(std::make_unique<TextsRenderer>(frustumCulling.get())),
guides(std::make_unique<GuidesRenderer>()) {
renderer = std::make_unique<ChunksRenderer>(
level, frontend->getContentGfxCache(), &engine->getSettings()
);
Expand All @@ -96,10 +98,7 @@ bool WorldRenderer::drawChunk(
size_t index, const Camera& camera, Shader& shader, bool culling
) {
auto chunk = level->chunks->getChunks()[index];
if (chunk == nullptr) {
return false;
}
if (!chunk->flags.lighted) {
if (chunk == nullptr || !chunk->flags.lighted) {
return false;
}
float distance = glm::distance(
Expand Down Expand Up @@ -297,66 +296,6 @@ void WorldRenderer::renderLines(
}
}

void WorldRenderer::renderDebugLines(
const DrawContext& pctx, const Camera& camera, Shader& linesShader
) {
DrawContext ctx = pctx.sub(lineBatch.get());
const auto& viewport = ctx.getViewport();
uint displayWidth = viewport.getWidth();
uint displayHeight = viewport.getHeight();

ctx.setDepthTest(true);

linesShader.use();

if (showChunkBorders) {
linesShader.uniformMatrix("u_projview", camera.getProjView());
glm::vec3 coord = player->fpCamera->position;
if (coord.x < 0) coord.x--;
if (coord.z < 0) coord.z--;
int cx = floordiv(static_cast<int>(coord.x), CHUNK_W);
int cz = floordiv(static_cast<int>(coord.z), CHUNK_D);

drawBorders(
cx * CHUNK_W,
0,
cz * CHUNK_D,
(cx + 1) * CHUNK_W,
CHUNK_H,
(cz + 1) * CHUNK_D
);
}

float length = 40.f;
glm::vec3 tsl(displayWidth / 2, displayHeight / 2, 0.f);
glm::mat4 model(glm::translate(glm::mat4(1.f), tsl));
linesShader.uniformMatrix(
"u_projview",
glm::ortho(
0.f,
static_cast<float>(displayWidth),
0.f,
static_cast<float>(displayHeight),
-length,
length
) * model *
glm::inverse(camera.rotation)
);

ctx.setDepthTest(false);
lineBatch->lineWidth(4.0f);
lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 0.f, 1.f);
lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 0.f, 1.f);
lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 0.f, 1.f);
lineBatch->flush();

ctx.setDepthTest(true);
lineBatch->lineWidth(2.0f);
lineBatch->line(0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 0.f, 1.f);
lineBatch->line(0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 0.f, 1.f);
lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 1.f);
}

void WorldRenderer::renderHands(
const Camera& camera, const Assets& assets, float delta
) {
Expand Down Expand Up @@ -441,8 +380,7 @@ void WorldRenderer::draw(
const auto& assets = *engine->getAssets();
auto& linesShader = assets.require<Shader>("lines");

// World render scope with diegetic HUD included
{
/* World render scope with diegetic HUD included */ {
DrawContext wctx = pctx.sub();
postProcessing->use(wctx);

Expand All @@ -451,8 +389,8 @@ void WorldRenderer::draw(
// Drawing background sky plane
skybox->draw(pctx, camera, assets, worldInfo.daytime, worldInfo.fog);

// Actually world render with depth buffer on
{

/* Actually world render with depth buffer on */ {
DrawContext ctx = wctx.sub();
ctx.setDepthTest(true);
ctx.setCullFace(true);
Expand All @@ -463,7 +401,9 @@ void WorldRenderer::draw(
// Debug lines
if (hudVisible) {
if (player->debug) {
renderDebugLines(ctx, camera, linesShader);
guides->renderDebugLines(
ctx, camera, *lineBatch, linesShader, showChunkBorders
);
}
renderLines(camera, linesShader, ctx);
if (player->currentCamera == player->fpCamera) {
Expand Down Expand Up @@ -527,34 +467,6 @@ void WorldRenderer::renderBlockOverlay(const DrawContext& wctx, const Assets& as
}
}

void WorldRenderer::drawBorders(
int sx, int sy, int sz, int ex, int ey, int ez
) {
int ww = ex - sx;
int dd = ez - sz;
/*corner*/ {
lineBatch->line(sx, sy, sz, sx, ey, sz, 0.8f, 0, 0.8f, 1);
lineBatch->line(sx, sy, ez, sx, ey, ez, 0.8f, 0, 0.8f, 1);
lineBatch->line(ex, sy, sz, ex, ey, sz, 0.8f, 0, 0.8f, 1);
lineBatch->line(ex, sy, ez, ex, ey, ez, 0.8f, 0, 0.8f, 1);
}
for (int i = 2; i < ww; i += 2) {
lineBatch->line(sx + i, sy, sz, sx + i, ey, sz, 0, 0, 0.8f, 1);
lineBatch->line(sx + i, sy, ez, sx + i, ey, ez, 0, 0, 0.8f, 1);
}
for (int i = 2; i < dd; i += 2) {
lineBatch->line(sx, sy, sz + i, sx, ey, sz + i, 0.8f, 0, 0, 1);
lineBatch->line(ex, sy, sz + i, ex, ey, sz + i, 0.8f, 0, 0, 1);
}
for (int i = sy; i < ey; i += 2) {
lineBatch->line(sx, i, sz, sx, i, ez, 0, 0.8f, 0, 1);
lineBatch->line(sx, i, ez, ex, i, ez, 0, 0.8f, 0, 1);
lineBatch->line(ex, i, ez, ex, i, sz, 0, 0.8f, 0, 1);
lineBatch->line(ex, i, sz, sx, i, sz, 0, 0.8f, 0, 1);
}
lineBatch->flush();
}

void WorldRenderer::clear() {
renderer->clear();
}
13 changes: 2 additions & 11 deletions src/graphics/render/WorldRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Batch3D;
class LineBatch;
class ChunksRenderer;
class ParticlesRenderer;
class GuidesRenderer;
class TextsRenderer;
class Shader;
class Frustum;
Expand Down Expand Up @@ -52,6 +53,7 @@ class WorldRenderer {
std::unique_ptr<LineBatch> lineBatch;
std::unique_ptr<ChunksRenderer> renderer;
std::unique_ptr<TextsRenderer> texts;
std::unique_ptr<GuidesRenderer> guides;
std::unique_ptr<Skybox> skybox;
std::unique_ptr<Batch3D> batch3d;
std::unique_ptr<ModelBatch> modelBatch;
Expand All @@ -75,16 +77,6 @@ class WorldRenderer {
const Camera& camera, Shader& linesShader, const DrawContext& pctx
);

/// @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,
Shader& linesShader
);

void renderBlockOverlay(const DrawContext& context, const Assets& assets);

void setupWorldShader(
Expand All @@ -110,7 +102,6 @@ class WorldRenderer {
float delta,
PostProcessing* postProcessing
);
void drawBorders(int sx, int sy, int sz, int ex, int ey, int ez);

/// @brief Render level without diegetic interface
/// @param context graphics context
Expand Down

0 comments on commit f9f4d20

Please sign in to comment.