]> git.localhorst.tv Git - blank.git/blob - src/world/World.cpp
split chunk redering from world model
[blank.git] / src / world / World.cpp
1 #include "World.hpp"
2
3 #include "EntityCollision.hpp"
4 #include "WorldCollision.hpp"
5 #include "../app/Assets.hpp"
6 #include "../graphics/Format.hpp"
7 #include "../graphics/Viewport.hpp"
8
9 #include <limits>
10 #include <glm/gtx/io.hpp>
11 #include <glm/gtx/transform.hpp>
12
13
14 namespace blank {
15
16 World::World(const BlockTypeRegistry &types, const Config &config, const WorldSave &save)
17 : block_type(types)
18 , generate(config.gen)
19 , chunks(config.load, types, generate, save)
20 , player()
21 , entities()
22 , light_direction(config.light_direction)
23 , fog_density(config.fog_density) {
24         generate.Space(0);
25         generate.Light(13);
26         generate.Solids({ 1, 4, 7, 10 });
27
28         player = &AddEntity();
29         player->Name("player");
30         player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
31         player->WorldCollidable(true);
32         player->Position(config.spawn);
33
34         chunks.QueueSurrounding(player->ChunkCoords());
35 }
36
37
38 namespace {
39
40 struct Candidate {
41         Chunk *chunk;
42         float dist;
43 };
44
45 std::vector<Candidate> candidates;
46
47 }
48
49 bool World::Intersection(
50         const Ray &ray,
51         const glm::mat4 &M,
52         const Chunk::Pos &reference,
53         WorldCollision &coll
54 ) {
55         candidates.clear();
56
57         for (Chunk &cur_chunk : chunks.Loaded()) {
58                 float cur_dist;
59                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(reference), cur_dist)) {
60                         candidates.push_back({ &cur_chunk, cur_dist });
61                 }
62         }
63
64         if (candidates.empty()) return false;
65
66         coll.chunk = nullptr;
67         coll.block = -1;
68         coll.depth = std::numeric_limits<float>::infinity();
69
70         for (Candidate &cand : candidates) {
71                 if (cand.dist > coll.depth) continue;
72                 WorldCollision cur_coll;
73                 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(reference), cur_coll)) {
74                         if (cur_coll.depth < coll.depth) {
75                                 coll = cur_coll;
76                         }
77                 }
78         }
79
80         return coll.chunk;
81 }
82
83 bool World::Intersection(
84         const Ray &ray,
85         const glm::mat4 &M,
86         const Entity &reference,
87         EntityCollision &coll
88 ) {
89         coll.entity = nullptr;
90         coll.depth = std::numeric_limits<float>::infinity();
91         for (Entity &cur_entity : entities) {
92                 if (&cur_entity == &reference) {
93                         continue;
94                 }
95                 float cur_dist;
96                 glm::vec3 cur_normal;
97                 if (blank::Intersection(ray, cur_entity.Bounds(), M * cur_entity.Transform(reference.ChunkCoords()), &cur_dist, &cur_normal)) {
98                         // TODO: fine grained check goes here? maybe?
99                         if (cur_dist < coll.depth) {
100                                 coll.entity = &cur_entity;
101                                 coll.depth = cur_dist;
102                                 coll.normal = cur_normal;
103                         }
104                 }
105         }
106
107         return coll.entity;
108 }
109
110 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
111         AABB box = e.Bounds();
112         Chunk::Pos reference = e.ChunkCoords();
113         glm::mat4 M = e.Transform(reference);
114         bool any = false;
115         for (Chunk &cur_chunk : chunks.Loaded()) {
116                 if (manhattan_radius(cur_chunk.Position() - e.ChunkCoords()) > 1) {
117                         // chunk is not one of the 3x3x3 surrounding the entity
118                         // since there's no entity which can extent over 16 blocks, they can be skipped
119                         continue;
120                 }
121                 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(reference), col)) {
122                         any = true;
123                 }
124         }
125         return any;
126 }
127
128
129 Chunk &World::PlayerChunk() {
130         return chunks.ForceLoad(player->ChunkCoords());
131 }
132
133
134 namespace {
135
136 std::vector<WorldCollision> col;
137
138 }
139
140 void World::Update(int dt) {
141         for (Entity &entity : entities) {
142                 entity.Update(dt);
143         }
144         for (Entity &entity : entities) {
145                 col.clear();
146                 if (entity.WorldCollidable() && Intersection(entity, col)) {
147                         // entity collides with the world
148                         Resolve(entity, col);
149                 }
150         }
151         for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
152                 if (iter->CanRemove()) {
153                         iter = entities.erase(iter);
154                 } else {
155                         ++iter;
156                 }
157         }
158         chunks.Rebase(player->ChunkCoords());
159         chunks.Update(dt);
160 }
161
162 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
163         // determine displacement for each cardinal axis and move entity accordingly
164         glm::vec3 min_disp(0.0f);
165         glm::vec3 max_disp(0.0f);
166         for (const WorldCollision &c : col) {
167                 if (!c.Blocks()) continue;
168                 glm::vec3 local_disp(c.normal * c.depth);
169                 // swap if neccessary (normal may point away from the entity)
170                 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
171                         local_disp *= -1;
172                 }
173                 min_disp = min(min_disp, local_disp);
174                 max_disp = max(max_disp, local_disp);
175         }
176         // for each axis
177         // if only one direction is set, use that as the final
178         // if both directions are set, use average
179         glm::vec3 final_disp(0.0f);
180         for (int axis = 0; axis < 3; ++axis) {
181                 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
182                         if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
183                                 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
184                         } else {
185                                 final_disp[axis] = min_disp[axis];
186                         }
187                 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
188                         final_disp[axis] = max_disp[axis];
189                 }
190         }
191         e.Move(final_disp);
192 }
193
194
195 void World::Render(Viewport &viewport) {
196         DirectionalLighting &entity_prog = viewport.EntityProgram();
197         entity_prog.SetLightDirection(light_direction);
198         entity_prog.SetFogDensity(fog_density);
199
200         for (Entity &entity : entities) {
201                 entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);
202         }
203 }
204
205 }