]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
split chunk loader from world
[blank.git] / src / world.cpp
1 #include "world.hpp"
2
3 #include <limits>
4 #include <glm/gtx/transform.hpp>
5
6
7 namespace blank {
8
9 World::World()
10 : blockType()
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 }})
14 , generate(0)
15 , chunks(blockType, generate)
16 , player() {
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
29
30         generate.Solids({ 1, 4, 7, 10 });
31
32         player.Position({ 4.0f, 4.0f, 4.0f });
33
34         chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
35 }
36
37
38 bool World::Intersection(
39                 const Ray &ray,
40                 const glm::mat4 &M,
41                 Chunk **chunk,
42                 int *blkid,
43                 float *dist,
44                 glm::vec3 *normal) {
45         Chunk *closest_chunk = nullptr;
46         int closest_blkid = -1;
47         float closest_dist = std::numeric_limits<float>::infinity();
48         glm::vec3 closest_normal;
49
50         for (Chunk &cur_chunk : chunks.Loaded()) {
51                 int cur_blkid;
52                 float cur_dist;
53                 glm::vec3 cur_normal;
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;
60                         }
61                 }
62         }
63
64         if (chunk) {
65                 *chunk = closest_chunk;
66         }
67         if (blkid) {
68                 *blkid = closest_blkid;
69         }
70         if (dist) {
71                 *dist = closest_dist;
72         }
73         if (normal) {
74                 *normal = closest_normal;
75         }
76         return closest_chunk;
77 }
78
79
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);
83 }
84
85
86 void World::Update(int dt) {
87         player.Update(dt);
88         chunks.Rebase(player.ChunkCoords());
89         chunks.Update();
90 }
91
92
93 void World::Render(DirectionalLighting &program) {
94         program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
95         program.SetView(glm::inverse(player.Transform(player.ChunkCoords())));
96
97         for (Chunk &chunk : chunks.Loaded()) {
98                 glm::mat4 m(chunk.Transform(player.ChunkCoords()));
99                 program.SetM(m);
100                 glm::mat4 mvp(program.GetVP() * m);
101                 if (!CullTest(Chunk::Bounds(), mvp)) {
102                         chunk.Draw();
103                 }
104         }
105 }
106
107 }