X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnoise.cpp;h=d653d5d54ae0215228701dfffa600e61aef999b5;hb=1121e49e7cad94ce4efd0a02c72a44469c81494e;hp=8b716140e78c5b301fbd00a383778eb482e9166f;hpb=7ef94f4e55a0b798eecbcc7166a80b8364b21ea4;p=blank.git diff --git a/src/noise.cpp b/src/noise.cpp index 8b71614..d653d5d 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -12,12 +12,12 @@ constexpr float one_sixth = 1.0f/6.0f; namespace blank { -GaloisLFSR::GaloisLFSR(std::uint64_t seed) +GaloisLFSR::GaloisLFSR(std::uint64_t seed) noexcept : state(seed) { } -bool GaloisLFSR::operator ()() { +bool GaloisLFSR::operator ()() noexcept { bool result = state & 1; state >>= 1; if (result) { @@ -30,7 +30,7 @@ bool GaloisLFSR::operator ()() { } -SimplexNoise::SimplexNoise(unsigned int seed) +SimplexNoise::SimplexNoise(unsigned int seed) noexcept : grad({ { 1.0f, 1.0f, 0.0f }, { -1.0f, 1.0f, 0.0f }, @@ -46,94 +46,121 @@ SimplexNoise::SimplexNoise(unsigned int seed) { 0.0f, -1.0f, -1.0f }, }) { GaloisLFSR random(seed ^ 0x0123456789ACBDEF); + unsigned char value; for (size_t i = 0; i < 256; ++i) { - random(perm[i]); + perm[i] = random(value); + perm[i] &= 0xFF; perm[i + 256] = perm[i]; + perm12[i] = perm[i] % 12; + perm12[i + 256] = perm12[i]; } } -float SimplexNoise::operator ()(const glm::vec3 &in) const { +float SimplexNoise::operator ()(const glm::vec3 &in) const noexcept { float skew = (in.x + in.y + in.z) * one_third; glm::vec3 skewed(glm::floor(in + skew)); float tr = (skewed.x + skewed.y + skewed.z) * one_sixth; glm::vec3 unskewed(skewed - tr); - glm::vec3 offset[4]; - offset[0] = in - unskewed; + glm::vec3 relative(in - unskewed); glm::vec3 second, third; - if (offset[0].x >= offset[0].y) { - if (offset[0].y >= offset[0].z) { - second = { 1.0f, 0.0f, 0.0f }; - third = { 1.0f, 1.0f, 0.0f }; - } else if (offset[0].x >= offset[0].z) { - second = { 1.0f, 0.0f, 0.0f }; - third = { 1.0f, 0.0f, 1.0f }; + if (relative.x >= relative.y) { + if (relative.y >= relative.z) { + second = { 1, 0, 0 }; + third = { 1, 1, 0 }; + } else if (relative.x >= relative.z) { + second = { 1, 0, 0 }; + third = { 1, 0, 1 }; } else { - second = { 0.0f, 0.0f, 1.0f }; - third = { 1.0f, 0.0f, 1.0f }; + second = { 0, 0, 1 }; + third = { 1, 0, 1 }; } - } else if (offset[0].y < offset[0].z) { - second = { 0.0f, 0.0f, 1.0f }; - third = { 0.0f, 1.0f, 1.0f }; - } else if (offset[0].x < offset[0].z) { - second = { 0.0f, 1.0f, 0.0f }; - third = { 0.0f, 1.0f, 1.0f }; + } else if (relative.y < relative.z) { + second = { 0, 0, 1 }; + third = { 0, 1, 1 }; + } else if (relative.x < relative.z) { + second = { 0, 1, 0 }; + third = { 0, 1, 1 }; } else { - second = { 0.0f, 1.0f, 0.0f }; - third = { 1.0f, 1.0f, 0.0f }; + second = { 0, 1, 0 }; + third = { 1, 1, 0 }; } - offset[1] = offset[0] - second + one_sixth; - offset[2] = offset[0] - third + one_third; - offset[3] = offset[0] - 0.5f; - - unsigned char index[3] = { - (unsigned char)(skewed.x), - (unsigned char)(skewed.y), - (unsigned char)(skewed.z), + glm::vec3 offset[4] = { + in - unskewed, + relative - second + one_sixth, + relative - third + one_third, + relative - 0.5f, }; - 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))), + + int index[3] = { + (int)(skewed.x) & 0xFF, + (int)(skewed.y) & 0xFF, + (int)(skewed.z) & 0xFF, }; - float n[4]; - float t[4]; - for (size_t i = 0; i < 4; ++i) { - t[i] = 0.6f - dot(offset[i], offset[i]); - if (t[i] < 0.0f) { - n[i] = 0.0f; - } else { - t[i] *= t[i]; - n[i] = t[i] * t[i] * dot(Grad(corner[i]), offset[i]); - } + + float n = 0.0f; + + // 0 + float t = 0.6f - dot(offset[0], offset[0]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + Perm(index[1] + Perm(index[2]))); + n += t * t * dot(Grad(corner), offset[0]); + } + + // 1 + t = 0.6f - dot(offset[1], offset[1]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + int(second.x) + Perm(index[1] + int(second.y) + Perm(index[2] + int(second.z)))); + n += t * t * dot(Grad(corner), offset[1]); + } + + // 2 + t = 0.6f - dot(offset[2], offset[2]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + int(third.x) + Perm(index[1] + int(third.y) + Perm(index[2] + int(third.z)))); + n += t * t * dot(Grad(corner), offset[2]); } - return 32.0f * (n[0] + n[1] + n[2] + n[3]); + // 3 + t = 0.6f - dot(offset[3], offset[3]); + if (t > 0.0f) { + t *= t; + int corner = Perm12(index[0] + 1 + Perm(index[1] + 1 + Perm(index[2] + 1))); + n += t * t * dot(Grad(corner), offset[3]); + } + + return 32.0f * n; } -unsigned char SimplexNoise::Perm(size_t idx) const { +int SimplexNoise::Perm(int idx) const noexcept { return perm[idx]; } -const glm::vec3 &SimplexNoise::Grad(size_t idx) const { - return grad[idx % 12]; +int SimplexNoise::Perm12(int idx) const noexcept { + return perm12[idx]; +} + +const glm::vec3 &SimplexNoise::Grad(int idx) const noexcept { + return grad[idx]; } -WorleyNoise::WorleyNoise(unsigned int seed) +WorleyNoise::WorleyNoise(unsigned int seed) noexcept : seed(seed) , num_points(8) { } -float WorleyNoise::operator ()(const glm::vec3 &in) const { +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 @@ -141,21 +168,21 @@ float WorleyNoise::operator ()(const glm::vec3 &in) const { for (int z = -1; z <= 1; ++z) { for (int y = -1; y <= 1; ++y) { for (int x = -1; x <= 1; ++x) { - glm::tvec3 cube(center.x + x, center.y + y, center.z + z); + glm::vec3 cube(center.x + x, center.y + y, center.z + z); unsigned int cube_rand = - (cube.x * 130223) ^ - (cube.y * 159899) ^ - (cube.z * 190717) ^ + (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 % 200000) / 200000.0f; + point.x += float(cube_rand % 262144) / 262144.0f; cube_rand = 135899 * cube_rand + 189169; - point.y += float(cube_rand % 200000) / 200000.0f; + point.y += float(cube_rand % 262144) / 262144.0f; cube_rand = 159739 * cube_rand + 112139; - point.z += float(cube_rand % 200000) / 200000.0f; + point.z += float(cube_rand % 262144) / 262144.0f; glm::vec3 diff(in - point); float distance = sqrt(dot(diff, diff));