feat: deferred shading (day 2)

This commit is contained in:
MihailRis
2025-07-08 23:56:22 +03:00
parent a585b52aa2
commit 0bd901d1a6
23 changed files with 236 additions and 92 deletions
+32 -15
View File
@@ -54,8 +54,11 @@
#include "Emitter.hpp"
#include "TextNote.hpp"
using namespace advanced_pipeline;
inline constexpr size_t BATCH3D_CAPACITY = 4096;
inline constexpr size_t MODEL_BATCH_CAPACITY = 20'000;
inline constexpr GLenum TEXTURE_MAIN = GL_TEXTURE0;
bool WorldRenderer::showChunkBorders = false;
bool WorldRenderer::showEntitiesDebug = false;
@@ -141,15 +144,15 @@ void WorldRenderer::setupWorldShader(
shader.uniform1f("u_shadowsOpacity", 1.0f - cloudsIntensity); // TODO: make it configurable
shader.uniform1f("u_shadowsSoftness", 1.0f + cloudsIntensity * 4); // TODO: make it configurable
glActiveTexture(GL_TEXTURE4);
shader.uniform1i("u_shadows[0]", 4);
glActiveTexture(GL_TEXTURE0 + TARGET_SHADOWS0);
shader.uniform1i("u_shadows[0]", TARGET_SHADOWS0);
glBindTexture(GL_TEXTURE_2D, shadowMap->getDepthMap());
glActiveTexture(GL_TEXTURE5);
shader.uniform1i("u_shadows[1]", 5);
glActiveTexture(GL_TEXTURE0 + TARGET_SHADOWS1);
shader.uniform1i("u_shadows[1]", TARGET_SHADOWS1);
glBindTexture(GL_TEXTURE_2D, wideShadowMap->getDepthMap());
glActiveTexture(GL_TEXTURE0);
glActiveTexture(TEXTURE_MAIN);
}
auto indices = level.content.getIndices();
@@ -438,9 +441,11 @@ void WorldRenderer::draw(
auto& mainShader = assets.require<Shader>("main");
auto& entityShader = assets.require<Shader>("entity");
auto& deferredShader = assets.require<PostEffect>("deferred_lighting").getShader();
const auto& settings = engine.getSettings();
gbufferPipeline = settings.graphics.advancedRender.get();
int shadowsQuality = settings.graphics.shadowsQuality.get();
int shadowsQuality = settings.graphics.shadowsQuality.get() * gbufferPipeline;
int resolution = 512 << shadowsQuality;
if (shadowsQuality > 0 && !shadows) {
shadowMap = std::make_unique<ShadowMap>(resolution);
@@ -449,6 +454,7 @@ void WorldRenderer::draw(
Shader::preprocessor->define("ENABLE_SHADOWS", "true");
mainShader.recompile();
entityShader.recompile();
deferredShader.recompile();
} else if (shadowsQuality == 0 && shadows) {
shadowMap.reset();
wideShadowMap.reset();
@@ -456,6 +462,7 @@ void WorldRenderer::draw(
Shader::preprocessor->undefine("ENABLE_SHADOWS");
mainShader.recompile();
entityShader.recompile();
deferredShader.recompile();
}
if (shadows && shadowMap->getResolution() != resolution) {
shadowMap = std::make_unique<ShadowMap>(resolution);
@@ -489,9 +496,6 @@ void WorldRenderer::draw(
display::clearDepth();
// Drawing background sky plane
skybox->draw(pctx, camera, assets, worldInfo.daytime, clouds);
/* Actually world render with depth buffer on */ {
DrawContext ctx = wctx.sub();
ctx.setDepthTest(true);
@@ -506,19 +510,32 @@ void WorldRenderer::draw(
}
}
}
{
DrawContext ctx = wctx.sub();
texts->render(ctx, camera, settings, hudVisible, true);
}
texts->render(pctx, camera, settings, hudVisible, true);
}
skybox->bind();
deferredShader.use();
float fogFactor =
15.0f / static_cast<float>(settings.chunks.loadDistance.get() - 2);
setupWorldShader(deferredShader, camera, settings, fogFactor);
postProcessing.render(
pctx,
assets,
timer,
camera,
shadows ? shadowMap->getDepthMap() : 0
shadows ? shadowMap->getDepthMap() : 0,
shadows ? wideShadowMap->getDepthMap() : 0,
shadowCamera.getProjView(),
wideShadowCamera.getProjView(),
shadows ? shadowMap->getResolution() : 0
);
{
DrawContext ctx = pctx.sub();
ctx.setDepthTest(true);
postProcessing.bindDepthBuffer();
// Drawing background sky plane
skybox->draw(ctx, camera, assets, worldInfo.daytime, clouds);
}
skybox->unbind();
if (player.currentCamera == player.fpCamera) {
DrawContext ctx = pctx.sub();
ctx.setDepthTest(true);