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