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.label = "White Block";
33 type.block_light = true;
34 type.collision = true;
35 type.collide_block = true;
36 type.fill = block_fill;
40 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &slabShape);
41 type.label = "White Slab";
42 type.block_light = true;
43 type.collision = true;
44 type.collide_block = true;
45 type.fill = slab_fill;
49 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
50 type.label = "White Stair";
51 type.block_light = true;
52 type.collision = true;
53 type.collide_block = true;
54 type.fill = stair_fill;
59 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
60 type.label = "Red Block";
61 type.block_light = true;
62 type.collision = true;
63 type.collide_block = true;
64 type.fill = block_fill;
68 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
69 type.label = "Red Slab";
70 type.block_light = true;
71 type.collision = true;
72 type.collide_block = true;
73 type.fill = slab_fill;
77 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
78 type.label = "Red Stair";
79 type.block_light = true;
80 type.collision = true;
81 type.collide_block = true;
82 type.fill = stair_fill;
87 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
88 type.label = "Green Block";
89 type.block_light = true;
90 type.collision = true;
91 type.collide_block = true;
92 type.fill = block_fill;
96 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
97 type.label = "Green Slab";
98 type.block_light = true;
99 type.collision = true;
100 type.collide_block = true;
101 type.fill = slab_fill;
105 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
106 type.label = "Green Stair";
107 type.block_light = true;
108 type.collision = true;
109 type.collide_block = true;
110 type.fill = stair_fill;
115 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
116 type.label = "Blue Block";
117 type.block_light = true;
118 type.collision = true;
119 type.collide_block = true;
120 type.fill = block_fill;
124 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
125 type.label = "Blue Slab";
126 type.block_light = true;
127 type.collision = true;
128 type.collide_block = true;
129 type.fill = slab_fill;
133 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
134 type.label = "Blue Stair";
135 type.block_light = true;
136 type.collision = true;
137 type.collide_block = true;
138 type.fill = stair_fill;
142 { // glowing yellow block
143 BlockType type(true, { 1.0f, 1.0f, 0.0f }, &blockShape);
144 type.label = "Light";
145 type.luminosity = 15;
146 type.block_light = true;
147 type.collision = true;
148 type.collide_block = true;
149 type.fill = block_fill;
155 generate.Solids({ 1, 4, 7, 10 });
157 player = &AddEntity();
158 player->Name("player");
159 player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
160 player->WorldCollidable(true);
161 player->Position(config.spawn);
163 chunks.GenerateSurrounding(player->ChunkCoords());
174 std::vector<Candidate> candidates;
178 bool World::Intersection(
188 for (Chunk &cur_chunk : chunks.Loaded()) {
190 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
191 candidates.push_back({ &cur_chunk, cur_dist });
195 if (candidates.empty()) return false;
198 dist = std::numeric_limits<float>::infinity();
201 for (Candidate &cand : candidates) {
202 if (cand.dist > dist) continue;
205 glm::vec3 cur_normal;
206 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
207 if (cur_dist < dist) {
219 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
220 AABB box = e.Bounds();
221 glm::mat4 M = e.Transform(player->ChunkCoords());
223 // TODO: this only needs to check the chunks surrounding the entity's chunk position
224 // need find out if that is quicker than the rough chunk bounds test
225 for (Chunk &cur_chunk : chunks.Loaded()) {
226 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) {
234 Chunk &World::PlayerChunk() {
235 return chunks.ForceLoad(player->ChunkCoords());
238 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
239 const Chunk::Pos tgt_pos = to.Position() + dir;
240 return chunks.ForceLoad(tgt_pos);
246 std::vector<WorldCollision> col;
250 void World::Update(int dt) {
251 for (Entity &entity : entities) {
254 for (Entity &entity : entities) {
256 if (entity.WorldCollidable() && Intersection(entity, col)) {
257 // entity collides with the world
258 Resolve(entity, col);
261 chunks.Rebase(player->ChunkCoords());
265 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
266 // determine displacement for each cardinal axis and move entity accordingly
267 glm::vec3 min_disp(0.0f);
268 glm::vec3 max_disp(0.0f);
269 for (const WorldCollision &c : col) {
270 if (!c.Blocks()) continue;
271 glm::vec3 local_disp(c.normal * c.depth);
272 // swap if neccessary (normal may point away from the entity)
273 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
276 min_disp = min(min_disp, local_disp);
277 max_disp = max(max_disp, local_disp);
280 // if only one direction is set, use that as the final
281 // if both directions are set, use average
282 glm::vec3 final_disp(0.0f);
283 for (int axis = 0; axis < 3; ++axis) {
284 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
285 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
286 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
288 final_disp[axis] = min_disp[axis];
290 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
291 final_disp[axis] = max_disp[axis];
298 void World::Render(BlockLighting &chunk_prog, DirectionalLighting &entity_prog) {
299 chunk_prog.Activate();
300 chunk_prog.SetFogDensity(fog_density);
301 chunk_prog.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
303 for (Chunk &chunk : chunks.Loaded()) {
304 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
306 glm::mat4 mvp(chunk_prog.GetVP() * m);
307 if (!CullTest(Chunk::Bounds(), mvp)) {
312 entity_prog.Activate();
313 entity_prog.SetLightDirection(light_direction);
314 entity_prog.SetFogDensity(fog_density);
315 entity_prog.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
317 for (Entity &entity : entities) {
318 if (entity.HasShape()) {
319 entity_prog.SetM(entity.Transform(player->ChunkCoords()));