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>
20 /// cube of size 16 (256 tiles, 4096 blocks)
24 explicit Chunk(const BlockTypeRegistry &) noexcept;
26 Chunk(Chunk &&) noexcept;
27 Chunk &operator =(Chunk &&) noexcept;
29 static constexpr int side = ExactLocation::scale;
30 static constexpr float fside = ExactLocation::fscale;
31 static constexpr int size = side * side * side;
33 static AABB Bounds() noexcept { return AABB{ { 0.0f, 0.0f, 0.0f }, ExactLocation::FExtent() }; }
34 static glm::vec3 Center() noexcept { return glm::vec3(8.0f); }
35 static float Radius() noexcept { return 27.71281292110203669632f; /* 16 * √3 */ }
37 static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept {
39 pos.x >= 0.0f && pos.x < fside &&
40 pos.y >= 0.0f && pos.y < fside &&
41 pos.z >= 0.0f && pos.z < fside;
43 static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept {
45 pos.x >= 0 && pos.x < side &&
46 pos.y >= 0 && pos.y < side &&
47 pos.z >= 0 && pos.z < side;
49 static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept {
50 return pos.x + pos.y * side + pos.z * side * side;
52 static constexpr bool InBounds(int idx) noexcept {
53 return idx >= 0 && idx < size;
55 static ExactLocation::Fine ToCoords(int idx) noexcept {
56 return ExactLocation::Fine(
58 0.5f + ((idx / side) % side),
59 0.5f + (idx / (side * side))
62 static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept {
63 return ExactLocation::Fine(pos) + 0.5f;
65 static RoughLocation::Fine ToPos(int idx) noexcept {
66 return RoughLocation::Fine(
68 ((idx / side) % side),
72 glm::mat4 ToTransform(const RoughLocation::Fine &pos, int idx) const noexcept;
74 ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept {
75 return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos;
78 static bool IsBorder(const RoughLocation::Fine &pos) noexcept {
87 static constexpr bool IsBorder(int idx) noexcept {
89 idx < side * side || // low Z plane
90 idx % side == 0 || // low X plane
91 (idx / (side * side)) == side - 1 || // high Z plane
92 idx % side == side - 1 || // high X plane
93 (idx / side) % side == 0 || // low Y plane
94 (idx / side) % side == side - 1; // high Y plane
97 bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
98 bool IsSurface(const ExactLocation::Fine &pos) const noexcept { return IsSurface(RoughLocation::Fine(pos)); }
99 bool IsSurface(const RoughLocation::Fine &pos) const noexcept;
101 void SetNeighbor(Block::Face, Chunk &) noexcept;
102 bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
103 Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
104 const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
105 void Unlink() noexcept;
107 // check which faces of a block at given index are obstructed (and therefore invisible)
108 Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept;
110 void SetBlock(int index, const Block &) noexcept;
111 void SetBlock(const ExactLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
112 void SetBlock(const RoughLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
114 const Block &BlockAt(int index) const noexcept { return blocks[index]; }
115 const Block &BlockAt(const ExactLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
116 const Block &BlockAt(const RoughLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
118 const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
119 const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
121 void SetLight(int index, int level) noexcept;
122 void SetLight(const ExactLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
123 void SetLight(const RoughLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
125 int GetLight(int index) const noexcept;
126 int GetLight(const ExactLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
127 int GetLight(const RoughLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
129 float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
136 return blank::Intersection(ray, Bounds(), M, &dist);
142 WorldCollision &) noexcept;
146 const glm::mat4 &Mbox,
147 const glm::mat4 &Mchunk,
148 std::vector<WorldCollision> &) noexcept;
151 const Entity &entity,
152 const glm::mat4 &Mentity,
153 const glm::mat4 &Mchunk,
154 std::vector<WorldCollision> &) noexcept;
156 void Position(const ExactLocation::Coarse &pos) noexcept { position = pos; }
157 const ExactLocation::Coarse &Position() const noexcept { return position; }
158 glm::mat4 Transform(const ExactLocation::Coarse &offset) const noexcept {
159 return glm::translate((position - offset) * ExactLocation::Extent());
162 void *BlockData() noexcept { return &blocks[0]; }
163 const void *BlockData() const noexcept { return &blocks[0]; }
164 static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
166 bool Generated() const noexcept { return generated; }
167 void SetGenerated() noexcept { generated = true; }
168 bool Lighted() const noexcept { return lighted; }
171 void Ref() noexcept { ++ref_count; }
172 void UnRef() noexcept { --ref_count; }
173 bool Referenced() const noexcept { return ref_count > 0; }
175 void Invalidate() noexcept { dirty_mesh = dirty_save = true; }
176 void InvalidateMesh() noexcept { dirty_mesh = true; }
177 void ClearMesh() noexcept { dirty_mesh = false; }
178 void ClearSave() noexcept { dirty_save = false; }
179 bool ShouldUpdateMesh() const noexcept { return dirty_mesh; }
180 bool ShouldUpdateSave() const noexcept { return dirty_save; }
182 void Update(BlockMesh &) noexcept;
185 const BlockTypeRegistry *types;
186 Chunk *neighbor[Block::FACE_COUNT];
189 unsigned char light[size];
193 ExactLocation::Coarse position;