]> git.localhorst.tv Git - blank.git/blob - src/world/Generator.cpp
sky box model & shader
[blank.git] / src / world / Generator.cpp
1 #include "Generator.hpp"
2
3 #include "Chunk.hpp"
4 #include "../rand/OctaveNoise.hpp"
5
6 #include <glm/glm.hpp>
7
8
9 namespace blank {
10
11 Generator::Generator(const Config &config) noexcept
12 : solidNoise(config.seed)
13 , typeNoise(config.seed)
14 , stretch(1.0f/config.stretch)
15 , solid_threshold(config.solid_threshold)
16 // TODO: stable dynamic generator configuration
17 , space(0)
18 , light(13)
19 , solids({ 1, 4, 7, 10 }) {
20
21 }
22
23
24 void Generator::operator ()(Chunk &chunk) const noexcept {
25         Chunk::Pos pos(chunk.Position());
26         glm::vec3 coords(pos * Chunk::Extent());
27         for (int z = 0; z < Chunk::depth; ++z) {
28                 for (int y = 0; y < Chunk::height; ++y) {
29                         for (int x = 0; x < Chunk::width; ++x) {
30                                 Block::Pos block_pos(x, y, z);
31                                 glm::vec3 gen_pos = (coords + block_pos) * stretch;
32                                 float val = OctaveNoise(solidNoise, coords + block_pos, 3, 0.5f, stretch, 2.0f);
33                                 if (val > solid_threshold) {
34                                         int type_val = int((typeNoise(gen_pos) + 1.0f) * solids.size()) % solids.size();
35                                         chunk.SetBlock(block_pos, Block(solids[type_val]));
36                                 } else {
37                                         chunk.SetBlock(block_pos, Block(space));
38                                 }
39                         }
40                 }
41         }
42         unsigned int random = 263167 * pos.x + 2097593 * pos.y + 426389 * pos.z;
43         for (int index = 0; index < Chunk::size; ++index) {
44                 if (chunk.IsSurface(index)) {
45                         random = random * 666649 + 7778777;
46                         if ((random % 32) == 0) {
47                                 chunk.SetBlock(index, Block(light));
48                         }
49                 }
50         }
51 }
52
53 }