5 /* ---- Ken Perlin's implementation of noise ---- */
9 #define NP 12 /* 2^N */
12 #define s_curve(t) (t * t * (3.0f - 2.0f * t))
14 #define setup(elem, b0, b1, r0, r1) \
23 #define setup_p(elem, b0, b1, r0, r1, p) \
26 b0 = (((int)t) & BM) % p; \
27 b1 = ((b0 + 1) & BM) % p; \
33 static int perm[B + B + 2]; /* permuted index from g_n onto themselves */
34 static float grad3[B + B + 2][3]; /* 3D random gradients */
35 static float grad2[B + B + 2][2]; /* 2D random gradients */
36 static float grad1[B + B + 2]; /* 1D random ... slopes */
37 static int tables_valid;
39 #define init_once() if(!tables_valid) init_noise()
41 static void init_noise()
46 /* calculate random gradients */
48 perm[i] = i; /* .. and initialize permutation mapping to identity */
50 grad1[i] = (float)((rand() % (B + B)) - B) / B;
52 grad2[i][0] = (float)((rand() % (B + B)) - B) / B;
53 grad2[i][1] = (float)((rand() % (B + B)) - B) / B;
54 if((len = sqrt(grad2[i][0] * grad2[i][0] + grad2[i][1] * grad2[i][1])) != 0.0f) {
59 grad3[i][0] = (float)((rand() % (B + B)) - B) / B;
60 grad3[i][1] = (float)((rand() % (B + B)) - B) / B;
61 grad3[i][2] = (float)((rand() % (B + B)) - B) / B;
62 if((len = sqrt(grad3[i][0] * grad3[i][0] + grad3[i][1] * grad3[i][1]) + grad3[i][2] * grad3[i][2]) != 0.0f) {
69 /* permute indices by swapping them randomly */
71 int rand_idx = rand() % B;
74 perm[i] = perm[rand_idx];
78 /* fill up the rest of the arrays by duplicating the existing gradients */
79 /* and permutations */
80 for(i=0; i<B+2; i++) {
81 perm[B + i] = perm[i];
82 grad1[B + i] = grad1[i];
83 grad2[B + i][0] = grad2[i][0];
84 grad2[B + i][1] = grad2[i][1];
85 grad3[B + i][0] = grad3[i][0];
86 grad3[B + i][1] = grad3[i][1];
87 grad3[B + i][2] = grad3[i][2];
93 #define lerp(a, b, t) ((a) + ((b) - (a)) * (t))
94 #define dotgrad2(g, x, y) ((g)[0] * (x) + (g)[1] * (y))
95 #define dotgrad3(g, x, y, z) ((g)[0] * (x) + (g)[1] * (y) + (g)[2] * (z))
100 float rx0, rx1, sx, u, v;
104 setup(x, bx0, bx1, rx0, rx1);
106 u = rx0 * grad1[perm[bx0]];
107 v = rx1 * grad1[perm[bx1]];
108 return lerp(u, v, sx);
111 float noise2(float x, float y)
113 int i, j, b00, b10, b01, b11;
114 int bx0, bx1, by0, by1;
115 float rx0, rx1, ry0, ry1;
116 float sx, sy, u, v, a, b;
120 setup(x, bx0, bx1, rx0, rx1);
121 setup(y, by0, by1, ry0, ry1);
131 /* calculate hermite inteprolating factors */
135 /* interpolate along the left edge */
136 u = dotgrad2(grad2[b00], rx0, ry0);
137 v = dotgrad2(grad2[b10], rx1, ry0);
140 /* interpolate along the right edge */
141 u = dotgrad2(grad2[b01], rx0, ry1);
142 v = dotgrad2(grad2[b11], rx1, ry1);
145 /* interpolate between them */
146 return lerp(a, b, sy);
149 float noise3(float x, float y, float z)
152 int bx0, bx1, by0, by1, bz0, bz1;
153 int b00, b10, b01, b11;
154 float rx0, rx1, ry0, ry1, rz0, rz1;
156 float u, v, a, b, c, d;
160 setup(x, bx0, bx1, rx0, rx1);
161 setup(y, by0, by1, ry0, ry1);
162 setup(z, bz0, bz1, rz0, rz1);
172 /* calculate hermite interpolating factors */
177 /* interpolate along the top slice of the cell */
178 u = dotgrad3(grad3[b00 + bz0], rx0, ry0, rz0);
179 v = dotgrad3(grad3[b10 + bz0], rx1, ry0, rz0);
182 u = dotgrad3(grad3[b01 + bz0], rx0, ry1, rz0);
183 v = dotgrad3(grad3[b11 + bz0], rx1, ry1, rz0);
188 /* interpolate along the bottom slice of the cell */
189 u = dotgrad3(grad3[b00 + bz1], rx0, ry0, rz1);
190 v = dotgrad3(grad3[b10 + bz1], rx1, ry0, rz1);
193 u = dotgrad3(grad3[b01 + bz1], rx0, ry1, rz1);
194 v = dotgrad3(grad3[b11 + bz1], rx1, ry1, rz1);
199 /* interpolate between slices */
200 return lerp(c, d, sz);
203 float noise4(float x, float y, float z, float w)
209 float pnoise1(float x, int period)
212 float rx0, rx1, sx, u, v;
216 setup_p(x, bx0, bx1, rx0, rx1, period);
218 u = rx0 * grad1[perm[bx0]];
219 v = rx1 * grad1[perm[bx1]];
220 return lerp(u, v, sx);
223 float pnoise2(float x, float y, int per_x, int per_y)
225 int i, j, b00, b10, b01, b11;
226 int bx0, bx1, by0, by1;
227 float rx0, rx1, ry0, ry1;
228 float sx, sy, u, v, a, b;
232 setup_p(x, bx0, bx1, rx0, rx1, per_x);
233 setup_p(y, by0, by1, ry0, ry1, per_y);
243 /* calculate hermite inteprolating factors */
247 /* interpolate along the left edge */
248 u = dotgrad2(grad2[b00], rx0, ry0);
249 v = dotgrad2(grad2[b10], rx1, ry0);
252 /* interpolate along the right edge */
253 u = dotgrad2(grad2[b01], rx0, ry1);
254 v = dotgrad2(grad2[b11], rx1, ry1);
257 /* interpolate between them */
258 return lerp(a, b, sy);
261 float pnoise3(float x, float y, float z, int per_x, int per_y, int per_z)
264 int bx0, bx1, by0, by1, bz0, bz1;
265 int b00, b10, b01, b11;
266 float rx0, rx1, ry0, ry1, rz0, rz1;
268 float u, v, a, b, c, d;
272 setup_p(x, bx0, bx1, rx0, rx1, per_x);
273 setup_p(y, by0, by1, ry0, ry1, per_y);
274 setup_p(z, bz0, bz1, rz0, rz1, per_z);
284 /* calculate hermite interpolating factors */
289 /* interpolate along the top slice of the cell */
290 u = dotgrad3(grad3[b00 + bz0], rx0, ry0, rz0);
291 v = dotgrad3(grad3[b10 + bz0], rx1, ry0, rz0);
294 u = dotgrad3(grad3[b01 + bz0], rx0, ry1, rz0);
295 v = dotgrad3(grad3[b11 + bz0], rx1, ry1, rz0);
300 /* interpolate along the bottom slice of the cell */
301 u = dotgrad3(grad3[b00 + bz1], rx0, ry0, rz1);
302 v = dotgrad3(grad3[b10 + bz1], rx1, ry0, rz1);
305 u = dotgrad3(grad3[b01 + bz1], rx0, ry1, rz1);
306 v = dotgrad3(grad3[b11 + bz1], rx1, ry1, rz1);
311 /* interpolate between slices */
312 return lerp(c, d, sz);
315 float pnoise4(float x, float y, float z, float w, int per_x, int per_y, int per_z, int per_w)
321 float fbm1(float x, int octaves)
324 float res = 0.0f, freq = 1.0f;
325 for(i=0; i<octaves; i++) {
326 res += noise1(x * freq) / freq;
332 float fbm2(float x, float y, int octaves)
335 float res = 0.0f, freq = 1.0f;
336 for(i=0; i<octaves; i++) {
337 res += noise2(x * freq, y * freq) / freq;
343 float fbm3(float x, float y, float z, int octaves)
346 float res = 0.0f, freq = 1.0f;
347 for(i=0; i<octaves; i++) {
348 res += noise3(x * freq, y * freq, z * freq) / freq;
355 float fbm4(float x, float y, float z, float w, int octaves)
358 float res = 0.0f, freq = 1.0f;
359 for(i=0; i<octaves; i++) {
360 res += noise4(x * freq, y * freq, z * freq, w * freq) / freq;
367 float pfbm1(float x, int per, int octaves)
370 float res = 0.0f, freq = 1.0f;
371 for(i=0; i<octaves; i++) {
372 res += pnoise1(x * freq, per) / freq;
379 float pfbm2(float x, float y, int per_x, int per_y, int octaves)
382 float res = 0.0f, freq = 1.0f;
383 for(i=0; i<octaves; i++) {
384 res += pnoise2(x * freq, y * freq, per_x, per_y) / freq;
392 float pfbm3(float x, float y, float z, int per_x, int per_y, int per_z, int octaves)
395 float res = 0.0f, freq = 1.0f;
396 for(i=0; i<octaves; i++) {
397 res += pnoise3(x * freq, y * freq, z * freq, per_x, per_y, per_z) / freq;
406 float pfbm4(float x, float y, float z, float w, int per_x, int per_y, int per_z, int per_w, int octaves)
409 float res = 0.0f, freq = 1.0f;
410 for(i=0; i<octaves; i++) {
411 res += pnoise4(x * freq, y * freq, z * freq, w * freq,
412 per_x, per_y, per_z, per_w) / freq;
423 float turbulence1(float x, int octaves)
426 float res = 0.0f, freq = 1.0f;
427 for(i=0; i<octaves; i++) {
428 res += fabs(noise1(x * freq) / freq);
434 float turbulence2(float x, float y, int octaves)
437 float res = 0.0f, freq = 1.0f;
438 for(i=0; i<octaves; i++) {
439 res += fabs(noise2(x * freq, y * freq) / freq);
445 float turbulence3(float x, float y, float z, int octaves)
448 float res = 0.0f, freq = 1.0f;
449 for(i=0; i<octaves; i++) {
450 res += fabs(noise3(x * freq, y * freq, z * freq) / freq);
456 float turbulence4(float x, float y, float z, float w, int octaves)
459 float res = 0.0f, freq = 1.0f;
460 for(i=0; i<octaves; i++) {
461 res += fabs(noise4(x * freq, y * freq, z * freq, w * freq) / freq);
468 float pturbulence1(float x, int per, int octaves)
471 float res = 0.0f, freq = 1.0f;
472 for(i=0; i<octaves; i++) {
473 res += fabs(pnoise1(x * freq, per) / freq);
480 float pturbulence2(float x, float y, int per_x, int per_y, int octaves)
483 float res = 0.0f, freq = 1.0f;
484 for(i=0; i<octaves; i++) {
485 res += fabs(pnoise2(x * freq, y * freq, per_x, per_y) / freq);
493 float pturbulence3(float x, float y, float z, int per_x, int per_y, int per_z, int octaves)
496 float res = 0.0f, freq = 1.0f;
497 for(i=0; i<octaves; i++) {
498 res += fabs(pnoise3(x * freq, y * freq, z * freq, per_x, per_y, per_z) / freq);
507 float pturbulence4(float x, float y, float z, float w, int per_x, int per_y, int per_z, int per_w, int octaves)
510 float res = 0.0f, freq = 1.0f;
511 for(i=0; i<octaves; i++) {
512 res += fabs(pnoise4(x * freq, y * freq, z * freq, w * freq,
513 per_x, per_y, per_z, per_w) / freq);