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/glm.hpp>
11 #include <glm/gtx/transform.hpp>
19 /// cube of size 16 (256 tiles, 4096 blocks)
23 using Pos = glm::ivec3;
26 explicit Chunk(const BlockTypeRegistry &) noexcept;
28 Chunk(Chunk &&) noexcept;
29 Chunk &operator =(Chunk &&) noexcept;
31 static constexpr int width = 16;
32 static constexpr int height = 16;
33 static constexpr int depth = 16;
34 static Pos Extent() noexcept { return { width, height, depth }; }
35 static constexpr int size = width * height * depth;
37 static AABB Bounds() noexcept { return AABB{ { 0, 0, 0 }, Extent() }; }
39 static constexpr bool InBounds(const Block::Pos &pos) noexcept {
41 pos.x >= 0 && pos.x < width &&
42 pos.y >= 0 && pos.y < height &&
43 pos.z >= 0 && pos.z < depth;
45 static constexpr bool InBounds(const Pos &pos) noexcept {
47 pos.x >= 0 && pos.x < width &&
48 pos.y >= 0 && pos.y < height &&
49 pos.z >= 0 && pos.z < depth;
51 static constexpr int ToIndex(const Pos &pos) noexcept {
52 return pos.x + pos.y * width + pos.z * width * height;
54 static constexpr bool InBounds(int idx) noexcept {
55 return idx >= 0 && idx < size;
57 static Block::Pos ToCoords(int idx) noexcept {
60 0.5f + ((idx / width) % height),
61 0.5f + (idx / (width * height))
64 static Block::Pos ToCoords(const Pos &pos) noexcept {
65 return Block::Pos(pos) + 0.5f;
67 static Pos ToPos(int idx) noexcept {
70 ((idx / width) % height),
71 (idx / (width * height))
74 glm::mat4 ToTransform(const Pos &pos, int idx) const noexcept;
76 Block::Pos ToSceneCoords(const Pos &base, const Block::Pos &pos) const noexcept {
77 return Block::Pos((position - base) * Extent()) + pos;
80 static bool IsBorder(const Pos &pos) noexcept {
85 pos.y == height - 1 ||
89 static constexpr bool IsBorder(int idx) noexcept {
91 idx < width * height || // low Z plane
92 idx % width == 0 || // low X plane
93 (idx / (width * height)) == depth - 1 || // high Z plane
94 idx % width == width - 1 || // high X plane
95 (idx / width) % height == 0 || // low Y plane
96 (idx / width) % height == height - 1; // high Y plane
99 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
100 bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
101 bool IsSurface(const Pos &pos) const noexcept;
103 void SetNeighbor(Chunk &) noexcept;
104 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
105 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
106 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
107 void ClearNeighbors() noexcept;
108 void Unlink() noexcept;
110 // check which faces of a block at given index are obstructed (and therefore invisible)
111 Block::FaceSet Obstructed(const Pos &) const noexcept;
113 void SetBlock(int index, const Block &) noexcept;
114 void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
115 void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
117 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
118 const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
119 const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
121 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
122 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
124 void SetLight(int index, int level) noexcept;
125 void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
126 void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
128 int GetLight(int index) const noexcept;
129 int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
130 int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
132 float GetVertexLight(const Pos &, const BlockModel::Position &, const EntityModel::Normal &) const noexcept;
139 return blank::Intersection(ray, Bounds(), M, &dist);
147 glm::vec3 &normal) const noexcept;
151 const glm::mat4 &Mbox,
152 const glm::mat4 &Mchunk,
153 std::vector<WorldCollision> &) const noexcept;
155 void Position(const Pos &pos) noexcept { position = pos; }
156 const Pos &Position() const noexcept { return position; }
157 glm::mat4 Transform(const Pos &offset) const noexcept {
158 return glm::translate((position - offset) * Extent());
161 void *BlockData() noexcept { return &blocks[0]; }
162 const void *BlockData() const noexcept { return &blocks[0]; }
163 static constexpr std::size_t BlockSize() noexcept { return sizeof(blocks) + sizeof(light); }
165 void Invalidate() noexcept { dirty_model = dirty_save = true; }
166 void InvalidateModel() noexcept { dirty_model = true; }
167 void ClearModel() noexcept { dirty_model = false; }
168 void ClearSave() noexcept { dirty_save = false; }
169 bool ShouldUpdateModel() const noexcept { return dirty_model; }
170 bool ShouldUpdateSave() const noexcept { return dirty_save; }
172 void CheckUpdate() noexcept;
173 void Draw() noexcept;
176 void Update() noexcept;
179 const BlockTypeRegistry *types;
180 Chunk *neighbor[Block::FACE_COUNT];
182 unsigned char light[size];