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.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
18 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
19 blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
20 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &blockShape }); // red block
21 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &stairShape }); // red stair
22 blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &slabShape }); // red slab
23 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &blockShape }); // green block
24 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &stairShape }); // green stair
25 blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &slabShape }); // green slab
26 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &blockShape }); // blue block
27 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
28 blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
31 generate.Solids({ 1, 4, 7, 10 });
33 player.Position({ 4.0f, 4.0f, 4.0f });
35 chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
39 bool World::Intersection(
46 Chunk *closest_chunk = nullptr;
47 int closest_blkid = -1;
48 float closest_dist = std::numeric_limits<float>::infinity();
49 glm::vec3 closest_normal;
51 for (Chunk &cur_chunk : chunks.Loaded()) {
55 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player.ChunkCoords()), &cur_blkid, &cur_dist, &cur_normal)) {
56 if (cur_dist < closest_dist) {
57 closest_chunk = &cur_chunk;
58 closest_blkid = cur_blkid;
59 closest_dist = cur_dist;
60 closest_normal = cur_normal;
66 *chunk = closest_chunk;
69 *blkid = closest_blkid;
75 *normal = closest_normal;
81 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
82 const Chunk::Pos tgt_pos = to.Position() + dir;
83 return chunks.ForceLoad(tgt_pos);
87 void World::Update(int dt) {
89 chunks.Rebase(player.ChunkCoords());
94 void World::Render(DirectionalLighting &program) {
95 program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
96 program.SetView(glm::inverse(player.Transform(player.ChunkCoords())));
98 for (Chunk &chunk : chunks.Loaded()) {
99 glm::mat4 m(chunk.Transform(player.ChunkCoords()));
101 glm::mat4 mvp(program.GetVP() * m);
102 if (!CullTest(Chunk::Bounds(), mvp)) {