X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fworld%2Fchunk.cpp;h=ef9f578fa3e90dd9c90a1336925e086c1d0b3611;hb=0d580658b896dfec07466c31ae4847455724ee95;hp=6fb46794a474f4ae30ef202912cd1391d16e8644;hpb=7fd76e64de47f564117b9e6f73f1482d93842108;p=blank.git diff --git a/src/world/chunk.cpp b/src/world/chunk.cpp index 6fb4679..ef9f578 100644 --- a/src/world/chunk.cpp +++ b/src/world/chunk.cpp @@ -9,9 +9,9 @@ #include "WorldCollision.hpp" #include "../app/Assets.hpp" #include "../graphics/BlockLighting.hpp" +#include "../graphics/BlockMesh.hpp" #include "../graphics/Viewport.hpp" #include "../io/WorldSave.hpp" -#include "../model/BlockModel.hpp" #include #include @@ -32,21 +32,27 @@ Chunk::Chunk(const BlockTypeRegistry &types) noexcept , neighbor{0} , blocks{} , light{0} +, generated(false) +, lighted(false) , position(0, 0, 0) , ref_count(0) -, dirty_model(false) +, dirty_mesh(false) , dirty_save(false) { } Chunk::Chunk(Chunk &&other) noexcept : types(other.types) +, generated(other.generated) +, lighted(other.lighted) , position(other.position) -, dirty_model(other.dirty_model) +, ref_count(other.ref_count) +, dirty_mesh(other.dirty_mesh) , dirty_save(other.dirty_save) { std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor); std::copy(other.blocks, other.blocks + sizeof(blocks), blocks); std::copy(other.light, other.light + sizeof(light), light); + other.ref_count = 0; } Chunk &Chunk::operator =(Chunk &&other) noexcept { @@ -54,8 +60,11 @@ Chunk &Chunk::operator =(Chunk &&other) noexcept { std::copy(other.neighbor, other.neighbor + sizeof(neighbor), neighbor); std::copy(other.blocks, other.blocks + sizeof(blocks), blocks); std::copy(other.light, other.light + sizeof(light), light); + generated = other.generated; + lighted = other.lighted; position = other.position; - dirty_model = other.dirty_save; + std::swap(ref_count, other.ref_count); + dirty_mesh = other.dirty_save; dirty_save = other.dirty_save; return *this; } @@ -159,7 +168,7 @@ void Chunk::SetBlock(int index, const Block &block) noexcept { blocks[index] = block; Invalidate(); - if (&old_type == &new_type) return; + if (!lighted || &old_type == &new_type) return; if (new_type.luminosity > old_type.luminosity) { // light added @@ -200,104 +209,27 @@ void Chunk::SetBlock(int index, const Block &block) noexcept { } } -namespace { - -// propagate light from a's edge to b -void edge_light( - Chunk &a, const Chunk::Pos &a_pos, - Chunk &b, const Chunk::Pos &b_pos -) noexcept { - if (a.GetLight(a_pos) > 1) { - const BlockType &b_type = b.Type(Chunk::ToIndex(b_pos)); - if (!b_type.block_light) { - light_queue.emplace(&a, a_pos); - } - if (b_type.visible) { - b.Invalidate(); +void Chunk::ScanLights() { + int idx = 0; + Pos pos(0, 0, 0); + for (; pos.z < depth; ++pos.z) { + for (pos.y = 0; pos.y < height; ++pos.y) { + for (pos.x = 0; pos.x < width; ++pos.x, ++idx) { + const BlockType &type = Type(blocks[idx]); + if (type.luminosity) { + SetLight(idx, type.luminosity); + light_queue.emplace(this, pos); + } + } } } -} - + work_light(); + lighted = true; } void Chunk::SetNeighbor(Block::Face face, Chunk &other) noexcept { neighbor[face] = &other; other.neighbor[Block::Opposite(face)] = this; - - switch (face) { - - default: - // error - break; - - case Block::FACE_LEFT: - for (int z = 0; z < depth; ++z) { - for (int y = 0; y < height; ++y) { - Pos my_pos(0, y, z); - Pos other_pos(width - 1, y, z); - edge_light(*this, my_pos, other, other_pos); - edge_light(other, other_pos, *this, my_pos); - } - } - break; - - case Block::FACE_RIGHT: - for (int z = 0; z < depth; ++z) { - for (int y = 0; y < height; ++y) { - Pos my_pos(width - 1, y, z); - Pos other_pos(0, y, z); - edge_light(*this, my_pos, other, other_pos); - edge_light(other, other_pos, *this, my_pos); - } - } - break; - - case Block::FACE_DOWN: - for (int z = 0; z < depth; ++z) { - for (int x = 0; x < width; ++x) { - Pos my_pos(x, 0, z); - Pos other_pos(x, height - 1, z); - edge_light(*this, my_pos, other, other_pos); - edge_light(other, other_pos, *this, my_pos); - } - } - break; - - case Block::FACE_UP: - for (int z = 0; z < depth; ++z) { - for (int x = 0; x < width; ++x) { - Pos my_pos(x, height - 1, z); - Pos other_pos(x, 0, z); - edge_light(*this, my_pos, other, other_pos); - edge_light(other, other_pos, *this, my_pos); - } - } - break; - - case Block::FACE_BACK: - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - Pos my_pos(x, y, 0); - Pos other_pos(x, y, depth - 1); - edge_light(*this, my_pos, other, other_pos); - edge_light(other, other_pos, *this, my_pos); - } - } - break; - - case Block::FACE_FRONT: - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - Pos my_pos(x, y, depth - 1); - Pos other_pos(x, y, 0); - edge_light(*this, my_pos, other, other_pos); - edge_light(other, other_pos, *this, my_pos); - } - } - break; - - } - work_light(); } void Chunk::Unlink() noexcept { @@ -321,7 +253,7 @@ int Chunk::GetLight(int index) const noexcept { return light[index]; } -float Chunk::GetVertexLight(const Pos &pos, const BlockModel::Position &vtx, const EntityModel::Normal &norm) const noexcept { +float Chunk::GetVertexLight(const Pos &pos, const BlockMesh::Position &vtx, const EntityMesh::Normal &norm) const noexcept { int index = ToIndex(pos); float light = GetLight(index); @@ -439,7 +371,7 @@ bool Chunk::Intersection( for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x, ++idx) { const BlockType &type = Type(idx); - if (!type.visible) { + if (!type.collision || !type.shape) { continue; } float cur_dist; @@ -480,7 +412,7 @@ bool Chunk::Intersection( for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x, ++idx) { const BlockType &type = Type(idx); - if (!type.collision) { + if (!type.collision || !type.shape) { continue; } if (type.shape->Intersects(Mchunk * ToTransform(Pos(x, y, z), idx), box, Mbox, penetration, normal)) { @@ -496,47 +428,51 @@ bool Chunk::Intersection( namespace { -BlockModel::Buffer buf; +BlockMesh::Buffer buf; } -void Chunk::Update(BlockModel &model) noexcept { +void Chunk::Update(BlockMesh &model) noexcept { int vtx_count = 0, idx_count = 0; for (const auto &block : blocks) { - const Shape *shape = Type(block).shape; - vtx_count += shape->VertexCount(); - idx_count += shape->VertexIndexCount(); + const BlockType &type = Type(block); + if (type.visible && type.shape) { + vtx_count += type.shape->VertexCount(); + idx_count += type.shape->IndexCount(); + } } buf.Clear(); buf.Reserve(vtx_count, idx_count); - int idx = 0; - BlockModel::Index vtx_counter = 0; - for (size_t z = 0; z < depth; ++z) { - for (size_t y = 0; y < height; ++y) { - for (size_t x = 0; x < width; ++x, ++idx) { - const BlockType &type = Type(BlockAt(idx)); - const Pos pos(x, y, z); - - if (!type.visible || Obstructed(pos).All()) continue; - - type.FillBlockModel(buf, ToTransform(pos, idx), vtx_counter); - size_t vtx_begin = vtx_counter; - vtx_counter += type.shape->VertexCount(); - - for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) { - buf.lights.emplace_back(GetVertexLight( - pos, - buf.vertices[vtx], - type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform()) - )); + if (idx_count > 0) { + int idx = 0; + BlockMesh::Index vtx_counter = 0; + for (size_t z = 0; z < depth; ++z) { + for (size_t y = 0; y < height; ++y) { + for (size_t x = 0; x < width; ++x, ++idx) { + const BlockType &type = Type(BlockAt(idx)); + const Pos pos(x, y, z); + + if (!type.visible || !type.shape || Obstructed(pos).All()) continue; + + type.FillBlockMesh(buf, ToTransform(pos, idx), vtx_counter); + size_t vtx_begin = vtx_counter; + vtx_counter += type.shape->VertexCount(); + + for (size_t vtx = vtx_begin; vtx < vtx_counter; ++vtx) { + buf.lights.emplace_back(GetVertexLight( + pos, + buf.vertices[vtx], + type.shape->VertexNormal(vtx - vtx_begin, BlockAt(idx).Transform()) + )); + } } } } } model.Update(buf); - ClearModel(); + ClearMesh(); } Block::FaceSet Chunk::Obstructed(const Pos &pos) const noexcept { @@ -680,13 +616,39 @@ bool ChunkLoader::LoadOne() { return false; } + bool generated = false; if (save.Exists(pos)) { save.Read(*chunk); - return false; } else { gen(*chunk); - return true; + generated = true; + } + + ChunkIndex *index = store.ClosestIndex(pos); + if (!index) { + return generated; + } + + Chunk::Pos begin(pos - Chunk::Pos(1)); + Chunk::Pos end(pos + Chunk::Pos(2)); + for (Chunk::Pos iter(begin); iter.z < end.z; ++iter.z) { + for (iter.y = begin.y; iter.y < end.y; ++iter.y) { + for (iter.x = begin.x; iter.x < end.x; ++iter.x) { + if (index->IsBorder(iter)) continue; + Chunk *light_chunk = index->Get(iter); + if (!light_chunk) continue; + if (index->HasAllSurrounding(iter)) { + if (!light_chunk->Lighted()) { + light_chunk->ScanLights(); + } else { + light_chunk->InvalidateMesh(); + } + } + } + } } + + return generated; } void ChunkLoader::LoadN(std::size_t n) { @@ -713,7 +675,7 @@ int ChunkRenderer::MissingChunks() const noexcept { return index.MissingChunks(); } -void ChunkRenderer::LoadTextures(const AssetLoader &loader, const TextureIndex &tex_index) { +void ChunkRenderer::LoadTextures(const AssetLoader &loader, const ResourceIndex &tex_index) { block_tex.Bind(); loader.LoadTextures(tex_index, block_tex); block_tex.FilterNearest(); @@ -721,7 +683,11 @@ void ChunkRenderer::LoadTextures(const AssetLoader &loader, const TextureIndex & void ChunkRenderer::Update(int dt) { for (int i = 0, updates = 0; updates < dt && i < index.TotalChunks(); ++i) { - if (index[i] && index[i]->ShouldUpdateModel()) { + if (!index[i]) continue; + if (!index[i]->Lighted() && index.HasAllSurrounding(index[i]->Position())) { + index[i]->ScanLights(); + } + if (index[i]->ShouldUpdateMesh()) { index[i]->Update(models[i]); ++updates; } @@ -738,7 +704,7 @@ void ChunkRenderer::Render(Viewport &viewport) { glm::mat4 m(index[i]->Transform(index.Base())); glm::mat4 mvp(chunk_prog.GetVP() * m); if (!CullTest(Chunk::Bounds(), mvp)) { - if (index[i]->ShouldUpdateModel()) { + if (index[i]->ShouldUpdateMesh()) { index[i]->Update(models[i]); } chunk_prog.SetM(m); @@ -766,7 +732,28 @@ ChunkIndex::~ChunkIndex() { } bool ChunkIndex::InRange(const Chunk::Pos &pos) const noexcept { - return manhattan_radius(pos - base) <= extent; + return Distance(pos) <= extent; +} + +bool ChunkIndex::IsBorder(const Chunk::Pos &pos) const noexcept { + return Distance(pos) == extent; +} + +int ChunkIndex::Distance(const Chunk::Pos &pos) const noexcept { + return manhattan_radius(pos - base); +} + +bool ChunkIndex::HasAllSurrounding(const Chunk::Pos &pos) const noexcept { + Chunk::Pos begin(pos - Chunk::Pos(1)); + Chunk::Pos end(pos + Chunk::Pos(2)); + for (Chunk::Pos iter(begin); iter.z < end.z; ++iter.z) { + for (iter.y = begin.y; iter.y < end.y; ++iter.y) { + for (iter.x = begin.x; iter.x < end.x; ++iter.x) { + if (!Get(iter)) return false; + } + } + } + return true; } int ChunkIndex::IndexOf(const Chunk::Pos &pos) const noexcept { @@ -962,6 +949,21 @@ void ChunkStore::UnregisterIndex(ChunkIndex &index) { } } +ChunkIndex *ChunkStore::ClosestIndex(const Chunk::Pos &pos) { + ChunkIndex *closest_index = nullptr; + int closest_distance = std::numeric_limits::max(); + + for (ChunkIndex &index : indices) { + int distance = index.Distance(pos); + if (distance < closest_distance) { + closest_index = &index; + closest_distance = distance; + } + } + + return closest_index; +} + Chunk *ChunkStore::Get(const Chunk::Pos &pos) { for (ChunkIndex &index : indices) { Chunk *chunk = index.Get(pos); @@ -1029,14 +1031,14 @@ Chunk::Pos ChunkStore::NextMissing() noexcept { void ChunkStore::Clean() { for (auto i = loaded.begin(), end = loaded.end(); i != end;) { - if (i->Referenced()) { + if (i->Referenced() || i->ShouldUpdateSave()) { ++i; } else { auto chunk = i; ++i; free.splice(free.end(), loaded, chunk); chunk->Unlink(); - chunk->InvalidateModel(); + chunk->InvalidateMesh(); } } }