]> git.localhorst.tv Git - blank.git/blob - src/world/World.cpp
set and display block type labels
[blank.git] / src / world / World.cpp
1 #include "World.hpp"
2
3 #include "WorldCollision.hpp"
4 #include "../graphics/BlockLighting.hpp"
5 #include "../graphics/DirectionalLighting.hpp"
6
7 #include <iostream>
8 #include <limits>
9 #include <glm/gtx/io.hpp>
10 #include <glm/gtx/transform.hpp>
11
12
13 namespace blank {
14
15 World::World(const Config &config)
16 : blockType()
17 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
18 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
19 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
20 , generate(config.gen)
21 , chunks(config.load, blockType, generate)
22 , player()
23 , entities()
24 , light_direction(config.light_direction)
25 , fog_density(config.fog_density) {
26         BlockType::Faces block_fill = {  true,  true,  true,  true,  true,  true };
27         BlockType::Faces slab_fill  = { false,  true, false, false, false, false };
28         BlockType::Faces stair_fill = { false,  true, false, false, false,  true };
29
30         { // white block
31                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &blockShape);
32                 type.label = "White Block";
33                 type.block_light = true;
34                 type.collision = true;
35                 type.collide_block = true;
36                 type.fill = block_fill;
37                 blockType.Add(type);
38         }
39         { // white slab
40                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &slabShape);
41                 type.label = "White Slab";
42                 type.block_light = true;
43                 type.collision = true;
44                 type.collide_block = true;
45                 type.fill = slab_fill;
46                 blockType.Add(type);
47         }
48         { // white stair
49                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
50                 type.label = "White Stair";
51                 type.block_light = true;
52                 type.collision = true;
53                 type.collide_block = true;
54                 type.fill = stair_fill;
55                 blockType.Add(type);
56         }
57
58         { // red block
59                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
60                 type.label = "Red Block";
61                 type.block_light = true;
62                 type.collision = true;
63                 type.collide_block = true;
64                 type.fill = block_fill;
65                 blockType.Add(type);
66         }
67         { // red slab
68                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
69                 type.label = "Red Slab";
70                 type.block_light = true;
71                 type.collision = true;
72                 type.collide_block = true;
73                 type.fill = slab_fill;
74                 blockType.Add(type);
75         }
76         { // red stair
77                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
78                 type.label = "Red Stair";
79                 type.block_light = true;
80                 type.collision = true;
81                 type.collide_block = true;
82                 type.fill = stair_fill;
83                 blockType.Add(type);
84         }
85
86         { // green block
87                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
88                 type.label = "Green Block";
89                 type.block_light = true;
90                 type.collision = true;
91                 type.collide_block = true;
92                 type.fill = block_fill;
93                 blockType.Add(type);
94         }
95         { // green slab
96                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
97                 type.label = "Green Slab";
98                 type.block_light = true;
99                 type.collision = true;
100                 type.collide_block = true;
101                 type.fill = slab_fill;
102                 blockType.Add(type);
103         }
104         { // green stair
105                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
106                 type.label = "Green Stair";
107                 type.block_light = true;
108                 type.collision = true;
109                 type.collide_block = true;
110                 type.fill = stair_fill;
111                 blockType.Add(type);
112         }
113
114         { // blue block
115                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
116                 type.label = "Blue Block";
117                 type.block_light = true;
118                 type.collision = true;
119                 type.collide_block = true;
120                 type.fill = block_fill;
121                 blockType.Add(type);
122         }
123         { // blue slab
124                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
125                 type.label = "Blue Slab";
126                 type.block_light = true;
127                 type.collision = true;
128                 type.collide_block = true;
129                 type.fill = slab_fill;
130                 blockType.Add(type);
131         }
132         { // blue stair
133                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
134                 type.label = "Blue Stair";
135                 type.block_light = true;
136                 type.collision = true;
137                 type.collide_block = true;
138                 type.fill = stair_fill;
139                 blockType.Add(type);
140         }
141
142         { // glowing yellow block
143                 BlockType type(true, { 1.0f, 1.0f, 0.0f }, &blockShape);
144                 type.label = "Light";
145                 type.luminosity = 15;
146                 type.block_light = true;
147                 type.collision = true;
148                 type.collide_block = true;
149                 type.fill = block_fill;
150                 blockType.Add(type);
151         }
152
153         generate.Space(0);
154         generate.Light(13);
155         generate.Solids({ 1, 4, 7, 10 });
156
157         player = &AddEntity();
158         player->Name("player");
159         player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
160         player->WorldCollidable(true);
161         player->Position(config.spawn);
162
163         chunks.GenerateSurrounding(player->ChunkCoords());
164 }
165
166
167 namespace {
168
169 struct Candidate {
170         Chunk *chunk;
171         float dist;
172 };
173
174 std::vector<Candidate> candidates;
175
176 }
177
178 bool World::Intersection(
179         const Ray &ray,
180         const glm::mat4 &M,
181         Chunk *&chunk,
182         int &blkid,
183         float &dist,
184         glm::vec3 &normal
185 ) {
186         candidates.clear();
187
188         for (Chunk &cur_chunk : chunks.Loaded()) {
189                 float cur_dist;
190                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
191                         candidates.push_back({ &cur_chunk, cur_dist });
192                 }
193         }
194
195         if (candidates.empty()) return false;
196
197         chunk = nullptr;
198         dist = std::numeric_limits<float>::infinity();
199         blkid = -1;
200
201         for (Candidate &cand : candidates) {
202                 if (cand.dist > dist) continue;
203                 int cur_blkid;
204                 float cur_dist;
205                 glm::vec3 cur_normal;
206                 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
207                         if (cur_dist < dist) {
208                                 chunk = cand.chunk;
209                                 blkid = cur_blkid;
210                                 dist = cur_dist;
211                                 normal = cur_normal;
212                         }
213                 }
214         }
215
216         return chunk;
217 }
218
219 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
220         AABB box = e.Bounds();
221         glm::mat4 M = e.Transform(player->ChunkCoords());
222         bool any = false;
223         // TODO: this only needs to check the chunks surrounding the entity's chunk position
224         //       need find out if that is quicker than the rough chunk bounds test
225         for (Chunk &cur_chunk : chunks.Loaded()) {
226                 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) {
227                         any = true;
228                 }
229         }
230         return any;
231 }
232
233
234 Chunk &World::PlayerChunk() {
235         return chunks.ForceLoad(player->ChunkCoords());
236 }
237
238 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
239         const Chunk::Pos tgt_pos = to.Position() + dir;
240         return chunks.ForceLoad(tgt_pos);
241 }
242
243
244 namespace {
245
246 std::vector<WorldCollision> col;
247
248 }
249
250 void World::Update(int dt) {
251         for (Entity &entity : entities) {
252                 entity.Update(dt);
253         }
254         for (Entity &entity : entities) {
255                 col.clear();
256                 if (entity.WorldCollidable() && Intersection(entity, col)) {
257                         // entity collides with the world
258                         Resolve(entity, col);
259                 }
260         }
261         chunks.Rebase(player->ChunkCoords());
262         chunks.Update(dt);
263 }
264
265 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
266         // determine displacement for each cardinal axis and move entity accordingly
267         glm::vec3 min_disp(0.0f);
268         glm::vec3 max_disp(0.0f);
269         for (const WorldCollision &c : col) {
270                 if (!c.Blocks()) continue;
271                 glm::vec3 local_disp(c.normal * c.depth);
272                 // swap if neccessary (normal may point away from the entity)
273                 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
274                         local_disp *= -1;
275                 }
276                 min_disp = min(min_disp, local_disp);
277                 max_disp = max(max_disp, local_disp);
278         }
279         // for each axis
280         // if only one direction is set, use that as the final
281         // if both directions are set, use average
282         glm::vec3 final_disp(0.0f);
283         for (int axis = 0; axis < 3; ++axis) {
284                 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
285                         if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
286                                 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
287                         } else {
288                                 final_disp[axis] = min_disp[axis];
289                         }
290                 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
291                         final_disp[axis] = max_disp[axis];
292                 }
293         }
294         e.Move(final_disp);
295 }
296
297
298 void World::Render(BlockLighting &chunk_prog, DirectionalLighting &entity_prog) {
299         chunk_prog.Activate();
300         chunk_prog.SetFogDensity(fog_density);
301         chunk_prog.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
302
303         for (Chunk &chunk : chunks.Loaded()) {
304                 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
305                 chunk_prog.SetM(m);
306                 glm::mat4 mvp(chunk_prog.GetVP() * m);
307                 if (!CullTest(Chunk::Bounds(), mvp)) {
308                         chunk.Draw();
309                 }
310         }
311
312         entity_prog.Activate();
313         entity_prog.SetLightDirection(light_direction);
314         entity_prog.SetFogDensity(fog_density);
315         entity_prog.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
316
317         for (Entity &entity : entities) {
318                 if (entity.HasShape()) {
319                         entity_prog.SetM(entity.Transform(player->ChunkCoords()));
320                         entity.Draw();
321                 }
322         }
323 }
324
325 }