]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
don't add obstructed blocks to meshes
[blank.git] / src / chunk.cpp
1 #include "chunk.hpp"
2
3 #include "generator.hpp"
4
5 #include <limits>
6 #include <glm/gtx/transform.hpp>
7
8
9 namespace {
10
11 blank::Model::Buffer buf;
12
13 }
14
15 namespace blank {
16
17 Chunk::Chunk(const BlockTypeRegistry &types)
18 : types(&types)
19 , blocks()
20 , model()
21 , position(0, 0, 0)
22 , dirty(false) {
23
24 }
25
26 Chunk::Chunk(Chunk &&other)
27 : types(other.types)
28 , blocks(std::move(other.blocks))
29 , model(std::move(other.model))
30 , dirty(other.dirty) {
31
32 }
33
34 Chunk &Chunk::operator =(Chunk &&other) {
35         types = other.types;
36         blocks = std::move(other.blocks);
37         model = std::move(other.model);
38         dirty = other.dirty;
39         return *this;
40 }
41
42
43 void Chunk::Allocate() {
44         blocks.resize(Size());
45 }
46
47
48 void Chunk::Draw() {
49         if (dirty) {
50                 Update();
51         }
52         model.Draw();
53 }
54
55
56 bool Chunk::Intersection(
57         const Ray &ray,
58         const glm::mat4 &M,
59         int *blkid,
60         float *dist,
61         glm::vec3 *normal) const {
62         { // rough check
63                 if (!blank::Intersection(ray, Bounds(), M)) {
64                         return false;
65                 }
66         }
67
68         if (!blkid && !dist && !normal) {
69                 return true;
70         }
71
72         // TODO: should be possible to heavily optimize this
73         int id = 0;
74         int closest_id = -1;
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) {
81                                         continue;
82                                 }
83                                 float cur_dist;
84                                 glm::vec3 cur_norm;
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) {
88                                                 closest_id = id;
89                                                 closest_dist = cur_dist;
90                                                 closest_normal = cur_norm;
91                                         }
92                                 }
93                         }
94                 }
95         }
96
97         if (closest_id < 0) {
98                 return false;
99         }
100
101         if (blkid) {
102                 *blkid = closest_id;
103         }
104         if (dist) {
105                 *dist = closest_dist;
106         }
107         if (normal) {
108                 *normal = closest_normal;
109         }
110         return true;
111 }
112
113 void Chunk::Position(const Pos &pos) {
114         position = pos;
115 }
116
117 glm::mat4 Chunk::Transform(const Pos &offset) const {
118         return glm::translate((position - offset) * Extent());
119 }
120
121
122 void Chunk::CheckUpdate() {
123         if (dirty) {
124                 Update();
125         }
126 }
127
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();
134         }
135         buf.Clear();
136         buf.Reserve(vtx_count, idx_count);
137
138         Model::Index vtx_counter = 0;
139         for (size_t i = 0; i < Size(); ++i) {
140                 if (Obstructed(i)) continue;
141
142                 const BlockType &type = Type(blocks[i]);
143                 type.FillModel(buf, ToCoords(i), vtx_counter);
144                 vtx_counter += type.shape->VertexCount();
145         }
146
147         model.Update(buf);
148         dirty = false;
149 }
150
151 bool Chunk::Obstructed(int idx) const {
152         if (IsBorder(idx)) return false;
153
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)
157
158         const BlockType &right = Type(blocks[idx + 1]);
159         if (!right.fill.left) return false;
160
161         const BlockType &left = Type(blocks[idx - 1]);
162         if (!left.fill.right) return false;
163
164         const BlockType &top = Type(blocks[idx + Width()]);
165         if (!top.fill.bottom) return false;
166
167         const BlockType &bottom = Type(blocks[idx - Width()]);
168         if (!bottom.fill.top) return false;
169
170         const BlockType &front = Type(blocks[idx + Width() * Height()]);
171         if (!front.fill.back) return false;
172
173         const BlockType &back = Type(blocks[idx - Width() * Height()]);
174         if (!back.fill.front) return false;
175
176         return true;
177 }
178
179
180 ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
181 : base(0, 0, 0)
182 , reg(reg)
183 , gen(gen)
184 , loaded()
185 , to_generate()
186 , to_free()
187 , load_dist(4)
188 , unload_dist(5) {
189
190 }
191
192 namespace {
193
194 struct ChunkLess {
195
196         explicit ChunkLess(const Chunk::Pos &base)
197         : base(base) { }
198
199         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
200                 Chunk::Pos da(base - a);
201                 Chunk::Pos db(base - b);
202                 return
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;
205         }
206
207         Chunk::Pos base;
208
209 };
210
211 }
212
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);
218                                 if (Known(pos)) {
219                                         continue;
220                                 } else if (x == 0 && y == 0 && z == 0) {
221                                         loaded.emplace_back(reg);
222                                         loaded.back().Position(pos);
223                                         gen(loaded.back());
224                                 } else {
225                                         to_generate.emplace_back(pos);
226                                 }
227                         }
228                 }
229         }
230         to_generate.sort(ChunkLess(base));
231 }
232
233 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
234         for (Chunk &chunk : loaded) {
235                 if (chunk.Position() == pos) {
236                         return &chunk;
237                 }
238         }
239         return nullptr;
240 }
241
242 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
243         for (const Chunk::Pos &chunk : to_generate) {
244                 if (chunk == pos) {
245                         return true;
246                 }
247         }
248         return nullptr;
249 }
250
251 bool ChunkLoader::Known(const Chunk::Pos &pos) {
252         if (Loaded(pos)) return true;
253         return Queued(pos);
254 }
255
256 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
257         Chunk *chunk = Loaded(pos);
258         if (chunk) {
259                 return *chunk;
260         }
261
262         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
263                 if (*iter == pos) {
264                         to_generate.erase(iter);
265                         break;
266                 }
267         }
268
269         loaded.emplace_back(reg);
270         loaded.back().Position(pos);
271         gen(loaded.back());
272         return loaded.back();
273 }
274
275 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
276         if (new_base == base) {
277                 return;
278         }
279         base = new_base;
280
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) {
286                         auto saved = iter;
287                         ++iter;
288                         to_free.splice(to_free.end(), loaded, saved);
289                 } else {
290                         ++iter;
291                 }
292         }
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);
299                 } else {
300                         ++iter;
301                 }
302         }
303         // add missing new chunks
304         const Chunk::Pos offset(load_dist, load_dist, load_dist);
305         Generate(base - offset, base + offset);
306 }
307
308 void ChunkLoader::Update() {
309         bool reused = false;
310         if (!to_generate.empty()) {
311                 Chunk::Pos pos(to_generate.front());
312
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);
316                                 reused = true;
317                                 break;
318                         }
319                 }
320
321                 if (!reused) {
322                         if (to_free.empty()) {
323                                 loaded.emplace_back(reg);
324                         } else {
325                                 loaded.splice(loaded.end(), to_free, to_free.begin());
326                                 reused = true;
327                         }
328                         loaded.back().Position(pos);
329                         gen(loaded.back());
330                 }
331                 to_generate.pop_front();
332         }
333
334         if (!reused && !to_free.empty()) {
335                 to_free.pop_front();
336         }
337 }
338
339 }