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 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 Invalidate() noexcept { dirty = true; }
115 void SetBlock(int index, const Block &) noexcept;
116 void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
117 void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
119 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
120 const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
121 const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
123 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
124 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
126 void SetLight(int index, int level) noexcept;
127 void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
128 void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
130 int GetLight(int index) const noexcept;
131 int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
132 int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
134 float GetVertexLight(const Pos &, const BlockModel::Position &, const EntityModel::Normal &) const noexcept;
141 return blank::Intersection(ray, Bounds(), M, &dist);
149 glm::vec3 &normal) const noexcept;
153 const glm::mat4 &Mbox,
154 const glm::mat4 &Mchunk,
155 std::vector<WorldCollision> &) const noexcept;
157 void Position(const Pos &pos) noexcept { position = pos; }
158 const Pos &Position() const noexcept { return position; }
159 glm::mat4 Transform(const Pos &offset) const noexcept {
160 return glm::translate((position - offset) * Extent());
163 void CheckUpdate() noexcept;
164 void Draw() noexcept;
167 void Update() noexcept;
170 const BlockTypeRegistry *types;
171 Chunk *neighbor[Block::FACE_COUNT];
173 unsigned char light[size];