1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
5 #include "geometry.hpp"
14 /// cube of size 16 (256 tiles, 4096 blocks)
18 using Pos = glm::tvec3<int>;
21 explicit Chunk(const BlockTypeRegistry &);
24 Chunk &operator =(Chunk &&);
26 static constexpr int Width() { return 16; }
27 static constexpr int Height() { return 16; }
28 static constexpr int Depth() { return 16; }
29 static Pos Extent() { return { Width(), Height(), Depth() }; }
30 static constexpr int Size() { return Width() * Height() * Depth(); }
32 static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
34 static constexpr bool InBounds(const glm::vec3 &pos) {
36 pos.x >= 0 && pos.x < Width() &&
37 pos.y >= 0 && pos.y < Height() &&
38 pos.z >= 0 && pos.z < Depth();
40 static constexpr int ToIndex(const glm::vec3 &pos) {
41 return int(pos.x) + int(pos.y) * Width() + int(pos.z) * Width() * Height();
43 static constexpr bool InBounds(int idx) {
44 return idx >= 0 && idx < Size();
46 static Block::Pos ToCoords(int idx) {
48 0.5f + (idx % Width()),
49 0.5f + ((idx / Width()) % Height()),
50 0.5f + (idx / (Width() * Height()))
55 void Invalidate() { dirty = true; }
57 Block &BlockAt(int index) { return blocks[index]; }
58 const Block &BlockAt(int index) const { return blocks[index]; }
59 Block &BlockAt(const Block::Pos &pos) { return BlockAt(ToIndex(pos)); }
60 const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
62 const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
68 float *dist = nullptr,
69 glm::vec3 *normal = nullptr) const;
71 void Position(const Pos &);
72 const Pos &Position() const { return position; }
73 glm::mat4 Transform(const Pos &offset) const;
82 const BlockTypeRegistry *types;
83 std::vector<Block> blocks;