]> git.localhorst.tv Git - blank.git/blob - src/noise.hpp
use light levels for shading of blocks
[blank.git] / src / noise.hpp
1 #ifndef BLANK_NOISE_HPP_
2 #define BLANK_NOISE_HPP_
3
4 #include <cstdint>
5 #include <limits>
6 #include <glm/glm.hpp>
7
8
9 namespace blank {
10
11 class GaloisLFSR {
12
13 public:
14         // seed should be non-zero
15         explicit GaloisLFSR(std::uint64_t seed);
16
17         // get the next bit
18         bool operator ()();
19
20         template<class T>
21         void operator ()(T &out) {
22                 constexpr int num_bits =
23                         std::numeric_limits<T>::digits +
24                         std::numeric_limits<T>::is_signed;
25                 for (int i = 0; i < num_bits; ++i) {
26                         operator ()();
27                 }
28                 out = static_cast<T>(state);
29         }
30
31 private:
32         std::uint64_t state;
33         // bits 64, 63, 61, and 60 set to 1 (counting from 1 lo to hi)
34         static constexpr std::uint64_t mask = 0xD800000000000000;
35
36 };
37
38 /// (3D only) adaptation of Stefan Gustavson's SimplexNoise java class
39 class SimplexNoise {
40
41 public:
42         explicit SimplexNoise(unsigned int seed);
43
44         float operator ()(const glm::vec3 &) const;
45
46 private:
47         unsigned char Perm(size_t idx) const;
48         const glm::vec3 &Grad(size_t idx) const;
49
50 private:
51         unsigned char perm[512];
52         glm::vec3 grad[12];
53
54 };
55
56
57 /// implementation of Worley noise (aka Cell or Voroni noise)
58 class WorleyNoise {
59
60 public:
61         explicit WorleyNoise(unsigned int seed);
62
63         float operator ()(const glm::vec3 &) const;
64
65 private:
66         const unsigned int seed;
67         const int num_points;
68
69 };
70
71 }
72
73 #endif