1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
5 #include "geometry.hpp"
10 #include <glm/glm.hpp>
11 #include <glm/gtx/transform.hpp>
16 /// cube of size 16 (256 tiles, 4096 blocks)
20 using Pos = glm::tvec3<int>;
23 explicit Chunk(const BlockTypeRegistry &) noexcept;
25 Chunk(Chunk &&) noexcept;
26 Chunk &operator =(Chunk &&) noexcept;
28 static constexpr int width = 16;
29 static constexpr int height = 16;
30 static constexpr int depth = 16;
31 static Pos Extent() noexcept { return { width, height, depth }; }
32 static constexpr int size = width * height * depth;
34 static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
36 static constexpr bool InBounds(const Block::Pos &pos) noexcept {
38 pos.x >= 0 && pos.x < width &&
39 pos.y >= 0 && pos.y < height &&
40 pos.z >= 0 && pos.z < depth;
42 static constexpr bool InBounds(const Pos &pos) noexcept {
44 pos.x >= 0 && pos.x < width &&
45 pos.y >= 0 && pos.y < height &&
46 pos.z >= 0 && pos.z < depth;
48 static constexpr int ToIndex(const Pos &pos) noexcept {
49 return pos.x + pos.y * width + pos.z * width * height;
51 static constexpr bool InBounds(int idx) noexcept {
52 return idx >= 0 && idx < size;
54 static Block::Pos ToCoords(int idx) noexcept {
57 0.5f + ((idx / width) % height),
58 0.5f + (idx / (width * height))
61 static Block::Pos ToCoords(const Pos &pos) noexcept {
62 return Block::Pos(pos) + 0.5f;
64 static Pos ToPos(int idx) noexcept {
67 ((idx / width) % height),
68 (idx / (width * height))
71 glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
73 static constexpr bool IsBorder(int idx) noexcept {
75 idx < width * height || // low Z plane
76 idx % width == 0 || // low X plane
77 (idx / (width * height)) == depth - 1 || // high Z plane
78 idx % width == width - 1 || // high X plane
79 (idx / width) % height == 0 || // low Y plane
80 (idx / width) % height == height - 1; // high Y plane
83 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
84 bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
85 bool IsSurface(const Pos &pos) const noexcept;
87 void SetNeighbor(Chunk &) noexcept;
88 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
89 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
90 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
91 void ClearNeighbors() noexcept;
92 void Unlink() noexcept;
93 void Relink() noexcept;
95 // check which faces of a block at given index are obstructed (and therefore invisible)
96 Block::FaceSet Obstructed(const Pos &) const noexcept;
98 void Invalidate() noexcept { dirty = true; }
100 void SetBlock(int index, const Block &) noexcept;
101 void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
102 void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
104 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
105 const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
106 const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
108 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
109 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
111 void SetLight(int index, int level) noexcept;
112 void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
113 void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
115 int GetLight(int index) const noexcept;
116 int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
117 int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
119 float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
126 return blank::Intersection(ray, Bounds(), M, &dist);
134 glm::vec3 &normal) const noexcept;
136 void Position(const Pos &pos) noexcept { position = pos; }
137 const Pos &Position() const noexcept { return position; }
138 glm::mat4 Transform(const Pos &offset) const noexcept {
139 return glm::translate((position - offset) * Extent());
142 void CheckUpdate() noexcept;
143 void Draw() noexcept;
146 void Update() noexcept;
149 const BlockTypeRegistry *types;
150 Chunk *neighbor[Block::FACE_COUNT];
151 Block blocks[16 * 16 * 16];
152 unsigned char light[16 * 16 * 16];
163 // resolve chunk/position from oob coordinates
164 BlockLookup(Chunk *c, const Chunk::Pos &p) noexcept;
166 // resolve chunk/position from ib coordinates and direction
167 BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir) noexcept;
169 // check if lookup was successful
170 operator bool() const { return chunk; }
172 // only valid if lookup was successful
173 Chunk &GetChunk() const noexcept { return *chunk; }
174 const Chunk::Pos &GetBlockPos() const noexcept { return pos; }
175 const Block &GetBlock() const noexcept { return GetChunk().BlockAt(GetBlockPos()); }
176 const BlockType &GetType() const noexcept { return GetChunk().Type(GetBlock()); }
177 int GetLight() const noexcept { return GetChunk().GetLight(GetBlockPos()); }
179 // traverse in given direction
180 BlockLookup Next(Block::Face f) const { return BlockLookup(chunk, pos, f); }
199 ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &) noexcept;
201 void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
202 void GenerateSurrounding(const Chunk::Pos &);
204 std::list<Chunk> &Loaded() noexcept { return loaded; }
206 Chunk *Loaded(const Chunk::Pos &) noexcept;
207 bool Queued(const Chunk::Pos &) noexcept;
208 bool Known(const Chunk::Pos &) noexcept;
209 Chunk &ForceLoad(const Chunk::Pos &);
211 void Rebase(const Chunk::Pos &);
215 Chunk &Generate(const Chunk::Pos &pos);
216 void Insert(Chunk &) noexcept;
217 void Remove(Chunk &) noexcept;
222 const BlockTypeRegistry ®
223 const Generator &gen;
225 std::list<Chunk> loaded;
226 std::list<Chunk::Pos> to_generate;
227 std::list<Chunk> to_free;