1 #include "GaloisLFSR.hpp"
2 #include "SimplexNoise.hpp"
3 #include "WorleyNoise.hpp"
10 constexpr float one_third = 1.0f/3.0f;
11 constexpr float one_sixth = 1.0f/6.0f;
17 GaloisLFSR::GaloisLFSR(std::uint64_t seed) noexcept
22 bool GaloisLFSR::operator ()() noexcept {
23 bool result = state & 1;
26 state |= 0x8000000000000000;
29 state &= 0x7FFFFFFFFFFFFFFF;
35 SimplexNoise::SimplexNoise(unsigned int seed) noexcept
38 { -1.0f, 1.0f, 0.0f },
39 { 1.0f, -1.0f, 0.0f },
40 { -1.0f, -1.0f, 0.0f },
42 { -1.0f, 0.0f, 1.0f },
43 { 1.0f, 0.0f, -1.0f },
44 { -1.0f, 0.0f, -1.0f },
46 { 0.0f, -1.0f, 1.0f },
47 { 0.0f, 1.0f, -1.0f },
48 { 0.0f, -1.0f, -1.0f },
50 GaloisLFSR random(seed ^ 0x0123456789ACBDEF);
52 for (size_t i = 0; i < 256; ++i) {
53 perm[i] = random(value);
55 perm[i + 256] = perm[i];
56 perm12[i] = perm[i] % 12;
57 perm12[i + 256] = perm12[i];
62 float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept {
63 float skew = (in.x + in.y + in.z) * one_third;
65 glm::vec3 skewed(glm::floor(in + skew));
66 float tr = (skewed.x + skewed.y + skewed.z) * one_sixth;
68 glm::vec3 unskewed(skewed - tr);
69 glm::vec3 relative(in - unskewed);
71 glm::vec3 second, third;
73 if (relative.x >= relative.y) {
74 if (relative.y >= relative.z) {
77 } else if (relative.x >= relative.z) {
84 } else if (relative.y < relative.z) {
87 } else if (relative.x < relative.z) {
95 glm::vec3 offset[4] = {
97 relative - second + one_sixth,
98 relative - third + one_third,
103 (int)(skewed.x) & 0xFF,
104 (int)(skewed.y) & 0xFF,
105 (int)(skewed.z) & 0xFF,
111 float t = 0.6f - dot(offset[0], offset[0]);
114 int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2])));
115 n += t * t * dot(Grad(corner), offset[0]);
119 t = 0.6f - dot(offset[1], offset[1]);
122 int corner = Perm12(index[0] + int(second.x) + Perm(index[1] + int(second.y) + Perm(index[2] + int(second.z))));
123 n += t * t * dot(Grad(corner), offset[1]);
127 t = 0.6f - dot(offset[2], offset[2]);
130 int corner = Perm12(index[0] + int(third.x) + Perm(index[1] + int(third.y) + Perm(index[2] + int(third.z))));
131 n += t * t * dot(Grad(corner), offset[2]);
135 t = 0.6f - dot(offset[3], offset[3]);
138 int corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1)));
139 n += t * t * dot(Grad(corner), offset[3]);
146 int SimplexNoise::Perm(int idx) const noexcept {
150 int SimplexNoise::Perm12(int idx) const noexcept {
154 const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept {
159 WorleyNoise::WorleyNoise(unsigned int seed) noexcept
165 float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept {
166 glm::vec3 center = floor(in);
168 float closest = 1.0f; // cannot be farther away than 1.0
170 for (int z = -1; z <= 1; ++z) {
171 for (int y = -1; y <= 1; ++y) {
172 for (int x = -1; x <= 1; ++x) {
173 glm::vec3 cube(center.x + x, center.y + y, center.z + z);
174 unsigned int cube_rand =
175 (unsigned(cube.x) * 130223) ^
176 (unsigned(cube.y) * 159899) ^
177 (unsigned(cube.z) * 190717) ^
180 for (int i = 0; i < num_points; ++i) {
181 glm::vec3 point(cube);
182 cube_rand = 190667 * cube_rand + 109807;
183 point.x += float(cube_rand % 262144) / 262144.0f;
184 cube_rand = 135899 * cube_rand + 189169;
185 point.y += float(cube_rand % 262144) / 262144.0f;
186 cube_rand = 159739 * cube_rand + 112139;
187 point.z += float(cube_rand % 262144) / 262144.0f;
189 glm::vec3 diff(in - point);
190 float distance = sqrt(dot(diff, diff));
191 if (distance < closest) {
199 // closest ranges (0, 1), so normalizing to (-1,1) is trivial
200 // though heavily biased towards lower numbers
201 return 2.0f * closest - 1.0f;