]> git.localhorst.tv Git - blank.git/blob - src/noise.cpp
c66153be5b56579438844176f0ffe2aacaed46be
[blank.git] / src / noise.cpp
1 #include "noise.hpp"
2
3 #include <cmath>
4
5
6 namespace {
7
8 constexpr float one_third = 1.0f/3.0f;
9 constexpr float one_sixth = 1.0f/6.0f;
10
11 }
12
13 namespace blank {
14
15 GaloisLFSR::GaloisLFSR(std::uint64_t seed) noexcept
16 : state(seed) {
17
18 }
19
20 bool GaloisLFSR::operator ()() noexcept {
21         bool result = state & 1;
22         state >>= 1;
23         if (result) {
24                 state |= 0x8000000000000000;
25                 state ^= mask;
26         } else {
27                 state &= 0x7FFFFFFFFFFFFFFF;
28         }
29         return result;
30 }
31
32
33 SimplexNoise::SimplexNoise(unsigned int seed) noexcept
34 : grad({
35         {  1.0f,  1.0f,  0.0f },
36         { -1.0f,  1.0f,  0.0f },
37         {  1.0f, -1.0f,  0.0f },
38         { -1.0f, -1.0f,  0.0f },
39         {  1.0f,  0.0f,  1.0f },
40         { -1.0f,  0.0f,  1.0f },
41         {  1.0f,  0.0f, -1.0f },
42         { -1.0f,  0.0f, -1.0f },
43         {  0.0f,  1.0f,  1.0f },
44         {  0.0f, -1.0f,  1.0f },
45         {  0.0f,  1.0f, -1.0f },
46         {  0.0f, -1.0f, -1.0f },
47 }) {
48         GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
49         for (size_t i = 0; i < 256; ++i) {
50                 random(perm[i]);
51                 perm[i + 256] = perm[i];
52                 perm12[i] = perm[i] % 12;
53                 perm12[i + 256] = perm12[i];
54         }
55 }
56
57
58 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
59         float skew = (in.x + in.y + in.z) * one_third;
60
61         glm::vec3 skewed(glm::floor(in + skew));
62         float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
63
64         glm::vec3 unskewed(skewed - tr);
65         glm::vec3 offset[4];
66         offset[0] = in - unskewed;
67
68         glm::vec3 second, third;
69
70         if (offset[0].x >= offset[0].y) {
71                 if (offset[0].y >= offset[0].z) {
72                         second = { 1.0f, 0.0f, 0.0f };
73                         third = { 1.0f, 1.0f, 0.0f };
74                 } else if (offset[0].x >= offset[0].z) {
75                         second = { 1.0f, 0.0f, 0.0f };
76                         third = { 1.0f, 0.0f, 1.0f };
77                 } else {
78                         second = { 0.0f, 0.0f, 1.0f };
79                         third = { 1.0f, 0.0f, 1.0f };
80                 }
81         } else if (offset[0].y < offset[0].z) {
82                 second = { 0.0f, 0.0f, 1.0f };
83                 third = { 0.0f, 1.0f, 1.0f };
84         } else if (offset[0].x < offset[0].z) {
85                 second = { 0.0f, 1.0f, 0.0f };
86                 third = { 0.0f, 1.0f, 1.0f };
87         } else {
88                 second = { 0.0f, 1.0f, 0.0f };
89                 third = { 1.0f, 1.0f, 0.0f };
90         }
91
92         offset[1] = offset[0] - second + one_sixth;
93         offset[2] = offset[0] - third + one_third;
94         offset[3] = offset[0] - 0.5f;
95
96         unsigned char index[3] = {
97                 (unsigned char)(skewed.x),
98                 (unsigned char)(skewed.y),
99                 (unsigned char)(skewed.z),
100         };
101         unsigned char corner[4] = {
102                 Perm12(index[0] + Perm(index[1] + Perm(index[2]))),
103                 Perm12(index[0] + int(second.x) + Perm(index[1] + int(second.y) + Perm(index[2] + int(second.z)))),
104                 Perm12(index[0] + int(third.x) + Perm(index[1] + int(third.y) + Perm(index[2] + int(third.z)))),
105                 Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1))),
106         };
107         float n[4];
108         float t[4];
109         for (size_t i = 0; i < 4; ++i) {
110                 t[i] = 0.6f - dot(offset[i], offset[i]);
111                 if (t[i] < 0.0f) {
112                         n[i] = 0.0f;
113                 } else {
114                         t[i] *= t[i];
115                         n[i] = t[i] * t[i] * dot(Grad(corner[i]), offset[i]);
116                 }
117         }
118
119         return 32.0f * (n[0] + n[1] + n[2] + n[3]);
120 }
121
122
123 unsigned char SimplexNoise::Perm(size_t idx) const noexcept {
124         return perm[idx];
125 }
126
127 unsigned char SimplexNoise::Perm12(size_t idx) const noexcept {
128         return perm12[idx];
129 }
130
131 const glm::vec3 &SimplexNoise::Grad(unsigned char idx) const noexcept {
132         return grad[idx];
133 }
134
135
136 WorleyNoise::WorleyNoise(unsigned int seed) noexcept
137 : seed(seed)
138 , num_points(8) {
139
140 }
141
142 float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept {
143         glm::vec3 center = floor(in);
144
145         float closest = 1.0f;  // cannot be farther away than 1.0
146
147         for (int z = -1; z <= 1; ++z) {
148                 for (int y = -1; y <= 1; ++y) {
149                         for (int x = -1; x <= 1; ++x) {
150                                 glm::vec3 cube(center.x + x, center.y + y, center.z + z);
151                                 unsigned int cube_rand =
152                                         (unsigned(cube.x) * 130223) ^
153                                         (unsigned(cube.y) * 159899) ^
154                                         (unsigned(cube.z) * 190717) ^
155                                         seed;
156
157                                 for (int i = 0; i < num_points; ++i) {
158                                         glm::vec3 point(cube);
159                                         cube_rand = 190667 * cube_rand + 109807;
160                                         point.x += float(cube_rand % 262144) / 262144.0f;
161                                         cube_rand = 135899 * cube_rand + 189169;
162                                         point.y += float(cube_rand % 262144) / 262144.0f;
163                                         cube_rand = 159739 * cube_rand + 112139;
164                                         point.z += float(cube_rand % 262144) / 262144.0f;
165
166                                         glm::vec3 diff(in - point);
167                                         float distance = sqrt(dot(diff, diff));
168                                         if (distance < closest) {
169                                                 closest = distance;
170                                         }
171                                 }
172                         }
173                 }
174         }
175
176         // closest ranges (0, 1), so normalizing to (-1,1) is trivial
177         // though heavily biased towards lower numbers
178         return 2.0f * closest - 1.0f;
179 }
180
181 }