From 8bb03a004fef3f9459fc332d8c0c062ffc561f23 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 10 May 2025 16:13:06 +0300 Subject: [PATCH] refactor & fix random deleting texture --- src/graphics/core/Batch2D.cpp | 10 +-- src/graphics/core/Batch3D.cpp | 2 - src/graphics/core/Framebuffer.cpp | 5 +- src/graphics/core/PostProcessing.cpp | 11 ++-- src/graphics/render/BlocksRenderer.cpp | 2 +- src/graphics/render/WorldRenderer.cpp | 91 +++++++++++++------------- src/graphics/render/WorldRenderer.hpp | 8 ++- 7 files changed, 65 insertions(+), 64 deletions(-) diff --git a/src/graphics/core/Batch2D.cpp b/src/graphics/core/Batch2D.cpp index 38224a3f..0db15c4b 100644 --- a/src/graphics/core/Batch2D.cpp +++ b/src/graphics/core/Batch2D.cpp @@ -60,17 +60,17 @@ void Batch2D::vertex( index++; } -void Batch2D::texture(const Texture* new_texture){ - if (currentTexture == new_texture) { +void Batch2D::texture(const Texture* newTexture){ + if (currentTexture == newTexture) { return; } flush(); - currentTexture = new_texture; - if (new_texture == nullptr) { + currentTexture = newTexture; + if (newTexture == nullptr) { blank->bind(); region = blank->getUVRegion(); } else { - new_texture->bind(); + newTexture->bind(); region = currentTexture->getUVRegion(); } } diff --git a/src/graphics/core/Batch3D.cpp b/src/graphics/core/Batch3D.cpp index 43d161f6..6230fc40 100644 --- a/src/graphics/core/Batch3D.cpp +++ b/src/graphics/core/Batch3D.cpp @@ -8,8 +8,6 @@ Batch3D::Batch3D(size_t capacity) : capacity(capacity) { - - buffer = std::make_unique(capacity); mesh = std::make_unique>(buffer.get(), 0); index = 0; diff --git a/src/graphics/core/Framebuffer.cpp b/src/graphics/core/Framebuffer.cpp index 7f86dd20..e009f747 100644 --- a/src/graphics/core/Framebuffer.cpp +++ b/src/graphics/core/Framebuffer.cpp @@ -42,9 +42,6 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha) // Setup color attachment (texture) texture = create_texture(width, height, format); - unsigned int attachments[1] = { GL_COLOR_ATTACHMENT0 }; - glDrawBuffers(1, attachments); - // Setup depth attachment glGenRenderbuffers(1, &depth); glBindRenderbuffer(GL_RENDERBUFFER, depth); @@ -60,7 +57,7 @@ Framebuffer::Framebuffer(uint width, uint height, bool alpha) Framebuffer::~Framebuffer() { glDeleteFramebuffers(1, &fbo); - glDeleteTextures(1, &depth); + glDeleteRenderbuffers(1, &depth); } void Framebuffer::bind() { diff --git a/src/graphics/core/PostProcessing.cpp b/src/graphics/core/PostProcessing.cpp index 498782d4..acebd519 100644 --- a/src/graphics/core/PostProcessing.cpp +++ b/src/graphics/core/PostProcessing.cpp @@ -135,20 +135,21 @@ void PostProcessing::render( const auto& vp = context.getViewport(); refreshFbos(vp.x, vp.y); + glActiveTexture(GL_TEXTURE4); + glBindTexture(GL_TEXTURE_2D, shadowMap); + if (gbuffer) { gbuffer->bindBuffers(); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, noiseTexture); - - glActiveTexture(GL_TEXTURE4); - glBindTexture(GL_TEXTURE_2D, shadowMap); + + glActiveTexture(GL_TEXTURE0); } else { + glActiveTexture(GL_TEXTURE0); fbo->getTexture()->bind(); } - glActiveTexture(GL_TEXTURE0); - if (totalPasses == 0) { auto& effect = assets.require("default"); auto& shader = effect.use(); diff --git a/src/graphics/render/BlocksRenderer.cpp b/src/graphics/render/BlocksRenderer.cpp index 2ed50ce1..1bc499fb 100644 --- a/src/graphics/render/BlocksRenderer.cpp +++ b/src/graphics/render/BlocksRenderer.cpp @@ -10,7 +10,7 @@ #include "frontend/ContentGfxCache.hpp" const glm::vec3 BlocksRenderer::SUN_VECTOR(0.528265f, 0.833149f, -0.163704f); -const float DIRECTIONAL_LIGHT_FACTOR = 0.2f; +const float DIRECTIONAL_LIGHT_FACTOR = 0.3f; BlocksRenderer::BlocksRenderer( size_t capacity, diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index 71f14a48..c30093bd 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -26,7 +26,6 @@ #include "voxels/Block.hpp" #include "voxels/Chunk.hpp" #include "voxels/Chunks.hpp" -#include "window/Camera.hpp" #include "window/Window.hpp" #include "world/Level.hpp" #include "world/LevelEvents.hpp" @@ -102,10 +101,6 @@ WorldRenderer::WorldRenderer( settings.graphics.skyboxResolution.get(), assets->require("skybox_gen") ); - - shadowMap = std::make_unique(1024 * 8); - - shadowCamera = std::make_unique(); } WorldRenderer::~WorldRenderer() = default; @@ -136,8 +131,11 @@ void WorldRenderer::setupWorldShader( shader.uniform1i("u_enableShadows", shadows); if (shadows) { - shader.uniformMatrix("u_shadowsMatrix", shadowCamera->getProjView()); - shader.uniform3f("u_sunDir", shadowCamera->front); + if (shadowMap == nullptr) { + shadowMap = std::make_unique(1024 * 8); + } + shader.uniformMatrix("u_shadowsMatrix", shadowCamera.getProjView()); + shader.uniform3f("u_sunDir", shadowCamera.front); shader.uniform1i("u_shadowsRes", shadowMap->getResolution()); glActiveTexture(GL_TEXTURE4); shader.uniform1i("u_shadows", 4); @@ -342,6 +340,44 @@ void WorldRenderer::renderHands( skybox->unbind(); } +void WorldRenderer::generateShadowsMap(const Camera& camera, const DrawContext& pctx) { + auto& shadowsShader = assets.require("shadows"); + + auto world = level.getWorld(); + const auto& worldInfo = world->getInfo(); + + const auto& settings = engine.getSettings(); + int resolution = shadowMap->getResolution(); + float shadowMapScale = 0.05f; + float shadowMapSize = resolution * shadowMapScale; + shadowCamera = Camera(camera.position, shadowMapSize); + shadowCamera.near = 0.1f; + shadowCamera.far = 800.0f; + shadowCamera.perspective = false; + shadowCamera.setAspectRatio(1.0f); + shadowCamera.rotate( + glm::radians(fmod(90.0f - worldInfo.daytime * 360.0f, 180.0f)), + glm::radians(-40.0f), + glm::radians(-0.0f) + ); + shadowCamera.updateVectors(); + shadowCamera.position -= shadowCamera.front * 200.0f; + shadowCamera.position -= shadowCamera.right * (resolution * shadowMapScale) * 0.5f; + shadowCamera.position -= shadowCamera.up * (resolution * shadowMapScale) * 0.5f; + shadowCamera.position = glm::floor(shadowCamera.position * 0.25f) * 4.0f; + { + frustumCulling->update(shadowCamera.getProjView()); + auto sctx = pctx.sub(); + sctx.setDepthTest(true); + sctx.setCullFace(true); + sctx.setViewport({resolution, resolution}); + shadowMap->bind(); + setupWorldShader(shadowsShader, shadowCamera, settings, 0.0f); + chunks->drawChunksShadowsPass(shadowCamera, shadowsShader); + shadowMap->unbind(); + } +} + void WorldRenderer::draw( const DrawContext& pctx, Camera& camera, @@ -370,43 +406,11 @@ void WorldRenderer::draw( skybox->refresh(pctx, worldInfo.daytime, mie, 4); - const auto& assets = *engine.getAssets(); - auto& linesShader = assets.require("lines"); - auto& shadowsShader = assets.require("shadows"); - if (shadows) { - int resolution = shadowMap->getResolution(); - float shadowMapScale = 0.05f; - float shadowMapSize = resolution * shadowMapScale; - *shadowCamera = Camera(camera.position, shadowMapSize); - shadowCamera->near = 0.1f; - shadowCamera->far = 800.0f; - shadowCamera->perspective = false; - shadowCamera->setAspectRatio(1.0f); - shadowCamera->rotate( - glm::radians(fmod(90.0f - worldInfo.daytime * 360.0f, 180.0f)), - glm::radians(-40.0f), - glm::radians(-0.0f) - ); - shadowCamera->updateVectors(); - //shadowCamera->position += camera.dir * shadowMapSize * 0.5f; - shadowCamera->position -= shadowCamera->front * 200.0f; - shadowCamera->position -= shadowCamera->right * (resolution * shadowMapScale) * 0.5f; - shadowCamera->position -= shadowCamera->up * (resolution * shadowMapScale) * 0.5f; - shadowCamera->position = glm::floor(shadowCamera->position * 0.25f) * 4.0f; - { - frustumCulling->update(shadowCamera->getProjView()); - auto sctx = pctx.sub(); - sctx.setDepthTest(true); - sctx.setCullFace(true); - sctx.setViewport({resolution, resolution}); - shadowMap->bind(); - setupWorldShader(shadowsShader, *shadowCamera, settings, 0.0f); - chunks->drawChunksShadowsPass(*shadowCamera, shadowsShader); - shadowMap->unbind(); - } + generateShadowsMap(camera, pctx); } + auto& linesShader = assets.require("lines"); /* World render scope with diegetic HUD included */ { DrawContext wctx = pctx.sub(); postProcessing.use(wctx, gbufferPipeline); @@ -446,10 +450,9 @@ void WorldRenderer::draw( camera, shadows ? shadowMap->getDepthMap() : 0 ); - glActiveTexture(GL_TEXTURE0); - glBindTexture(GL_TEXTURE_2D, 0); - renderBlockOverlay(pctx); + + glActiveTexture(GL_TEXTURE0); } void WorldRenderer::renderBlockOverlay(const DrawContext& wctx) { diff --git a/src/graphics/render/WorldRenderer.hpp b/src/graphics/render/WorldRenderer.hpp index 8e9d4d01..7db4e44a 100644 --- a/src/graphics/render/WorldRenderer.hpp +++ b/src/graphics/render/WorldRenderer.hpp @@ -9,6 +9,7 @@ #include "presets/WeatherPreset.hpp" #include "world/Weather.hpp" +#include "window/Camera.hpp" class Level; class Player; @@ -48,14 +49,13 @@ class WorldRenderer { std::unique_ptr skybox; std::unique_ptr shadowMap; Weather weather {}; - - std::unique_ptr shadowCamera; + Camera shadowCamera; float timer = 0.0f; bool debug = false; bool lightsDebug = false; bool gbufferPipeline = false; - bool shadows = true; + bool shadows = false; /// @brief Render block selection lines void renderBlockSelection(); @@ -77,6 +77,8 @@ class WorldRenderer { const EngineSettings& settings, float fogFactor ); + + void generateShadowsMap(const Camera& camera, const DrawContext& pctx); public: std::unique_ptr particles; std::unique_ptr texts;