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