]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
simplify ray/chunk intersection test
[blank.git] / src / world / Chunk.hpp
1 #ifndef BLANK_WORLD_CHUNK_HPP_
2 #define BLANK_WORLD_CHUNK_HPP_
3
4 #include "Block.hpp"
5 #include "BlockTypeRegistry.hpp"
6 #include "../geometry/Location.hpp"
7 #include "../geometry/primitive.hpp"
8
9 #include <vector>
10 #include <glm/glm.hpp>
11 #include <glm/gtx/transform.hpp>
12
13
14 namespace blank {
15
16 class BlockType;
17 class Entity;
18 class WorldCollision;
19
20 /// cube of size 16 (256 tiles, 4096 blocks)
21 class Chunk {
22
23 public:
24         explicit Chunk(const BlockTypeRegistry &) noexcept;
25
26         Chunk(Chunk &&) noexcept;
27         Chunk &operator =(Chunk &&) noexcept;
28
29         static constexpr int side = ExactLocation::scale;
30         static constexpr float fside = ExactLocation::fscale;
31         static constexpr int size = side * side * side;
32
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 */ }
36
37         static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept {
38                 return
39                         pos.x >= 0.0f && pos.x < fside &&
40                         pos.y >= 0.0f && pos.y < fside &&
41                         pos.z >= 0.0f && pos.z < fside;
42         }
43         static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept {
44                 return
45                         pos.x >= 0 && pos.x < side &&
46                         pos.y >= 0 && pos.y < side &&
47                         pos.z >= 0 && pos.z < side;
48         }
49         static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept {
50                 return pos.x + pos.y * side + pos.z * side * side;
51         }
52         static constexpr bool InBounds(int idx) noexcept {
53                 return idx >= 0 && idx < size;
54         }
55         static ExactLocation::Fine ToCoords(int idx) noexcept {
56                 return ExactLocation::Fine(
57                         0.5f + (idx % side),
58                         0.5f + ((idx / side) % side),
59                         0.5f + (idx / (side * side))
60                 );
61         }
62         static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept {
63                 return ExactLocation::Fine(pos) + 0.5f;
64         }
65         static RoughLocation::Fine ToPos(int idx) noexcept {
66                 return RoughLocation::Fine(
67                         (idx % side),
68                         ((idx / side) % side),
69                         (idx / (side * side))
70                 );
71         }
72         /// get a chunk-local transform for block at given position and index
73         /// (position and index are redundant)
74         glm::mat4 ToTransform(const RoughLocation::Fine &position, int index) const noexcept;
75         /// same as above, but also apply offset to given reference
76         glm::mat4 ToTransform(const ExactLocation::Coarse &ref, const RoughLocation::Fine &pos, int idx) const noexcept;
77
78         ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept {
79                 return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos;
80         }
81
82         static bool IsBorder(const RoughLocation::Fine &pos) noexcept {
83                 return
84                         pos.x == 0 ||
85                         pos.x == side - 1 ||
86                         pos.y == 0 ||
87                         pos.y == side - 1 ||
88                         pos.z == 0 ||
89                         pos.z == side - 1;
90         }
91         static constexpr bool IsBorder(int idx) noexcept {
92                 return
93                         idx < side * side ||                 // low Z plane
94                         idx % side == 0 ||                   // low X plane
95                         (idx / (side * side)) == side - 1 || // high Z plane
96                         idx % side == side - 1 ||            // high X plane
97                         (idx / side) % side == 0 ||          // low Y plane
98                         (idx / side) % side == side - 1;     // high Y plane
99         }
100
101         bool IsSurface(int index) const noexcept { return IsSurface(ToPos(index)); }
102         bool IsSurface(const ExactLocation::Fine &pos) const noexcept { return IsSurface(RoughLocation::Fine(pos)); }
103         bool IsSurface(const RoughLocation::Fine &pos) const noexcept;
104
105         void SetNeighbor(Block::Face, Chunk &) noexcept;
106         bool HasNeighbor(Block::Face f) const noexcept { return neighbor[f]; }
107         Chunk &GetNeighbor(Block::Face f) noexcept { return *neighbor[f]; }
108         const Chunk &GetNeighbor(Block::Face f) const noexcept { return *neighbor[f]; }
109         void Unlink() noexcept;
110
111         // check which faces of a block at given index are obstructed (and therefore invisible)
112         Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept;
113
114         void SetBlock(int index, const Block &) noexcept;
115         void SetBlock(const ExactLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
116         void SetBlock(const RoughLocation::Fine &pos, const Block &block) noexcept { SetBlock(ToIndex(pos), block); }
117
118         const Block &BlockAt(int index) const noexcept { return blocks[index]; }
119         const Block &BlockAt(const ExactLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
120         const Block &BlockAt(const RoughLocation::Fine &pos) const noexcept { return BlockAt(ToIndex(pos)); }
121
122         const BlockType &Type(const Block &b) const noexcept { return types->Get(b.type); }
123         const BlockType &Type(int index) const noexcept { return Type(BlockAt(index)); }
124
125         void SetLight(int index, int level) noexcept;
126         void SetLight(const ExactLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
127         void SetLight(const RoughLocation::Fine &pos, int level) noexcept { SetLight(ToIndex(pos), level); }
128
129         int GetLight(int index) const noexcept;
130         int GetLight(const ExactLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
131         int GetLight(const RoughLocation::Fine &pos) const noexcept { return GetLight(ToIndex(pos)); }
132
133         float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
134
135         bool Intersection(
136                 const Ray &ray,
137                 const ExactLocation::Coarse &reference,
138                 float &dist
139         ) const noexcept {
140                 return blank::Intersection(ray, Bounds(), Transform(reference), &dist);
141         }
142
143         /// check if given ray intersects any block of this chunk
144         /// given reference indicated the chunk offset of the ray in world space
145         bool Intersection(
146                 const Ray &,
147                 const ExactLocation::Coarse &reference,
148                 WorldCollision &) noexcept;
149
150         bool Intersection(
151                 const AABB &box,
152                 const glm::mat4 &Mbox,
153                 const glm::mat4 &Mchunk,
154                 std::vector<WorldCollision> &) noexcept;
155
156         bool Intersection(
157                 const Entity &entity,
158                 const glm::mat4 &Mentity,
159                 const glm::mat4 &Mchunk,
160                 std::vector<WorldCollision> &) noexcept;
161
162         void Position(const ExactLocation::Coarse &pos) noexcept { position = pos; }
163         const ExactLocation::Coarse &Position() const noexcept { return position; }
164
165         glm::mat4 Transform(const ExactLocation::Coarse &offset) const noexcept {
166                 return glm::translate((position - offset) * ExactLocation::Extent());
167         }
168
169         void *BlockData() noexcept { return &blocks[0]; }
170         const void *BlockData() const noexcept { return &blocks[0]; }
171         static constexpr std::size_t BlockSize() noexcept { return offsetof(Chunk, position) - offsetof(Chunk, blocks); }
172
173         bool Generated() const noexcept { return generated; }
174         void SetGenerated() noexcept { generated = true; }
175         bool Lighted() const noexcept { return lighted; }
176         void ScanLights();
177
178         void Ref() noexcept { ++ref_count; }
179         void UnRef() noexcept { --ref_count; }
180         bool Referenced() const noexcept { return ref_count > 0; }
181
182         void Invalidate() noexcept { dirty_mesh = dirty_save = true; }
183         void InvalidateMesh() noexcept { dirty_mesh = true; }
184         void ClearMesh() noexcept { dirty_mesh = false; }
185         void ClearSave() noexcept { dirty_save = false; }
186         bool ShouldUpdateMesh() const noexcept { return dirty_mesh; }
187         bool ShouldUpdateSave() const noexcept { return dirty_save; }
188
189         void Update(BlockMesh &) noexcept;
190
191 private:
192         const BlockTypeRegistry *types;
193         Chunk *neighbor[Block::FACE_COUNT];
194
195         Block blocks[size];
196         unsigned char light[size];
197         bool generated;
198         bool lighted;
199
200         ExactLocation::Coarse position;
201         int ref_count;
202         bool dirty_mesh;
203         bool dirty_save;
204
205 };
206
207 }
208
209 #endif