3 #include "WorldCollision.hpp"
4 #include "../app/Assets.hpp"
5 #include "../graphics/Format.hpp"
6 #include "../graphics/Viewport.hpp"
9 #include <glm/gtx/io.hpp>
10 #include <glm/gtx/transform.hpp>
15 World::World(const Assets &assets, const Config &config, const WorldSave &save)
18 , generate(config.gen)
19 , chunks(config.load, block_type, generate, save)
22 , light_direction(config.light_direction)
23 , fog_density(config.fog_density) {
25 block_tex.Reserve(16, 16, 4, Format());
26 assets.LoadTexture("debug", block_tex, 0);
27 assets.LoadTexture("rock-1", block_tex, 1);
28 assets.LoadTexture("rock-2", block_tex, 2);
29 assets.LoadTexture("rock-3", block_tex, 3);
30 block_tex.FilterNearest();
32 assets.LoadBlockTypes("default", block_type);
36 generate.Solids({ 1, 4, 7, 10 });
38 player = &AddEntity();
39 player->Name("player");
40 player->Bounds({ { -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f } });
41 player->WorldCollidable(true);
42 player->Position(config.spawn);
44 chunks.QueueSurrounding(player->ChunkCoords());
55 std::vector<Candidate> candidates;
59 bool World::Intersection(
69 for (Chunk &cur_chunk : chunks.Loaded()) {
71 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
72 candidates.push_back({ &cur_chunk, cur_dist });
76 if (candidates.empty()) return false;
79 dist = std::numeric_limits<float>::infinity();
82 for (Candidate &cand : candidates) {
83 if (cand.dist > dist) continue;
87 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
88 if (cur_dist < dist) {
100 bool World::Intersection(const Entity &e, std::vector<WorldCollision> &col) {
101 AABB box = e.Bounds();
102 glm::mat4 M = e.Transform(player->ChunkCoords());
104 // TODO: this only needs to check the chunks surrounding the entity's chunk position
105 // need find out if that is quicker than the rough chunk bounds test
106 for (Chunk &cur_chunk : chunks.Loaded()) {
107 if (cur_chunk.Intersection(box, M, cur_chunk.Transform(player->ChunkCoords()), col)) {
115 Chunk &World::PlayerChunk() {
116 return chunks.ForceLoad(player->ChunkCoords());
119 Chunk &World::Next(const Chunk &to, const glm::ivec3 &dir) {
120 const Chunk::Pos tgt_pos = to.Position() + dir;
121 return chunks.ForceLoad(tgt_pos);
127 std::vector<WorldCollision> col;
131 void World::Update(int dt) {
132 for (Entity &entity : entities) {
135 for (Entity &entity : entities) {
137 if (entity.WorldCollidable() && Intersection(entity, col)) {
138 // entity collides with the world
139 Resolve(entity, col);
142 for (auto iter = entities.begin(), end = entities.end(); iter != end;) {
143 if (iter->CanRemove()) {
144 iter = entities.erase(iter);
149 chunks.Rebase(player->ChunkCoords());
153 void World::Resolve(Entity &e, std::vector<WorldCollision> &col) {
154 // determine displacement for each cardinal axis and move entity accordingly
155 glm::vec3 min_disp(0.0f);
156 glm::vec3 max_disp(0.0f);
157 for (const WorldCollision &c : col) {
158 if (!c.Blocks()) continue;
159 glm::vec3 local_disp(c.normal * c.depth);
160 // swap if neccessary (normal may point away from the entity)
161 if (dot(c.normal, e.Position() - c.BlockCoords()) < 0) {
164 min_disp = min(min_disp, local_disp);
165 max_disp = max(max_disp, local_disp);
168 // if only one direction is set, use that as the final
169 // if both directions are set, use average
170 glm::vec3 final_disp(0.0f);
171 for (int axis = 0; axis < 3; ++axis) {
172 if (std::abs(min_disp[axis]) > std::numeric_limits<float>::epsilon()) {
173 if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
174 final_disp[axis] = (min_disp[axis] + max_disp[axis]) * 0.5f;
176 final_disp[axis] = min_disp[axis];
178 } else if (std::abs(max_disp[axis]) > std::numeric_limits<float>::epsilon()) {
179 final_disp[axis] = max_disp[axis];
186 void World::Render(Viewport &viewport) {
187 viewport.WorldPosition(player->Transform(player->ChunkCoords()));
189 BlockLighting &chunk_prog = viewport.ChunkProgram();
190 chunk_prog.SetTexture(block_tex);
191 chunk_prog.SetFogDensity(fog_density);
193 for (Chunk &chunk : chunks.Loaded()) {
194 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
196 glm::mat4 mvp(chunk_prog.GetVP() * m);
197 if (!CullTest(Chunk::Bounds(), mvp)) {
202 DirectionalLighting &entity_prog = viewport.EntityProgram();
203 entity_prog.SetLightDirection(light_direction);
204 entity_prog.SetFogDensity(fog_density);
206 for (Entity &entity : entities) {
207 entity.Render(entity.ChunkTransform(player->ChunkCoords()), entity_prog);