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"
11 #include <glm/glm.hpp>
12 #include <glm/gtx/transform.hpp>
21 /// cube of size 16 (256 tiles, 4096 blocks)
25 explicit Chunk(const BlockTypeRegistry &) noexcept;
27 Chunk(Chunk &&) noexcept;
28 Chunk &operator =(Chunk &&) noexcept;
30 static constexpr int side = ExactLocation::scale;
31 static constexpr float fside = ExactLocation::fscale;
32 static constexpr int size = side * side * side;
34 static AABB Bounds() noexcept { return AABB{ { 0.0f, 0.0f, 0.0f }, ExactLocation::FExtent() }; }
35 static glm::vec3 Center() noexcept { return glm::vec3(8.0f); }
36 static float Radius() noexcept { return 27.71281292110203669632f; /* 16 * √3 */ }
38 /// get bounding box relative to given reference chunk
39 AABB RelativeBounds(const ExactLocation::Coarse &ref) const noexcept {
41 bounds.min = (position - ref) * ExactLocation::Extent();
42 bounds.max = bounds.min + ExactLocation::FExtent();
46 static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept {
48 pos.x >= 0.0f && pos.x < fside &&
49 pos.y >= 0.0f && pos.y < fside &&
50 pos.z >= 0.0f && pos.z < fside;
52 static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept {
54 pos.x >= 0 && pos.x < side &&
55 pos.y >= 0 && pos.y < side &&
56 pos.z >= 0 && pos.z < side;
58 static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept {
59 return pos.x + pos.y * side + pos.z * side * side;
61 static constexpr bool InBounds(int idx) noexcept {
62 return idx >= 0 && idx < size;
64 static ExactLocation::Fine ToCoords(int idx) noexcept {
65 return ExactLocation::Fine(
67 0.5f + ((idx / side) % side),
68 0.5f + (idx / (side * side))
71 static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept {
72 return ExactLocation::Fine(pos) + 0.5f;
74 static RoughLocation::Fine ToPos(int idx) noexcept {
75 return RoughLocation::Fine(
77 ((idx / side) % side),
81 /// get a chunk-local transform for block at given position and index
82 /// (position and index are redundant)
83 glm::mat4 ToTransform(const RoughLocation::Fine &position, int index) const noexcept;
84 /// same as above, but also apply offset to given reference
85 glm::mat4 ToTransform(const ExactLocation::Coarse &ref, const RoughLocation::Fine &pos, int idx) const noexcept;
87 ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept {
88 return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos;
91 static bool IsBorder(const RoughLocation::Fine &pos) noexcept {
100 static constexpr bool IsBorder(int idx) noexcept {
102 idx < side * side || // low Z plane
103 idx % side == 0 || // low X plane
104 (idx / (side * side)) == side - 1 || // high Z plane
105 idx % side == side - 1 || // high X plane
106 (idx / side) % side == 0 || // low Y plane
107 (idx / side) % side == side - 1; // high Y plane
110 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
111 bool IsSurface(const ExactLocation::Fine &pos) const noexcept { return IsSurface(RoughLocation::Fine(pos)); }
112 bool IsSurface(const RoughLocation::Fine &pos) const noexcept;
114 void SetNeighbor(Block::Face, Chunk &) noexcept;
115 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
116 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
117 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
118 void Unlink() noexcept;
120 // check which faces of a block at given index are obstructed (and therefore invisible)
121 Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept;
123 void SetBlock(int index, const Block &) noexcept;
124 void SetBlock(const ExactLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
125 void SetBlock(const RoughLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
127 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
128 const Block &BlockAt(const ExactLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
129 const Block &BlockAt(const RoughLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
131 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
132 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
134 void SetLight(int index, int level) noexcept;
135 void SetLight(const ExactLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
136 void SetLight(const RoughLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
138 int GetLight(int index) const noexcept;
139 int GetLight(const ExactLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
140 int GetLight(const RoughLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
142 float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
144 /// get gravity for one unit mass at given point
145 glm::vec3 GravityAt(const ExactLocation &) const noexcept;
149 const ExactLocation::Coarse &reference,
152 return blank::Intersection(ray, RelativeBounds(reference), dist);
155 /// check if given ray intersects any block of this chunk
156 /// given reference indicated the chunk offset of the ray in world space
159 const ExactLocation::Coarse &reference,
160 WorldCollision &) noexcept;
164 const glm::mat4 &Mbox,
165 const glm::mat4 &Mchunk,
166 std::vector<WorldCollision> &) noexcept;
169 const Entity &entity,
170 const glm::mat4 &Mentity,
171 const glm::mat4 &Mchunk,
172 std::vector<WorldCollision> &) noexcept;
174 void Position(const ExactLocation::Coarse &pos) noexcept { position = pos; }
175 const ExactLocation::Coarse &Position() const noexcept { return position; }
177 glm::mat4 Transform(const ExactLocation::Coarse &offset) const noexcept {
178 return glm::translate((position - offset) * ExactLocation::Extent());
181 void *BlockData() noexcept { return &blocks[0]; }
182 const void *BlockData() const noexcept { return &blocks[0]; }
183 static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
185 bool Generated() const noexcept { return generated; }
186 void SetGenerated() noexcept { generated = true; }
187 bool Lighted() const noexcept { return lighted; }
190 /// check for active blocks, should be called after
191 /// block data was modified by means other than SetBlock()
194 void Ref() noexcept { ++ref_count; }
195 void UnRef() noexcept { --ref_count; }
196 bool Referenced() const noexcept { return ref_count > 0; }
198 void Invalidate() noexcept { dirty_mesh = dirty_save = true; }
199 void InvalidateMesh() noexcept { dirty_mesh = true; }
200 void ClearMesh() noexcept { dirty_mesh = false; }
201 void ClearSave() noexcept { dirty_save = false; }
202 bool ShouldUpdateMesh() const noexcept { return dirty_mesh; }
203 bool ShouldUpdateSave() const noexcept { return dirty_save; }
205 void Update(BlockMesh &) noexcept;
208 const BlockTypeRegistry *types;
209 Chunk *neighbor[Block::FACE_COUNT];
211 std::set<int> gravity;
214 unsigned char light[size];
218 ExactLocation::Coarse position;