3 #include "../graphics/BlockLighting.hpp"
4 #include "../graphics/DirectionalLighting.hpp"
7 #include <glm/gtx/transform.hpp>
12 World::World(const Config &config)
14 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
15 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
16 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
17 , generate(config.gen)
18 , chunks(config.load, blockType, generate)
21 , light_direction(config.light_direction)
22 , fog_density(config.fog_density) {
23 BlockType::Faces block_fill = { true, true, true, true, true, true };
24 BlockType::Faces slab_fill = { false, true, false, false, false, false };
25 BlockType::Faces stair_fill = { false, true, false, false, false, true };
28 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &blockShape);
29 type.block_light = true;
30 type.fill = block_fill;
34 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &slabShape);
35 type.block_light = true;
36 type.fill = slab_fill;
40 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
41 type.block_light = true;
42 type.fill = stair_fill;
47 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
48 type.block_light = true;
49 type.fill = block_fill;
53 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
54 type.block_light = true;
55 type.fill = slab_fill;
59 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
60 type.block_light = true;
61 type.fill = stair_fill;
66 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
67 type.block_light = true;
68 type.fill = block_fill;
72 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
73 type.block_light = true;
74 type.fill = slab_fill;
78 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
79 type.block_light = true;
80 type.fill = stair_fill;
85 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
86 type.block_light = true;
87 type.fill = block_fill;
91 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
92 type.block_light = true;
93 type.fill = slab_fill;
97 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
98 type.block_light = true;
99 type.fill = stair_fill;
103 { // glowing yellow block
104 BlockType type(true, { 1.0f, 1.0f, 0.0f }, &blockShape);
105 type.luminosity = 15;
106 type.block_light = true;
107 type.fill = block_fill;
113 generate.Solids({ 1, 4, 7, 10 });
115 player = &AddEntity();
116 player->Position(config.spawn);
118 chunks.GenerateSurrounding(player->ChunkCoords());
129 std::vector<Candidate> candidates;
133 bool World::Intersection(
142 for (Chunk &cur_chunk : chunks.Loaded()) {
144 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
145 candidates.push_back({ &cur_chunk, cur_dist });
149 if (candidates.empty()) return false;
151 Chunk *closest_chunk = nullptr;
152 float closest_dist = std::numeric_limits<float>::infinity();
153 int closest_blkid = -1;
154 glm::vec3 closest_normal;
156 for (Candidate &cand : candidates) {
157 if (cand.dist > closest_dist) continue;
160 glm::vec3 cur_normal;
161 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
162 if (cur_dist < closest_dist) {
163 closest_chunk = cand.chunk;
164 closest_blkid = cur_blkid;
165 closest_dist = cur_dist;
166 closest_normal = cur_normal;
172 *chunk = closest_chunk;
175 *blkid = closest_blkid;
178 *dist = closest_dist;
181 *normal = closest_normal;
183 return closest_chunk;
187 Chunk &World::PlayerChunk() {
188 return chunks.ForceLoad(player->ChunkCoords());
191 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
192 const Chunk::Pos tgt_pos = to.Position() + dir;
193 return chunks.ForceLoad(tgt_pos);
197 void World::Update(int dt) {
198 for (Entity &entity : entities) {
201 chunks.Rebase(player->ChunkCoords());
206 void World::Render(BlockLighting &chunk_prog, DirectionalLighting &entity_prog) {
207 chunk_prog.Activate();
208 chunk_prog.SetFogDensity(fog_density);
209 chunk_prog.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
211 for (Chunk &chunk : chunks.Loaded()) {
212 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
214 glm::mat4 mvp(chunk_prog.GetVP() * m);
215 if (!CullTest(Chunk::Bounds(), mvp)) {
220 entity_prog.Activate();
221 entity_prog.SetLightDirection(light_direction);
222 entity_prog.SetFogDensity(fog_density);
223 entity_prog.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
225 for (Entity &entity : entities) {
226 if (entity.HasShape()) {
227 entity_prog.SetM(entity.Transform(player->ChunkCoords()));