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 constexpr bool IsBorder(int idx) noexcept {
76 idx < width * height || // low Z plane
77 idx % width == 0 || // low X plane
78 (idx / (width * height)) == depth - 1 || // high Z plane
79 idx % width == width - 1 || // high X plane
80 (idx / width) % height == 0 || // low Y plane
81 (idx / width) % height == height - 1; // high Y plane
84 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
85 bool IsSurface(const Block::Pos &pos) const noexcept { return IsSurface(Pos(pos)); }
86 bool IsSurface(const Pos &pos) const noexcept;
88 void SetNeighbor(Chunk &) noexcept;
89 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
90 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
91 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
92 void ClearNeighbors() noexcept;
93 void Unlink() noexcept;
94 void Relink() noexcept;
96 // check which faces of a block at given index are obstructed (and therefore invisible)
97 Block::FaceSet Obstructed(const Pos &) const noexcept;
99 void Invalidate() noexcept { dirty = true; }
101 void SetBlock(int index, const Block &) noexcept;
102 void SetBlock(const Block::Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
103 void SetBlock(const Pos &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
105 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
106 const Block &BlockAt(const Block::Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
107 const Block &BlockAt(const Pos &pos) const noexcept { return BlockAt(ToIndex(pos)); }
109 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
110 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
112 void SetLight(int index, int level) noexcept;
113 void SetLight(const Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
114 void SetLight(const Block::Pos &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
116 int GetLight(int index) const noexcept;
117 int GetLight(const Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
118 int GetLight(const Block::Pos &pos) const noexcept { return GetLight(ToIndex(pos)); }
120 float GetVertexLight(const Pos &, const BlockModel::Position &, const Model::Normal &) const noexcept;
127 return blank::Intersection(ray, Bounds(), M, &dist);
135 glm::vec3 &normal) const noexcept;
137 void Position(const Pos &pos) noexcept { position = pos; }
138 const Pos &Position() const noexcept { return position; }
139 glm::mat4 Transform(const Pos &offset) const noexcept {
140 return glm::translate((position - offset) * Extent());
143 void CheckUpdate() noexcept;
144 void Draw() noexcept;
147 void Update() noexcept;
150 const BlockTypeRegistry *types;
151 Chunk *neighbor[Block::FACE_COUNT];
152 Block blocks[16 * 16 * 16];
153 unsigned char light[16 * 16 * 16];