Backlight for tranparent blocks

This commit is contained in:
MihailRis 2023-11-22 18:58:40 +03:00
parent d2c87f4ba5
commit b472626d8d
5 changed files with 25 additions and 14 deletions

View File

@ -16,15 +16,6 @@
#include "window/input.h"
#include "graphics/Batch2D.h"
#include "graphics/ImageData.h"
#include "world/World.h"
#include "world/Level.h"
#include "voxels/Chunk.h"
#include "voxels/Chunks.h"
#include "voxels/ChunksController.h"
#include "voxels/ChunksStorage.h"
#include "objects/Player.h"
#include "frontend/world_render.h"
#include "frontend/hud.h"
#include "frontend/gui/GUI.h"
#include "frontend/screens.h"
#include "util/platform.h"
@ -133,4 +124,4 @@ void Engine::setScreen(shared_ptr<Screen> screen) {
const Content* Engine::getContent() const {
return content;
}
}

View File

@ -346,7 +346,7 @@ void BlocksRenderer::render(const voxel* voxels, int atlas_size) {
Mesh* BlocksRenderer::render(const Chunk* chunk, int atlas_size, const ChunksStorage* chunks) {
this->chunk = chunk;
voxelsBuffer->setPosition(chunk->x * CHUNK_W - 1, 0, chunk->z * CHUNK_D - 1);
chunks->getVoxels(voxelsBuffer);
chunks->getVoxels(voxelsBuffer, true);
overflow = false;
vertexOffset = 0;
indexOffset = indexSize = 0;

View File

@ -71,6 +71,10 @@ public:
return map;
}
static inline light_t combine(int r, int g, int b, int s) {
return r | (g << 4) | (b << 8) | (s << 12);
}
static inline light_t extract(light_t light, ubyte channel) {
return (light >> (channel << 2)) & 0xF;
}

View File

@ -4,6 +4,8 @@
#include "VoxelsVolume.h"
#include "Chunk.h"
#include "Block.h"
#include "../content/Content.h"
#include "../files/WorldFiles.h"
#include "../world/Level.h"
#include "../world/World.h"
@ -52,7 +54,9 @@ std::shared_ptr<Chunk> ChunksStorage::create(int x, int z) {
}
// some magic code
void ChunksStorage::getVoxels(VoxelsVolume* volume) const {
void ChunksStorage::getVoxels(VoxelsVolume* volume, bool backlight) const {
const Content* content = level->content;
const ContentIndices* indices = content->indices;
voxel* voxels = volume->getVoxels();
light_t* lights = volume->getLights();
int x = volume->getX();
@ -106,7 +110,19 @@ void ChunksStorage::getVoxels(VoxelsVolume* volume) const {
uint cidx = vox_index(lx - cx * CHUNK_W, ly,
lz - cz * CHUNK_D, CHUNK_W, CHUNK_D);
voxels[vidx] = cvoxels[cidx];
lights[vidx] = clights[cidx];
light_t light = clights[cidx];
if (backlight) {
const Block* block = indices->getBlockDef(voxels[vidx].id);
if (block->lightPassing) {
light = Lightmap::combine(
min(15, Lightmap::extract(light, 0)+1),
min(15, Lightmap::extract(light, 1)+1),
min(15, Lightmap::extract(light, 2)+1),
min(15, Lightmap::extract(light, 3)+1)
);
}
}
lights[vidx] = light;
}
}
}

View File

@ -23,7 +23,7 @@ public:
std::shared_ptr<Chunk> get(int x, int z) const;
void store(std::shared_ptr<Chunk> chunk);
void remove(int x, int y);
void getVoxels(VoxelsVolume* volume) const;
void getVoxels(VoxelsVolume* volume, bool backlight=false) const;
std::shared_ptr<Chunk> create(int x, int z);
light_t getLight(int x, int y, int z, ubyte channel) const;