]> git.localhorst.tv Git - blank.git/blob - src/chunk.cpp
fixed oriented block occlusion check
[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
62 ) const {
63         // TODO: should be possible to heavily optimize this
64         int id = 0;
65         blkid = -1;
66         dist = std::numeric_limits<float>::infinity();
67         for (int z = 0; z < Depth(); ++z) {
68                 for (int y = 0; y < Height(); ++y) {
69                         for (int x = 0; x < Width(); ++x, ++id) {
70                                 if (!Type(blocks[id]).visible) {
71                                         continue;
72                                 }
73                                 float cur_dist;
74                                 glm::vec3 cur_norm;
75                                 if (Type(blocks[id]).shape->Intersects(ray, M * ToTransform(id), cur_dist, cur_norm)) {
76                                         if (cur_dist < dist) {
77                                                 blkid = id;
78                                                 dist = cur_dist;
79                                                 normal = cur_norm;
80                                         }
81                                 }
82                         }
83                 }
84         }
85
86         if (blkid < 0) {
87                 return false;
88         } else {
89                 normal = glm::vec3(BlockAt(blkid).Transform() * glm::vec4(normal, 0.0f));
90                 return true;
91         }
92 }
93
94 void Chunk::Position(const Pos &pos) {
95         position = pos;
96 }
97
98 glm::mat4 Chunk::Transform(const Pos &offset) const {
99         return glm::translate((position - offset) * Extent());
100 }
101
102
103 void Chunk::CheckUpdate() {
104         if (dirty) {
105                 Update();
106         }
107 }
108
109 void Chunk::Update() {
110         int vtx_count = 0, idx_count = 0;
111         for (const auto &block : blocks) {
112                 const Shape *shape = Type(block).shape;
113                 vtx_count += shape->VertexCount();
114                 idx_count += shape->VertexIndexCount();
115         }
116         buf.Clear();
117         buf.Reserve(vtx_count, idx_count);
118
119         Model::Index vtx_counter = 0;
120         for (size_t i = 0; i < Size(); ++i) {
121                 if (Obstructed(i)) continue;
122
123                 const BlockType &type = Type(blocks[i]);
124                 type.FillModel(buf, ToTransform(i), vtx_counter);
125                 vtx_counter += type.shape->VertexCount();
126         }
127
128         model.Update(buf);
129         dirty = false;
130 }
131
132 bool Chunk::Obstructed(int idx) const {
133         if (IsBorder(idx)) return false;
134
135         // not checking neighbor visibility here, so all
136         // invisible blocks must have their fill set to 6x false
137         // (the default, so should be okay)
138
139         const Block &right = blocks[idx + 1];
140         if (!Type(right).FaceFilled(right, Block::FACE_LEFT)) return false;
141
142         const Block &left = blocks[idx - 1];
143         if (!Type(left).FaceFilled(left, Block::FACE_RIGHT)) return false;
144
145         const Block &up = blocks[idx + Width()];
146         if (!Type(up).FaceFilled(up, Block::FACE_DOWN)) return false;
147
148         const Block &down = blocks[idx - Width()];
149         if (!Type(down).FaceFilled(down, Block::FACE_UP)) return false;
150
151         const Block &front = blocks[idx + Width() * Height()];
152         if (!Type(front).FaceFilled(front, Block::FACE_BACK)) return false;
153
154         const Block &back = blocks[idx - Width() * Height()];
155         if (!Type(back).FaceFilled(back, Block::FACE_FRONT)) return false;
156
157         return true;
158 }
159
160 glm::mat4 Chunk::ToTransform(int idx) const {
161         return glm::translate(glm::mat4(1.0f), ToCoords(idx)) * blocks[idx].Transform();
162 }
163
164
165 ChunkLoader::ChunkLoader(const BlockTypeRegistry &reg, const Generator &gen)
166 : base(0, 0, 0)
167 , reg(reg)
168 , gen(gen)
169 , loaded()
170 , to_generate()
171 , to_free()
172 , load_dist(4)
173 , unload_dist(5) {
174
175 }
176
177 namespace {
178
179 struct ChunkLess {
180
181         explicit ChunkLess(const Chunk::Pos &base)
182         : base(base) { }
183
184         bool operator ()(const Chunk::Pos &a, const Chunk::Pos &b) const {
185                 Chunk::Pos da(base - a);
186                 Chunk::Pos db(base - b);
187                 return
188                         da.x * da.x + da.y * da.y + da.z * da.z <
189                         db.x * db.x + db.y * db.y + db.z * db.z;
190         }
191
192         Chunk::Pos base;
193
194 };
195
196 }
197
198 void ChunkLoader::Generate(const Chunk::Pos &from, const Chunk::Pos &to) {
199         for (int z = from.z; z < to.z; ++z) {
200                 for (int y = from.y; y < to.y; ++y) {
201                         for (int x = from.x; x < to.x; ++x) {
202                                 Chunk::Pos pos(x, y, z);
203                                 if (Known(pos)) {
204                                         continue;
205                                 } else if (x == 0 && y == 0 && z == 0) {
206                                         loaded.emplace_back(reg);
207                                         loaded.back().Position(pos);
208                                         gen(loaded.back());
209
210                                 //      orientation testing
211                                 //      for (int i = 0; i < Block::FACE_COUNT; ++i) {
212                                 //              for (int j = 0; j < Block::TURN_COUNT; ++j) {
213                                 //                      loaded.back().BlockAt(512 * j + 2 * i) = Block(3 * (j + 1), Block::Face(i), Block::Turn(j));
214                                 //              }
215                                 //      }
216                                 //      loaded.back().Invalidate();
217                                 //      loaded.back().CheckUpdate();
218                                 } else {
219                                         to_generate.emplace_back(pos);
220                                 }
221                         }
222                 }
223         }
224         to_generate.sort(ChunkLess(base));
225 }
226
227 Chunk *ChunkLoader::Loaded(const Chunk::Pos &pos) {
228         for (Chunk &chunk : loaded) {
229                 if (chunk.Position() == pos) {
230                         return &chunk;
231                 }
232         }
233         return nullptr;
234 }
235
236 bool ChunkLoader::Queued(const Chunk::Pos &pos) {
237         for (const Chunk::Pos &chunk : to_generate) {
238                 if (chunk == pos) {
239                         return true;
240                 }
241         }
242         return nullptr;
243 }
244
245 bool ChunkLoader::Known(const Chunk::Pos &pos) {
246         if (Loaded(pos)) return true;
247         return Queued(pos);
248 }
249
250 Chunk &ChunkLoader::ForceLoad(const Chunk::Pos &pos) {
251         Chunk *chunk = Loaded(pos);
252         if (chunk) {
253                 return *chunk;
254         }
255
256         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end; ++iter) {
257                 if (*iter == pos) {
258                         to_generate.erase(iter);
259                         break;
260                 }
261         }
262
263         loaded.emplace_back(reg);
264         loaded.back().Position(pos);
265         gen(loaded.back());
266         return loaded.back();
267 }
268
269 void ChunkLoader::Rebase(const Chunk::Pos &new_base) {
270         if (new_base == base) {
271                 return;
272         }
273         base = new_base;
274
275         // unload far away chunks
276         for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
277                 if (std::abs(base.x - iter->Position().x) > unload_dist
278                                 || std::abs(base.y - iter->Position().y) > unload_dist
279                                 || std::abs(base.z - iter->Position().z) > unload_dist) {
280                         auto saved = iter;
281                         ++iter;
282                         to_free.splice(to_free.end(), loaded, saved);
283                 } else {
284                         ++iter;
285                 }
286         }
287         // abort far away queued chunks
288         for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
289                 if (std::abs(base.x - iter->x) > unload_dist
290                                 || std::abs(base.y - iter->y) > unload_dist
291                                 || std::abs(base.z - iter->z) > unload_dist) {
292                         iter = to_generate.erase(iter);
293                 } else {
294                         ++iter;
295                 }
296         }
297         // add missing new chunks
298         const Chunk::Pos offset(load_dist, load_dist, load_dist);
299         Generate(base - offset, base + offset);
300 }
301
302 void ChunkLoader::Update() {
303         bool reused = false;
304         if (!to_generate.empty()) {
305                 Chunk::Pos pos(to_generate.front());
306
307                 for (auto iter(to_free.begin()), end(to_free.end()); iter != end; ++iter) {
308                         if (iter->Position() == pos) {
309                                 loaded.splice(loaded.end(), to_free, iter);
310                                 reused = true;
311                                 break;
312                         }
313                 }
314
315                 if (!reused) {
316                         if (to_free.empty()) {
317                                 loaded.emplace_back(reg);
318                         } else {
319                                 loaded.splice(loaded.end(), to_free, to_free.begin());
320                                 reused = true;
321                         }
322                         loaded.back().Position(pos);
323                         gen(loaded.back());
324                 }
325                 to_generate.pop_front();
326         }
327
328         if (!reused && !to_free.empty()) {
329                 to_free.pop_front();
330         }
331 }
332
333 }