]> git.localhorst.tv Git - blank.git/blob - src/chunk.hpp
use light levels for shading of blocks
[blank.git] / src / chunk.hpp
1 #ifndef BLANK_CHUNK_HPP_
2 #define BLANK_CHUNK_HPP_
3
4 #include "block.hpp"
5 #include "geometry.hpp"
6 #include "model.hpp"
7
8 #include <list>
9 #include <vector>
10 #include <glm/glm.hpp>
11
12
13 namespace blank {
14
15 /// cube of size 16 (256 tiles, 4096 blocks)
16 class Chunk {
17
18 public:
19         using Pos = glm::tvec3<int>;
20
21 public:
22         explicit Chunk(const BlockTypeRegistry &);
23
24         Chunk(Chunk &&);
25         Chunk &operator =(Chunk &&);
26
27         static constexpr int Width() { return 16; }
28         static constexpr int Height() { return 16; }
29         static constexpr int Depth() { return 16; }
30         static Pos Extent() { return { Width(), Height(), Depth() }; }
31         static constexpr int Size() { return Width() * Height() * Depth(); }
32
33         static AABB Bounds() { return AABB{ { 0, 0, 0 }, Extent() }; }
34
35         static constexpr bool InBounds(const Block::Pos &pos) {
36                 return
37                         pos.x >= 0 && pos.x < Width() &&
38                         pos.y >= 0 && pos.y < Height() &&
39                         pos.z >= 0 && pos.z < Depth();
40         }
41         static constexpr bool InBounds(const Pos &pos) {
42                 return
43                         pos.x >= 0 && pos.x < Width() &&
44                         pos.y >= 0 && pos.y < Height() &&
45                         pos.z >= 0 && pos.z < Depth();
46         }
47         static constexpr int ToIndex(const Pos &pos) {
48                 return pos.x + pos.y * Width() + pos.z * Width() * Height();
49         }
50         static constexpr bool InBounds(int idx) {
51                 return idx >= 0 && idx < Size();
52         }
53         static Block::Pos ToCoords(int idx) {
54                 return Block::Pos(
55                         0.5f + (idx % Width()),
56                         0.5f + ((idx / Width()) % Height()),
57                         0.5f + (idx / (Width() * Height()))
58                 );
59         }
60         static Pos ToPos(int idx) {
61                 return Pos(
62                         (idx % Width()),
63                         ((idx / Width()) % Height()),
64                         (idx / (Width() * Height()))
65                 );
66         }
67         glm::mat4 ToTransform(int idx) const;
68
69         static constexpr bool IsBorder(int idx) {
70                 return
71                         idx < Width() * Height() ||                    // low Z plane
72                         idx % Width() == 0 ||                          // low X plane
73                         (idx / (Width() * Height())) == Depth() - 1 || // high Z plane
74                         idx % Width() == Width() - 1 ||                // high X plane
75                         (idx / Width()) % Height() == 0 ||             // low Y plane
76                         (idx / Width()) % Height() == Height() - 1;    // high Y plane
77         }
78
79         bool IsSurface(int index) const { return IsSurface(ToPos(index)); }
80         bool IsSurface(const Block::Pos &pos) const { return IsSurface(Pos(pos)); }
81         bool IsSurface(const Pos &pos) const;
82
83         void SetNeighbor(Chunk &);
84         bool HasNeighbor(Block::Face f) const { return neighbor[f]; }
85         Chunk &GetNeighbor(Block::Face f) { return *neighbor[f]; }
86         const Chunk &GetNeighbor(Block::Face f) const { return *neighbor[f]; }
87         void ClearNeighbors();
88         void Unlink();
89         void Relink();
90
91         // check if block at given index is completely enclosed (and therefore invisible)
92         bool Obstructed(int idx) const;
93
94         void Allocate();
95         void Invalidate() { dirty = true; }
96
97         void SetBlock(int index, const Block &);
98         void SetBlock(const Block::Pos &pos, const Block &block) { SetBlock(ToIndex(pos), block); }
99         void SetBlock(const Pos &pos, const Block &block) { SetBlock(ToIndex(pos), block); }
100
101         const Block &BlockAt(int index) const { return blocks[index]; }
102         const Block &BlockAt(const Block::Pos &pos) const { return BlockAt(ToIndex(pos)); }
103         const Block &BlockAt(const Pos &pos) const { return BlockAt(ToIndex(pos)); }
104
105         const Block *FindNext(const Pos &pos, Block::Face face) const;
106         const Block *FindNext(const Block::Pos &pos, Block::Face face) const { return FindNext(Pos(pos), face); }
107         const Block *FindNext(int index, Block::Face face) const { return FindNext(ToPos(index), face); }
108
109         const BlockType &Type(const Block &b) const { return *types->Get(b.type); }
110
111         void SetLight(int index, int level);
112         void SetLight(const Pos &pos, int level) { SetLight(ToIndex(pos), level); }
113         void SetLight(const Block::Pos &pos, int level) { SetLight(ToIndex(pos), level); }
114
115         int GetLight(int index) const;
116         int GetLight(const Pos &pos) const { return GetLight(ToIndex(pos)); }
117         int GetLight(const Block::Pos &pos) const { return GetLight(ToIndex(pos)); }
118
119         float GetVertexLight(int index, const BlockModel::Position &, const BlockModel::Normal &) const;
120
121         bool Intersection(
122                 const Ray &ray,
123                 const glm::mat4 &M,
124                 float &dist
125         ) const {
126                 return blank::Intersection(ray, Bounds(), M, &dist);
127         }
128
129         bool Intersection(
130                 const Ray &,
131                 const glm::mat4 &M,
132                 int &blkid,
133                 float &dist,
134                 glm::vec3 &normal) const;
135
136         void Position(const Pos &);
137         const Pos &Position() const { return position; }
138         glm::mat4 Transform(const Pos &offset) const;
139
140         void CheckUpdate();
141         void Draw();
142
143 private:
144         void Update();
145
146 private:
147         const BlockTypeRegistry *types;
148         Chunk *neighbor[Block::FACE_COUNT];
149         std::vector<Block> blocks;
150         std::vector<unsigned char> light;
151         BlockModel model;
152         Pos position;
153         bool dirty;
154
155 };
156
157
158 struct BlockLookup {
159
160         Chunk *chunk;
161         Chunk::Pos pos;
162         const Block *result;
163
164         // resolve chunk/position/block from oob coordinates
165         // result will be nullptr if unsuccessful
166         BlockLookup(Chunk *c, const Chunk::Pos &p);
167
168         // resolve chunk/position/block from ib coordinates and direction
169         // result will be nullptr if unsuccessful
170         BlockLookup(Chunk *c, const Chunk::Pos &p, Block::Face dir);
171
172 };
173
174
175 class Generator;
176
177 class ChunkLoader {
178
179 public:
180         struct Config {
181                 int load_dist = 6;
182                 int unload_dist = 8;
183         };
184
185         ChunkLoader(const Config &, const BlockTypeRegistry &, const Generator &);
186
187         void Generate(const Chunk::Pos &from, const Chunk::Pos &to);
188         void GenerateSurrounding(const Chunk::Pos &);
189
190         std::list<Chunk> &Loaded() { return loaded; }
191
192         Chunk *Loaded(const Chunk::Pos &);
193         bool Queued(const Chunk::Pos &);
194         bool Known(const Chunk::Pos &);
195         Chunk &ForceLoad(const Chunk::Pos &);
196
197         void Rebase(const Chunk::Pos &);
198         void Update();
199
200 private:
201         Chunk &Generate(const Chunk::Pos &pos);
202         void Insert(Chunk &);
203         void Remove(Chunk &);
204
205 private:
206         Chunk::Pos base;
207
208         const BlockTypeRegistry &reg;
209         const Generator &gen;
210
211         std::list<Chunk> loaded;
212         std::list<Chunk::Pos> to_generate;
213         std::list<Chunk> to_free;
214
215         int load_dist;
216         int unload_dist;
217
218 };
219
220 }
221
222 #endif