X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fnoise.cpp;h=6d59224c2f41f4de89ae982627387fcacd7235a2;hb=e74f1ad236429f05db90c0ace825277e2a3fbc05;hp=1e1c6d86882703604475d2830812307b90db709a;hpb=e70967c971f77a4ac0f5c074e6eb94bdd0e2b7ab;p=blank.git diff --git a/src/noise.cpp b/src/noise.cpp index 1e1c6d8..6d59224 100644 --- a/src/noise.cpp +++ b/src/noise.cpp @@ -12,6 +12,24 @@ constexpr float one_sixth = 1.0f/6.0f; namespace blank { +GaloisLFSR::GaloisLFSR(std::uint64_t seed) +: state(seed) { + +} + +bool GaloisLFSR::operator ()() { + bool result = state & 1; + state >>= 1; + if (result) { + state |= 0x8000000000000000; + state ^= mask; + } else { + state &= 0x7FFFFFFFFFFFFFFF; + } + return result; +} + + SimplexNoise::SimplexNoise(unsigned int seed) : grad({ { 1.0f, 1.0f, 0.0f }, @@ -109,4 +127,50 @@ const glm::vec3 &SimplexNoise::Grad(size_t idx) const { return grad[idx % 12]; } + +WorleyNoise::WorleyNoise(unsigned int seed) +: seed(seed) +, num_points(8) { + +} + +float WorleyNoise::operator ()(const glm::vec3 &in) const { + 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::tvec3 cube(center.x + x, center.y + y, center.z + z); + unsigned int cube_rand = + (cube.x * 130223) ^ + (cube.y * 159899) ^ + (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; + cube_rand = 135899 * cube_rand + 189169; + point.y += float(cube_rand % 200000) / 200000.0f; + cube_rand = 159739 * cube_rand + 112139; + point.z += float(cube_rand % 200000) / 200000.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; +} + }