fix: backlight not applied to entities

This commit is contained in:
MihailRis 2024-10-14 09:34:41 +03:00
parent bc24151a63
commit 45a1e1df82
3 changed files with 43 additions and 19 deletions

View File

@ -8,6 +8,7 @@
#include "window/Window.hpp"
#include "voxels/Chunks.hpp"
#include "lighting/Lightmap.hpp"
#include "settings.hpp"
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/ext/matrix_transform.hpp>
@ -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<float[]>(capacity * VERTEX_SIZE)),
capacity(capacity),
index(0),
mesh(std::make_unique<Mesh>(buffer.get(), 0, attrs)),
assets(assets),
chunks(chunks)
{
ModelBatch::ModelBatch(
size_t capacity,
Assets* assets,
Chunks* chunks,
const EngineSettings* settings
)
: buffer(std::make_unique<float[]>(capacity * VERTEX_SIZE)),
capacity(capacity),
index(0),
mesh(std::make_unique<Mesh>(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();

View File

@ -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<DrawEntry> 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,

View File

@ -55,7 +55,8 @@ WorldRenderer::WorldRenderer(
frustumCulling(std::make_unique<Frustum>()),
lineBatch(std::make_unique<LineBatch>()),
modelBatch(std::make_unique<ModelBatch>(
20'000, engine->getAssets(), level->chunks.get()
20'000, engine->getAssets(), level->chunks.get(),
&engine->getSettings()
)) {
renderer = std::make_unique<ChunksRenderer>(
level, frontend->getContentGfxCache(), &engine->getSettings()