]> git.localhorst.tv Git - blank.git/blobdiff - src/world/Chunk.hpp
composite model is the canonical model
[blank.git] / src / world / Chunk.hpp
index 8493ef6e2ef1b7f4f61ef5439484282a8e3ffbab..eaed28a0fd48f727b0274f5cf1f6e6a3f7b20fe0 100644 (file)
@@ -3,9 +3,9 @@
 
 #include "Block.hpp"
 #include "BlockTypeRegistry.hpp"
-#include "../model/BlockModel.hpp"
 #include "../model/geometry.hpp"
 
+#include <vector>
 #include <glm/glm.hpp>
 #include <glm/gtx/transform.hpp>
 
 namespace blank {
 
 class BlockType;
+class WorldCollision;
 
 /// cube of size 16 (256 tiles, 4096 blocks)
 class Chunk {
 
 public:
-       using Pos = glm::tvec3<int>;
+       using Pos = glm::ivec3;
 
 public:
        explicit Chunk(const BlockTypeRegistry &) noexcept;
@@ -71,6 +72,19 @@ public:
        }
        glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
 
+       Block::Pos ToSceneCoords(const Pos &base, const Block::Pos &pos) const noexcept {
+               return Block::Pos((position - base) * Extent()) + pos;
+       }
+
+       static bool IsBorder(const Pos &pos) noexcept {
+               return
+                       pos.x == 0 ||
+                       pos.x == width - 1 ||
+                       pos.y == 0 ||
+                       pos.y == height - 1 ||
+                       pos.z == 0 ||
+                       pos.z == depth - 1;
+       }
        static constexpr bool IsBorder(int idx) noexcept {
                return
                        idx < width * height ||                    // low Z plane
@@ -85,19 +99,15 @@ public:
        bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
        bool IsSurface(const Pos &pos) const noexcept;
 
-       void SetNeighbor(Chunk &) noexcept;
+       void SetNeighbor(Block::Face, Chunk &) noexcept;
        bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
        Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
        const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
-       void ClearNeighbors() noexcept;
        void Unlink() noexcept;
-       void Relink() noexcept;
 
        // check which faces of a block at given index are obstructed (and therefore invisible)
        Block::FaceSet Obstructed(const Pos &) const noexcept;
 
-       void Invalidate() noexcept { dirty = true; }
-
        void SetBlock(int index, const Block &) noexcept;
        void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
        void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
@@ -117,7 +127,7 @@ public:
        int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
        int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
 
-       float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
+       float GetVertexLight(const Pos &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
 
        bool Intersection(
                const Ray &ray,
@@ -130,9 +140,13 @@ public:
        bool Intersection(
                const Ray &,
                const glm::mat4 &M,
-               int &blkid,
-               float &dist,
-               glm::vec3 &normal) const noexcept;
+               WorldCollision &) noexcept;
+
+       bool Intersection(
+               const AABB &box,
+               const glm::mat4 &Mbox,
+               const glm::mat4 &Mchunk,
+               std::vector<WorldCollision> &) noexcept;
 
        void Position(const Pos &pos) noexcept { position = pos; }
        const Pos &Position() const noexcept { return position; }
@@ -140,20 +154,41 @@ public:
                return glm::translate((position - offset) * Extent());
        }
 
-       void CheckUpdate() noexcept;
-       void Draw() noexcept;
+       void *BlockData() noexcept { return &blocks[0]; }
+       const void *BlockData() const noexcept { return &blocks[0]; }
+       static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
 
-private:
-       void Update() noexcept;
+       bool Generated() const noexcept { return generated; }
+       void SetGenerated() noexcept { generated = true; }
+       bool Lighted() const noexcept { return lighted; }
+       void ScanLights();
+
+       void Ref() noexcept { ++ref_count; }
+       void UnRef() noexcept { --ref_count; }
+       bool Referenced() const noexcept { return ref_count > 0; }
+
+       void Invalidate() noexcept { dirty_mesh = dirty_save = true; }
+       void InvalidateMesh() noexcept { dirty_mesh = true; }
+       void ClearMesh() noexcept { dirty_mesh = false; }
+       void ClearSave() noexcept { dirty_save = false; }
+       bool ShouldUpdateMesh() const noexcept { return dirty_mesh; }
+       bool ShouldUpdateSave() const noexcept { return dirty_save; }
+
+       void Update(BlockMesh &) noexcept;
 
 private:
        const BlockTypeRegistry *types;
        Chunk *neighbor[Block::FACE_COUNT];
-       Block blocks[16 * 16 * 16];
-       unsigned char light[16 * 16 * 16];
-       BlockModel model;
+
+       Block blocks[size];
+       unsigned char light[size];
+       bool generated;
+       bool lighted;
+
        Pos position;
-       bool dirty;
+       int ref_count;
+       bool dirty_mesh;
+       bool dirty_save;
 
 };