]> git.localhorst.tv Git - blank.git/blob - src/world/Chunk.hpp
tentative optimization of 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 WorldCollision;
18
19 /// cube of size 16 (256 tiles, 4096 blocks)
20 class Chunk {
21
22 public:
23         explicit Chunk(const BlockTypeRegistry &) noexcept;
24
25         Chunk(Chunk &&) noexcept;
26         Chunk &operator =(Chunk &&) noexcept;
27
28         static constexpr int side = ExactLocation::scale;
29         static constexpr float fside = ExactLocation::fscale;
30         static constexpr int size = side * side * side;
31
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 */ }
35
36         static constexpr bool InBounds(const ExactLocation::Fine &pos) noexcept {
37                 return
38                         pos.x >= 0.0f && pos.x < fside &&
39                         pos.y >= 0.0f && pos.y < fside &&
40                         pos.z >= 0.0f && pos.z < fside;
41         }
42         static constexpr bool InBounds(const RoughLocation::Fine &pos) noexcept {
43                 return
44                         pos.x >= 0 && pos.x < side &&
45                         pos.y >= 0 && pos.y < side &&
46                         pos.z >= 0 && pos.z < side;
47         }
48         static constexpr int ToIndex(const RoughLocation::Fine &pos) noexcept {
49                 return pos.x + pos.y * side + pos.z * side * side;
50         }
51         static constexpr bool InBounds(int idx) noexcept {
52                 return idx >= 0 && idx < size;
53         }
54         static ExactLocation::Fine ToCoords(int idx) noexcept {
55                 return ExactLocation::Fine(
56                         0.5f + (idx % side),
57                         0.5f + ((idx / side) % side),
58                         0.5f + (idx / (side * side))
59                 );
60         }
61         static ExactLocation::Fine ToCoords(const RoughLocation::Fine &pos) noexcept {
62                 return ExactLocation::Fine(pos) + 0.5f;
63         }
64         static RoughLocation::Fine ToPos(int idx) noexcept {
65                 return RoughLocation::Fine(
66                         (idx % side),
67                         ((idx / side) % side),
68                         (idx / (side * side))
69                 );
70         }
71         glm::mat4 ToTransform(const RoughLocation::Fine &pos, int idx) const noexcept;
72
73         ExactLocation::Fine ToSceneCoords(const ExactLocation::Coarse &base, const ExactLocation::Fine &pos) const noexcept {
74                 return ExactLocation::Fine((position - base) * ExactLocation::Extent()) + pos;
75         }
76
77         static bool IsBorder(const RoughLocation::Fine &pos) noexcept {
78                 return
79                         pos.x == 0 ||
80                         pos.x == side - 1 ||
81                         pos.y == 0 ||
82                         pos.y == side - 1 ||
83                         pos.z == 0 ||
84                         pos.z == side - 1;
85         }
86         static constexpr bool IsBorder(int idx) noexcept {
87                 return
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
94         }
95
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;
99
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;
105
106         // check which faces of a block at given index are obstructed (and therefore invisible)
107         Block::FaceSet Obstructed(const RoughLocation::Fine &) const noexcept;
108
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); }
112
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)); }
116
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)); }
119
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); }
123
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)); }
127
128         float GetVertexLight(const RoughLocation::Fine &, const BlockMesh::Position &, const EntityMesh::Normal &) const noexcept;
129
130         bool Intersection(
131                 const Ray &ray,
132                 const glm::mat4 &M,
133                 float &dist
134         ) const noexcept {
135                 return blank::Intersection(ray, Bounds(), M, &dist);
136         }
137
138         bool Intersection(
139                 const Ray &,
140                 const glm::mat4 &M,
141                 WorldCollision &) noexcept;
142
143         bool Intersection(
144                 const AABB &box,
145                 const glm::mat4 &Mbox,
146                 const glm::mat4 &Mchunk,
147                 std::vector<WorldCollision> &) noexcept;
148
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());
153         }
154
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); }
158
159         bool Generated() const noexcept { return generated; }
160         void SetGenerated() noexcept { generated = true; }
161         bool Lighted() const noexcept { return lighted; }
162         void ScanLights();
163
164         void Ref() noexcept { ++ref_count; }
165         void UnRef() noexcept { --ref_count; }
166         bool Referenced() const noexcept { return ref_count > 0; }
167
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; }
174
175         void Update(BlockMesh &) noexcept;
176
177 private:
178         const BlockTypeRegistry *types;
179         Chunk *neighbor[Block::FACE_COUNT];
180
181         Block blocks[size];
182         unsigned char light[size];
183         bool generated;
184         bool lighted;
185
186         ExactLocation::Coarse position;
187         int ref_count;
188         bool dirty_mesh;
189         bool dirty_save;
190
191 };
192
193 }
194
195 #endif