3 #include "generator.hpp"
6 #include <glm/gtx/transform.hpp>
11 blank::Model::Buffer buf;
17 Chunk::Chunk(const BlockTypeRegistry &types)
19 , neighbor{ 0, 0, 0, 0, 0, 0 }
27 Chunk::Chunk(Chunk &&other)
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];
37 Chunk &Chunk::operator =(Chunk &&other) {
39 for (size_t i = 0; i < Block::FACE_COUNT; ++i) {
40 neighbor[i] = other.neighbor[i];
42 blocks = std::move(other.blocks);
43 model = std::move(other.model);
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;
71 void Chunk::ClearNeighbors() {
72 for (int i = 0; i < Block::FACE_COUNT; ++i) {
73 neighbor[i] = nullptr;
77 void Chunk::Unlink() {
78 if (neighbor[Block::FACE_UP]) {
79 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = nullptr;
81 if (neighbor[Block::FACE_DOWN]) {
82 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = nullptr;
84 if (neighbor[Block::FACE_LEFT]) {
85 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = nullptr;
87 if (neighbor[Block::FACE_RIGHT]) {
88 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = nullptr;
90 if (neighbor[Block::FACE_FRONT]) {
91 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = nullptr;
93 if (neighbor[Block::FACE_BACK]) {
94 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = nullptr;
98 void Chunk::Relink() {
99 if (neighbor[Block::FACE_UP]) {
100 neighbor[Block::FACE_UP]->neighbor[Block::FACE_DOWN] = this;
102 if (neighbor[Block::FACE_DOWN]) {
103 neighbor[Block::FACE_DOWN]->neighbor[Block::FACE_UP] = this;
105 if (neighbor[Block::FACE_LEFT]) {
106 neighbor[Block::FACE_LEFT]->neighbor[Block::FACE_RIGHT] = this;
108 if (neighbor[Block::FACE_RIGHT]) {
109 neighbor[Block::FACE_RIGHT]->neighbor[Block::FACE_LEFT] = this;
111 if (neighbor[Block::FACE_FRONT]) {
112 neighbor[Block::FACE_FRONT]->neighbor[Block::FACE_BACK] = this;
114 if (neighbor[Block::FACE_BACK]) {
115 neighbor[Block::FACE_BACK]->neighbor[Block::FACE_FRONT] = this;
120 void Chunk::Allocate() {
121 blocks.resize(Size());
133 bool Chunk::Intersection(
140 // TODO: should be possible to heavily optimize this
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) {
152 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
153 if (cur_dist < dist) {
166 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
171 void Chunk::Position(const Pos &pos) {
175 glm::mat4 Chunk::Transform(const Pos &offset) const {
176 return glm::translate((position - offset) * Extent());
180 void Chunk::CheckUpdate() {
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();
194 buf.Reserve(vtx_count, idx_count);
196 Model::Index vtx_counter = 0;
197 for (size_t i = 0; i < Size(); ++i) {
198 const BlockType &type = Type(blocks[i]);
200 if (!type.visible || Obstructed(i)) continue;
202 type.FillModel(buf, ToTransform(i), vtx_counter);
203 vtx_counter += type.shape->VertexCount();
210 bool Chunk::Obstructed(int idx) const {
211 Chunk::Pos pos(ToPos(idx));
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);
223 if (!Type(*left_block).FaceFilled(*left_block, Block::FACE_RIGHT)) {
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);
237 if (!Type(*right_block).FaceFilled(*right_block, Block::FACE_LEFT)) {
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);
251 if (!Type(*down_block).FaceFilled(*down_block, Block::FACE_UP)) {
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);
265 if (!Type(*up_block).FaceFilled(*up_block, Block::FACE_DOWN)) {
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);
279 if (!Type(*back_block).FaceFilled(*back_block, Block::FACE_FRONT)) {
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);
293 if (!Type(*front_block).FaceFilled(*front_block, Block::FACE_BACK)) {
300 glm::mat4 Chunk::ToTransform(int idx) const {
301 return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
305 ChunkLoader::ChunkLoader(const BlockTypeRegistry ®, const Generator &gen)
321 explicit ChunkLess(const Chunk::Pos &base)
324 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
325 Chunk::Pos da(base - a);
326 Chunk::Pos db(base - b);
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;
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);
345 } else if (pos == base) {
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));
354 // loaded.back().Invalidate();
355 // loaded.back().CheckUpdate();
357 to_generate.emplace_back(pos);
362 to_generate.sort(ChunkLess(base));
365 Chunk &ChunkLoader::Generate(const Chunk::Pos &pos) {
366 loaded.emplace_back(reg);
367 Chunk &chunk = loaded.back();
374 void ChunkLoader::Insert(Chunk &chunk) {
375 for (Chunk &other : loaded) {
376 chunk.SetNeighbor(other);
380 void ChunkLoader::Remove(Chunk &chunk) {
384 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
385 for (Chunk &chunk : loaded) {
386 if (chunk.Position() == pos) {
393 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
394 for (const Chunk::Pos &chunk : to_generate) {
402 bool ChunkLoader::Known(const Chunk::Pos &pos) {
403 if (Loaded(pos)) return true;
407 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
408 Chunk *chunk = Loaded(pos);
413 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
415 to_generate.erase(iter);
420 return Generate(pos);
423 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
424 if (new_base == base) {
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) {
437 to_free.splice(to_free.end(), loaded, saved);
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);
452 // add missing new chunks
453 const Chunk::Pos offset(load_dist, load_dist, load_dist);
454 Generate(base - offset, base + offset);
457 void ChunkLoader::Update() {
459 if (!to_generate.empty()) {
460 Chunk::Pos pos(to_generate.front());
462 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
463 if (iter->Position() == pos) {
465 loaded.splice(loaded.end(), to_free, iter);
472 if (to_free.empty()) {
473 loaded.emplace_back(reg);
475 to_free.front().ClearNeighbors();
476 loaded.splice(loaded.end(), to_free, to_free.begin());
479 Chunk &chunk = loaded.back();
484 to_generate.pop_front();
487 if (!reused && !to_free.empty()) {