]> git.localhorst.tv Git - blank.git/blob - src/generator.cpp
add some light blocks to generated surfaces
[blank.git] / src / generator.cpp
1 #include "generator.hpp"
2
3 #include <glm/glm.hpp>
4
5
6 namespace blank {
7
8 Generator::Generator(unsigned int seed)
9 : solidNoise(seed)
10 , typeNoise(seed + 1)
11 , stretch(64.0f)
12 , solid_threshold(0.8f)
13 , space(0)
14 , light(0)
15 , solids() {
16
17 }
18
19
20 void Generator::operator ()(Chunk &chunk) const {
21         chunk.Allocate();
22         Chunk::Pos pos(chunk.Position());
23         glm::vec3 coords(pos * Chunk::Extent());
24         for (int z = 0; z < Chunk::Depth(); ++z) {
25                 for (int y = 0; y < Chunk::Height(); ++y) {
26                         for (int x = 0; x < Chunk::Width(); ++x) {
27                                 Block::Pos block_pos(x, y, z);
28                                 glm::vec3 gen_pos = (coords + block_pos) / stretch;
29                                 float val = solidNoise(gen_pos);
30                                 if (val > solid_threshold) {
31                                         int type_val = int((typeNoise(gen_pos) + 1.0f) * solids.size()) % solids.size();
32                                         chunk.SetBlock(block_pos, Block(solids[type_val]));
33                                 } else {
34                                         chunk.SetBlock(block_pos, Block(space));
35                                 }
36                         }
37                 }
38         }
39         unsigned int random = 263167 * pos.x + 2097593 * pos.y + 426389 * pos.z;
40         for (int index = 0; index < Chunk::Size(); ++index) {
41                 if (chunk.IsSurface(index)) {
42                         random = random * 666649 + 7778777;
43                         if ((random % 32) == 0) {
44                                 chunk.SetBlock(index, Block(light));
45                         }
46                 }
47         }
48         chunk.Invalidate();
49         chunk.CheckUpdate();
50 }
51
52 }