3 #include "generator.hpp"
6 #include <glm/gtx/transform.hpp>
11 Chunk::Chunk(const BlockTypeRegistry &types)
20 Chunk::Chunk(Chunk &&other)
22 , blocks(std::move(other.blocks))
23 , model(std::move(other.model))
24 , dirty(other.dirty) {
28 Chunk &Chunk::operator =(Chunk &&other) {
30 blocks = std::move(other.blocks);
31 model = std::move(other.model);
37 void Chunk::Allocate() {
38 blocks.resize(Size());
50 bool Chunk::Intersection(
55 glm::vec3 *normal) const {
57 if (!blank::Intersection(ray, Bounds(), M)) {
62 if (!blkid && !dist && !normal) {
66 // TODO: should be possible to heavily optimize this
69 float closest_dist = std::numeric_limits<float>::infinity();
70 glm::vec3 closest_normal(0, 1, 0);
71 for (int z = 0; z < Depth(); ++z) {
72 for (int y = 0; y < Height(); ++y) {
73 for (int x = 0; x < Width(); ++x, ++id) {
74 if (!Type(blocks[id]).visible) {
79 Block::Pos pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
80 if (Type(blocks[id]).shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
81 if (cur_dist < closest_dist) {
83 closest_dist = cur_dist;
84 closest_normal = cur_norm;
102 *normal = closest_normal;
107 void Chunk::Position(const Pos &pos) {
111 glm::mat4 Chunk::Transform(const Pos &offset) const {
112 return glm::translate((position - offset) * Extent());
116 void Chunk::CheckUpdate() {
123 void Chunk::Update() {
124 int vtx_count = 0, idx_count = 0;
125 for (const auto &block : blocks) {
126 const Shape *shape = Type(block).shape;
127 vtx_count += shape->VertexCount();
128 idx_count += shape->VertexIndexCount();
131 model.Reserve(vtx_count, idx_count);
133 Model::Index vtx_counter = 0;
134 for (size_t i = 0; i < Size(); ++i) {
135 const BlockType &type = Type(blocks[i]);
136 type.FillModel(model, ToCoords(i), vtx_counter);
137 vtx_counter += type.shape->VertexCount();
145 ChunkLoader::ChunkLoader(const BlockTypeRegistry ®, const Generator &gen)
161 explicit ChunkLess(const Chunk::Pos &base)
164 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
165 Chunk::Pos da(base - a);
166 Chunk::Pos db(base - b);
168 da.x * da.x + da.y * da.y + da.z * da.z <
169 db.x * db.x + db.y * db.y + db.z * db.z;
178 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
179 for (int z = from.z; z < to.z; ++z) {
180 for (int y = from.y; y < to.y; ++y) {
181 for (int x = from.x; x < to.x; ++x) {
182 Chunk::Pos pos(x, y, z);
185 } else if (x == 0 && y == 0 && z == 0) {
186 loaded.emplace_back(reg);
187 loaded.back().Position(pos);
190 to_generate.emplace_back(pos);
195 to_generate.sort(ChunkLess(base));
198 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
199 for (Chunk &chunk : loaded) {
200 if (chunk.Position() == pos) {
207 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
208 for (const Chunk::Pos &chunk : to_generate) {
216 bool ChunkLoader::Known(const Chunk::Pos &pos) {
217 if (Loaded(pos)) return true;
221 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
222 Chunk *chunk = Loaded(pos);
227 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
229 to_generate.erase(iter);
234 loaded.emplace_back(reg);
235 loaded.back().Position(pos);
237 return loaded.back();
240 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
241 if (new_base == base) {
246 // unload far away chunks
247 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
248 if (std::abs(base.x - iter->Position().x) > unload_dist
249 || std::abs(base.y - iter->Position().y) > unload_dist
250 || std::abs(base.z - iter->Position().z) > unload_dist) {
253 to_free.splice(to_free.end(), loaded, saved);
258 // abort far away queued chunks
259 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
260 if (std::abs(base.x - iter->x) > unload_dist
261 || std::abs(base.y - iter->y) > unload_dist
262 || std::abs(base.z - iter->z) > unload_dist) {
263 iter = to_generate.erase(iter);
268 // add missing new chunks
269 const Chunk::Pos offset(load_dist, load_dist, load_dist);
270 Generate(base - offset, base + offset);
273 void ChunkLoader::Update() {
275 if (!to_generate.empty()) {
276 Chunk::Pos pos(to_generate.front());
278 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
279 if (iter->Position() == pos) {
280 loaded.splice(loaded.end(), to_free, iter);
287 if (to_free.empty()) {
288 loaded.emplace_back(reg);
290 loaded.splice(loaded.end(), to_free, to_free.begin());
293 loaded.back().Position(pos);
296 to_generate.pop_front();
299 if (!reused && !to_free.empty()) {