]> git.localhorst.tv Git - tacos.git/blob - src/rand/noise.cpp
basic floor idea
[tacos.git] / src / rand / noise.cpp
1 #include "GaloisLFSR.hpp"
2 #include "SimplexNoise.hpp"
3
4 #include <cmath>
5
6
7 namespace {
8
9 constexpr float one_third = 1.0f/3.0f;
10 constexpr float one_sixth = 1.0f/6.0f;
11
12 }
13
14 namespace tacos {
15
16 SimplexNoise::SimplexNoise(std::uint64_t seed) noexcept
17 : grad({
18         {  1.0f,  1.0f,  0.0f },
19         { -1.0f,  1.0f,  0.0f },
20         {  1.0f, -1.0f,  0.0f },
21         { -1.0f, -1.0f,  0.0f },
22         {  1.0f,  0.0f,  1.0f },
23         { -1.0f,  0.0f,  1.0f },
24         {  1.0f,  0.0f, -1.0f },
25         { -1.0f,  0.0f, -1.0f },
26         {  0.0f,  1.0f,  1.0f },
27         {  0.0f, -1.0f,  1.0f },
28         {  0.0f,  1.0f, -1.0f },
29         {  0.0f, -1.0f, -1.0f },
30 })
31 , second_ints({
32                      // x>y x>z y>z
33         { 0, 0, 1 }, //  0   0   0  ZYX
34         { 0, 1, 0 }, //  0   0   1  YZX
35         { 0, 0, 1 }, //  0   1   0  illogical, but ZYX
36         { 0, 1, 0 }, //  0   1   1  YXZ
37         { 0, 0, 1 }, //  1   0   0  ZXY
38         { 1, 0, 0 }, //  1   0   1  illogical, but XYZ
39         { 1, 0, 0 }, //  1   1   0  XZY
40         { 1, 0, 0 }, //  1   1   1  XYZ
41 })
42 , third_ints({
43                      // x>y x>z y>z
44         { 0, 1, 1 }, //  0   0   0  ZYX
45         { 0, 1, 1 }, //  0   0   1  YZX
46         { 0, 1, 1 }, //  0   1   0  illogical, but ZYX
47         { 1, 1, 0 }, //  0   1   1  YXZ
48         { 1, 0, 1 }, //  1   0   0  ZXY
49         { 1, 1, 0 }, //  1   0   1  illogical, but XYZ
50         { 1, 0, 1 }, //  1   1   0  XZY
51         { 1, 1, 0 }, //  1   1   1  XYZ
52 })
53 , second_floats({
54                               // x>y x>z y>z
55         { 0.0f, 0.0f, 1.0f }, //  0   0   0  ZYX
56         { 0.0f, 1.0f, 0.0f }, //  0   0   1  YZX
57         { 0.0f, 0.0f, 1.0f }, //  0   1   0  illogical, but ZYX
58         { 0.0f, 1.0f, 0.0f }, //  0   1   1  YXZ
59         { 0.0f, 0.0f, 1.0f }, //  1   0   0  ZXY
60         { 1.0f, 0.0f, 0.0f }, //  1   0   1  illogical, but XYZ
61         { 1.0f, 0.0f, 0.0f }, //  1   1   0  XZY
62         { 1.0f, 0.0f, 0.0f }, //  1   1   1  XYZ
63 })
64 , third_floats({
65                               // x>y x>z y>z
66         { 0.0f, 1.0f, 1.0f }, //  0   0   0  ZYX
67         { 0.0f, 1.0f, 1.0f }, //  0   0   1  YZX
68         { 0.0f, 1.0f, 1.0f }, //  0   1   0  illogical, but ZYX
69         { 1.0f, 1.0f, 0.0f }, //  0   1   1  YXZ
70         { 1.0f, 0.0f, 1.0f }, //  1   0   0  ZXY
71         { 1.0f, 1.0f, 0.0f }, //  1   0   1  illogical, but XYZ
72         { 1.0f, 0.0f, 1.0f }, //  1   1   0  XZY
73         { 1.0f, 1.0f, 0.0f }, //  1   1   1  XYZ
74 }) {
75         GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
76         unsigned char value;
77         for (size_t i = 0; i < 256; ++i) {
78                 perm[i] = random(value);
79                 perm[i] &= 0xFF;
80                 perm[i + 256] = perm[i];
81                 perm12[i] = perm[i] % 12;
82                 perm12[i + 256] = perm12[i];
83         }
84 }
85
86
87 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
88         float skew = (in.x + in.y + in.z) * one_third;
89
90         glm::vec3 skewed(glm::floor(in + skew));
91         float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
92
93         glm::vec3 unskewed(skewed - tr);
94         glm::vec3 relative(in - unskewed);
95
96         bool x_ge_y = relative.x >= relative.y;
97         bool x_ge_z = relative.x >= relative.z;
98         bool y_ge_z = relative.y >= relative.z;
99         unsigned int st = (x_ge_y << 2) | (x_ge_z << 1) | y_ge_z;
100
101         glm::ivec3 second_int(second_ints[st]);
102         glm::ivec3 third_int(third_ints[st]);
103         glm::vec3 second_float(second_floats[st]);
104         glm::vec3 third_float(third_floats[st]);
105
106         glm::vec3 offset[4] = {
107                 in - unskewed,
108                 relative - second_float + one_sixth,
109                 relative - third_float + one_third,
110                 relative - 0.5f,
111         };
112
113         int index[3] = {
114                 (int)(skewed.x) & 0xFF,
115                 (int)(skewed.y) & 0xFF,
116                 (int)(skewed.z) & 0xFF,
117         };
118
119         float n = 0.0f;
120
121         // 0
122         float t = glm::clamp(0.5f - dot(offset[0], offset[0]), 0.0f, 1.0f);
123         t *= t;
124         int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
125         n += t * t * dot(Grad(corner), offset[0]);
126
127         // 1
128         t = glm::clamp(0.5f - dot(offset[1], offset[1]), 0.0f, 1.0f);
129         t *= t;
130         corner = Perm12(index[0] + second_int.x + Perm(index[1] + second_int.y + Perm(index[2] + second_int.z)));
131         n += t * t * dot(Grad(corner), offset[1]);
132
133         // 2
134         t = glm::clamp(0.5f - dot(offset[2], offset[2]), 0.0f, 1.0f);
135         t *= t;
136         corner = Perm12(index[0] + third_int.x + Perm(index[1] + third_int.y + Perm(index[2] + third_int.z)));
137         n += t * t * dot(Grad(corner), offset[2]);
138
139         // 3
140         t = glm::clamp(0.5f - dot(offset[3], offset[3]), 0.0f, 1.0f);
141         t *= t;
142         corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
143         n += t * t * dot(Grad(corner), offset[3]);
144
145         return 32.0f * n;
146 }
147
148
149 int SimplexNoise::Perm(int idx) const noexcept {
150         return perm[idx];
151 }
152
153 int SimplexNoise::Perm12(int idx) const noexcept {
154         return perm12[idx];
155 }
156
157 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {
158         return grad[idx];
159 }
160
161 }