X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnoise.cpp;h=c66153be5b56579438844176f0ffe2aacaed46be;hb=7554debb33158b1507c13f9d6fb5969345a570cd;hp=d192b27fcce8646109d310754b2d7e70c8536f75;hpb=f62562b0f87d571bd7b32ae2f8ca659c24e9911b;p=blank.git diff --git a/src/noise.cpp b/src/noise.cpp index d192b27..c66153b 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -3,10 +3,34 @@ #include +namespace { + +constexpr float one_third = 1.0f/3.0f; +constexpr float one_sixth = 1.0f/6.0f; + +} + namespace blank { +GaloisLFSR::GaloisLFSR(std::uint64_t seed) noexcept +: state(seed) { + +} + +bool GaloisLFSR::operator ()() noexcept { + bool result = state & 1; + state >>= 1; + if (result) { + state |= 0x8000000000000000; + state ^= mask; + } else { + state &= 0x7FFFFFFFFFFFFFFF; + } + return result; +} + -SimplexNoise::SimplexNoise(unsigned int seed) +SimplexNoise::SimplexNoise(unsigned int seed) noexcept : grad({ { 1.0f, 1.0f, 0.0f }, { -1.0f, 1.0f, 0.0f }, @@ -21,21 +45,23 @@ SimplexNoise::SimplexNoise(unsigned int seed) { 0.0f, 1.0f, -1.0f }, { 0.0f, -1.0f, -1.0f }, }) { - unsigned int val = seed; + GaloisLFSR random(seed ^ 0x0123456789ACBDEF); for (size_t i = 0; i < 256; ++i) { - val = 2346765 * val + 6446345; - perm[i] = val % 256; + random(perm[i]); + perm[i + 256] = perm[i]; + perm12[i] = perm[i] % 12; + perm12[i + 256] = perm12[i]; } } -float SimplexNoise::operator ()(const glm::vec3 &in) const { - float skew = (in.x + in.y + in.z) / 3.0f; +float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept { + float skew = (in.x + in.y + in.z) * one_third; - glm::vec3 skewed(std::floor(in.x + skew), std::floor(in.y + skew), std::floor(in.z + skew)); - float tr = (skewed.x + skewed.y + skewed.z) / 6.0f; + glm::vec3 skewed(glm::floor(in + skew)); + float tr = (skewed.x + skewed.y + skewed.z) * one_sixth; - glm::vec3 unskewed(skewed.x - tr, skewed.y - tr, skewed.z - tr); + glm::vec3 unskewed(skewed - tr); glm::vec3 offset[4]; offset[0] = in - unskewed; @@ -63,20 +89,20 @@ float SimplexNoise::operator ()(const glm::vec3 &in) const { third = { 1.0f, 1.0f, 0.0f }; } - offset[1] = offset[0] - second + glm::vec3(1.0f/6.0f); - offset[2] = offset[0] - third + glm::vec3(1.0f/3.0f); - offset[3] = offset[0] - glm::vec3(0.5f); + offset[1] = offset[0] - second + one_sixth; + offset[2] = offset[0] - third + one_third; + offset[3] = offset[0] - 0.5f; - size_t index[3] = { - unsigned(skewed.x) % 256, - unsigned(skewed.y) % 256, - unsigned(skewed.z) % 256, + unsigned char index[3] = { + (unsigned char)(skewed.x), + (unsigned char)(skewed.y), + (unsigned char)(skewed.z), }; - size_t corner[4] = { - Perm(index[0] + Perm(index[1] + Perm(index[2]))), - Perm(index[0] + second.x + Perm(index[1] + second.y + Perm(index[2] + second.z))), - Perm(index[0] + third.x + Perm(index[1] + third.y + Perm(index[2] + third.z))), - Perm(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1))), + unsigned char corner[4] = { + Perm12(index[0] + Perm(index[1] + Perm(index[2]))), + Perm12(index[0] + int(second.x) + Perm(index[1] + int(second.y) + Perm(index[2] + int(second.z)))), + Perm12(index[0] + int(third.x) + Perm(index[1] + int(third.y) + Perm(index[2] + int(third.z)))), + Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1))), }; float n[4]; float t[4]; @@ -94,12 +120,62 @@ float SimplexNoise::operator ()(const glm::vec3 &in) const { } -unsigned char SimplexNoise::Perm(size_t idx) const { - return perm[idx % 256]; +unsigned char SimplexNoise::Perm(size_t idx) const noexcept { + return perm[idx]; } -const glm::vec3 &SimplexNoise::Grad(size_t idx) const { - return grad[idx % 12]; +unsigned char SimplexNoise::Perm12(size_t idx) const noexcept { + return perm12[idx]; +} + +const glm::vec3 &SimplexNoise::Grad(unsigned char idx) const noexcept { + return grad[idx]; +} + + +WorleyNoise::WorleyNoise(unsigned int seed) noexcept +: seed(seed) +, num_points(8) { + +} + +float WorleyNoise::operator ()(const glm::vec3 &in) const noexcept { + glm::vec3 center = floor(in); + + float closest = 1.0f; // cannot be farther away than 1.0 + + for (int z = -1; z <= 1; ++z) { + for (int y = -1; y <= 1; ++y) { + for (int x = -1; x <= 1; ++x) { + glm::vec3 cube(center.x + x, center.y + y, center.z + z); + unsigned int cube_rand = + (unsigned(cube.x) * 130223) ^ + (unsigned(cube.y) * 159899) ^ + (unsigned(cube.z) * 190717) ^ + seed; + + for (int i = 0; i < num_points; ++i) { + glm::vec3 point(cube); + cube_rand = 190667 * cube_rand + 109807; + point.x += float(cube_rand % 262144) / 262144.0f; + cube_rand = 135899 * cube_rand + 189169; + point.y += float(cube_rand % 262144) / 262144.0f; + cube_rand = 159739 * cube_rand + 112139; + point.z += float(cube_rand % 262144) / 262144.0f; + + glm::vec3 diff(in - point); + float distance = sqrt(dot(diff, diff)); + if (distance < closest) { + closest = distance; + } + } + } + } + } + + // closest ranges (0, 1), so normalizing to (-1,1) is trivial + // though heavily biased towards lower numbers + return 2.0f * closest - 1.0f; } }