X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fchunk.hpp;h=d86f4170b8deff7e22d346299170bfece8860fc8;hb=e53a0e2e711a7d8bd9b0ddacd1360aa14370643f;hp=28ba4e357aa6a87137b62ecfdc61b73a6205f17a;hpb=7caa2326d25d4fc5ba98318dfccb508bb3e16820;p=blank.git diff --git a/src/chunk.hpp b/src/chunk.hpp index 28ba4e3..d86f417 100644 --- a/src/chunk.hpp +++ b/src/chunk.hpp @@ -5,8 +5,10 @@ #include "geometry.hpp" #include "model.hpp" +#include #include #include +#include namespace blank { @@ -18,75 +20,217 @@ public: using Pos = glm::tvec3; 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 glm::vec3 &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 int ToIndex(const glm::vec3 &pos) { - return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height(); + 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) { - 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 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)) + ); + } + 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() 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); } + + 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)); } - void Allocate(); - void Invalidate() { dirty = true; } + const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); } + const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); } - Block &BlockAt(int index) { return blocks[index]; } - const Block &BlockAt(int index) const { return blocks[index]; } - Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); } - const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); } + 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); } - const BlockType &Type(const Block &b) const { return *types->Get(b.type); } + 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 &, + const Ray &ray, const glm::mat4 &M, - int *blkid = nullptr, - float *dist = nullptr, - glm::vec3 *normal = nullptr) const; + float &dist + ) const noexcept { + return blank::Intersection(ray, Bounds(), M, &dist); + } - void Position(const Pos &); - const Pos &Position() const { return position; } - glm::mat4 Transform(const Pos &offset) const; + bool Intersection( + const Ray &, + const glm::mat4 &M, + 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 Draw(); + void CheckUpdate() noexcept; + void Draw() noexcept; private: - int VertexCount() const; - void Update(); + void Update() noexcept; private: const BlockTypeRegistry *types; - std::vector blocks; - Model model; + 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; + + // check if lookup was successful + operator bool() const { return chunk; } + + // 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: + 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 &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: + Chunk &Generate(const Chunk::Pos &pos); + void Insert(Chunk &) noexcept; + void Remove(Chunk &) noexcept; + +private: + Chunk::Pos base; + + const BlockTypeRegistry ® + const Generator &gen; + + std::list loaded; + std::list to_generate; + std::list to_free; + + int load_dist; + int unload_dist; + +}; + } #endif