]> git.localhorst.tv Git - blank.git/blobdiff - src/chunk.hpp
minor optimizations in chunk
[blank.git] / src / chunk.hpp
index 185b3eabefd5deb1076b77f36dd8a45950257900..d86f4170b8deff7e22d346299170bfece8860fc8 100644 (file)
@@ -5,8 +5,10 @@
 #include "geometry.hpp"
 #include "model.hpp"
 
+#include <list>
 #include <vector>
 #include <glm/glm.hpp>
+#include <glm/gtx/transform.hpp>
 
 
 namespace blank {
@@ -15,66 +17,217 @@ namespace blank {
 class Chunk {
 
 public:
-       Chunk();
+       using Pos = glm::tvec3<int>;
 
-       Chunk(Chunk &&);
-       Chunk &operator =(Chunk &&);
+public:
+       explicit Chunk(const BlockTypeRegistry &) noexcept;
+
+       Chunk(Chunk &&) noexcept;
+       Chunk &operator =(Chunk &&) noexcept;
 
-       static constexpr int Width() { return 16; }
-       static constexpr int Height() { return 16; }
-       static constexpr int Depth() { return 16; }
-       static glm::vec3 Extent() { return glm::vec3(Width(), Height(), Depth()); }
-       static constexpr int Size() { return Width() * Height() * Depth(); }
+       static constexpr int width = 16;
+       static constexpr int height = 16;
+       static constexpr int depth = 16;
+       static Pos Extent() noexcept { return { width, height, depth }; }
+       static constexpr int size = width * height * depth;
 
-       static constexpr bool InBounds(const glm::vec3 &pos) {
+       static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
+
+       static constexpr bool InBounds(const Block::Pos &pos) noexcept {
                return
-                       pos.x >= 0 && pos.x < Width() &&
-                       pos.y >= 0 && pos.y < Height() &&
-                       pos.z >= 0 && pos.z < Depth();
+                       pos.x >= 0 && pos.x < width &&
+                       pos.y >= 0 && pos.y < height &&
+                       pos.z >= 0 && pos.z < depth;
+       }
+       static constexpr bool InBounds(const Pos &pos) noexcept {
+               return
+                       pos.x >= 0 && pos.x < width &&
+                       pos.y >= 0 && pos.y < height &&
+                       pos.z >= 0 && pos.z < depth;
+       }
+       static constexpr int ToIndex(const Pos &pos) noexcept {
+               return pos.x + pos.y * width + pos.z * width * height;
+       }
+       static constexpr bool InBounds(int idx) noexcept {
+               return idx >= 0 && idx < size;
        }
-       static constexpr int ToIndex(const glm::vec3 &pos) {
-               return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
+       static Block::Pos ToCoords(int idx) noexcept {
+               return Block::Pos(
+                       0.5f + (idx % width),
+                       0.5f + ((idx / width) % height),
+                       0.5f + (idx / (width * height))
+               );
        }
-       static constexpr bool InBounds(int idx) {
-               return idx >= 0 && idx < Size();
+       static Block::Pos ToCoords(const Pos &pos) noexcept {
+               return Block::Pos(pos) + 0.5f;
        }
-       static glm::vec3 ToCoords(int idx) {
-               return glm::vec3(
-                       0.5f + idx % Width(),
-                       0.5f + (idx / Width()) % Height(),
-                       0.5f + idx / (Width() * Height())
+       static Pos ToPos(int idx) noexcept {
+               return Pos(
+                       (idx % width),
+                       ((idx / width) % height),
+                       (idx / (width * height))
                );
        }
+       glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
+
+       static constexpr bool IsBorder(int idx) noexcept {
+               return
+                       idx < width * height ||                    // low Z plane
+                       idx % width == 0 ||                          // low X plane
+                       (idx / (width * height)) == depth - 1 || // high Z plane
+                       idx % width == width - 1 ||                // high X plane
+                       (idx / width) % height == 0 ||             // low Y plane
+                       (idx / width) % height == height - 1;    // high Y plane
+       }
+
+       bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
+       bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
+       bool IsSurface(const Pos &pos) const noexcept;
+
+       void SetNeighbor(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() { dirty = true; }
+       void Invalidate() noexcept { dirty = true; }
 
-       Block &BlockAt(int index) { return blocks[index]; }
-       const Block &BlockAt(int index) const { return blocks[index]; }
-       Block &BlockAt(const glm::vec3 &pos) { return BlockAt(ToIndex(pos)); }
-       const Block &BlockAt(const glm::vec3 &pos) const { return BlockAt(ToIndex(pos)); }
+       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); }
+
+       const Block &BlockAt(int index) const noexcept { return blocks[index]; }
+       const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
+       const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
+
+       const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
+       const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
+
+       void SetLight(int index, int level) noexcept;
+       void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
+       void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
+
+       int GetLight(int index) const noexcept;
+       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;
+
+       bool Intersection(
+               const Ray &ray,
+               const glm::mat4 &M,
+               float &dist
+       ) const noexcept {
+               return blank::Intersection(ray, Bounds(), M, &dist);
+       }
 
        bool Intersection(
                const Ray &,
                const glm::mat4 &M,
-               int *blkid = nullptr,
-               float *dist = nullptr,
-               glm::vec3 *normal = nullptr) const;
+               int &blkid,
+               float &dist,
+               glm::vec3 &normal) const noexcept;
+
+       void Position(const Pos &pos) noexcept { position = pos; }
+       const Pos &Position() const noexcept { return position; }
+       glm::mat4 Transform(const Pos &offset) const noexcept {
+               return glm::translate((position - offset) * Extent());
+       }
+
+       void CheckUpdate() noexcept;
+       void Draw() noexcept;
+
+private:
+       void Update() noexcept;
+
+private:
+       const BlockTypeRegistry *types;
+       Chunk *neighbor[Block::FACE_COUNT];
+       Block blocks[16 * 16 * 16];
+       unsigned char light[16 * 16 * 16];
+       BlockModel model;
+       Pos position;
+       bool dirty;
+
+};
+
+
+class BlockLookup {
+
+public:
+       // resolve chunk/position from oob coordinates
+       BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept;
+
+       // resolve chunk/position from ib coordinates and direction
+       BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept;
 
-       void Position(const glm::vec3 &);
-       const glm::vec3 &Position() const { return position; }
-       glm::mat4 Transform(const glm::vec3 &offset) const;
+       // check if lookup was successful
+       operator bool() const { return chunk; }
 
-       void Draw();
+       // only valid if lookup was successful
+       Chunk &GetChunk() const noexcept { return *chunk; }
+       const Chunk::Pos &GetBlockPos() const noexcept { return pos; }
+       const Block &GetBlock() const noexcept { return GetChunk().BlockAt(GetBlockPos()); }
+       const BlockType &GetType() const noexcept { return GetChunk().Type(GetBlock()); }
+       int GetLight() const noexcept { return GetChunk().GetLight(GetBlockPos()); }
+
+       // traverse in given direction
+       BlockLookup Next(Block::Face f) const { return BlockLookup(chunk, pos, f); }
 
 private:
-       int VertexCount() const;
+       Chunk *chunk;
+       Chunk::Pos pos;
+
+};
+
+
+class Generator;
+
+class ChunkLoader {
+
+public:
+       struct Config {
+               int load_dist = 6;
+               int unload_dist = 8;
+       };
+
+       ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept;
+
+       void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
+       void GenerateSurrounding(const Chunk::Pos &);
+
+       std::list<Chunk> &Loaded() noexcept { return loaded; }
+
+       Chunk *Loaded(const Chunk::Pos &) noexcept;
+       bool Queued(const Chunk::Pos &) noexcept;
+       bool Known(const Chunk::Pos &) noexcept;
+       Chunk &ForceLoad(const Chunk::Pos &);
+
+       void Rebase(const Chunk::Pos &);
        void Update();
 
 private:
-       std::vector<Block> blocks;
-       Model model;
-       glm::vec3 position;
-       bool dirty;
+       Chunk &Generate(const Chunk::Pos &pos);
+       void Insert(Chunk &) noexcept;
+       void Remove(Chunk &) noexcept;
+
+private:
+       Chunk::Pos base;
+
+       const BlockTypeRegistry &reg;
+       const Generator &gen;
+
+       std::list<Chunk> loaded;
+       std::list<Chunk::Pos> to_generate;
+       std::list<Chunk> to_free;
+
+       int load_dist;
+       int unload_dist;
 
 };