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
30 generate.Solids({ 1, 4, 7, 10 });
32 player.Position({ 4.0f, 4.0f, 4.0f });
34 chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
38 bool World::Intersection(
45 Chunk *closest_chunk = nullptr;
46 int closest_blkid = -1;
47 float closest_dist = std::numeric_limits<float>::infinity();
48 glm::vec3 closest_normal;
50 for (Chunk &cur_chunk : chunks.Loaded()) {
54 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player.ChunkCoords()), &cur_blkid, &cur_dist, &cur_normal)) {
55 if (cur_dist < closest_dist) {
56 closest_chunk = &cur_chunk;
57 closest_blkid = cur_blkid;
58 closest_dist = cur_dist;
59 closest_normal = cur_normal;
65 *chunk = closest_chunk;
68 *blkid = closest_blkid;
74 *normal = closest_normal;
80 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
81 const Chunk::Pos tgt_pos = to.Position() + dir;
82 return chunks.ForceLoad(tgt_pos);
86 void World::Update(int dt) {
88 chunks.Rebase(player.ChunkCoords());
93 void World::Render(DirectionalLighting &program) {
94 program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
95 program.SetView(glm::inverse(player.Transform(player.ChunkCoords())));
97 for (Chunk &chunk : chunks.Loaded()) {
98 glm::mat4 m(chunk.Transform(player.ChunkCoords()));
100 glm::mat4 mvp(program.GetVP() * m);
101 if (!CullTest(Chunk::Bounds(), mvp)) {