[opengtl-commits] [450] add perlin noise

[ Thread Index | Date Index | More lists.tuxfamily.org/opengtl-commits Archives ]


Revision: 450
Author:   cyrille
Date:     2008-10-12 22:10:46 +0200 (Sun, 12 Oct 2008)

Log Message:
-----------
add perlin noise

Modified Paths:
--------------
    trunk/OpenGTL/OpenShiva/tests/imagegenerators/CMakeLists.txt

Added Paths:
-----------
    trunk/OpenGTL/OpenShiva/tests/imagegenerators/PerlinNoise.shiva


Modified: trunk/OpenGTL/OpenShiva/tests/imagegenerators/CMakeLists.txt
===================================================================
--- trunk/OpenGTL/OpenShiva/tests/imagegenerators/CMakeLists.txt	2008-10-12 19:54:43 UTC (rev 449)
+++ trunk/OpenGTL/OpenShiva/tests/imagegenerators/CMakeLists.txt	2008-10-12 20:10:46 UTC (rev 450)
@@ -3,6 +3,7 @@
   Gradient.shiva
   MandelbrotSet.shiva
   PlainGenerator.shiva
+  PerlinNoise.shiva
   )
 
 FOREACH( TEST_FILE ${TESTS_FILES} )

Added: trunk/OpenGTL/OpenShiva/tests/imagegenerators/PerlinNoise.shiva
===================================================================
--- trunk/OpenGTL/OpenShiva/tests/imagegenerators/PerlinNoise.shiva	                        (rev 0)
+++ trunk/OpenGTL/OpenShiva/tests/imagegenerators/PerlinNoise.shiva	2008-10-12 20:10:46 UTC (rev 450)
@@ -0,0 +1,92 @@
+kernel PerlinNoise
+{
+  const float amount = 0.5;
+  const float offset = 0.0;
+  const float factor = 1.0;
+  const float xscale = 4.0;
+  const float yscale = 4.0;
+  
+  const int width = 800;
+  const int height = 600;
+  
+  const int octaves = 2;
+  
+  const int RAND_MAX  = 2147483647;
+
+  dependent float grad_x[64];
+  dependent float grad_y[64];
+  dependent int permutation[64];
+  
+  float fade(float t)
+  {
+    return ((2.0*fabs(t)-3.0)*(t)*(t)+1.0);
+  }
+  
+  // Pregenerate grad_x, grad_y and permuation
+  void evaluateDependents()
+  {
+    // Init permutations
+    for(int i = 0; i < 64; ++i)
+    {
+       permutation[i] = i;
+    }
+    for (int i = 0; i < 1000; ++i) {
+      int j = rand() % 64;
+      int k = rand() % 64;
+      int t = permutation[j];
+      permutation[j] = permutation[k];
+      permutation[k] = t;
+    }
+    // Initialize the gradient table
+    for(int i = 0; i < 64; ++i)
+    {
+      grad_x[i] = rand() / float(RAND_MAX) - 0.5;
+      grad_y[i] = rand() / float(RAND_MAX) - 0.5;
+      float norm = 1.0 / sqrt(grad_x[i] * grad_x[i] + grad_y[i] * grad_y[i]);
+      grad_x[i] *= norm;
+      grad_y[i] *= norm;
+    }
+  }
+  
+  void evaluatePixel(out pixel4 result)
+  {
+    float total = 0.0;
+    int frequency = 1;
+    float x = (result.coord.x ) / float(width);
+    float y = (result.coord.y ) / float(height);
+    x *= xscale;
+    y *= yscale;
+    for(int oct = 0; oct < octaves; ++oct)
+    {
+      float xs = x * frequency;
+      float ys = y * frequency;
+      int xs_frac = int(xs);
+      int ys_frac = int(ys);
+      float intermtotal = 0.0;
+      for(int i = 0; i < 2; ++i)
+      {
+        for(int j = 0; j < 2; ++j)
+        {
+          int n = permutation[(xs_frac + i + permutation[(ys_frac + j) % 64]) % 64];
+          float vx = xs - xs_frac - i;
+          float vy = ys - ys_frac - j;
+          intermtotal += fade(vx) * fade(vy) * (grad_x[n] * vx + grad_y[n] * vy );
+        }
+      }
+      
+      total += intermtotal / frequency;
+      frequency *= 2;
+    }
+    total += offset;
+    total *= factor;
+    if( total > 1.0) total = 1.0;
+    if( total < 0) total = 0;
+    result = float4(total,total,total,1.0);
+  }
+  
+  region generated()
+  {
+    region reg1 = { 0, 0, 800, 600 };
+    return reg1;
+  }
+}


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/