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::tvec3<int>;
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 static bool IsBorder(const Pos &pos) noexcept {
81 pos.y == height - 1 ||
85 static constexpr bool IsBorder(int idx) noexcept {
87 idx < width * height || // low Z plane
88 idx % width == 0 || // low X plane
89 (idx / (width * height)) == depth - 1 || // high Z plane
90 idx % width == width - 1 || // high X plane
91 (idx / width) % height == 0 || // low Y plane
92 (idx / width) % height == height - 1; // high Y plane
95 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
96 bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
97 bool IsSurface(const Pos &pos) const noexcept;
99 void SetNeighbor(Chunk &) noexcept;
100 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
101 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
102 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
103 void ClearNeighbors() noexcept;
104 void Unlink() noexcept;
105 void Relink() noexcept;
107 // check which faces of a block at given index are obstructed (and therefore invisible)
108 Block::FaceSet Obstructed(const Pos &) const noexcept;
110 void Invalidate() noexcept { dirty = true; }
112 void SetBlock(int index, const Block &) noexcept;
113 void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
114 void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
116 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
117 const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
118 const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
120 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
121 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
123 void SetLight(int index, int level) noexcept;
124 void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
125 void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
127 int GetLight(int index) const noexcept;
128 int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
129 int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
131 float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
138 return blank::Intersection(ray, Bounds(), M, &dist);
146 glm::vec3 &normal) const noexcept;
150 const glm::mat4 &Mbox,
151 const glm::mat4 &Mchunk,
152 std::vector<WorldCollision> &) const noexcept;
154 void Position(const Pos &pos) noexcept { position = pos; }
155 const Pos &Position() const noexcept { return position; }
156 glm::mat4 Transform(const Pos &offset) const noexcept {
157 return glm::translate((position - offset) * Extent());
160 void CheckUpdate() noexcept;
161 void Draw() noexcept;
164 void Update() noexcept;
167 const BlockTypeRegistry *types;
168 Chunk *neighbor[Block::FACE_COUNT];
170 unsigned char light[size];