4 #include <glm/gtx/transform.hpp>
9 World::World(unsigned int seed)
11 , blockShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }})
12 , stairShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.5f, 0.5f }}, { 0.0f, 0.0f })
13 , slabShape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }})
15 , chunks(blockType, generate)
17 BlockType::Faces block_fill = { true, true, true, true, true, true };
18 BlockType::Faces slab_fill = { false, true, false, false, false, false };
19 BlockType::Faces stair_fill = { false, true, false, false, false, true };
22 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &blockShape);
23 type.block_light = true;
24 type.fill = block_fill;
28 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &slabShape);
29 type.block_light = true;
30 type.fill = slab_fill;
34 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
35 type.block_light = true;
36 type.fill = stair_fill;
41 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
42 type.block_light = true;
43 type.fill = block_fill;
47 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
48 type.block_light = true;
49 type.fill = slab_fill;
53 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
54 type.block_light = true;
55 type.fill = stair_fill;
60 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
61 type.block_light = true;
62 type.fill = block_fill;
66 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
67 type.block_light = true;
68 type.fill = slab_fill;
72 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
73 type.block_light = true;
74 type.fill = stair_fill;
79 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
80 type.block_light = true;
81 type.fill = block_fill;
85 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
86 type.block_light = true;
87 type.fill = slab_fill;
91 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
92 type.block_light = true;
93 type.fill = stair_fill;
97 { // glowing yellow block
98 BlockType type(true, { 1.0f, 1.0f, 0.0f }, &blockShape);
100 type.block_light = true;
101 type.fill = block_fill;
107 generate.Solids({ 1, 4, 7, 10 });
109 player = &AddEntity();
110 player->Position({ 4.0f, 4.0f, 4.0f });
112 chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
123 std::vector<Candidate> candidates;
127 bool World::Intersection(
136 for (Chunk &cur_chunk : chunks.Loaded()) {
138 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
139 candidates.push_back({ &cur_chunk, cur_dist });
143 if (candidates.empty()) return false;
145 Chunk *closest_chunk = nullptr;
146 float closest_dist = std::numeric_limits<float>::infinity();
147 int closest_blkid = -1;
148 glm::vec3 closest_normal;
150 for (Candidate &cand : candidates) {
151 if (cand.dist > closest_dist) continue;
154 glm::vec3 cur_normal;
155 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
156 if (cur_dist < closest_dist) {
157 closest_chunk = cand.chunk;
158 closest_blkid = cur_blkid;
159 closest_dist = cur_dist;
160 closest_normal = cur_normal;
166 *chunk = closest_chunk;
169 *blkid = closest_blkid;
172 *dist = closest_dist;
175 *normal = closest_normal;
177 return closest_chunk;
181 Chunk &World::PlayerChunk() {
182 return chunks.ForceLoad(player->ChunkCoords());
185 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
186 const Chunk::Pos tgt_pos = to.Position() + dir;
187 return chunks.ForceLoad(tgt_pos);
191 void World::Update(int dt) {
192 for (Entity &entity : entities) {
195 chunks.Rebase(player->ChunkCoords());
200 void World::Render(DirectionalLighting &program) {
201 program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
202 // fade out reaches 1/e (0.3679) at 1/fog_density,
203 // gets less than 0.01 at e/(2 * fog_density)
204 // I chose 0.011 because it yields 91 and 124 for those, so
205 // slightly less than 6 and 8 chunks
206 program.SetFogDensity(0.011f);
207 program.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
209 for (Chunk &chunk : chunks.Loaded()) {
210 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
212 glm::mat4 mvp(program.GetVP() * m);
213 if (!CullTest(Chunk::Bounds(), mvp)) {
218 for (Entity &entity : entities) {
219 if (entity.HasShape()) {
220 program.SetM(entity.Transform(player->ChunkCoords()));