]> git.localhorst.tv Git - blank.git/blobdiff - src/noise.cpp
normalize worley noise values to (-1,1)
[blank.git] / src / noise.cpp
index d192b27fcce8646109d310754b2d7e70c8536f75..c86f833ac3c126c894a53eeb5ad6df6c9c835d02 100644 (file)
@@ -3,8 +3,14 @@
 #include <cmath>
 
 
-namespace blank {
+namespace {
+
+constexpr float one_third = 1.0f/3.0f;
+constexpr float one_sixth = 1.0f/6.0f;
 
+}
+
+namespace blank {
 
 SimplexNoise::SimplexNoise(unsigned int seed)
 : grad({
@@ -25,17 +31,18 @@ SimplexNoise::SimplexNoise(unsigned int seed)
        for (size_t i = 0; i < 256; ++i) {
                val = 2346765 * val + 6446345;
                perm[i] = val % 256;
+               perm[i + 256] = perm[i];
        }
 }
 
 
 float SimplexNoise::operator ()(const glm::vec3 &in) const {
-       float skew = (in.x + in.y + in.z) / 3.0f;
+       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,14 +70,14 @@ 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]))),
@@ -95,11 +102,57 @@ float SimplexNoise::operator ()(const glm::vec3 &in) const {
 
 
 unsigned char SimplexNoise::Perm(size_t idx) const {
-       return perm[idx % 256];
+       return perm[idx];
 }
 
 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<int> 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;
+}
+
 }