3 #include "WorldCollision.hpp"
4 #include "../graphics/BlockLighting.hpp"
5 #include "../graphics/DirectionalLighting.hpp"
9 #include <glm/gtx/io.hpp>
10 #include <glm/gtx/transform.hpp>
15 World::World(const Config &config)
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)
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 };
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
142 generate.Solids({ 1, 4, 7, 10 });
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);
150 chunks.GenerateSurrounding(player->ChunkCoords());
161 std::vector<Candidate> candidates;
165 bool World::Intersection(
175 for (Chunk &cur_chunk : chunks.Loaded()) {
177 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
178 candidates.push_back({ &cur_chunk, cur_dist });
182 if (candidates.empty()) return false;
185 dist = std::numeric_limits<float>::infinity();
188 for (Candidate &cand : candidates) {
189 if (cand.dist > dist) continue;
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) {
206 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
207 AABB box = e.Bounds();
208 glm::mat4 M = e.Transform(player->ChunkCoords());
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)) {
221 Chunk &World::PlayerChunk() {
222 return chunks.ForceLoad(player->ChunkCoords());
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);
233 std::vector<WorldCollision> col;
237 void World::Update(int dt) {
238 for (Entity &entity : entities) {
241 for (Entity &entity : entities) {
243 if (entity.WorldCollidable() && Intersection(entity, col)) {
244 // entity collides with the world
245 Resolve(entity, col);
248 chunks.Rebase(player->ChunkCoords());
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) {
263 min_disp = min(min_disp, local_disp);
264 max_disp = max(max_disp, local_disp);
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;
275 final_disp[axis] = min_disp[axis];
277 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
278 final_disp[axis] = max_disp[axis];
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())));
290 for (Chunk &chunk : chunks.Loaded()) {
291 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
293 glm::mat4 mvp(chunk_prog.GetVP() * m);
294 if (!CullTest(Chunk::Bounds(), mvp)) {
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())));
304 for (Entity &entity : entities) {
305 if (entity.HasShape()) {
306 entity_prog.SetM(entity.Transform(player->ChunkCoords()));