]> git.localhorst.tv Git - blank.git/blobdiff - src/world/chunk.cpp
split geometry lib
[blank.git] / src / world / chunk.cpp
index 14c3af5c9c9af0fdc6560a2718d3b118553db205..8340d56cd50b1888b6798108fd747d028d3baa37 100644 (file)
@@ -8,6 +8,7 @@
 #include "Generator.hpp"
 #include "WorldCollision.hpp"
 #include "../app/Assets.hpp"
+#include "../geometry/distance.hpp"
 #include "../graphics/BlockLighting.hpp"
 #include "../graphics/BlockMesh.hpp"
 #include "../graphics/Viewport.hpp"
@@ -371,7 +372,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;
@@ -412,7 +413,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)) {
@@ -435,33 +436,37 @@ BlockMesh::Buffer buf;
 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;
-       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 || 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())
-                                       ));
+       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())
+                                               ));
+                                       }
                                }
                        }
                }
@@ -632,9 +637,13 @@ bool ChunkLoader::LoadOne() {
                        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 || light_chunk->Lighted()) continue;
+                               if (!light_chunk) continue;
                                if (index->HasAllSurrounding(iter)) {
-                                       light_chunk->ScanLights();
+                                       if (!light_chunk->Lighted()) {
+                                               light_chunk->ScanLights();
+                                       } else {
+                                               light_chunk->InvalidateMesh();
+                                       }
                                }
                        }
                }
@@ -667,7 +676,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();