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