]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
fix normal generated by chunk intersection test
[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                                 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
86                                         if (cur_dist < closest_dist) {
87                                                 closest_id = id;
88                                                 closest_dist = cur_dist;
89                                                 closest_normal = cur_norm;
90                                         }
91                                 }
92                         }
93                 }
94         }
95
96         if (closest_id < 0) {
97                 return false;
98         }
99
100         if (blkid) {
101                 *blkid = closest_id;
102         }
103         if (dist) {
104                 *dist = closest_dist;
105         }
106         if (normal) {
107                 *normal = glm::vec3(BlockAt(closest_id).Transform() * glm::vec4(closest_normal, 0.0f));
108         }
109         return true;
110 }
111
112 void Chunk::Position(const Pos &pos) {
113         position = pos;
114 }
115
116 glm::mat4 Chunk::Transform(const Pos &offset) const {
117         return glm::translate((position - offset) * Extent());
118 }
119
120
121 void Chunk::CheckUpdate() {
122         if (dirty) {
123                 Update();
124         }
125 }
126
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();
133         }
134         buf.Clear();
135         buf.Reserve(vtx_count, idx_count);
136
137         Model::Index vtx_counter = 0;
138         for (size_t i = 0; i < Size(); ++i) {
139                 if (Obstructed(i)) continue;
140
141                 const BlockType &type = Type(blocks[i]);
142                 type.FillModel(buf, ToTransform(i), vtx_counter);
143                 vtx_counter += type.shape->VertexCount();
144         }
145
146         model.Update(buf);
147         dirty = false;
148 }
149
150 bool Chunk::Obstructed(int idx) const {
151         if (IsBorder(idx)) return false;
152
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)
156
157         const BlockType &right = Type(blocks[idx + 1]);
158         if (!right.fill.left) return false;
159
160         const BlockType &left = Type(blocks[idx - 1]);
161         if (!left.fill.right) return false;
162
163         const BlockType &up = Type(blocks[idx + Width()]);
164         if (!up.fill.down) return false;
165
166         const BlockType &down = Type(blocks[idx - Width()]);
167         if (!down.fill.up) return false;
168
169         const BlockType &front = Type(blocks[idx + Width() * Height()]);
170         if (!front.fill.back) return false;
171
172         const BlockType &back = Type(blocks[idx - Width() * Height()]);
173         if (!back.fill.front) return false;
174
175         return true;
176 }
177
178 glm::mat4 Chunk::ToTransform(int idx) const {
179         return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
180 }
181
182
183 ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
184 : base(0, 0, 0)
185 , reg(reg)
186 , gen(gen)
187 , loaded()
188 , to_generate()
189 , to_free()
190 , load_dist(4)
191 , unload_dist(5) {
192
193 }
194
195 namespace {
196
197 struct ChunkLess {
198
199         explicit ChunkLess(const Chunk::Pos &base)
200         : base(base) { }
201
202         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
203                 Chunk::Pos da(base - a);
204                 Chunk::Pos db(base - b);
205                 return
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;
208         }
209
210         Chunk::Pos base;
211
212 };
213
214 }
215
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);
221                                 if (Known(pos)) {
222                                         continue;
223                                 } else if (x == 0 && y == 0 && z == 0) {
224                                         loaded.emplace_back(reg);
225                                         loaded.back().Position(pos);
226                                         gen(loaded.back());
227
228                                 //      orientation testing
229                                 //      for (int i = 0; i < Block::FACE_COUNT; ++i) {
230                                 //              for (int j = 0; j < Block::TURN_COUNT; ++j) {
231                                 //                      loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
232                                 //              }
233                                 //      }
234                                 //      loaded.back().Invalidate();
235                                 //      loaded.back().CheckUpdate();
236                                 } else {
237                                         to_generate.emplace_back(pos);
238                                 }
239                         }
240                 }
241         }
242         to_generate.sort(ChunkLess(base));
243 }
244
245 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
246         for (Chunk &chunk : loaded) {
247                 if (chunk.Position() == pos) {
248                         return &chunk;
249                 }
250         }
251         return nullptr;
252 }
253
254 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
255         for (const Chunk::Pos &chunk : to_generate) {
256                 if (chunk == pos) {
257                         return true;
258                 }
259         }
260         return nullptr;
261 }
262
263 bool ChunkLoader::Known(const Chunk::Pos &pos) {
264         if (Loaded(pos)) return true;
265         return Queued(pos);
266 }
267
268 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
269         Chunk *chunk = Loaded(pos);
270         if (chunk) {
271                 return *chunk;
272         }
273
274         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
275                 if (*iter == pos) {
276                         to_generate.erase(iter);
277                         break;
278                 }
279         }
280
281         loaded.emplace_back(reg);
282         loaded.back().Position(pos);
283         gen(loaded.back());
284         return loaded.back();
285 }
286
287 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
288         if (new_base == base) {
289                 return;
290         }
291         base = new_base;
292
293         // unload far away chunks
294         for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
295                 if (std::abs(base.x - iter->Position().x) > unload_dist
296                                 || std::abs(base.y - iter->Position().y) > unload_dist
297                                 || std::abs(base.z - iter->Position().z) > unload_dist) {
298                         auto saved = iter;
299                         ++iter;
300                         to_free.splice(to_free.end(), loaded, saved);
301                 } else {
302                         ++iter;
303                 }
304         }
305         // abort far away queued chunks
306         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
307                 if (std::abs(base.x - iter->x) > unload_dist
308                                 || std::abs(base.y - iter->y) > unload_dist
309                                 || std::abs(base.z - iter->z) > unload_dist) {
310                         iter = to_generate.erase(iter);
311                 } else {
312                         ++iter;
313                 }
314         }
315         // add missing new chunks
316         const Chunk::Pos offset(load_dist, load_dist, load_dist);
317         Generate(base - offset, base + offset);
318 }
319
320 void ChunkLoader::Update() {
321         bool reused = false;
322         if (!to_generate.empty()) {
323                 Chunk::Pos pos(to_generate.front());
324
325                 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
326                         if (iter->Position() == pos) {
327                                 loaded.splice(loaded.end(), to_free, iter);
328                                 reused = true;
329                                 break;
330                         }
331                 }
332
333                 if (!reused) {
334                         if (to_free.empty()) {
335                                 loaded.emplace_back(reg);
336                         } else {
337                                 loaded.splice(loaded.end(), to_free, to_free.begin());
338                                 reused = true;
339                         }
340                         loaded.back().Position(pos);
341                         gen(loaded.back());
342                 }
343                 to_generate.pop_front();
344         }
345
346         if (!reused && !to_free.empty()) {
347                 to_free.pop_front();
348         }
349 }
350
351 }