]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
675c29ffbb97a284737974053ae61c86cc22fa2c
[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::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 };
20
21         { // white block
22                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &blockShape);
23                 type.fill = block_fill;
24                 blockType.Add(type);
25         }
26         { // white slab
27                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &slabShape);
28                 type.fill = slab_fill;
29                 blockType.Add(type);
30         }
31         { // white stair
32                 BlockType type(true, { 1.0f, 1.0f, 1.0f }, &stairShape);
33                 type.fill = stair_fill;
34                 blockType.Add(type);
35         }
36
37         { // red block
38                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &blockShape);
39                 type.fill = block_fill;
40                 blockType.Add(type);
41         }
42         { // red slab
43                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &slabShape);
44                 type.fill = slab_fill;
45                 blockType.Add(type);
46         }
47         { // red stair
48                 BlockType type(true, { 1.0f, 0.0f, 0.0f }, &stairShape);
49                 type.fill = stair_fill;
50                 blockType.Add(type);
51         }
52
53         { // green block
54                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &blockShape);
55                 type.fill = block_fill;
56                 blockType.Add(type);
57         }
58         { // green slab
59                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &slabShape);
60                 type.fill = slab_fill;
61                 blockType.Add(type);
62         }
63         { // green stair
64                 BlockType type(true, { 0.0f, 1.0f, 0.0f }, &stairShape);
65                 type.fill = stair_fill;
66                 blockType.Add(type);
67         }
68
69         { // blue block
70                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &blockShape);
71                 type.fill = block_fill;
72                 blockType.Add(type);
73         }
74         { // blue slab
75                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &slabShape);
76                 type.fill = slab_fill;
77                 blockType.Add(type);
78         }
79         { // blue stair
80                 BlockType type(true, { 0.0f, 0.0f, 1.0f }, &stairShape);
81                 type.fill = stair_fill;
82                 blockType.Add(type);
83         }
84
85         generate.Space(0);
86         generate.Solids({ 1, 4, 7, 10 });
87
88         player.Position({ 4.0f, 4.0f, 4.0f });
89
90         chunks.Generate({ -4, -4, -4 }, { 5, 5, 5});
91 }
92
93
94 namespace {
95
96 struct Candidate {
97         Chunk *chunk;
98         float dist;
99 };
100
101 std::vector<Candidate> candidates;
102
103 }
104
105 bool World::Intersection(
106                 const Ray &ray,
107                 const glm::mat4 &M,
108                 Chunk **chunk,
109                 int *blkid,
110                 float *dist,
111                 glm::vec3 *normal) {
112         candidates.clear();
113
114         for (Chunk &cur_chunk : chunks.Loaded()) {
115                 float cur_dist;
116                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player.ChunkCoords()), cur_dist)) {
117                         candidates.push_back({ &cur_chunk, cur_dist });
118                 }
119         }
120
121         if (candidates.empty()) return false;
122
123         Chunk *closest_chunk = nullptr;
124         float closest_dist = std::numeric_limits<float>::infinity();
125         int closest_blkid = -1;
126         glm::vec3 closest_normal;
127
128         for (Candidate &cand : candidates) {
129                 if (cand.dist > closest_dist) continue;
130                 int cur_blkid;
131                 float cur_dist;
132                 glm::vec3 cur_normal;
133                 if (cand.chunk->Intersection(ray, M * cand.chunk->Transform(player.ChunkCoords()), cur_blkid, cur_dist, cur_normal)) {
134                         if (cur_dist < closest_dist) {
135                                 closest_chunk = cand.chunk;
136                                 closest_blkid = cur_blkid;
137                                 closest_dist = cur_dist;
138                                 closest_normal = cur_normal;
139                         }
140                 }
141         }
142
143         if (chunk) {
144                 *chunk = closest_chunk;
145         }
146         if (blkid) {
147                 *blkid = closest_blkid;
148         }
149         if (dist) {
150                 *dist = closest_dist;
151         }
152         if (normal) {
153                 *normal = closest_normal;
154         }
155         return closest_chunk;
156 }
157
158
159 Chunk &World::Next(const Chunk &to, const glm::tvec3<int> &dir) {
160         const Chunk::Pos tgt_pos = to.Position() + dir;
161         return chunks.ForceLoad(tgt_pos);
162 }
163
164
165 void World::Update(int dt) {
166         player.Update(dt);
167         chunks.Rebase(player.ChunkCoords());
168         chunks.Update();
169 }
170
171
172 void World::Render(DirectionalLighting &program) {
173         program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
174         program.SetView(glm::inverse(player.Transform(player.ChunkCoords())));
175
176         for (Chunk &chunk : chunks.Loaded()) {
177                 glm::mat4 m(chunk.Transform(player.ChunkCoords()));
178                 program.SetM(m);
179                 glm::mat4 mvp(program.GetVP() * m);
180                 if (!CullTest(Chunk::Bounds(), mvp)) {
181                         chunk.Draw();
182                 }
183         }
184 }
185
186 }