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