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