]> git.localhorst.tv Git - blank.git/blob - src/world.cpp
generate and unload chunks on player move
[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 , blockNoise(0)
15 , colorNoise(1)
16 , player()
17 , player_chunk(0, 0, 0)
18 , loaded()
19 , to_generate() {
20         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &blockShape }); // white block
21         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &stairShape }); // white stair
22         blockType.Add(BlockType{ true, { 1.0f, 1.0f, 1.0f }, &slabShape }); // white slab
23         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &blockShape }); // red block
24         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &stairShape }); // red stair
25         blockType.Add(BlockType{ true, { 1.0f, 0.0f, 0.0f }, &slabShape }); // red slab
26         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &blockShape }); // green block
27         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &stairShape }); // green stair
28         blockType.Add(BlockType{ true, { 0.0f, 1.0f, 0.0f }, &slabShape }); // green slab
29         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &blockShape }); // blue block
30         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &stairShape }); // blue stair
31         blockType.Add(BlockType{ true, { 0.0f, 0.0f, 1.0f }, &slabShape }); // blue slab
32
33         player.Position({ 4.0f, 4.0f, 4.0f });
34 }
35
36
37 namespace {
38
39 bool ChunkLess(const Chunk &a, const Chunk &b) {
40         return dot(a.Position(), a.Position()) < dot(b.Position(), b.Position());
41 }
42
43 }
44
45 void World::Generate(const glm::tvec3<int> &from, const glm::tvec3<int> &to) {
46         for (int z = from.z; z < to.z; ++z) {
47                 for (int y = from.y; y < to.y; ++y) {
48                         for (int x = from.x; x < to.x; ++x) {
49                                 glm::vec3 pos{float(x), float(y), float(z)};
50                                 if (ChunkAvailable(pos)) {
51                                         continue;
52                                 } else if (x == 0 && y == 0 && z == 0) {
53                                         loaded.emplace_back();
54                                         loaded.back().Position(pos);
55                                         Generate(loaded.back());
56                                 } else {
57                                         to_generate.emplace_back();
58                                         to_generate.back().Position(pos);
59                                 }
60                         }
61                 }
62         }
63         to_generate.sort(ChunkLess);
64 }
65
66 void World::Generate(Chunk &chunk) {
67         glm::vec3 pos(chunk.Position());
68         if (pos.x == 0 && pos.y == 0 && pos.z == 0) {
69                 for (size_t i = 1; i < blockType.Size(); ++i) {
70                         chunk.BlockAt(i) = Block(blockType[i]);
71                         chunk.BlockAt(i + 257) = Block(blockType[i]);
72                         chunk.BlockAt(i + 514) = Block(blockType[i]);
73                 }
74         } else {
75                 for (int z = 0; z < Chunk::Depth(); ++z) {
76                         for (int y = 0; y < Chunk::Height(); ++y) {
77                                 for (int x = 0; x < Chunk::Width(); ++x) {
78                                         glm::vec3 block_pos{float(x), float(y), float(z)};
79                                         glm::vec3 gen_pos = (pos * Chunk::Extent() + block_pos) / 64.0f;
80                                         float val = blockNoise(gen_pos);
81                                         if (val > 0.8f) {
82                                                 int col_val = int((colorNoise(gen_pos) + 1.0f) * 2.0f) % 4;
83                                                 chunk.BlockAt(block_pos) = Block(blockType[col_val * 3 + 1]);
84                                         }
85                                 }
86                         }
87                 }
88         }
89         chunk.Invalidate();
90 }
91
92 bool World::Intersection(
93                 const Ray &ray,
94                 const glm::mat4 &M,
95                 Chunk **chunk,
96                 int *blkid,
97                 float *dist,
98                 glm::vec3 *normal) {
99         Chunk *closest_chunk = nullptr;
100         int closest_blkid = -1;
101         float closest_dist = std::numeric_limits<float>::infinity();
102         glm::vec3 closest_normal;
103
104         for (Chunk &cur_chunk : loaded) {
105                 int cur_blkid;
106                 float cur_dist;
107                 glm::vec3 cur_normal;
108                 if (cur_chunk.Intersection(ray, M * cur_chunk.Transform(player.ChunkCoords()), &cur_blkid, &cur_dist, &cur_normal)) {
109                         if (cur_dist < closest_dist) {
110                                 closest_chunk = &cur_chunk;
111                                 closest_blkid = cur_blkid;
112                                 closest_dist = cur_dist;
113                                 closest_normal = cur_normal;
114                         }
115                 }
116         }
117
118         if (chunk) {
119                 *chunk = closest_chunk;
120         }
121         if (blkid) {
122                 *blkid = closest_blkid;
123         }
124         if (dist) {
125                 *dist = closest_dist;
126         }
127         if (normal) {
128                 *normal = closest_normal;
129         }
130         return closest_chunk;
131 }
132
133
134 Chunk *World::ChunkLoaded(const glm::tvec3<int> &pos) {
135         for (Chunk &chunk : loaded) {
136                 if (glm::tvec3<int>(chunk.Position()) == pos) {
137                         return &chunk;
138                 }
139         }
140         return nullptr;
141 }
142
143 Chunk *World::ChunkQueued(const glm::tvec3<int> &pos) {
144         for (Chunk &chunk : to_generate) {
145                 if (glm::tvec3<int>(chunk.Position()) == pos) {
146                         return &chunk;
147                 }
148         }
149         return nullptr;
150 }
151
152 Chunk *World::ChunkAvailable(const glm::tvec3<int> &pos) {
153         Chunk *chunk = ChunkLoaded(pos);
154         if (chunk) return chunk;
155
156         return ChunkQueued(pos);
157 }
158
159 Chunk &World::Next(const Chunk &to, const glm::vec3 &dir) {
160         const glm::vec3 tgt_pos = to.Position() + dir;
161
162         Chunk *chunk = ChunkLoaded(tgt_pos);
163         if (chunk) {
164                 return *chunk;
165         }
166
167         chunk = ChunkQueued(tgt_pos);
168         if (chunk) {
169                 Generate(*chunk);
170                 return *chunk;
171         }
172
173         loaded.emplace_back();
174         loaded.back().Position(tgt_pos);
175         Generate(loaded.back());
176         return loaded.back();
177 }
178
179
180 void World::Update(int dt) {
181         player.Update(dt);
182
183         CheckChunkGeneration();
184 }
185
186 void World::CheckChunkGeneration() {
187         if (player.ChunkCoords() != player_chunk) {
188                 player_chunk = player.ChunkCoords();
189
190                 constexpr int max_dist = 8;
191                 // unload far away chunks
192                 for (auto iter(loaded.begin()), end(loaded.end()); iter != end;) {
193                         if (std::abs(player_chunk.x - iter->Position().x) > max_dist
194                                         || std::abs(player_chunk.y - iter->Position().y) > max_dist
195                                         || std::abs(player_chunk.z - iter->Position().z) > max_dist) {
196                                 iter = loaded.erase(iter);
197                         } else {
198                                 ++iter;
199                         }
200                 }
201                 // abort far away queued chunks
202                 for (auto iter(to_generate.begin()), end(to_generate.end()); iter != end;) {
203                         if (std::abs(player_chunk.x - iter->Position().x) > max_dist
204                                         || std::abs(player_chunk.y - iter->Position().y) > max_dist
205                                         || std::abs(player_chunk.z - iter->Position().z) > max_dist) {
206                                 iter = to_generate.erase(iter);
207                         } else {
208                                 ++iter;
209                         }
210                 }
211                 // add missing new chunks
212                 glm::tvec3<int> offset(max_dist, max_dist, max_dist);
213                 Generate(player_chunk - offset, player_chunk + offset);
214         }
215
216         if (!to_generate.empty()) {
217                 Generate(to_generate.front());
218                 loaded.splice(loaded.end(), to_generate, to_generate.begin());
219         }
220 }
221
222
223 void World::Render(DirectionalLighting &program) {
224         program.SetLightDirection({ -1.0f, -3.0f, -2.0f });
225         program.SetView(glm::inverse(player.Transform(player.ChunkCoords())));
226
227         for (Chunk &chunk : LoadedChunks()) {
228                 glm::mat4 m(chunk.Transform(player.ChunkCoords()));
229                 program.SetM(m);
230                 glm::mat4 mvp(program.GetVP() * m);
231                 if (!CullTest(Chunk::Bounds(), mvp)) {
232                         chunk.Draw();
233                 }
234         }
235 }
236
237 }