]> git.localhorst.tv Git - blank.git/blobdiff - src/chunk.cpp
don't add obstructed blocks to meshes
[blank.git] / src / chunk.cpp
index 10031e77e359deb60145bdefe24d2195baecfa6e..16ff4e5023bb65f4f68495294b7b940007b67239 100644 (file)
@@ -6,6 +6,12 @@
 #include <glm/gtx/transform.hpp>
 
 
+namespace {
+
+blank::Model::Buffer buf;
+
+}
+
 namespace blank {
 
 Chunk::Chunk(const BlockTypeRegistry &types)
@@ -117,7 +123,6 @@ void Chunk::CheckUpdate() {
        if (dirty) {
                Update();
        }
-       model.CheckUpdate();
 }
 
 void Chunk::Update() {
@@ -127,20 +132,50 @@ void Chunk::Update() {
                vtx_count += shape->VertexCount();
                idx_count += shape->VertexIndexCount();
        }
-       model.Clear();
-       model.Reserve(vtx_count, idx_count);
+       buf.Clear();
+       buf.Reserve(vtx_count, idx_count);
 
        Model::Index vtx_counter = 0;
        for (size_t i = 0; i < Size(); ++i) {
+               if (Obstructed(i)) continue;
+
                const BlockType &type = Type(blocks[i]);
-               type.FillModel(model, ToCoords(i), vtx_counter);
+               type.FillModel(buf, ToCoords(i), vtx_counter);
                vtx_counter += type.shape->VertexCount();
        }
 
-       model.Invalidate();
+       model.Update(buf);
        dirty = false;
 }
 
+bool Chunk::Obstructed(int idx) const {
+       if (IsBorder(idx)) return false;
+
+       // not checking neighbor visibility here, so all
+       // invisible blocks must have their fill set to 6x false
+       // (the default, so should be okay)
+
+       const BlockType &right = Type(blocks[idx + 1]);
+       if (!right.fill.left) return false;
+
+       const BlockType &left = Type(blocks[idx - 1]);
+       if (!left.fill.right) return false;
+
+       const BlockType &top = Type(blocks[idx + Width()]);
+       if (!top.fill.bottom) return false;
+
+       const BlockType &bottom = Type(blocks[idx - Width()]);
+       if (!bottom.fill.top) return false;
+
+       const BlockType &front = Type(blocks[idx + Width() * Height()]);
+       if (!front.fill.back) return false;
+
+       const BlockType &back = Type(blocks[idx - Width() * Height()]);
+       if (!back.fill.front) return false;
+
+       return true;
+}
+
 
 ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
 : base(0, 0, 0)