3 #include "WorldCollision.hpp"
4 #include "../graphics/Viewport.hpp"
8 #include <glm/gtx/io.hpp>
9 #include <glm/gtx/transform.hpp>
14 World::World(const Config &config)
16 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
17 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
18 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
19 , generate(config.gen)
20 , chunks(config.load, blockType, generate)
23 , light_direction(config.light_direction)
24 , fog_density(config.fog_density) {
25 BlockType::Faces block_fill = { true, true, true, true, true, true };
26 BlockType::Faces slab_fill = { false, true, false, false, false, false };
27 BlockType::Faces stair_fill = { false, true, false, false, false, true };
30 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &blockShape);
31 type.label = "White Block";
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.label = "White Slab";
41 type.block_light = true;
42 type.collision = true;
43 type.collide_block = true;
44 type.fill = slab_fill;
48 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
49 type.label = "White Stair";
50 type.block_light = true;
51 type.collision = true;
52 type.collide_block = true;
53 type.fill = stair_fill;
58 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
59 type.label = "Red Block";
60 type.block_light = true;
61 type.collision = true;
62 type.collide_block = true;
63 type.fill = block_fill;
67 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
68 type.label = "Red Slab";
69 type.block_light = true;
70 type.collision = true;
71 type.collide_block = true;
72 type.fill = slab_fill;
76 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
77 type.label = "Red Stair";
78 type.block_light = true;
79 type.collision = true;
80 type.collide_block = true;
81 type.fill = stair_fill;
86 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
87 type.label = "Green Block";
88 type.block_light = true;
89 type.collision = true;
90 type.collide_block = true;
91 type.fill = block_fill;
95 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
96 type.label = "Green Slab";
97 type.block_light = true;
98 type.collision = true;
99 type.collide_block = true;
100 type.fill = slab_fill;
104 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
105 type.label = "Green Stair";
106 type.block_light = true;
107 type.collision = true;
108 type.collide_block = true;
109 type.fill = stair_fill;
114 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
115 type.label = "Blue Block";
116 type.block_light = true;
117 type.collision = true;
118 type.collide_block = true;
119 type.fill = block_fill;
123 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
124 type.label = "Blue Slab";
125 type.block_light = true;
126 type.collision = true;
127 type.collide_block = true;
128 type.fill = slab_fill;
132 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
133 type.label = "Blue Stair";
134 type.block_light = true;
135 type.collision = true;
136 type.collide_block = true;
137 type.fill = stair_fill;
141 { // glowing yellow block
142 BlockType type(true, { 1.0f, 1.0f, 0.0f }, &blockShape);
143 type.label = "Light";
144 type.luminosity = 15;
145 type.block_light = true;
146 type.collision = true;
147 type.collide_block = true;
148 type.fill = block_fill;
154 generate.Solids({ 1, 4, 7, 10 });
156 player = &AddEntity();
157 player->Name("player");
158 player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
159 player->WorldCollidable(true);
160 player->Position(config.spawn);
162 chunks.GenerateSurrounding(player->ChunkCoords());
173 std::vector<Candidate> candidates;
177 bool World::Intersection(
187 for (Chunk &cur_chunk : chunks.Loaded()) {
189 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
190 candidates.push_back({ &cur_chunk, cur_dist });
194 if (candidates.empty()) return false;
197 dist = std::numeric_limits<float>::infinity();
200 for (Candidate &cand : candidates) {
201 if (cand.dist > dist) continue;
204 glm::vec3 cur_normal;
205 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
206 if (cur_dist < dist) {
218 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
219 AABB box = e.Bounds();
220 glm::mat4 M = e.Transform(player->ChunkCoords());
222 // TODO: this only needs to check the chunks surrounding the entity's chunk position
223 // need find out if that is quicker than the rough chunk bounds test
224 for (Chunk &cur_chunk : chunks.Loaded()) {
225 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) {
233 Chunk &World::PlayerChunk() {
234 return chunks.ForceLoad(player->ChunkCoords());
237 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
238 const Chunk::Pos tgt_pos = to.Position() + dir;
239 return chunks.ForceLoad(tgt_pos);
245 std::vector<WorldCollision> col;
249 void World::Update(int dt) {
250 for (Entity &entity : entities) {
253 for (Entity &entity : entities) {
255 if (entity.WorldCollidable() && Intersection(entity, col)) {
256 // entity collides with the world
257 Resolve(entity, col);
260 chunks.Rebase(player->ChunkCoords());
264 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
265 // determine displacement for each cardinal axis and move entity accordingly
266 glm::vec3 min_disp(0.0f);
267 glm::vec3 max_disp(0.0f);
268 for (const WorldCollision &c : col) {
269 if (!c.Blocks()) continue;
270 glm::vec3 local_disp(c.normal * c.depth);
271 // swap if neccessary (normal may point away from the entity)
272 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
275 min_disp = min(min_disp, local_disp);
276 max_disp = max(max_disp, local_disp);
279 // if only one direction is set, use that as the final
280 // if both directions are set, use average
281 glm::vec3 final_disp(0.0f);
282 for (int axis = 0; axis < 3; ++axis) {
283 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
284 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
285 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
287 final_disp[axis] = min_disp[axis];
289 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
290 final_disp[axis] = max_disp[axis];
297 void World::Render(Viewport &viewport) {
298 viewport.WorldPosition(player->Transform(player->ChunkCoords()));
300 BlockLighting &chunk_prog = viewport.ChunkProgram();
301 chunk_prog.SetFogDensity(fog_density);
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 DirectionalLighting &entity_prog = viewport.EntityProgram();
313 entity_prog.SetLightDirection(light_direction);
314 entity_prog.SetFogDensity(fog_density);
316 for (Entity &entity : entities) {
317 if (entity.HasShape()) {
318 entity_prog.SetM(entity.Transform(player->ChunkCoords()));