]> git.localhorst.tv Git - blank.git/blobdiff - src/chunk.hpp
minor optimizations in chunk
[blank.git] / src / chunk.hpp
index 4addb56d7a9bf9b87935664dba54fdfe266756f7..d86f4170b8deff7e22d346299170bfece8860fc8 100644 (file)
@@ -8,6 +8,7 @@
 #include <list>
 #include <vector>
 #include <glm/glm.hpp>
+#include <glm/gtx/transform.hpp>
 
 
 namespace blank {
@@ -19,105 +20,109 @@ public:
        using Pos = glm::tvec3<int>;
 
 public:
-       explicit Chunk(const BlockTypeRegistry &);
+       explicit Chunk(const BlockTypeRegistry &) noexcept;
 
-       Chunk(Chunk &&);
-       Chunk &operator =(Chunk &&);
+       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 Pos Extent() { return { 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 AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
+       static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
 
-       static constexpr bool InBounds(const Block::Pos &pos) {
+       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) {
+       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();
+                       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) {
-               return pos.x + pos.y * Width() + pos.z * Width() * Height();
+       static constexpr int ToIndex(const Pos &pos) noexcept {
+               return pos.x + pos.y * width + pos.z * width * height;
        }
-       static constexpr bool InBounds(int idx) {
-               return idx >= 0 && idx < Size();
+       static constexpr bool InBounds(int idx) noexcept {
+               return idx >= 0 && idx < size;
        }
-       static Block::Pos ToCoords(int idx) {
+       static Block::Pos ToCoords(int idx) noexcept {
                return Block::Pos(
-                       0.5f + (idx % Width()),
-                       0.5f + ((idx / Width()) % Height()),
-                       0.5f + (idx / (Width() * Height()))
+                       0.5f + (idx % width),
+                       0.5f + ((idx / width) % height),
+                       0.5f + (idx / (width * height))
                );
        }
-       static Pos ToPos(int idx) {
+       static Block::Pos ToCoords(const Pos &pos) noexcept {
+               return Block::Pos(pos) + 0.5f;
+       }
+       static Pos ToPos(int idx) noexcept {
                return Pos(
-                       (idx % Width()),
-                       ((idx / Width()) % Height()),
-                       (idx / (Width() * Height()))
+                       (idx % width),
+                       ((idx / width) % height),
+                       (idx / (width * height))
                );
        }
-       glm::mat4 ToTransform(int idx) const;
+       glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
 
-       static constexpr bool IsBorder(int idx) {
+       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
+                       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 { return IsSurface(ToPos(index)); }
-       bool IsSurface(const Block::Pos &pos) const { return IsSurface(Pos(pos)); }
-       bool IsSurface(const Pos &pos) const;
+       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 &);
-       bool HasNeighbor(Block::Face f) const { return neighbor[f]; }
-       Chunk &GetNeighbor(Block::Face f) { return *neighbor[f]; }
-       const Chunk &GetNeighbor(Block::Face f) const { return *neighbor[f]; }
-       void ClearNeighbors();
-       void Unlink();
-       void Relink();
+       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 if block at given index is completely enclosed (and therefore invisible)
-       bool Obstructed(int idx) const;
+       // 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; }
 
-       void SetBlock(int index, const Block &);
-       void SetBlock(const Block::Pos &pos, const Block &block) { SetBlock(ToIndex(pos), block); }
-       void SetBlock(const Pos &pos, const Block &block) { SetBlock(ToIndex(pos), block); }
+       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 { return blocks[index]; }
-       const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
-       const Block &BlockAt(const Pos &pos) const { return BlockAt(ToIndex(pos)); }
+       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 { return *types->Get(b.type); }
+       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);
-       void SetLight(const Pos &pos, int level) { SetLight(ToIndex(pos), level); }
-       void SetLight(const Block::Pos &pos, int level) { SetLight(ToIndex(pos), level); }
+       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;
-       int GetLight(const Pos &pos) const { return GetLight(ToIndex(pos)); }
-       int GetLight(const Block::Pos &pos) const { return GetLight(ToIndex(pos)); }
+       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(int index, const BlockModel::Position &, const Model::Normal &) const;
+       float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
 
        bool Intersection(
                const Ray &ray,
                const glm::mat4 &M,
                float &dist
-       ) const {
+       ) const noexcept {
                return blank::Intersection(ray, Bounds(), M, &dist);
        }
 
@@ -126,17 +131,19 @@ public:
                const glm::mat4 &M,
                int &blkid,
                float &dist,
-               glm::vec3 &normal) const;
+               glm::vec3 &normal) const noexcept;
 
-       void Position(const Pos &);
-       const Pos &Position() const { return position; }
-       glm::mat4 Transform(const Pos &offset) const;
+       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();
-       void Draw();
+       void CheckUpdate() noexcept;
+       void Draw() noexcept;
 
 private:
-       void Update();
+       void Update() noexcept;
 
 private:
        const BlockTypeRegistry *types;
@@ -154,20 +161,23 @@ class BlockLookup {
 
 public:
        // resolve chunk/position from oob coordinates
-       BlockLookup(Chunk *c, const Chunk::Pos &p);
+       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);
+       BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept;
 
        // check if lookup was successful
        operator bool() const { return chunk; }
 
        // only valid if lookup was successful
-       Chunk &GetChunk() const { return *chunk; }
-       const Chunk::Pos &GetBlockPos() const { return pos; }
-       const Block &GetBlock() const { return GetChunk().BlockAt(GetBlockPos()); }
-       const BlockType &GetType() const { return GetChunk().Type(GetBlock()); }
-       int GetLight() const { return GetChunk().GetLight(GetBlockPos()); }
+       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:
        Chunk *chunk;
@@ -186,16 +196,16 @@ public:
                int unload_dist = 8;
        };
 
-       ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &);
+       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() { return loaded; }
+       std::list<Chunk> &Loaded() noexcept { return loaded; }
 
-       Chunk *Loaded(const Chunk::Pos &);
-       bool Queued(const Chunk::Pos &);
-       bool Known(const Chunk::Pos &);
+       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 &);
@@ -203,8 +213,8 @@ public:
 
 private:
        Chunk &Generate(const Chunk::Pos &pos);
-       void Insert(Chunk &);
-       void Remove(Chunk &);
+       void Insert(Chunk &) noexcept;
+       void Remove(Chunk &) noexcept;
 
 private:
        Chunk::Pos base;