graphics

experiments with opengl3.2/ogles3.3 on linux and win7
git clone http://frotz.net/git/graphics.git
Log | Files | Refs

simplexnoise.cc (20253B)


      1 /* SimplexNoise1234, Simplex noise with true analytic derivative in 1D to 4D.
      2  *
      3  * Author: Stefan Gustavson, 2003-2005
      4  * Contact: stegu@itn.liu.se
      5  * 
      6  * This code was GPL licensed until February 2011.  As the original
      7  * author of this code, I hereby release it irrevocably into the public
      8  * domain.  Please feel free to use it for whatever you want.  Credit
      9  * is appreciated where appropriate, and I also appreciate being told
     10  * where this code finds any use, but you may do as you like.
     11  * Alternatively, if you want to have a familiar OSI-approved license,
     12  * you may use This code under the terms of the MIT license:
     13  *
     14  * Copyright (C) 2003-2005 by Stefan Gustavson. All rights reserved.
     15  * This code is licensed to you under the terms of the MIT license:
     16  *
     17  * Permission is hereby granted, free of charge, to any person obtaining
     18  * a copy of this software and associated documentation files (the 
     19  * "Software"), to deal in the Software without restriction, including 
     20  * without limitation the rights to use, copy, modify, merge, publish, 
     21  * distribute, sublicense, and/or sell copies of the Software, and to 
     22  * permit persons to whom the Software is furnished to do so, subject 
     23  * to the following conditions:
     24  * 
     25  * The above copyright notice and this permission notice shall be 
     26  * included in all copies or substantial portions of the Software.
     27  *
     28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
     29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
     30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     31  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 
     32  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
     33  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
     34  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     35  *
     36  */
     37 
     38 /*
     39  * This implementation is "Simplex Noise" as presented by
     40  * Ken Perlin at a relatively obscure and not often cited course
     41  * session "Real-Time Shading" at Siggraph 2001 (before real
     42  * time shading actually took on), under the title "hardware noise".
     43  * The 3D function is numerically equivalent to his Java reference
     44  * code available in the PDF course notes, although I re-implemented
     45  * it from scratch to get more readable code. The 1D, 2D and 4D cases
     46  * were implemented from scratch by me from Ken Perlin's text.
     47  *
     48  * This file has no dependencies on any other file, not even its own
     49  * header file. The header file is made for use by external code only.
     50  */
     51 
     52 #define FASTFLOOR(x) ( ((x)>0) ? ((int)x) : (((int)x)-1) )
     53 
     54 //---------------------------------------------------------------------
     55 // Static data
     56 
     57 /*
     58  * Permutation table. This is just a random jumble of all numbers 0-255,
     59  * repeated twice to avoid wrapping the index at 255 for each lookup.
     60  * This needs to be exactly the same for all instances on all platforms,
     61  * so it's easiest to just keep it as static explicit data.
     62  * This also removes the need for any initialisation of this class.
     63  *
     64  * Note that making this an int[] instead of a char[] might make the
     65  * code run faster on platforms with a high penalty for unaligned single
     66  * byte addressing. Intel x86 is generally single-byte-friendly, but
     67  * some other CPUs are faster with 4-aligned reads.
     68  * However, a char[] is smaller, which avoids cache trashing, and that
     69  * is probably the most important aspect on most architectures.
     70  * This array is accessed a *lot* by the noise functions.
     71  * A vector-valued noise over 3D accesses it 96 times, and a
     72  * float-valued 4D noise 64 times. We want this to fit in the cache!
     73  */
     74 static unsigned char perm[512] = {151,160,137,91,90,15,
     75   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
     76   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
     77   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
     78   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
     79   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
     80   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
     81   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
     82   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
     83   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
     84   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
     85   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
     86   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180,
     87   151,160,137,91,90,15,
     88   131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23,
     89   190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33,
     90   88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166,
     91   77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244,
     92   102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196,
     93   135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123,
     94   5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42,
     95   223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9,
     96   129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228,
     97   251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107,
     98   49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254,
     99   138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 
    100 };
    101 
    102 //---------------------------------------------------------------------
    103 
    104 /*
    105  * Helper functions to compute gradients-dot-residualvectors (1D to 4D)
    106  * Note that these generate gradients of more than unit length. To make
    107  * a close match with the value range of classic Perlin noise, the final
    108  * noise values need to be rescaled to fit nicely within [-1,1].
    109  * (The simplex noise functions as such also have different scaling.)
    110  * Note also that these noise functions are the most practical and useful
    111  * signed version of Perlin noise. To return values according to the
    112  * RenderMan specification from the SL noise() and pnoise() functions,
    113  * the noise values need to be scaled and offset to [0,1], like this:
    114  * float SLnoise = (noise(x,y,z) + 1.0) * 0.5;
    115  */
    116 
    117 static float  grad1( int hash, float x ) {
    118     int h = hash & 15;
    119     float grad = 1.0f + (h & 7);   // Gradient value 1.0, 2.0, ..., 8.0
    120     if (h&8) grad = -grad;         // Set a random sign for the gradient
    121     return ( grad * x );           // Multiply the gradient with the distance
    122 }
    123 
    124 static float  grad2( int hash, float x, float y ) {
    125     int h = hash & 7;      // Convert low 3 bits of hash code
    126     float u = h<4 ? x : y;  // into 8 simple gradient directions,
    127     float v = h<4 ? y : x;  // and compute the dot product with (x,y).
    128     return ((h&1)? -u : u) + ((h&2)? -2.0f*v : 2.0f*v);
    129 }
    130 
    131 static float  grad3( int hash, float x, float y , float z ) {
    132     int h = hash & 15;     // Convert low 4 bits of hash code into 12 simple
    133     float u = h<8 ? x : y; // gradient directions, and compute dot product.
    134     float v = h<4 ? y : h==12||h==14 ? x : z; // Fix repeats at h = 12 to 15
    135     return ((h&1)? -u : u) + ((h&2)? -v : v);
    136 }
    137 
    138 static float  grad4( int hash, float x, float y, float z, float t ) {
    139     int h = hash & 31;      // Convert low 5 bits of hash code into 32 simple
    140     float u = h<24 ? x : y; // gradient directions, and compute dot product.
    141     float v = h<16 ? y : z;
    142     float w = h<8 ? z : t;
    143     return ((h&1)? -u : u) + ((h&2)? -v : v) + ((h&4)? -w : w);
    144 }
    145 
    146   // A lookup table to traverse the simplex around a given point in 4D.
    147   // Details can be found where this table is used, in the 4D noise method.
    148   /* TODO: This should not be required, backport it from Bill's GLSL code! */
    149 static unsigned char simplex[64][4] = {
    150     {0,1,2,3},{0,1,3,2},{0,0,0,0},{0,2,3,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,2,3,0},
    151     {0,2,1,3},{0,0,0,0},{0,3,1,2},{0,3,2,1},{0,0,0,0},{0,0,0,0},{0,0,0,0},{1,3,2,0},
    152     {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
    153     {1,2,0,3},{0,0,0,0},{1,3,0,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,3,0,1},{2,3,1,0},
    154     {1,0,2,3},{1,0,3,2},{0,0,0,0},{0,0,0,0},{0,0,0,0},{2,0,3,1},{0,0,0,0},{2,1,3,0},
    155     {0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},
    156     {2,0,1,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,0,1,2},{3,0,2,1},{0,0,0,0},{3,1,2,0},
    157     {2,1,0,3},{0,0,0,0},{0,0,0,0},{0,0,0,0},{3,1,0,2},{0,0,0,0},{3,2,0,1},{3,2,1,0}};
    158 
    159 // 1D simplex noise
    160 float snoise(float x) {
    161 
    162   int i0 = FASTFLOOR(x);
    163   int i1 = i0 + 1;
    164   float x0 = x - i0;
    165   float x1 = x0 - 1.0f;
    166 
    167   float n0, n1;
    168 
    169   float t0 = 1.0f - x0*x0;
    170 //  if(t0 < 0.0f) t0 = 0.0f; // this never happens for the 1D case
    171   t0 *= t0;
    172   n0 = t0 * t0 * grad1(perm[i0 & 0xff], x0);
    173 
    174   float t1 = 1.0f - x1*x1;
    175 //  if(t1 < 0.0f) t1 = 0.0f; // this never happens for the 1D case
    176   t1 *= t1;
    177   n1 = t1 * t1 * grad1(perm[i1 & 0xff], x1);
    178   // The maximum value of this noise is 8*(3/4)^4 = 2.53125
    179   // A factor of 0.395 would scale to fit exactly within [-1,1], but
    180   // we want to match PRMan's 1D noise, so we scale it down some more.
    181   return 0.25f * (n0 + n1);
    182 
    183 }
    184 
    185 // 2D simplex noise
    186 float snoise(float x, float y) {
    187 
    188 #define F2 0.366025403 // F2 = 0.5*(sqrt(3.0)-1.0)
    189 #define G2 0.211324865 // G2 = (3.0-Math.sqrt(3.0))/6.0
    190 
    191     float n0, n1, n2; // Noise contributions from the three corners
    192 
    193     // Skew the input space to determine which simplex cell we're in
    194     float s = (x+y)*F2; // Hairy factor for 2D
    195     float xs = x + s;
    196     float ys = y + s;
    197     int i = FASTFLOOR(xs);
    198     int j = FASTFLOOR(ys);
    199 
    200     float t = (float)(i+j)*G2;
    201     float X0 = i-t; // Unskew the cell origin back to (x,y) space
    202     float Y0 = j-t;
    203     float x0 = x-X0; // The x,y distances from the cell origin
    204     float y0 = y-Y0;
    205 
    206     // For the 2D case, the simplex shape is an equilateral triangle.
    207     // Determine which simplex we are in.
    208     int i1, j1; // Offsets for second (middle) corner of simplex in (i,j) coords
    209     if(x0>y0) {i1=1; j1=0;} // lower triangle, XY order: (0,0)->(1,0)->(1,1)
    210     else {i1=0; j1=1;}      // upper triangle, YX order: (0,0)->(0,1)->(1,1)
    211 
    212     // A step of (1,0) in (i,j) means a step of (1-c,-c) in (x,y), and
    213     // a step of (0,1) in (i,j) means a step of (-c,1-c) in (x,y), where
    214     // c = (3-sqrt(3))/6
    215 
    216     float x1 = x0 - i1 + G2; // Offsets for middle corner in (x,y) unskewed coords
    217     float y1 = y0 - j1 + G2;
    218     float x2 = x0 - 1.0f + 2.0f * G2; // Offsets for last corner in (x,y) unskewed coords
    219     float y2 = y0 - 1.0f + 2.0f * G2;
    220 
    221     // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
    222     int ii = i & 0xff;
    223     int jj = j & 0xff;
    224 
    225     // Calculate the contribution from the three corners
    226     float t0 = 0.5f - x0*x0-y0*y0;
    227     if(t0 < 0.0f) n0 = 0.0f;
    228     else {
    229       t0 *= t0;
    230       n0 = t0 * t0 * grad2(perm[ii+perm[jj]], x0, y0); 
    231     }
    232 
    233     float t1 = 0.5f - x1*x1-y1*y1;
    234     if(t1 < 0.0f) n1 = 0.0f;
    235     else {
    236       t1 *= t1;
    237       n1 = t1 * t1 * grad2(perm[ii+i1+perm[jj+j1]], x1, y1);
    238     }
    239 
    240     float t2 = 0.5f - x2*x2-y2*y2;
    241     if(t2 < 0.0f) n2 = 0.0f;
    242     else {
    243       t2 *= t2;
    244       n2 = t2 * t2 * grad2(perm[ii+1+perm[jj+1]], x2, y2);
    245     }
    246 
    247     // Add contributions from each corner to get the final noise value.
    248     // The result is scaled to return values in the interval [-1,1].
    249     return 40.0f * (n0 + n1 + n2); // TODO: The scale factor is preliminary!
    250   }
    251 
    252 // 3D simplex noise
    253 float snoise(float x, float y, float z) {
    254 
    255 // Simple skewing factors for the 3D case
    256 #define F3 0.333333333
    257 #define G3 0.166666667
    258 
    259     float n0, n1, n2, n3; // Noise contributions from the four corners
    260 
    261     // Skew the input space to determine which simplex cell we're in
    262     float s = (x+y+z)*F3; // Very nice and simple skew factor for 3D
    263     float xs = x+s;
    264     float ys = y+s;
    265     float zs = z+s;
    266     int i = FASTFLOOR(xs);
    267     int j = FASTFLOOR(ys);
    268     int k = FASTFLOOR(zs);
    269 
    270     float t = (float)(i+j+k)*G3; 
    271     float X0 = i-t; // Unskew the cell origin back to (x,y,z) space
    272     float Y0 = j-t;
    273     float Z0 = k-t;
    274     float x0 = x-X0; // The x,y,z distances from the cell origin
    275     float y0 = y-Y0;
    276     float z0 = z-Z0;
    277 
    278     // For the 3D case, the simplex shape is a slightly irregular tetrahedron.
    279     // Determine which simplex we are in.
    280     int i1, j1, k1; // Offsets for second corner of simplex in (i,j,k) coords
    281     int i2, j2, k2; // Offsets for third corner of simplex in (i,j,k) coords
    282 
    283 /* This code would benefit from a backport from the GLSL version! */
    284     if(x0>=y0) {
    285       if(y0>=z0)
    286         { i1=1; j1=0; k1=0; i2=1; j2=1; k2=0; } // X Y Z order
    287         else if(x0>=z0) { i1=1; j1=0; k1=0; i2=1; j2=0; k2=1; } // X Z Y order
    288         else { i1=0; j1=0; k1=1; i2=1; j2=0; k2=1; } // Z X Y order
    289       }
    290     else { // x0<y0
    291       if(y0<z0) { i1=0; j1=0; k1=1; i2=0; j2=1; k2=1; } // Z Y X order
    292       else if(x0<z0) { i1=0; j1=1; k1=0; i2=0; j2=1; k2=1; } // Y Z X order
    293       else { i1=0; j1=1; k1=0; i2=1; j2=1; k2=0; } // Y X Z order
    294     }
    295 
    296     // A step of (1,0,0) in (i,j,k) means a step of (1-c,-c,-c) in (x,y,z),
    297     // a step of (0,1,0) in (i,j,k) means a step of (-c,1-c,-c) in (x,y,z), and
    298     // a step of (0,0,1) in (i,j,k) means a step of (-c,-c,1-c) in (x,y,z), where
    299     // c = 1/6.
    300 
    301     float x1 = x0 - i1 + G3; // Offsets for second corner in (x,y,z) coords
    302     float y1 = y0 - j1 + G3;
    303     float z1 = z0 - k1 + G3;
    304     float x2 = x0 - i2 + 2.0f*G3; // Offsets for third corner in (x,y,z) coords
    305     float y2 = y0 - j2 + 2.0f*G3;
    306     float z2 = z0 - k2 + 2.0f*G3;
    307     float x3 = x0 - 1.0f + 3.0f*G3; // Offsets for last corner in (x,y,z) coords
    308     float y3 = y0 - 1.0f + 3.0f*G3;
    309     float z3 = z0 - 1.0f + 3.0f*G3;
    310 
    311     // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
    312     int ii = i & 0xff;
    313     int jj = j & 0xff;
    314     int kk = k & 0xff;
    315 
    316     // Calculate the contribution from the four corners
    317     float t0 = 0.6f - x0*x0 - y0*y0 - z0*z0;
    318     if(t0 < 0.0f) n0 = 0.0f;
    319     else {
    320       t0 *= t0;
    321       n0 = t0 * t0 * grad3(perm[ii+perm[jj+perm[kk]]], x0, y0, z0);
    322     }
    323 
    324     float t1 = 0.6f - x1*x1 - y1*y1 - z1*z1;
    325     if(t1 < 0.0f) n1 = 0.0f;
    326     else {
    327       t1 *= t1;
    328       n1 = t1 * t1 * grad3(perm[ii+i1+perm[jj+j1+perm[kk+k1]]], x1, y1, z1);
    329     }
    330 
    331     float t2 = 0.6f - x2*x2 - y2*y2 - z2*z2;
    332     if(t2 < 0.0f) n2 = 0.0f;
    333     else {
    334       t2 *= t2;
    335       n2 = t2 * t2 * grad3(perm[ii+i2+perm[jj+j2+perm[kk+k2]]], x2, y2, z2);
    336     }
    337 
    338     float t3 = 0.6f - x3*x3 - y3*y3 - z3*z3;
    339     if(t3<0.0f) n3 = 0.0f;
    340     else {
    341       t3 *= t3;
    342       n3 = t3 * t3 * grad3(perm[ii+1+perm[jj+1+perm[kk+1]]], x3, y3, z3);
    343     }
    344 
    345     // Add contributions from each corner to get the final noise value.
    346     // The result is scaled to stay just inside [-1,1]
    347     return 32.0f * (n0 + n1 + n2 + n3); // TODO: The scale factor is preliminary!
    348   }
    349 
    350 
    351 // 4D simplex noise
    352 float snoise(float x, float y, float z, float w) {
    353   
    354   // The skewing and unskewing factors are hairy again for the 4D case
    355 #define F4 0.309016994 // F4 = (Math.sqrt(5.0)-1.0)/4.0
    356 #define G4 0.138196601 // G4 = (5.0-Math.sqrt(5.0))/20.0
    357 
    358     float n0, n1, n2, n3, n4; // Noise contributions from the five corners
    359 
    360     // Skew the (x,y,z,w) space to determine which cell of 24 simplices we're in
    361     float s = (x + y + z + w) * F4; // Factor for 4D skewing
    362     float xs = x + s;
    363     float ys = y + s;
    364     float zs = z + s;
    365     float ws = w + s;
    366     int i = FASTFLOOR(xs);
    367     int j = FASTFLOOR(ys);
    368     int k = FASTFLOOR(zs);
    369     int l = FASTFLOOR(ws);
    370 
    371     float t = (i + j + k + l) * G4; // Factor for 4D unskewing
    372     float X0 = i - t; // Unskew the cell origin back to (x,y,z,w) space
    373     float Y0 = j - t;
    374     float Z0 = k - t;
    375     float W0 = l - t;
    376 
    377     float x0 = x - X0;  // The x,y,z,w distances from the cell origin
    378     float y0 = y - Y0;
    379     float z0 = z - Z0;
    380     float w0 = w - W0;
    381 
    382     // For the 4D case, the simplex is a 4D shape I won't even try to describe.
    383     // To find out which of the 24 possible simplices we're in, we need to
    384     // determine the magnitude ordering of x0, y0, z0 and w0.
    385     // The method below is a good way of finding the ordering of x,y,z,w and
    386     // then find the correct traversal order for the simplex we’re in.
    387     // First, six pair-wise comparisons are performed between each possible pair
    388     // of the four coordinates, and the results are used to add up binary bits
    389     // for an integer index.
    390     int c1 = (x0 > y0) ? 32 : 0;
    391     int c2 = (x0 > z0) ? 16 : 0;
    392     int c3 = (y0 > z0) ? 8 : 0;
    393     int c4 = (x0 > w0) ? 4 : 0;
    394     int c5 = (y0 > w0) ? 2 : 0;
    395     int c6 = (z0 > w0) ? 1 : 0;
    396     int c = c1 + c2 + c3 + c4 + c5 + c6;
    397 
    398     int i1, j1, k1, l1; // The integer offsets for the second simplex corner
    399     int i2, j2, k2, l2; // The integer offsets for the third simplex corner
    400     int i3, j3, k3, l3; // The integer offsets for the fourth simplex corner
    401 
    402     // simplex[c] is a 4-vector with the numbers 0, 1, 2 and 3 in some order.
    403     // Many values of c will never occur, since e.g. x>y>z>w makes x<z, y<w and x<w
    404     // impossible. Only the 24 indices which have non-zero entries make any sense.
    405     // We use a thresholding to set the coordinates in turn from the largest magnitude.
    406     // The number 3 in the "simplex" array is at the position of the largest coordinate.
    407     i1 = simplex[c][0]>=3 ? 1 : 0;
    408     j1 = simplex[c][1]>=3 ? 1 : 0;
    409     k1 = simplex[c][2]>=3 ? 1 : 0;
    410     l1 = simplex[c][3]>=3 ? 1 : 0;
    411     // The number 2 in the "simplex" array is at the second largest coordinate.
    412     i2 = simplex[c][0]>=2 ? 1 : 0;
    413     j2 = simplex[c][1]>=2 ? 1 : 0;
    414     k2 = simplex[c][2]>=2 ? 1 : 0;
    415     l2 = simplex[c][3]>=2 ? 1 : 0;
    416     // The number 1 in the "simplex" array is at the second smallest coordinate.
    417     i3 = simplex[c][0]>=1 ? 1 : 0;
    418     j3 = simplex[c][1]>=1 ? 1 : 0;
    419     k3 = simplex[c][2]>=1 ? 1 : 0;
    420     l3 = simplex[c][3]>=1 ? 1 : 0;
    421     // The fifth corner has all coordinate offsets = 1, so no need to look that up.
    422 
    423     float x1 = x0 - i1 + G4; // Offsets for second corner in (x,y,z,w) coords
    424     float y1 = y0 - j1 + G4;
    425     float z1 = z0 - k1 + G4;
    426     float w1 = w0 - l1 + G4;
    427     float x2 = x0 - i2 + 2.0f*G4; // Offsets for third corner in (x,y,z,w) coords
    428     float y2 = y0 - j2 + 2.0f*G4;
    429     float z2 = z0 - k2 + 2.0f*G4;
    430     float w2 = w0 - l2 + 2.0f*G4;
    431     float x3 = x0 - i3 + 3.0f*G4; // Offsets for fourth corner in (x,y,z,w) coords
    432     float y3 = y0 - j3 + 3.0f*G4;
    433     float z3 = z0 - k3 + 3.0f*G4;
    434     float w3 = w0 - l3 + 3.0f*G4;
    435     float x4 = x0 - 1.0f + 4.0f*G4; // Offsets for last corner in (x,y,z,w) coords
    436     float y4 = y0 - 1.0f + 4.0f*G4;
    437     float z4 = z0 - 1.0f + 4.0f*G4;
    438     float w4 = w0 - 1.0f + 4.0f*G4;
    439 
    440     // Wrap the integer indices at 256, to avoid indexing perm[] out of bounds
    441     int ii = i & 0xff;
    442     int jj = j & 0xff;
    443     int kk = k & 0xff;
    444     int ll = l & 0xff;
    445 
    446     // Calculate the contribution from the five corners
    447     float t0 = 0.6f - x0*x0 - y0*y0 - z0*z0 - w0*w0;
    448     if(t0 < 0.0f) n0 = 0.0f;
    449     else {
    450       t0 *= t0;
    451       n0 = t0 * t0 * grad4(perm[ii+perm[jj+perm[kk+perm[ll]]]], x0, y0, z0, w0);
    452     }
    453 
    454    float t1 = 0.6f - x1*x1 - y1*y1 - z1*z1 - w1*w1;
    455     if(t1 < 0.0f) n1 = 0.0f;
    456     else {
    457       t1 *= t1;
    458       n1 = t1 * t1 * grad4(perm[ii+i1+perm[jj+j1+perm[kk+k1+perm[ll+l1]]]], x1, y1, z1, w1);
    459     }
    460 
    461    float t2 = 0.6f - x2*x2 - y2*y2 - z2*z2 - w2*w2;
    462     if(t2 < 0.0f) n2 = 0.0f;
    463     else {
    464       t2 *= t2;
    465       n2 = t2 * t2 * grad4(perm[ii+i2+perm[jj+j2+perm[kk+k2+perm[ll+l2]]]], x2, y2, z2, w2);
    466     }
    467 
    468    float t3 = 0.6f - x3*x3 - y3*y3 - z3*z3 - w3*w3;
    469     if(t3 < 0.0f) n3 = 0.0f;
    470     else {
    471       t3 *= t3;
    472       n3 = t3 * t3 * grad4(perm[ii+i3+perm[jj+j3+perm[kk+k3+perm[ll+l3]]]], x3, y3, z3, w3);
    473     }
    474 
    475    float t4 = 0.6f - x4*x4 - y4*y4 - z4*z4 - w4*w4;
    476     if(t4 < 0.0f) n4 = 0.0f;
    477     else {
    478       t4 *= t4;
    479       n4 = t4 * t4 * grad4(perm[ii+1+perm[jj+1+perm[kk+1+perm[ll+1]]]], x4, y4, z4, w4);
    480     }
    481 
    482     // Sum up and scale the result to cover the range [-1,1]
    483     return 27.0f * (n0 + n1 + n2 + n3 + n4); // TODO: The scale factor is preliminary!
    484 }