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
+21 -20
View File
@@ -18,9 +18,7 @@
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/constants.hpp>
#ifndef M_PI
#define M_PI 3.141592
#endif // M_PI
using namespace advanced_pipeline;
const int STARS_COUNT = 3000;
const int STARS_SEED = 632;
@@ -47,14 +45,6 @@ Skybox::Skybox(uint size, Shader& shader)
mesh = std::make_unique<Mesh<SkyboxVertex>>(vertices, 6);
sprites.push_back(SkySprite {
"misc/moon_flare",
glm::pi<float>() * 0.5f,
0.5f,
false,
glm::pi<float>() * 0.25f,
});
sprites.push_back(SkySprite {
"misc/moon",
glm::pi<float>() * 0.5f,
@@ -62,6 +52,14 @@ Skybox::Skybox(uint size, Shader& shader)
false,
glm::pi<float>() * 0.25f,
});
sprites.push_back(SkySprite {
"misc/moon_flare",
glm::pi<float>() * 0.5f,
0.5f,
false,
glm::pi<float>() * 0.25f,
});
sprites.push_back(SkySprite {
"misc/sun",
@@ -118,6 +116,8 @@ void Skybox::draw(
{
const glm::uvec2& viewport = pctx.getViewport();
glActiveTexture(GL_TEXTURE0);
drawBackground(camera, assets, viewport.x, viewport.y);
DrawContext ctx = pctx.sub();
@@ -130,22 +130,23 @@ void Skybox::draw(
batch3d->begin();
float angle = daytime * glm::pi<float>() * 2.0f;
float opacity = glm::pow(1.0f-fog, 7.0f);
float opacity = glm::pow(1.0f - fog, 7.0f);
float depthScale = 1e3;
for (auto& sprite : sprites) {
batch3d->texture(assets.get<Texture>(sprite.texture));
float sangle = daytime * glm::pi<float>() * 2.0 + sprite.phase;
float distance = sprite.distance;
float distance = sprite.distance * depthScale;
glm::mat4 rotation = glm::rotate(glm::mat4(1.0f), -sangle + glm::pi<float>() * 0.5f, glm::vec3(0, 0, -1));
rotation = glm::rotate(rotation, sprite.altitude, glm::vec3(1, 0, 0));
glm::vec3 pos = glm::vec4(0, distance, 0, 1) * rotation;
glm::vec3 up = glm::vec4(1, 0, 0, 1) * rotation;
glm::vec3 right = glm::vec4(0, 0, 1, 1) * rotation;
glm::vec3 up = glm::vec4(depthScale, 0, 0, 1) * rotation;
glm::vec3 right = glm::vec4(0, 0, depthScale, 1) * rotation;
glm::vec4 tint (1,1,1, opacity);
if (!sprite.emissive) {
tint *= 0.6f+std::cos(angle)*0.4;
tint *= 0.6f + std::cos(angle)*0.4;
}
batch3d->sprite(pos, right,
up, 1, 1, UVRegion(), tint);
@@ -167,10 +168,10 @@ void Skybox::refresh(const DrawContext& pctx, float t, float mie, uint quality)
assert(cubemap != nullptr);
ready = true;
glActiveTexture(GL_TEXTURE1);
glActiveTexture(GL_TEXTURE0 + TARGET_SKYBOX);
cubemap->bind();
shader.use();
t *= glm::pi<float>()*2.0f;
t *= glm::two_pi<float>();
lightDir = glm::normalize(glm::vec3(sin(t), -cos(t), 0.0f));
shader.uniform1i("u_quality", quality);
@@ -237,13 +238,13 @@ void Skybox::refreshFace(uint face, Cubemap* cubemap) {
}
void Skybox::bind() const {
glActiveTexture(GL_TEXTURE1);
glActiveTexture(GL_TEXTURE0 + TARGET_SKYBOX);
fbo->getTexture()->bind();
glActiveTexture(GL_TEXTURE0);
}
void Skybox::unbind() const {
glActiveTexture(GL_TEXTURE1);
glActiveTexture(GL_TEXTURE0 + TARGET_SKYBOX);
fbo->getTexture()->unbind();
glActiveTexture(GL_TEXTURE0);
}
+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);