3 #include "generator.hpp"
6 #include <glm/gtx/transform.hpp>
11 blank::Model::Buffer buf;
17 Chunk::Chunk(const BlockTypeRegistry &types)
26 Chunk::Chunk(Chunk &&other)
28 , blocks(std::move(other.blocks))
29 , model(std::move(other.model))
30 , dirty(other.dirty) {
34 Chunk &Chunk::operator =(Chunk &&other) {
36 blocks = std::move(other.blocks);
37 model = std::move(other.model);
43 void Chunk::Allocate() {
44 blocks.resize(Size());
56 bool Chunk::Intersection(
63 // TODO: should be possible to heavily optimize this
66 dist = std::numeric_limits<float>::infinity();
67 for (int z = 0; z < Depth(); ++z) {
68 for (int y = 0; y < Height(); ++y) {
69 for (int x = 0; x < Width(); ++x, ++id) {
70 if (!Type(blocks[id]).visible) {
75 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
76 if (cur_dist < dist) {
89 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
94 void Chunk::Position(const Pos &pos) {
98 glm::mat4 Chunk::Transform(const Pos &offset) const {
99 return glm::translate((position - offset) * Extent());
103 void Chunk::CheckUpdate() {
109 void Chunk::Update() {
110 int vtx_count = 0, idx_count = 0;
111 for (const auto &block : blocks) {
112 const Shape *shape = Type(block).shape;
113 vtx_count += shape->VertexCount();
114 idx_count += shape->VertexIndexCount();
117 buf.Reserve(vtx_count, idx_count);
119 Model::Index vtx_counter = 0;
120 for (size_t i = 0; i < Size(); ++i) {
121 if (Obstructed(i)) continue;
123 const BlockType &type = Type(blocks[i]);
124 type.FillModel(buf, ToTransform(i), vtx_counter);
125 vtx_counter += type.shape->VertexCount();
132 bool Chunk::Obstructed(int idx) const {
133 if (IsBorder(idx)) return false;
135 // not checking neighbor visibility here, so all
136 // invisible blocks must have their fill set to 6x false
137 // (the default, so should be okay)
139 const BlockType &right = Type(blocks[idx + 1]);
140 if (!right.fill.left) return false;
142 const BlockType &left = Type(blocks[idx - 1]);
143 if (!left.fill.right) return false;
145 const BlockType &up = Type(blocks[idx + Width()]);
146 if (!up.fill.down) return false;
148 const BlockType &down = Type(blocks[idx - Width()]);
149 if (!down.fill.up) return false;
151 const BlockType &front = Type(blocks[idx + Width() * Height()]);
152 if (!front.fill.back) return false;
154 const BlockType &back = Type(blocks[idx - Width() * Height()]);
155 if (!back.fill.front) return false;
160 glm::mat4 Chunk::ToTransform(int idx) const {
161 return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
165 ChunkLoader::ChunkLoader(const BlockTypeRegistry ®, const Generator &gen)
181 explicit ChunkLess(const Chunk::Pos &base)
184 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
185 Chunk::Pos da(base - a);
186 Chunk::Pos db(base - b);
188 da.x * da.x + da.y * da.y + da.z * da.z <
189 db.x * db.x + db.y * db.y + db.z * db.z;
198 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
199 for (int z = from.z; z < to.z; ++z) {
200 for (int y = from.y; y < to.y; ++y) {
201 for (int x = from.x; x < to.x; ++x) {
202 Chunk::Pos pos(x, y, z);
205 } else if (x == 0 && y == 0 && z == 0) {
206 loaded.emplace_back(reg);
207 loaded.back().Position(pos);
210 // orientation testing
211 // for (int i = 0; i < Block::FACE_COUNT; ++i) {
212 // for (int j = 0; j < Block::TURN_COUNT; ++j) {
213 // loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
216 // loaded.back().Invalidate();
217 // loaded.back().CheckUpdate();
219 to_generate.emplace_back(pos);
224 to_generate.sort(ChunkLess(base));
227 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
228 for (Chunk &chunk : loaded) {
229 if (chunk.Position() == pos) {
236 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
237 for (const Chunk::Pos &chunk : to_generate) {
245 bool ChunkLoader::Known(const Chunk::Pos &pos) {
246 if (Loaded(pos)) return true;
250 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
251 Chunk *chunk = Loaded(pos);
256 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
258 to_generate.erase(iter);
263 loaded.emplace_back(reg);
264 loaded.back().Position(pos);
266 return loaded.back();
269 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
270 if (new_base == base) {
275 // unload far away chunks
276 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
277 if (std::abs(base.x - iter->Position().x) > unload_dist
278 || std::abs(base.y - iter->Position().y) > unload_dist
279 || std::abs(base.z - iter->Position().z) > unload_dist) {
282 to_free.splice(to_free.end(), loaded, saved);
287 // abort far away queued chunks
288 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
289 if (std::abs(base.x - iter->x) > unload_dist
290 || std::abs(base.y - iter->y) > unload_dist
291 || std::abs(base.z - iter->z) > unload_dist) {
292 iter = to_generate.erase(iter);
297 // add missing new chunks
298 const Chunk::Pos offset(load_dist, load_dist, load_dist);
299 Generate(base - offset, base + offset);
302 void ChunkLoader::Update() {
304 if (!to_generate.empty()) {
305 Chunk::Pos pos(to_generate.front());
307 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
308 if (iter->Position() == pos) {
309 loaded.splice(loaded.end(), to_free, iter);
316 if (to_free.empty()) {
317 loaded.emplace_back(reg);
319 loaded.splice(loaded.end(), to_free, to_free.begin());
322 loaded.back().Position(pos);
325 to_generate.pop_front();
328 if (!reused && !to_free.empty()) {