]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
made chunks aware of their neighbors
[blank.git] / src / chunk.cpp
1 #include "chunk.hpp"
2
3 #include "generator.hpp"
4
5 #include <limits>
6 #include <glm/gtx/transform.hpp>
7
8
9 namespace {
10
11 blank::Model::Buffer buf;
12
13 }
14
15 namespace blank {
16
17 Chunk::Chunk(const BlockTypeRegistry &types)
18 : types(&types)
19 , neighbor{ 0, 0, 0, 0, 0, 0 }
20 , blocks()
21 , model()
22 , position(0, 0, 0)
23 , dirty(false) {
24
25 }
26
27 Chunk::Chunk(Chunk &&other)
28 : types(other.types)
29 , blocks(std::move(other.blocks))
30 , model(std::move(other.model))
31 , dirty(other.dirty) {
32         for (size_t i = 0; i < Block::FACE_COUNT; ++i) {
33                 neighbor[i] = other.neighbor[i];
34         }
35 }
36
37 Chunk &Chunk::operator =(Chunk &&other) {
38         types = other.types;
39         for (size_t i = 0; i < Block::FACE_COUNT; ++i) {
40                 neighbor[i] = other.neighbor[i];
41         }
42         blocks = std::move(other.blocks);
43         model = std::move(other.model);
44         dirty = other.dirty;
45         return *this;
46 }
47
48
49 void Chunk::SetNeighbor(Chunk &other) {
50         if (other.position == position - Pos(-1, 0, 0)) {
51                 neighbor[Block::FACE_LEFT] = &other;
52                 other.neighbor[Block::FACE_RIGHT] = this;
53         } else if (other.position == position - Pos(1, 0, 0)) {
54                 neighbor[Block::FACE_RIGHT] = &other;
55                 other.neighbor[Block::FACE_LEFT] = this;
56         } else if (other.position == position - Pos(0, -1, 0)) {
57                 neighbor[Block::FACE_DOWN] = &other;
58                 other.neighbor[Block::FACE_UP] = this;
59         } else if (other.position == position - Pos(0, 1, 0)) {
60                 neighbor[Block::FACE_UP] = &other;
61                 other.neighbor[Block::FACE_DOWN] = this;
62         } else if (other.position == position - Pos(0, 0, -1)) {
63                 neighbor[Block::FACE_BACK] = &other;
64                 other.neighbor[Block::FACE_FRONT] = this;
65         } else if (other.position == position - Pos(0, 0, 1)) {
66                 neighbor[Block::FACE_FRONT] = &other;
67                 other.neighbor[Block::FACE_BACK] = this;
68         }
69 }
70
71 void Chunk::ClearNeighbors() {
72         for (int i = 0; i < Block::FACE_COUNT; ++i) {
73                 neighbor[i] = nullptr;
74         }
75 }
76
77 void Chunk::Unlink() {
78         if (neighbor[Block::FACE_UP]) {
79                 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = nullptr;
80         }
81         if (neighbor[Block::FACE_DOWN]) {
82                 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = nullptr;
83         }
84         if (neighbor[Block::FACE_LEFT]) {
85                 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = nullptr;
86         }
87         if (neighbor[Block::FACE_RIGHT]) {
88                 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = nullptr;
89         }
90         if (neighbor[Block::FACE_FRONT]) {
91                 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = nullptr;
92         }
93         if (neighbor[Block::FACE_BACK]) {
94                 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = nullptr;
95         }
96 }
97
98 void Chunk::Relink() {
99         if (neighbor[Block::FACE_UP]) {
100                 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = this;
101         }
102         if (neighbor[Block::FACE_DOWN]) {
103                 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = this;
104         }
105         if (neighbor[Block::FACE_LEFT]) {
106                 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = this;
107         }
108         if (neighbor[Block::FACE_RIGHT]) {
109                 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = this;
110         }
111         if (neighbor[Block::FACE_FRONT]) {
112                 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = this;
113         }
114         if (neighbor[Block::FACE_BACK]) {
115                 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = this;
116         }
117 }
118
119
120 void Chunk::Allocate() {
121         blocks.resize(Size());
122 }
123
124
125 void Chunk::Draw() {
126         if (dirty) {
127                 Update();
128         }
129         model.Draw();
130 }
131
132
133 bool Chunk::Intersection(
134         const Ray &ray,
135         const glm::mat4 &M,
136         int &blkid,
137         float &dist,
138         glm::vec3 &normal
139 ) const {
140         // TODO: should be possible to heavily optimize this
141         int id = 0;
142         blkid = -1;
143         dist = std::numeric_limits<float>::infinity();
144         for (int z = 0; z < Depth(); ++z) {
145                 for (int y = 0; y < Height(); ++y) {
146                         for (int x = 0; x < Width(); ++x, ++id) {
147                                 if (!Type(blocks[id]).visible) {
148                                         continue;
149                                 }
150                                 float cur_dist;
151                                 glm::vec3 cur_norm;
152                                 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
153                                         if (cur_dist < dist) {
154                                                 blkid = id;
155                                                 dist = cur_dist;
156                                                 normal = cur_norm;
157                                         }
158                                 }
159                         }
160                 }
161         }
162
163         if (blkid < 0) {
164                 return false;
165         } else {
166                 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
167                 return true;
168         }
169 }
170
171 void Chunk::Position(const Pos &pos) {
172         position = pos;
173 }
174
175 glm::mat4 Chunk::Transform(const Pos &offset) const {
176         return glm::translate((position - offset) * Extent());
177 }
178
179
180 void Chunk::CheckUpdate() {
181         if (dirty) {
182                 Update();
183         }
184 }
185
186 void Chunk::Update() {
187         int vtx_count = 0, idx_count = 0;
188         for (const auto &block : blocks) {
189                 const Shape *shape = Type(block).shape;
190                 vtx_count += shape->VertexCount();
191                 idx_count += shape->VertexIndexCount();
192         }
193         buf.Clear();
194         buf.Reserve(vtx_count, idx_count);
195
196         Model::Index vtx_counter = 0;
197         for (size_t i = 0; i < Size(); ++i) {
198                 const BlockType &type = Type(blocks[i]);
199
200                 if (!type.visible || Obstructed(i)) continue;
201
202                 type.FillModel(buf, ToTransform(i), vtx_counter);
203                 vtx_counter += type.shape->VertexCount();
204         }
205
206         model.Update(buf);
207         dirty = false;
208 }
209
210 bool Chunk::Obstructed(int idx) const {
211         Chunk::Pos pos(ToPos(idx));
212
213         Chunk::Pos left_pos(pos + Chunk::Pos(-1, 0, 0));
214         const Block *left_block = nullptr;
215         if (InBounds(left_pos)) {
216                 left_block = &BlockAt(left_pos);
217         } else if (HasNeighbor(Block::FACE_LEFT)) {
218                 left_pos += Chunk::Pos(Width(), 0, 0);
219                 left_block = &GetNeighbor(Block::FACE_LEFT).BlockAt(left_pos);
220         } else {
221                 return false;
222         }
223         if (!Type(*left_block).FaceFilled(*left_block, Block::FACE_RIGHT)) {
224                 return false;
225         }
226
227         Chunk::Pos right_pos(pos + Chunk::Pos(1, 0, 0));
228         const Block *right_block = nullptr;
229         if (InBounds(right_pos)) {
230                 right_block = &BlockAt(right_pos);
231         } else if (HasNeighbor(Block::FACE_RIGHT)) {
232                 right_pos += Chunk::Pos(-Width(), 0, 0);
233                 right_block = &GetNeighbor(Block::FACE_RIGHT).BlockAt(right_pos);
234         } else {
235                 return false;
236         }
237         if (!Type(*right_block).FaceFilled(*right_block, Block::FACE_LEFT)) {
238                 return false;
239         }
240
241         Chunk::Pos down_pos(pos + Chunk::Pos(0, -1, 0));
242         const Block *down_block = nullptr;
243         if (InBounds(down_pos)) {
244                 down_block = &BlockAt(down_pos);
245         } else if (HasNeighbor(Block::FACE_DOWN)) {
246                 down_pos += Chunk::Pos(0, Height(), 0);
247                 down_block = &GetNeighbor(Block::FACE_DOWN).BlockAt(down_pos);
248         } else {
249                 return false;
250         }
251         if (!Type(*down_block).FaceFilled(*down_block, Block::FACE_UP)) {
252                 return false;
253         }
254
255         Chunk::Pos up_pos(pos + Chunk::Pos(0, 1, 0));
256         const Block *up_block = nullptr;
257         if (InBounds(up_pos)) {
258                 up_block = &BlockAt(up_pos);
259         } else if (HasNeighbor(Block::FACE_UP)) {
260                 up_pos += Chunk::Pos(0, -Height(), 0);
261                 up_block = &GetNeighbor(Block::FACE_UP).BlockAt(up_pos);
262         } else {
263                 return false;
264         }
265         if (!Type(*up_block).FaceFilled(*up_block, Block::FACE_DOWN)) {
266                 return false;
267         }
268
269         Chunk::Pos back_pos(pos + Chunk::Pos(0, 0, -1));
270         const Block *back_block = nullptr;
271         if (InBounds(back_pos)) {
272                 back_block = &BlockAt(back_pos);
273         } else if (HasNeighbor(Block::FACE_BACK)) {
274                 back_pos += Chunk::Pos(0, 0, Depth());
275                 back_block = &GetNeighbor(Block::FACE_BACK).BlockAt(back_pos);
276         } else {
277                 return false;
278         }
279         if (!Type(*back_block).FaceFilled(*back_block, Block::FACE_FRONT)) {
280                 return false;
281         }
282
283         Chunk::Pos front_pos(pos + Chunk::Pos(0, 0, 1));
284         const Block *front_block = nullptr;
285         if (InBounds(front_pos)) {
286                 front_block = &BlockAt(front_pos);
287         } else if (HasNeighbor(Block::FACE_FRONT)) {
288                 front_pos += Chunk::Pos(0, 0, -Depth());
289                 front_block = &GetNeighbor(Block::FACE_FRONT).BlockAt(front_pos);
290         } else {
291                 return false;
292         }
293         if (!Type(*front_block).FaceFilled(*front_block, Block::FACE_BACK)) {
294                 return false;
295         }
296
297         return true;
298 }
299
300 glm::mat4 Chunk::ToTransform(int idx) const {
301         return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
302 }
303
304
305 ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
306 : base(0, 0, 0)
307 , reg(reg)
308 , gen(gen)
309 , loaded()
310 , to_generate()
311 , to_free()
312 , load_dist(4)
313 , unload_dist(5) {
314
315 }
316
317 namespace {
318
319 struct ChunkLess {
320
321         explicit ChunkLess(const Chunk::Pos &base)
322         : base(base) { }
323
324         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
325                 Chunk::Pos da(base - a);
326                 Chunk::Pos db(base - b);
327                 return
328                         da.x * da.x + da.y * da.y + da.z * da.z <
329                         db.x * db.x + db.y * db.y + db.z * db.z;
330         }
331
332         Chunk::Pos base;
333
334 };
335
336 }
337
338 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
339         for (int z = from.z; z < to.z; ++z) {
340                 for (int y = from.y; y < to.y; ++y) {
341                         for (int x = from.x; x < to.x; ++x) {
342                                 Chunk::Pos pos(x, y, z);
343                                 if (Known(pos)) {
344                                         continue;
345                                 } else if (pos == base) {
346                                         Generate(pos);
347
348                                 //      orientation testing
349                                 //      for (int i = 0; i < Block::FACE_COUNT; ++i) {
350                                 //              for (int j = 0; j < Block::TURN_COUNT; ++j) {
351                                 //                      loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
352                                 //              }
353                                 //      }
354                                 //      loaded.back().Invalidate();
355                                 //      loaded.back().CheckUpdate();
356                                 } else {
357                                         to_generate.emplace_back(pos);
358                                 }
359                         }
360                 }
361         }
362         to_generate.sort(ChunkLess(base));
363 }
364
365 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
366         loaded.emplace_back(reg);
367         Chunk &chunk = loaded.back();
368         chunk.Position(pos);
369         Insert(chunk);
370         gen(chunk);
371         return chunk;
372 }
373
374 void ChunkLoader::Insert(Chunk &chunk) {
375         for (Chunk &other : loaded) {
376                 chunk.SetNeighbor(other);
377         }
378 }
379
380 void ChunkLoader::Remove(Chunk &chunk) {
381         chunk.Unlink();
382 }
383
384 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
385         for (Chunk &chunk : loaded) {
386                 if (chunk.Position() == pos) {
387                         return &chunk;
388                 }
389         }
390         return nullptr;
391 }
392
393 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
394         for (const Chunk::Pos &chunk : to_generate) {
395                 if (chunk == pos) {
396                         return true;
397                 }
398         }
399         return nullptr;
400 }
401
402 bool ChunkLoader::Known(const Chunk::Pos &pos) {
403         if (Loaded(pos)) return true;
404         return Queued(pos);
405 }
406
407 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
408         Chunk *chunk = Loaded(pos);
409         if (chunk) {
410                 return *chunk;
411         }
412
413         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
414                 if (*iter == pos) {
415                         to_generate.erase(iter);
416                         break;
417                 }
418         }
419
420         return Generate(pos);
421 }
422
423 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
424         if (new_base == base) {
425                 return;
426         }
427         base = new_base;
428
429         // unload far away chunks
430         for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
431                 if (std::abs(base.x - iter->Position().x) > unload_dist
432                                 || std::abs(base.y - iter->Position().y) > unload_dist
433                                 || std::abs(base.z - iter->Position().z) > unload_dist) {
434                         auto saved = iter;
435                         Remove(*saved);
436                         ++iter;
437                         to_free.splice(to_free.end(), loaded, saved);
438                 } else {
439                         ++iter;
440                 }
441         }
442         // abort far away queued chunks
443         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
444                 if (std::abs(base.x - iter->x) > unload_dist
445                                 || std::abs(base.y - iter->y) > unload_dist
446                                 || std::abs(base.z - iter->z) > unload_dist) {
447                         iter = to_generate.erase(iter);
448                 } else {
449                         ++iter;
450                 }
451         }
452         // add missing new chunks
453         const Chunk::Pos offset(load_dist, load_dist, load_dist);
454         Generate(base - offset, base + offset);
455 }
456
457 void ChunkLoader::Update() {
458         bool reused = false;
459         if (!to_generate.empty()) {
460                 Chunk::Pos pos(to_generate.front());
461
462                 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
463                         if (iter->Position() == pos) {
464                                 iter->Relink();
465                                 loaded.splice(loaded.end(), to_free, iter);
466                                 reused = true;
467                                 break;
468                         }
469                 }
470
471                 if (!reused) {
472                         if (to_free.empty()) {
473                                 loaded.emplace_back(reg);
474                         } else {
475                                 to_free.front().ClearNeighbors();
476                                 loaded.splice(loaded.end(), to_free, to_free.begin());
477                                 reused = true;
478                         }
479                         Chunk &chunk = loaded.back();
480                         chunk.Position(pos);
481                         Insert(chunk);
482                         gen(chunk);
483                 }
484                 to_generate.pop_front();
485         }
486
487         if (!reused && !to_free.empty()) {
488                 to_free.pop_front();
489         }
490 }
491
492 }