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 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
86 if (cur_dist < closest_dist) {
88 closest_dist = cur_dist;
89 closest_normal = cur_norm;
104 *dist = closest_dist;
107 *normal = closest_normal;
112 void Chunk::Position(const Pos &pos) {
116 glm::mat4 Chunk::Transform(const Pos &offset) const {
117 return glm::translate((position - offset) * Extent());
121 void Chunk::CheckUpdate() {
127 void Chunk::Update() {
128 int vtx_count = 0, idx_count = 0;
129 for (const auto &block : blocks) {
130 const Shape *shape = Type(block).shape;
131 vtx_count += shape->VertexCount();
132 idx_count += shape->VertexIndexCount();
135 buf.Reserve(vtx_count, idx_count);
137 Model::Index vtx_counter = 0;
138 for (size_t i = 0; i < Size(); ++i) {
139 if (Obstructed(i)) continue;
141 const BlockType &type = Type(blocks[i]);
142 type.FillModel(buf, ToTransform(i), vtx_counter);
143 vtx_counter += type.shape->VertexCount();
150 bool Chunk::Obstructed(int idx) const {
151 if (IsBorder(idx)) return false;
153 // not checking neighbor visibility here, so all
154 // invisible blocks must have their fill set to 6x false
155 // (the default, so should be okay)
157 const BlockType &right = Type(blocks[idx + 1]);
158 if (!right.fill.left) return false;
160 const BlockType &left = Type(blocks[idx - 1]);
161 if (!left.fill.right) return false;
163 const BlockType &top = Type(blocks[idx + Width()]);
164 if (!top.fill.bottom) return false;
166 const BlockType &bottom = Type(blocks[idx - Width()]);
167 if (!bottom.fill.top) return false;
169 const BlockType &front = Type(blocks[idx + Width() * Height()]);
170 if (!front.fill.back) return false;
172 const BlockType &back = Type(blocks[idx - Width() * Height()]);
173 if (!back.fill.front) return false;
178 glm::mat4 Chunk::ToTransform(int idx) const {
179 return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
183 ChunkLoader::ChunkLoader(const BlockTypeRegistry ®, const Generator &gen)
199 explicit ChunkLess(const Chunk::Pos &base)
202 bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
203 Chunk::Pos da(base - a);
204 Chunk::Pos db(base - b);
206 da.x * da.x + da.y * da.y + da.z * da.z <
207 db.x * db.x + db.y * db.y + db.z * db.z;
216 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
217 for (int z = from.z; z < to.z; ++z) {
218 for (int y = from.y; y < to.y; ++y) {
219 for (int x = from.x; x < to.x; ++x) {
220 Chunk::Pos pos(x, y, z);
223 } else if (x == 0 && y == 0 && z == 0) {
224 loaded.emplace_back(reg);
225 loaded.back().Position(pos);
228 to_generate.emplace_back(pos);
233 to_generate.sort(ChunkLess(base));
236 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
237 for (Chunk &chunk : loaded) {
238 if (chunk.Position() == pos) {
245 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
246 for (const Chunk::Pos &chunk : to_generate) {
254 bool ChunkLoader::Known(const Chunk::Pos &pos) {
255 if (Loaded(pos)) return true;
259 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
260 Chunk *chunk = Loaded(pos);
265 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
267 to_generate.erase(iter);
272 loaded.emplace_back(reg);
273 loaded.back().Position(pos);
275 return loaded.back();
278 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
279 if (new_base == base) {
284 // unload far away chunks
285 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
286 if (std::abs(base.x - iter->Position().x) > unload_dist
287 || std::abs(base.y - iter->Position().y) > unload_dist
288 || std::abs(base.z - iter->Position().z) > unload_dist) {
291 to_free.splice(to_free.end(), loaded, saved);
296 // abort far away queued chunks
297 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
298 if (std::abs(base.x - iter->x) > unload_dist
299 || std::abs(base.y - iter->y) > unload_dist
300 || std::abs(base.z - iter->z) > unload_dist) {
301 iter = to_generate.erase(iter);
306 // add missing new chunks
307 const Chunk::Pos offset(load_dist, load_dist, load_dist);
308 Generate(base - offset, base + offset);
311 void ChunkLoader::Update() {
313 if (!to_generate.empty()) {
314 Chunk::Pos pos(to_generate.front());
316 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
317 if (iter->Position() == pos) {
318 loaded.splice(loaded.end(), to_free, iter);
325 if (to_free.empty()) {
326 loaded.emplace_back(reg);
328 loaded.splice(loaded.end(), to_free, to_free.begin());
331 loaded.back().Position(pos);
334 to_generate.pop_front();
337 if (!reused && !to_free.empty()) {