4 #include <glm/gtx/transform.hpp>
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;
106 generate.Solids({ 1, 4, 7, 10 });
108 player = &AddEntity();
109 player->Position({ 4.0f, 4.0f, 4.0f });
111 chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
122 std::vector<Candidate> candidates;
126 bool World::Intersection(
135 for (Chunk &cur_chunk : chunks.Loaded()) {
137 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player->ChunkCoords()), cur_dist)) {
138 candidates.push_back({ &cur_chunk, cur_dist });
142 if (candidates.empty()) return false;
144 Chunk *closest_chunk = nullptr;
145 float closest_dist = std::numeric_limits<float>::infinity();
146 int closest_blkid = -1;
147 glm::vec3 closest_normal;
149 for (Candidate &cand : candidates) {
150 if (cand.dist > closest_dist) continue;
153 glm::vec3 cur_normal;
154 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player->ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
155 if (cur_dist < closest_dist) {
156 closest_chunk = cand.chunk;
157 closest_blkid = cur_blkid;
158 closest_dist = cur_dist;
159 closest_normal = cur_normal;
165 *chunk = closest_chunk;
168 *blkid = closest_blkid;
171 *dist = closest_dist;
174 *normal = closest_normal;
176 return closest_chunk;
180 Chunk &World::PlayerChunk() {
181 return chunks.ForceLoad(player->ChunkCoords());
184 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
185 const Chunk::Pos tgt_pos = to.Position() + dir;
186 return chunks.ForceLoad(tgt_pos);
190 void World::Update(int dt) {
191 for (Entity &entity : entities) {
194 chunks.Rebase(player->ChunkCoords());
199 void World::Render(DirectionalLighting &program) {
200 program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
201 program.SetView(glm::inverse(player->Transform(player->ChunkCoords())));
203 for (Chunk &chunk : chunks.Loaded()) {
204 glm::mat4 m(chunk.Transform(player->ChunkCoords()));
206 glm::mat4 mvp(program.GetVP() * m);
207 if (!CullTest(Chunk::Bounds(), mvp)) {
212 for (Entity &entity : entities) {
213 if (entity.HasShape()) {
214 program.SetM(entity.Transform(player->ChunkCoords()));