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 &a, const Chunk &b) const {
165 Chunk::Pos da(base - a.Position());
166 Chunk::Pos db(base - b.Position());
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(reg);
191 to_generate.back().Position(pos);
196 to_generate.sort(ChunkLess(base));
199 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
200 for (Chunk &chunk : loaded) {
201 if (chunk.Position() == pos) {
208 Chunk *ChunkLoader::Queued(const Chunk::Pos &pos) {
209 for (Chunk &chunk : to_generate) {
210 if (chunk.Position() == pos) {
217 Chunk *ChunkLoader::Known(const Chunk::Pos &pos) {
218 Chunk *chunk = Loaded(pos);
219 if (chunk) return chunk;
224 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
225 Chunk *chunk = Loaded(pos);
236 loaded.emplace_back(reg);
237 loaded.back().Position(pos);
239 return loaded.back();
242 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
243 if (new_base == base) {
248 // unload far away chunks
249 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
250 if (std::abs(base.x - iter->Position().x) > unload_dist
251 || std::abs(base.y - iter->Position().y) > unload_dist
252 || std::abs(base.z - iter->Position().z) > unload_dist) {
255 to_free.splice(to_free.end(), loaded, saved);
260 // abort far away queued chunks
261 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
262 if (std::abs(base.x - iter->Position().x) > unload_dist
263 || std::abs(base.y - iter->Position().y) > unload_dist
264 || std::abs(base.z - iter->Position().z) > unload_dist) {
265 iter = to_generate.erase(iter);
270 // add missing new chunks
271 const Chunk::Pos offset(load_dist, load_dist, load_dist);
272 Generate(base - offset, base + offset);
275 void ChunkLoader::Update() {
276 if (!to_generate.empty()) {
277 gen(to_generate.front());
278 loaded.splice(loaded.end(), to_generate, to_generate.begin());
281 if (!to_free.empty()) {