WorldRenderer refactor

This commit is contained in:
MihailRis 2024-03-08 22:21:18 +03:00
parent 8ea3db9962
commit 07dc12143e
4 changed files with 163 additions and 118 deletions

View File

@ -138,37 +138,15 @@ void WorldRenderer::drawChunks(Chunks* chunks, Camera* camera, Shader* shader) {
} }
} }
void WorldRenderer::renderLevel(
void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible){ const GfxContext& ctx,
EngineSettings& settings = engine->getSettings(); Camera* camera,
skybox->refresh(pctx, level->world->daytime, 1.0f+fog*2.0f, 4); const EngineSettings& settings
) {
const Content* content = level->content;
auto indices = content->getIndices();
Assets* assets = engine->getAssets(); Assets* assets = engine->getAssets();
Atlas* atlas = assets->getAtlas("blocks"); Atlas* atlas = assets->getAtlas("blocks");
Shader* shader = assets->getShader("main"); Shader* shader = assets->getShader("main");
Shader* linesShader = assets->getShader("lines"); auto indices = level->content->getIndices();
const Viewport& viewport = pctx.getViewport();
int displayWidth = viewport.getWidth();
int displayHeight = viewport.getHeight();
// World render scope with diegetic HUD included
{
GfxContext wctx = pctx.sub();
postProcessing->use(wctx);
Window::clearDepth();
// Drawing background sky plane
skybox->draw(pctx, camera, assets, level->getWorld()->daytime, fog);
// Actually world render with depth buffer on
{
GfxContext ctx = wctx.sub();
ctx.setDepthTest(true);
ctx.setCullFace(true);
float fogFactor = 15.0f / ((float)settings.chunks.loadDistance-2); float fogFactor = 15.0f / ((float)settings.chunks.loadDistance-2);
@ -203,8 +181,11 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
drawChunks(level->chunks.get(), camera, shader); drawChunks(level->chunks.get(), camera, shader);
// Selected block skybox->unbind();
if (PlayerController::selectedBlockId != -1 && hudVisible){ }
void WorldRenderer::renderBlockSelection(Camera* camera, Shader* linesShader) {
auto indices = level->content->getIndices();
blockid_t id = PlayerController::selectedBlockId; blockid_t id = PlayerController::selectedBlockId;
auto block = indices->getBlockDef(id); auto block = indices->getBlockDef(id);
const glm::vec3 pos = PlayerController::selectedBlockPosition; const glm::vec3 pos = PlayerController::selectedBlockPosition;
@ -227,12 +208,19 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
} }
} }
lineBatch->render(); lineBatch->render();
} }
skybox->unbind();
}
if (hudVisible && player->debug) { void WorldRenderer::renderDebugLines(
const GfxContext& pctx,
Camera* camera,
Shader* linesShader,
const EngineSettings& settings
) {
GfxContext ctx = pctx.sub(); GfxContext ctx = pctx.sub();
const auto& viewport = ctx.getViewport();
uint displayWidth = viewport.getWidth();
uint displayHeight = viewport.getHeight();
ctx.setDepthTest(true); ctx.setDepthTest(true);
linesShader->use(); linesShader->use();
@ -245,8 +233,10 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
int cx = floordiv((int)coord.x, CHUNK_W); int cx = floordiv((int)coord.x, CHUNK_W);
int cz = floordiv((int)coord.z, CHUNK_D); int cz = floordiv((int)coord.z, CHUNK_D);
drawBorders(cx * CHUNK_W, 0, cz * CHUNK_D, drawBorders(
(cx + 1) * CHUNK_W, CHUNK_H, (cz + 1) * CHUNK_D); cx * CHUNK_W, 0, cz * CHUNK_D,
(cx + 1) * CHUNK_W, CHUNK_H, (cz + 1) * CHUNK_D
);
} }
float length = 40.f; float length = 40.f;
@ -255,7 +245,8 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
linesShader->uniformMatrix("u_projview", glm::ortho( linesShader->uniformMatrix("u_projview", glm::ortho(
0.f, (float)displayWidth, 0.f, (float)displayWidth,
0.f, (float)displayHeight, 0.f, (float)displayHeight,
-length, length) * model * glm::inverse(camera->rotation)); -length, length) * model * glm::inverse(camera->rotation)
);
ctx.setDepthTest(false); ctx.setDepthTest(false);
lineBatch->lineWidth(4.0f); lineBatch->lineWidth(4.0f);
@ -270,20 +261,48 @@ void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible
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, 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); lineBatch->line(0.f, 0.f, 0.f, 0.f, 0.f, length, 0.f, 0.f, 1.f, 1.f);
lineBatch->render(); lineBatch->render();
} }
}
void WorldRenderer::draw(const GfxContext& pctx, Camera* camera, bool hudVisible){
EngineSettings& settings = engine->getSettings();
skybox->refresh(pctx, level->world->daytime, 1.0f+fog*2.0f, 4);
Assets* assets = engine->getAssets();
Shader* linesShader = assets->getShader("lines");
// World render scope with diegetic HUD included
{ {
GfxContext ctx = pctx.sub(); GfxContext wctx = pctx.sub();
ctx.setDepthTest(false); postProcessing->use(wctx);
auto shader = assets->getShader("screen"); Window::clearDepth();
shader->use();
shader->uniform1f("u_timer", Window::time());
shader->uniform1f("u_dayTime", level->world->daytime);
postProcessing->render(ctx, shader); // Drawing background sky plane
skybox->draw(pctx, camera, assets, level->getWorld()->daytime, fog);
// Actually world render with depth buffer on
{
GfxContext ctx = wctx.sub();
ctx.setDepthTest(true);
ctx.setCullFace(true);
renderLevel(ctx, camera, settings);
// Selected block
if (PlayerController::selectedBlockId != -1 && hudVisible){
renderBlockSelection(camera, linesShader);
} }
}
if (hudVisible && player->debug) {
renderDebugLines(wctx, camera, linesShader, settings);
}
}
// Rendering fullscreen quad with
auto screenShader = assets->getShader("screen");
screenShader->use();
screenShader->uniform1f("u_timer", Window::time());
screenShader->uniform1f("u_dayTime", level->world->daytime);
postProcessing->render(pctx, screenShader);
} }
void WorldRenderer::drawBorders(int sx, int sy, int sz, int ex, int ey, int ez) { void WorldRenderer::drawBorders(int sx, int sy, int sz, int ex, int ey, int ez) {

View File

@ -39,12 +39,38 @@ class WorldRenderer {
std::unique_ptr<Batch3D> batch3d; std::unique_ptr<Batch3D> batch3d;
bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling); bool drawChunk(size_t index, Camera* camera, Shader* shader, bool culling);
void drawChunks(Chunks* chunks, Camera* camera, Shader* shader); void drawChunks(Chunks* chunks, Camera* camera, Shader* shader);
/// @brief Render level without diegetic interface
/// @param context graphics context
/// @param camera active camera
/// @param settings engine settings
void renderLevel(
const GfxContext& context,
Camera* camera,
const EngineSettings& settings
);
/// @brief Render block selection lines
/// @param camera active camera
/// @param linesShader shader used
void renderBlockSelection(Camera* camera, Shader* linesShader);
/// @brief Render all debug lines (chunks borders, coord system guides)
/// @param context graphics context
/// @param camera active camera
/// @param linesShader shader used
/// @param settings engine settings
void renderDebugLines(
const GfxContext& context,
Camera* camera,
Shader* linesShader,
const EngineSettings& settings
);
public: public:
WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player); WorldRenderer(Engine* engine, LevelFrontend* frontend, Player* player);
~WorldRenderer(); ~WorldRenderer();
void draw(const GfxContext& context, Camera* camera, bool hudVisible); void draw(const GfxContext& context, Camera* camera, bool hudVisible);
void drawDebug(const GfxContext& context, Camera* camera);
void drawBorders(int sx, int sy, int sz, int ex, int ey, int ez); void drawBorders(int sx, int sy, int sz, int ex, int ey, int ez);
static float fog; static float fog;

View File

@ -29,7 +29,7 @@ void PostProcessing::use(GfxContext& context) {
context.setFramebuffer(fbo.get()); context.setFramebuffer(fbo.get());
} }
void PostProcessing::render(GfxContext& context, Shader* screenShader) { void PostProcessing::render(const GfxContext& context, Shader* screenShader) {
if (fbo == nullptr) { if (fbo == nullptr) {
throw std::runtime_error("'use(...)' was never called"); throw std::runtime_error("'use(...)' was never called");
} }

View File

@ -31,7 +31,7 @@ public:
/// @param context graphics context /// @param context graphics context
/// @param screenShader shader used for fullscreen quad /// @param screenShader shader used for fullscreen quad
/// @throws std::runtime_error if use(...) wasn't called before /// @throws std::runtime_error if use(...) wasn't called before
void render(GfxContext& context, Shader* screenShader); void render(const GfxContext& context, Shader* screenShader);
}; };
#endif // GRAPHICS_POST_PROCESSING_H_ #endif // GRAPHICS_POST_PROCESSING_H_