]> git.localhorst.tv Git - blank.git/blob - src/generator.cpp
separate file for world generation
[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 , solids() {
14
15 }
16
17
18 void Generator::operator ()(Chunk &chunk) const {
19         chunk.Allocate();
20         Chunk::Pos pos(chunk.Position());
21         glm::vec3 coords(pos * Chunk::Extent());
22         for (int z = 0; z < Chunk::Depth(); ++z) {
23                 for (int y = 0; y < Chunk::Height(); ++y) {
24                         for (int x = 0; x < Chunk::Width(); ++x) {
25                                 Block::Pos block_pos(x, y, z);
26                                 glm::vec3 gen_pos = (coords + block_pos) / stretch;
27                                 float val = solidNoise(gen_pos);
28                                 if (val > solid_threshold) {
29                                         int type_val = int((typeNoise(gen_pos) + 1.0f) * solids.size()) % solids.size();
30                                         chunk.BlockAt(block_pos) = Block(solids[type_val]);
31                                 }
32                         }
33                 }
34         }
35         chunk.Invalidate();
36 }
37
38 }