]> git.localhorst.tv Git - blank.git/commitdiff
rough (untested) implementation of Worley noise
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 17 Mar 2015 15:38:37 +0000 (16:38 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 17 Mar 2015 15:38:37 +0000 (16:38 +0100)
prng and distance formula may need some tweaking

src/noise.cpp
src/noise.hpp

index 1e1c6d86882703604475d2830812307b90db709a..4400a1a4988e0b57aee77e7d6b7ae73a522a4441 100644 (file)
@@ -109,4 +109,48 @@ 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 = 4.0f;
+
+       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 = dot(diff, diff);
+                                       if (distance < closest) {
+                                               closest = distance;
+                                       }
+                               }
+                       }
+               }
+       }
+
+       return closest;
+}
+
 }
index c081fc6a64f842dc45042385f5a4c761da219380..4c26de92eadf0ae3bacd679cc069e0337bf27861 100644 (file)
@@ -24,6 +24,21 @@ private:
 
 };
 
+
+/// implementation of Worley noise (aka Cell or Voroni noise)
+class WorleyNoise {
+
+public:
+       explicit WorleyNoise(unsigned int seed);
+
+       float operator ()(const glm::vec3 &) const;
+
+private:
+       const unsigned int seed;
+       const int num_points;
+
+};
+
 }
 
 #endif