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(
61 glm::vec3 *normal) const {
63 if (!blank::Intersection(ray, Bounds(), M)) {
68 if (!blkid && !dist && !normal) {
72 // TODO: should be possible to heavily optimize this
75 float closest_dist = std::numeric_limits<float>::infinity();
76 glm::vec3 closest_normal(0, 1, 0);
77 for (int z = 0; z < Depth(); ++z) {
78 for (int y = 0; y < Height(); ++y) {
79 for (int x = 0; x < Width(); ++x, ++id) {
80 if (!Type(blocks[id]).visible) {
85 Block::Pos pos(float(x) + 0.5f, float(y) + 0.5f, float(z) + 0.5f);
86 if (Type(blocks[id]).shape->Intersects(ray, glm::translate(M, pos), cur_dist, cur_norm)) {
87 if (cur_dist < closest_dist) {
89 closest_dist = cur_dist;
90 closest_normal = cur_norm;
105 *dist = closest_dist;
108 *normal = closest_normal;
113 void Chunk::Position(const Pos &pos) {
117 glm::mat4 Chunk::Transform(const Pos &offset) const {
118 return glm::translate((position - offset) * Extent());
122 void Chunk::CheckUpdate() {
128 void Chunk::Update() {
129 int vtx_count = 0, idx_count = 0;
130 for (const auto &block : blocks) {
131 const Shape *shape = Type(block).shape;
132 vtx_count += shape->VertexCount();
133 idx_count += shape->VertexIndexCount();
136 buf.Reserve(vtx_count, idx_count);
138 Model::Index vtx_counter = 0;
139 for (size_t i = 0; i < Size(); ++i) {
140 if (Obstructed(i)) continue;
142 const BlockType &type = Type(blocks[i]);
143 type.FillModel(buf, ToCoords(i), vtx_counter);
144 vtx_counter += type.shape->VertexCount();
151 bool Chunk::Obstructed(int idx) const {
152 if (IsBorder(idx)) return false;
154 // not checking neighbor visibility here, so all
155 // invisible blocks must have their fill set to 6x false
156 // (the default, so should be okay)
158 const BlockType &right = Type(blocks[idx + 1]);
159 if (!right.fill.left) return false;
161 const BlockType &left = Type(blocks[idx - 1]);
162 if (!left.fill.right) return false;
164 const BlockType &top = Type(blocks[idx + Width()]);
165 if (!top.fill.bottom) return false;
167 const BlockType &bottom = Type(blocks[idx - Width()]);
168 if (!bottom.fill.top) return false;
170 const BlockType &front = Type(blocks[idx + Width() * Height()]);
171 if (!front.fill.back) return false;
173 const BlockType &back = Type(blocks[idx - Width() * Height()]);
174 if (!back.fill.front) return false;
180 ChunkLoader::ChunkLoader(const BlockTypeRegistry ®, const Generator &gen)
196 explicit ChunkLess(const Chunk::Pos &base)
199 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
200 Chunk::Pos da(base - a);
201 Chunk::Pos db(base - b);
203 da.x * da.x + da.y * da.y + da.z * da.z <
204 db.x * db.x + db.y * db.y + db.z * db.z;
213 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
214 for (int z = from.z; z < to.z; ++z) {
215 for (int y = from.y; y < to.y; ++y) {
216 for (int x = from.x; x < to.x; ++x) {
217 Chunk::Pos pos(x, y, z);
220 } else if (x == 0 && y == 0 && z == 0) {
221 loaded.emplace_back(reg);
222 loaded.back().Position(pos);
225 to_generate.emplace_back(pos);
230 to_generate.sort(ChunkLess(base));
233 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
234 for (Chunk &chunk : loaded) {
235 if (chunk.Position() == pos) {
242 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
243 for (const Chunk::Pos &chunk : to_generate) {
251 bool ChunkLoader::Known(const Chunk::Pos &pos) {
252 if (Loaded(pos)) return true;
256 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
257 Chunk *chunk = Loaded(pos);
262 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
264 to_generate.erase(iter);
269 loaded.emplace_back(reg);
270 loaded.back().Position(pos);
272 return loaded.back();
275 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
276 if (new_base == base) {
281 // unload far away chunks
282 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
283 if (std::abs(base.x - iter->Position().x) > unload_dist
284 || std::abs(base.y - iter->Position().y) > unload_dist
285 || std::abs(base.z - iter->Position().z) > unload_dist) {
288 to_free.splice(to_free.end(), loaded, saved);
293 // abort far away queued chunks
294 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
295 if (std::abs(base.x - iter->x) > unload_dist
296 || std::abs(base.y - iter->y) > unload_dist
297 || std::abs(base.z - iter->z) > unload_dist) {
298 iter = to_generate.erase(iter);
303 // add missing new chunks
304 const Chunk::Pos offset(load_dist, load_dist, load_dist);
305 Generate(base - offset, base + offset);
308 void ChunkLoader::Update() {
310 if (!to_generate.empty()) {
311 Chunk::Pos pos(to_generate.front());
313 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
314 if (iter->Position() == pos) {
315 loaded.splice(loaded.end(), to_free, iter);
322 if (to_free.empty()) {
323 loaded.emplace_back(reg);
325 loaded.splice(loaded.end(), to_free, to_free.begin());
328 loaded.back().Position(pos);
331 to_generate.pop_front();
334 if (!reused && !to_free.empty()) {