1 #ifndef BLANK_WORLD_CHUNK_HPP_
2 #define BLANK_WORLD_CHUNK_HPP_
5 #include "BlockTypeRegistry.hpp"
6 #include "../model/BlockModel.hpp"
7 #include "../model/geometry.hpp"
10 #include <glm/gtx/transform.hpp>
17 /// cube of size 16 (256 tiles, 4096 blocks)
21 using Pos = glm::tvec3<int>;
24 explicit Chunk(const BlockTypeRegistry &) noexcept;
26 Chunk(Chunk &&) noexcept;
27 Chunk &operator =(Chunk &&) noexcept;
29 static constexpr int width = 16;
30 static constexpr int height = 16;
31 static constexpr int depth = 16;
32 static Pos Extent() noexcept { return { width, height, depth }; }
33 static constexpr int size = width * height * depth;
35 static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
37 static constexpr bool InBounds(const Block::Pos &pos) noexcept {
39 pos.x >= 0 && pos.x < width &&
40 pos.y >= 0 && pos.y < height &&
41 pos.z >= 0 && pos.z < depth;
43 static constexpr bool InBounds(const Pos &pos) noexcept {
45 pos.x >= 0 && pos.x < width &&
46 pos.y >= 0 && pos.y < height &&
47 pos.z >= 0 && pos.z < depth;
49 static constexpr int ToIndex(const Pos &pos) noexcept {
50 return pos.x + pos.y * width + pos.z * width * height;
52 static constexpr bool InBounds(int idx) noexcept {
53 return idx >= 0 && idx < size;
55 static Block::Pos ToCoords(int idx) noexcept {
58 0.5f + ((idx / width) % height),
59 0.5f + (idx / (width * height))
62 static Block::Pos ToCoords(const Pos &pos) noexcept {
63 return Block::Pos(pos) + 0.5f;
65 static Pos ToPos(int idx) noexcept {
68 ((idx / width) % height),
69 (idx / (width * height))
72 glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
74 static bool IsBorder(const Pos &pos) noexcept {
79 pos.y == height - 1 ||
83 static constexpr bool IsBorder(int idx) noexcept {
85 idx < width * height || // low Z plane
86 idx % width == 0 || // low X plane
87 (idx / (width * height)) == depth - 1 || // high Z plane
88 idx % width == width - 1 || // high X plane
89 (idx / width) % height == 0 || // low Y plane
90 (idx / width) % height == height - 1; // high Y plane
93 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
94 bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
95 bool IsSurface(const Pos &pos) const noexcept;
97 void SetNeighbor(Chunk &) noexcept;
98 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
99 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
100 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
101 void ClearNeighbors() noexcept;
102 void Unlink() noexcept;
103 void Relink() noexcept;
105 // check which faces of a block at given index are obstructed (and therefore invisible)
106 Block::FaceSet Obstructed(const Pos &) const noexcept;
108 void Invalidate() noexcept { dirty = true; }
110 void SetBlock(int index, const Block &) noexcept;
111 void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
112 void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
114 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
115 const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
116 const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
118 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
119 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
121 void SetLight(int index, int level) noexcept;
122 void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
123 void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
125 int GetLight(int index) const noexcept;
126 int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
127 int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
129 float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
136 return blank::Intersection(ray, Bounds(), M, &dist);
144 glm::vec3 &normal) const noexcept;
146 void Position(const Pos &pos) noexcept { position = pos; }
147 const Pos &Position() const noexcept { return position; }
148 glm::mat4 Transform(const Pos &offset) const noexcept {
149 return glm::translate((position - offset) * Extent());
152 void CheckUpdate() noexcept;
153 void Draw() noexcept;
156 void Update() noexcept;
159 const BlockTypeRegistry *types;
160 Chunk *neighbor[Block::FACE_COUNT];
161 Block blocks[16 * 16 * 16];
162 unsigned char light[16 * 16 * 16];