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