From 45a1e1df82967141dfb6d4b9b298deb4dfbf44c0 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 14 Oct 2024 09:34:41 +0300 Subject: [PATCH] fix: backlight not applied to entities --- src/graphics/render/ModelBatch.cpp | 47 ++++++++++++++++++--------- src/graphics/render/ModelBatch.hpp | 12 +++++-- src/graphics/render/WorldRenderer.cpp | 3 +- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/src/graphics/render/ModelBatch.cpp b/src/graphics/render/ModelBatch.cpp index f3c8e098..bda65de3 100644 --- a/src/graphics/render/ModelBatch.cpp +++ b/src/graphics/render/ModelBatch.cpp @@ -8,6 +8,7 @@ #include "window/Window.hpp" #include "voxels/Chunks.hpp" #include "lighting/Lightmap.hpp" +#include "settings.hpp" #define GLM_ENABLE_EXPERIMENTAL #include @@ -49,14 +50,19 @@ static glm::mat4 extract_rotation(glm::mat4 matrix) { return glm::toMat3(rotation); } -ModelBatch::ModelBatch(size_t capacity, Assets* assets, Chunks* chunks) - : buffer(std::make_unique(capacity * VERTEX_SIZE)), - capacity(capacity), - index(0), - mesh(std::make_unique(buffer.get(), 0, attrs)), - assets(assets), - chunks(chunks) -{ +ModelBatch::ModelBatch( + size_t capacity, + Assets* assets, + Chunks* chunks, + const EngineSettings* settings +) + : buffer(std::make_unique(capacity * VERTEX_SIZE)), + capacity(capacity), + index(0), + mesh(std::make_unique(buffer.get(), 0, attrs)), + assets(assets), + chunks(chunks), + settings(settings) { const ubyte pixels[] = { 255, 255, 255, 255, }; @@ -68,18 +74,19 @@ ModelBatch::~ModelBatch() = default; void ModelBatch::draw(const model::Mesh& mesh, const glm::mat4& matrix, const glm::mat3& rotation, glm::vec3 tint, - const texture_names_map* varTextures) { + const texture_names_map* varTextures, + bool backlight) { glm::vec3 gpos = matrix * glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); light_t light = chunks->getLight( std::floor(gpos.x), std::floor(std::min(CHUNK_H-1.0f, gpos.y)), std::floor(gpos.z)); - - glm::vec4 lights ( - Lightmap::extract(light, 0) / 15.0f, - Lightmap::extract(light, 1) / 15.0f, - Lightmap::extract(light, 2) / 15.0f, - Lightmap::extract(light, 3) / 15.0f + light_t minIntensity = backlight ? 1 : 0; + glm::vec4 lights( + glm::max(Lightmap::extract(light, 0), minIntensity) / 15.0f, + glm::max(Lightmap::extract(light, 1), minIntensity) / 15.0f, + glm::max(Lightmap::extract(light, 2), minIntensity) / 15.0f, + glm::max(Lightmap::extract(light, 3), minIntensity) / 15.0f ); setTexture(mesh.texture, varTextures); size_t vcount = mesh.vertices.size(); @@ -115,8 +122,16 @@ void ModelBatch::render() { return a.mesh->texture < b.mesh->texture; } ); + bool backlight = settings->graphics.backlight.get(); for (auto& entry : entries) { - draw(*entry.mesh, entry.matrix, entry.rotation, entry.tint, entry.varTextures); + draw( + *entry.mesh, + entry.matrix, + entry.rotation, + entry.tint, + entry.varTextures, + backlight + ); } flush(); entries.clear(); diff --git a/src/graphics/render/ModelBatch.hpp b/src/graphics/render/ModelBatch.hpp index 31dfc292..fc3b1295 100644 --- a/src/graphics/render/ModelBatch.hpp +++ b/src/graphics/render/ModelBatch.hpp @@ -12,6 +12,7 @@ class Mesh; class Texture; class Chunks; class Assets; +struct EngineSettings; namespace model { struct Mesh; @@ -32,6 +33,7 @@ class ModelBatch { Chunks* chunks; Texture* texture = nullptr; UVRegion region {0.0f, 0.0f, 1.0f, 1.0f}; + const EngineSettings* settings; static inline glm::vec3 SUN_VECTOR {0.411934f, 0.863868f, -0.279161f}; @@ -65,7 +67,8 @@ class ModelBatch { const glm::mat4& matrix, const glm::mat3& rotation, glm::vec3 tint, - const texture_names_map* varTextures); + const texture_names_map* varTextures, + bool backlight); void setTexture(const std::string& name, const texture_names_map* varTextures); void setTexture(Texture* texture); @@ -80,7 +83,12 @@ class ModelBatch { }; std::vector entries; public: - ModelBatch(size_t capacity, Assets* assets, Chunks* chunks); + ModelBatch( + size_t capacity, + Assets* assets, + Chunks* chunks, + const EngineSettings* settings + ); ~ModelBatch(); void draw(glm::mat4 matrix, diff --git a/src/graphics/render/WorldRenderer.cpp b/src/graphics/render/WorldRenderer.cpp index a293ae2f..5c47d8ee 100644 --- a/src/graphics/render/WorldRenderer.cpp +++ b/src/graphics/render/WorldRenderer.cpp @@ -55,7 +55,8 @@ WorldRenderer::WorldRenderer( frustumCulling(std::make_unique()), lineBatch(std::make_unique()), modelBatch(std::make_unique( - 20'000, engine->getAssets(), level->chunks.get() + 20'000, engine->getAssets(), level->chunks.get(), + &engine->getSettings() )) { renderer = std::make_unique( level, frontend->getContentGfxCache(), &engine->getSettings()