]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
move outline definition to block type
[blank.git] / src / world.cpp
1 #include "world.hpp"
2
3 #include <limits>
4 #include <glm/gtx/transform.hpp>
5
6
7 namespace blank {
8
9 const BlockType BlockType::DEFAULT;
10
11 void BlockType::FillVBO(
12         const glm::vec3 &pos,
13         std::vector<glm::vec3> &vertices,
14         std::vector<glm::vec3> &colors,
15         std::vector<glm::vec3> &normals
16 ) const {
17         vertices.emplace_back(pos.x    , pos.y    , pos.z + 1); // front
18         vertices.emplace_back(pos.x + 1, pos.y    , pos.z + 1);
19         vertices.emplace_back(pos.x    , pos.y + 1, pos.z + 1);
20         vertices.emplace_back(pos.x + 1, pos.y    , pos.z + 1);
21         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
22         vertices.emplace_back(pos.x    , pos.y + 1, pos.z + 1);
23         vertices.emplace_back(pos.x    , pos.y    , pos.z    ); // back
24         vertices.emplace_back(pos.x    , pos.y + 1, pos.z    );
25         vertices.emplace_back(pos.x + 1, pos.y    , pos.z    );
26         vertices.emplace_back(pos.x + 1, pos.y    , pos.z    );
27         vertices.emplace_back(pos.x    , pos.y + 1, pos.z    );
28         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z    );
29         vertices.emplace_back(pos.x    , pos.y + 1, pos.z    ); // top
30         vertices.emplace_back(pos.x    , pos.y + 1, pos.z + 1);
31         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z    );
32         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z    );
33         vertices.emplace_back(pos.x    , pos.y + 1, pos.z + 1);
34         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
35         vertices.emplace_back(pos.x    , pos.y    , pos.z    ); // bottom
36         vertices.emplace_back(pos.x + 1, pos.y    , pos.z    );
37         vertices.emplace_back(pos.x    , pos.y    , pos.z + 1);
38         vertices.emplace_back(pos.x + 1, pos.y    , pos.z    );
39         vertices.emplace_back(pos.x + 1, pos.y    , pos.z + 1);
40         vertices.emplace_back(pos.x    , pos.y    , pos.z + 1);
41         vertices.emplace_back(pos.x    , pos.y    , pos.z    ); // left
42         vertices.emplace_back(pos.x    , pos.y    , pos.z + 1);
43         vertices.emplace_back(pos.x    , pos.y + 1, pos.z    );
44         vertices.emplace_back(pos.x    , pos.y + 1, pos.z    );
45         vertices.emplace_back(pos.x    , pos.y    , pos.z + 1);
46         vertices.emplace_back(pos.x    , pos.y + 1, pos.z + 1);
47         vertices.emplace_back(pos.x + 1, pos.y    , pos.z    ); // right
48         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z    );
49         vertices.emplace_back(pos.x + 1, pos.y    , pos.z + 1);
50         vertices.emplace_back(pos.x + 1, pos.y    , pos.z + 1);
51         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z    );
52         vertices.emplace_back(pos.x + 1, pos.y + 1, pos.z + 1);
53
54         colors.insert(colors.end(), 6 * 6, color);
55
56         normals.insert(normals.end(), 6, glm::vec3( 0.0f,  0.0f,  1.0f)); // front
57         normals.insert(normals.end(), 6, glm::vec3( 0.0f,  0.0f, -1.0f)); // back
58         normals.insert(normals.end(), 6, glm::vec3( 0.0f,  1.0f,  0.0f)); // top
59         normals.insert(normals.end(), 6, glm::vec3( 0.0f, -1.0f,  0.0f)); // bottom
60         normals.insert(normals.end(), 6, glm::vec3(-1.0f,  0.0f,  0.0f)); // left
61         normals.insert(normals.end(), 6, glm::vec3( 1.0f,  0.0f,  0.0f)); // right
62 }
63
64 void BlockType::FillOutlineVBO(
65         std::vector<glm::vec3> &vertices,
66         std::vector<glm::vec3> &colors
67 ) const {
68         vertices = std::vector<glm::vec3>({
69                 { 0.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f },
70                 { 1.0f, 0.0f, 0.0f }, { 1.0f, 1.0f, 0.0f },
71                 { 1.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 0.0f },
72                 { 0.0f, 1.0f, 0.0f }, { 0.0f, 0.0f, 0.0f },
73                 { 0.0f, 0.0f, 0.0f }, { 0.0f, 0.0f, 1.0f },
74                 { 1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 1.0f },
75                 { 1.0f, 1.0f, 0.0f }, { 1.0f, 1.0f, 1.0f },
76                 { 0.0f, 1.0f, 0.0f }, { 0.0f, 1.0f, 1.0f },
77                 { 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 1.0f },
78                 { 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f },
79                 { 1.0f, 1.0f, 1.0f }, { 0.0f, 1.0f, 1.0f },
80                 { 0.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 1.0f },
81         });
82         colors.resize(24, outline_color);
83 }
84
85
86 BlockTypeRegistry::BlockTypeRegistry() {
87         Add(BlockType::DEFAULT);
88 }
89
90 int BlockTypeRegistry::Add(const BlockType &t) {
91         int id = types.size();
92         types.push_back(t);
93         types.back().id = id;
94         return id;
95 }
96
97
98 Chunk::Chunk()
99 : blocks(Size())
100 , model()
101 , transform(1.0f)
102 , dirty(false) {
103
104 }
105
106 Chunk::Chunk(Chunk &&other)
107 : blocks(std::move(other.blocks))
108 , model(std::move(other.model))
109 , transform(other.transform)
110 , dirty(other.dirty) {
111
112 }
113
114 Chunk &Chunk::operator =(Chunk &&other) {
115         blocks = std::move(other.blocks);
116         model = std::move(other.model);
117         transform = other.transform;
118         dirty = other.dirty;
119         return *this;
120 }
121
122
123 void Chunk::Draw() {
124         if (dirty) {
125                 Update();
126         }
127         model.Draw();
128 }
129
130
131 bool Chunk::Intersection(
132         const Ray &ray,
133         const glm::mat4 &M,
134         int *blkid,
135         float *dist,
136         glm::vec3 *normal) const {
137         { // rough check
138                 const AABB bb{{0, 0, 0}, {Width(), Height(), Depth()}};
139                 if (!blank::Intersection(ray, bb, M)) {
140                         return false;
141                 }
142         }
143
144         if (!blkid && !dist && !normal) {
145                 return true;
146         }
147
148         // TODO: should be possible to heavily optimize this
149         int id = 0;
150         int closest_id = -1;
151         float closest_dist = std::numeric_limits<float>::infinity();
152         glm::vec3 closest_normal(0, 1, 0);
153         for (int z = 0; z < Depth(); ++z) {
154                 for (int y = 0; y < Height(); ++y) {
155                         for (int x = 0; x < Width(); ++x, ++id) {
156                                 if (!blocks[id].type->visible) {
157                                         continue;
158                                 }
159                                 const AABB bb{{x, y, z}, {x+1, y+1, z+1}};
160                                 float cur_dist;
161                                 glm::vec3 cur_norm;
162                                 if (blank::Intersection(ray, bb, M, &cur_dist, &cur_norm)) {
163                                         if (cur_dist < closest_dist) {
164                                                 closest_id = id;
165                                                 closest_dist = cur_dist;
166                                                 closest_normal = cur_norm;
167                                         }
168                                 }
169                         }
170                 }
171         }
172
173         if (closest_id < 0) {
174                 return false;
175         }
176
177         if (blkid) {
178                 *blkid = closest_id;
179         }
180         if (dist) {
181                 *dist = closest_dist;
182         }
183         if (normal) {
184                 *normal = closest_normal;
185         }
186         return true;
187 }
188
189 void Chunk::Position(const glm::vec3 &pos) {
190         position = pos;
191         transform = glm::translate(pos * Extent());
192 }
193
194
195 int Chunk::VertexCount() const {
196         // TODO: query blocks as soon as type shapes are implemented
197         return Size() * 6 * 6;
198 }
199
200 void Chunk::Update() {
201         model.Clear();
202         model.Reserve(VertexCount());
203
204         for (int i = 0; i < Size(); ++i) {
205                 if (blocks[i].type->visible) {
206                         blocks[i].type->FillModel(ToCoords(i), model);
207                 }
208         }
209
210         model.Invalidate();
211         dirty = false;
212 }
213
214
215 World::World()
216 : blockType()
217 , chunks() {
218         blockType.Add(BlockType(true, glm::vec3(1, 1, 1)));
219         blockType.Add(BlockType(true, glm::vec3(1, 0, 0)));
220         blockType.Add(BlockType(true, glm::vec3(0, 1, 0)));
221         blockType.Add(BlockType(true, glm::vec3(0, 0, 1)));
222 }
223
224
225 void World::Generate() {
226         for (int z = -1; z < 2; ++z) {
227                 for (int y = -1; y < 2; ++y) {
228                         for (int x = -1; x < 2; ++x) {
229                                 Generate(glm::vec3(x, y, z));
230                         }
231                 }
232         }
233 }
234
235 Chunk &World::Generate(const glm::vec3 &pos) {
236         chunks.emplace_back();
237         Chunk &chunk = chunks.back();
238         chunk.Position(pos);
239         chunk.BlockAt(glm::vec3(0, 0, 0)) = Block(blockType[4]);
240         chunk.BlockAt(glm::vec3(0, 0, 1)) = Block(blockType[1]);
241         chunk.BlockAt(glm::vec3(1, 0, 0)) = Block(blockType[2]);
242         chunk.BlockAt(glm::vec3(1, 0, 1)) = Block(blockType[3]);
243         chunk.BlockAt(glm::vec3(2, 0, 0)) = Block(blockType[4]);
244         chunk.BlockAt(glm::vec3(2, 0, 1)) = Block(blockType[1]);
245         chunk.BlockAt(glm::vec3(3, 0, 0)) = Block(blockType[2]);
246         chunk.BlockAt(glm::vec3(3, 0, 1)) = Block(blockType[3]);
247         chunk.BlockAt(glm::vec3(2, 0, 2)) = Block(blockType[4]);
248         chunk.BlockAt(glm::vec3(2, 0, 3)) = Block(blockType[1]);
249         chunk.BlockAt(glm::vec3(3, 0, 2)) = Block(blockType[2]);
250         chunk.BlockAt(glm::vec3(3, 0, 3)) = Block(blockType[3]);
251         chunk.BlockAt(glm::vec3(1, 1, 0)) = Block(blockType[1]);
252         chunk.BlockAt(glm::vec3(1, 1, 1)) = Block(blockType[4]);
253         chunk.BlockAt(glm::vec3(2, 1, 1)) = Block(blockType[3]);
254         chunk.BlockAt(glm::vec3(2, 2, 1)) = Block(blockType[2]);
255         chunk.Invalidate();
256         return chunk;
257 }
258
259 bool World::Intersection(
260                 const Ray &ray,
261                 const glm::mat4 &M,
262                 Chunk **chunk,
263                 int *blkid,
264                 float *dist,
265                 glm::vec3 *normal) {
266         Chunk *closest_chunk = nullptr;
267         int closest_blkid = -1;
268         float closest_dist = std::numeric_limits<float>::infinity();
269         glm::vec3 closest_normal;
270
271         for (Chunk &cur_chunk : chunks) {
272                 int cur_blkid;
273                 float cur_dist;
274                 glm::vec3 cur_normal;
275                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(), &cur_blkid, &cur_dist, &cur_normal)) {
276                         if (cur_dist < closest_dist) {
277                                 closest_chunk = &cur_chunk;
278                                 closest_blkid = cur_blkid;
279                                 closest_dist = cur_dist;
280                                 closest_normal = cur_normal;
281                         }
282                 }
283         }
284
285         if (chunk) {
286                 *chunk = closest_chunk;
287         }
288         if (blkid) {
289                 *blkid = closest_blkid;
290         }
291         if (dist) {
292                 *dist = closest_dist;
293         }
294         if (normal) {
295                 *normal = closest_normal;
296         }
297         return closest_chunk;
298 }
299
300
301 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
302         const glm::vec3 tgt_pos = to.Position() + dir;
303         for (Chunk &chunk : chunks) {
304                 if (chunk.Position() == tgt_pos) {
305                         return chunk;
306                 }
307         }
308         return Generate(tgt_pos);
309 }
310
311 }