1 #ifndef BLANK_WORLD_CHUNK_HPP_
2 #define BLANK_WORLD_CHUNK_HPP_
5 #include "BlockTypeRegistry.hpp"
6 #include "../geometry/Location.hpp"
7 #include "../geometry/primitive.hpp"
10 #include <glm/glm.hpp>
11 #include <glm/gtx/transform.hpp>
19 /// cube of size 16 (256 tiles, 4096 blocks)
23 explicit Chunk(const BlockTypeRegistry &) noexcept;
25 Chunk(Chunk &&) noexcept;
26 Chunk &operator =(Chunk &&) noexcept;
28 static constexpr int side = ExactLocation::scale;
29 static constexpr float fside = ExactLocation::fscale;
30 static constexpr int size = side * side * side;
32 static AABB Bounds() noexcept { return AABB{ { 0.0f, 0.0f, 0.0f }, ExactLocation::FExtent() }; }
33 static glm::vec3 Center() noexcept { return glm::vec3(8.0f); }
34 static float Radius() noexcept { return 27.71281292110203669632f; /* 16 * √3 */ }
36 static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept {
38 pos.x >= 0.0f && pos.x < fside &&
39 pos.y >= 0.0f && pos.y < fside &&
40 pos.z >= 0.0f && pos.z < fside;
42 static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept {
44 pos.x >= 0 && pos.x < side &&
45 pos.y >= 0 && pos.y < side &&
46 pos.z >= 0 && pos.z < side;
48 static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept {
49 return pos.x + pos.y * side + pos.z * side * side;
51 static constexpr bool InBounds(int idx) noexcept {
52 return idx >= 0 && idx < size;
54 static ExactLocation::Fine ToCoords(int idx) noexcept {
55 return ExactLocation::Fine(
57 0.5f + ((idx / side) % side),
58 0.5f + (idx / (side * side))
61 static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept {
62 return ExactLocation::Fine(pos) + 0.5f;
64 static RoughLocation::Fine ToPos(int idx) noexcept {
65 return RoughLocation::Fine(
67 ((idx / side) % side),
71 glm::mat4 ToTransform(const RoughLocation::Fine &pos, int idx) const noexcept;
73 ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept {
74 return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos;
77 static bool IsBorder(const RoughLocation::Fine &pos) noexcept {
86 static constexpr bool IsBorder(int idx) noexcept {
88 idx < side * side || // low Z plane
89 idx % side == 0 || // low X plane
90 (idx / (side * side)) == side - 1 || // high Z plane
91 idx % side == side - 1 || // high X plane
92 (idx / side) % side == 0 || // low Y plane
93 (idx / side) % side == side - 1; // high Y plane
96 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
97 bool IsSurface(const ExactLocation::Fine &pos) const noexcept { return IsSurface(RoughLocation::Fine(pos)); }
98 bool IsSurface(const RoughLocation::Fine &pos) const noexcept;
100 void SetNeighbor(Block::Face, Chunk &) noexcept;
101 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
102 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
103 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
104 void Unlink() noexcept;
106 // check which faces of a block at given index are obstructed (and therefore invisible)
107 Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept;
109 void SetBlock(int index, const Block &) noexcept;
110 void SetBlock(const ExactLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
111 void SetBlock(const RoughLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
113 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
114 const Block &BlockAt(const ExactLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
115 const Block &BlockAt(const RoughLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
117 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
118 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
120 void SetLight(int index, int level) noexcept;
121 void SetLight(const ExactLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
122 void SetLight(const RoughLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
124 int GetLight(int index) const noexcept;
125 int GetLight(const ExactLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
126 int GetLight(const RoughLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
128 float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
135 return blank::Intersection(ray, Bounds(), M, &dist);
141 WorldCollision &) noexcept;
145 const glm::mat4 &Mbox,
146 const glm::mat4 &Mchunk,
147 std::vector<WorldCollision> &) noexcept;
149 void Position(const ExactLocation::Coarse &pos) noexcept { position = pos; }
150 const ExactLocation::Coarse &Position() const noexcept { return position; }
151 glm::mat4 Transform(const ExactLocation::Coarse &offset) const noexcept {
152 return glm::translate((position - offset) * ExactLocation::Extent());
155 void *BlockData() noexcept { return &blocks[0]; }
156 const void *BlockData() const noexcept { return &blocks[0]; }
157 static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
159 bool Generated() const noexcept { return generated; }
160 void SetGenerated() noexcept { generated = true; }
161 bool Lighted() const noexcept { return lighted; }
164 void Ref() noexcept { ++ref_count; }
165 void UnRef() noexcept { --ref_count; }
166 bool Referenced() const noexcept { return ref_count > 0; }
168 void Invalidate() noexcept { dirty_mesh = dirty_save = true; }
169 void InvalidateMesh() noexcept { dirty_mesh = true; }
170 void ClearMesh() noexcept { dirty_mesh = false; }
171 void ClearSave() noexcept { dirty_save = false; }
172 bool ShouldUpdateMesh() const noexcept { return dirty_mesh; }
173 bool ShouldUpdateSave() const noexcept { return dirty_save; }
175 void Update(BlockMesh &) noexcept;
178 const BlockTypeRegistry *types;
179 Chunk *neighbor[Block::FACE_COUNT];
182 unsigned char light[size];
186 ExactLocation::Coarse position;