6 |
6 |
7 #include <math.h> |
7 #include <math.h> |
8 |
8 |
9 #include "noise.h" |
9 #include "noise.h" |
10 |
10 |
11 double fade(double t) |
11 Float fade(Float t) |
12 { |
12 { |
13 return t * t * t * (t * (t * 6 - 15) + 10); |
13 return t * t * t * (t * (t * 6 - 15) + 10); |
14 } |
14 } |
15 |
15 |
16 double lerp(double t, double a, double b) |
16 Float lerp(Float t, Float a, Float b) |
17 { |
17 { |
18 return a + t * (b - a); |
18 return a + t * (b - a); |
19 } |
19 } |
20 |
20 |
21 double grad(unsigned char hash, double x, double y, double z) |
21 Float grad(unsigned char hash, Float x, Float y, Float z) |
22 { |
22 { |
23 unsigned char h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE |
23 unsigned char h = hash & 15; // CONVERT LO 4 BITS OF HASH CODE |
24 double u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS. |
24 Float u = h<8 ? x : y, // INTO 12 GRADIENT DIRECTIONS. |
25 v = h<4 ? y : h==12||h==14 ? x : z; |
25 v = h<4 ? y : h==12||h==14 ? x : z; |
26 return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v); |
26 return ((h&1) == 0 ? u : -u) + ((h&2) == 0 ? v : -v); |
27 } |
27 } |
28 |
28 |
29 unsigned char p[512] = { 151,160,137,91,90,15, |
29 unsigned char p[512] = { 151,160,137,91,90,15, |
54 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, |
54 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, |
55 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 |
55 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180 |
56 }; |
56 }; |
57 |
57 |
58 |
58 |
59 double perlin(double x, double y, double z) |
59 Float perlin(Float x, Float y, Float z) |
60 { |
60 { |
61 int X = (int)floor(x) & 255, // FIND UNIT CUBE THAT |
61 int X = (int)floor(x) & 255, // FIND UNIT CUBE THAT |
62 Y = (int)floor(y) & 255, // CONTAINS POINT. |
62 Y = (int)floor(y) & 255, // CONTAINS POINT. |
63 Z = (int)floor(z) & 255; |
63 Z = (int)floor(z) & 255; |
64 x -= floor(x); // FIND RELATIVE X,Y,Z |
64 x -= floor(x); // FIND RELATIVE X,Y,Z |
65 y -= floor(y); // OF POINT IN CUBE. |
65 y -= floor(y); // OF POINT IN CUBE. |
66 z -= floor(z); |
66 z -= floor(z); |
67 double u = fade(x), // COMPUTE FADE CURVES |
67 Float u = fade(x), // COMPUTE FADE CURVES |
68 v = fade(y), // FOR EACH OF X,Y,Z. |
68 v = fade(y), // FOR EACH OF X,Y,Z. |
69 w = fade(z); |
69 w = fade(z); |
70 int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF |
70 int A = p[X ]+Y, AA = p[A]+Z, AB = p[A+1]+Z, // HASH COORDINATES OF |
71 B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS, |
71 B = p[X+1]+Y, BA = p[B]+Z, BB = p[B+1]+Z; // THE 8 CUBE CORNERS, |
72 |
72 |