1 | #include <math.h> |
---|
2 | #include <stdlib.h> |
---|
3 | |
---|
4 | /* Coherent noise function over 1, 2 or 3 dimensions */ |
---|
5 | /* (copyright Ken Perlin) */ |
---|
6 | |
---|
7 | #define MAXB 0x100 |
---|
8 | #define N 0x1000 |
---|
9 | #define NP 12 /* 2^N */ |
---|
10 | #define NM 0xfff |
---|
11 | |
---|
12 | #define s_curve(t) ( t * t * (3. - 2. * t) ) |
---|
13 | #define lerp(t, a, b) ( a + t * (b - a) ) |
---|
14 | #define setup(i,b0,b1,r0,r1)\ |
---|
15 | t = vec[i] + N;\ |
---|
16 | b0 = ((int)t) & BM;\ |
---|
17 | b1 = (b0+1) & BM;\ |
---|
18 | r0 = t - (int)t;\ |
---|
19 | r1 = r0 - 1.; |
---|
20 | #define at2(rx,ry) ( rx * q[0] + ry * q[1] ) |
---|
21 | #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] ) |
---|
22 | |
---|
23 | static void initNoise(void); |
---|
24 | |
---|
25 | static int p[MAXB + MAXB + 2]; |
---|
26 | static double g3[MAXB + MAXB + 2][3]; |
---|
27 | static double g2[MAXB + MAXB + 2][2]; |
---|
28 | static double g1[MAXB + MAXB + 2]; |
---|
29 | |
---|
30 | int start; |
---|
31 | int B; |
---|
32 | int BM; |
---|
33 | |
---|
34 | |
---|
35 | void SetNoiseFrequency(int frequency) |
---|
36 | { |
---|
37 | start = 1; |
---|
38 | B = frequency; |
---|
39 | BM = B-1; |
---|
40 | } |
---|
41 | |
---|
42 | double noise1(double arg) |
---|
43 | { |
---|
44 | int bx0, bx1; |
---|
45 | double rx0, rx1, sx, t, u, v, vec[1]; |
---|
46 | |
---|
47 | vec[0] = arg; |
---|
48 | if (start) { |
---|
49 | start = 0; |
---|
50 | initNoise(); |
---|
51 | } |
---|
52 | |
---|
53 | setup(0,bx0,bx1,rx0,rx1); |
---|
54 | |
---|
55 | sx = s_curve(rx0); |
---|
56 | u = rx0 * g1[ p[ bx0 ] ]; |
---|
57 | v = rx1 * g1[ p[ bx1 ] ]; |
---|
58 | |
---|
59 | return(lerp(sx, u, v)); |
---|
60 | } |
---|
61 | |
---|
62 | double noise2(double vec[2]) |
---|
63 | { |
---|
64 | int bx0, bx1, by0, by1, b00, b10, b01, b11; |
---|
65 | double rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v; |
---|
66 | int i, j; |
---|
67 | |
---|
68 | if (start) { |
---|
69 | start = 0; |
---|
70 | initNoise(); |
---|
71 | } |
---|
72 | |
---|
73 | setup(0, bx0,bx1, rx0,rx1); |
---|
74 | setup(1, by0,by1, ry0,ry1); |
---|
75 | |
---|
76 | i = p[ bx0 ]; |
---|
77 | j = p[ bx1 ]; |
---|
78 | |
---|
79 | b00 = p[ i + by0 ]; |
---|
80 | b10 = p[ j + by0 ]; |
---|
81 | b01 = p[ i + by1 ]; |
---|
82 | b11 = p[ j + by1 ]; |
---|
83 | |
---|
84 | sx = s_curve(rx0); |
---|
85 | sy = s_curve(ry0); |
---|
86 | |
---|
87 | q = g2[ b00 ] ; u = at2(rx0,ry0); |
---|
88 | q = g2[ b10 ] ; v = at2(rx1,ry0); |
---|
89 | a = lerp(sx, u, v); |
---|
90 | |
---|
91 | q = g2[ b01 ] ; u = at2(rx0,ry1); |
---|
92 | q = g2[ b11 ] ; v = at2(rx1,ry1); |
---|
93 | b = lerp(sx, u, v); |
---|
94 | |
---|
95 | return lerp(sy, a, b); |
---|
96 | } |
---|
97 | |
---|
98 | double noise3(double vec[3]) |
---|
99 | { |
---|
100 | int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11; |
---|
101 | double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v; |
---|
102 | int i, j; |
---|
103 | |
---|
104 | if (start) { |
---|
105 | start = 0; |
---|
106 | initNoise(); |
---|
107 | } |
---|
108 | |
---|
109 | setup(0, bx0,bx1, rx0,rx1); |
---|
110 | setup(1, by0,by1, ry0,ry1); |
---|
111 | setup(2, bz0,bz1, rz0,rz1); |
---|
112 | |
---|
113 | i = p[ bx0 ]; |
---|
114 | j = p[ bx1 ]; |
---|
115 | |
---|
116 | b00 = p[ i + by0 ]; |
---|
117 | b10 = p[ j + by0 ]; |
---|
118 | b01 = p[ i + by1 ]; |
---|
119 | b11 = p[ j + by1 ]; |
---|
120 | |
---|
121 | t = s_curve(rx0); |
---|
122 | sy = s_curve(ry0); |
---|
123 | sz = s_curve(rz0); |
---|
124 | |
---|
125 | q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0); |
---|
126 | q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0); |
---|
127 | a = lerp(t, u, v); |
---|
128 | |
---|
129 | q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0); |
---|
130 | q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0); |
---|
131 | b = lerp(t, u, v); |
---|
132 | |
---|
133 | c = lerp(sy, a, b); |
---|
134 | |
---|
135 | q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1); |
---|
136 | q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1); |
---|
137 | a = lerp(t, u, v); |
---|
138 | |
---|
139 | q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1); |
---|
140 | q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1); |
---|
141 | b = lerp(t, u, v); |
---|
142 | |
---|
143 | d = lerp(sy, a, b); |
---|
144 | |
---|
145 | //fprintf(stderr, "%f\n", lerp(sz, c, d)); |
---|
146 | |
---|
147 | return lerp(sz, c, d); |
---|
148 | } |
---|
149 | |
---|
150 | void normalize2(double v[2]) |
---|
151 | { |
---|
152 | double s; |
---|
153 | |
---|
154 | s = sqrt(v[0] * v[0] + v[1] * v[1]); |
---|
155 | v[0] = v[0] / s; |
---|
156 | v[1] = v[1] / s; |
---|
157 | } |
---|
158 | |
---|
159 | void normalize3(double v[3]) |
---|
160 | { |
---|
161 | double s; |
---|
162 | |
---|
163 | s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); |
---|
164 | v[0] = v[0] / s; |
---|
165 | v[1] = v[1] / s; |
---|
166 | v[2] = v[2] / s; |
---|
167 | } |
---|
168 | |
---|
169 | void initNoise(void) |
---|
170 | { |
---|
171 | int i, j, k; |
---|
172 | |
---|
173 | srand(30757); |
---|
174 | for (i = 0 ; i < B ; i++) { |
---|
175 | p[i] = i; |
---|
176 | g1[i] = (double)((rand() % (B + B)) - B) / B; |
---|
177 | |
---|
178 | for (j = 0 ; j < 2 ; j++) |
---|
179 | g2[i][j] = (double)((rand() % (B + B)) - B) / B; |
---|
180 | normalize2(g2[i]); |
---|
181 | |
---|
182 | for (j = 0 ; j < 3 ; j++) |
---|
183 | g3[i][j] = (double)((rand() % (B + B)) - B) / B; |
---|
184 | normalize3(g3[i]); |
---|
185 | } |
---|
186 | |
---|
187 | while (--i) { |
---|
188 | k = p[i]; |
---|
189 | p[i] = p[j = rand() % B]; |
---|
190 | p[j] = k; |
---|
191 | } |
---|
192 | |
---|
193 | for (i = 0 ; i < B + 2 ; i++) { |
---|
194 | p[B + i] = p[i]; |
---|
195 | g1[B + i] = g1[i]; |
---|
196 | for (j = 0 ; j < 2 ; j++) |
---|
197 | g2[B + i][j] = g2[i][j]; |
---|
198 | for (j = 0 ; j < 3 ; j++) |
---|
199 | g3[B + i][j] = g3[i][j]; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | /* --- My harmonic summing functions - PDB --------------------------*/ |
---|
204 | |
---|
205 | /* |
---|
206 | In what follows "alpha" is the weight when the sum is formed. |
---|
207 | Typically it is 2, As this approaches 1 the function is noisier. |
---|
208 | "beta" is the harmonic scaling/spacing, typically 2. |
---|
209 | */ |
---|
210 | |
---|
211 | double PerlinNoise1D(double x,double alpha,double beta,int n) |
---|
212 | { |
---|
213 | int i; |
---|
214 | double val,sum = 0; |
---|
215 | double p,scale = 1; |
---|
216 | |
---|
217 | p = x; |
---|
218 | for (i=0;i<n;i++) { |
---|
219 | val = noise1(p); |
---|
220 | sum += val / scale; |
---|
221 | scale *= alpha; |
---|
222 | p *= beta; |
---|
223 | } |
---|
224 | return(sum); |
---|
225 | } |
---|
226 | |
---|
227 | double PerlinNoise2D(double x,double y,double alpha,double beta,int n) |
---|
228 | { |
---|
229 | int i; |
---|
230 | double val,sum = 0; |
---|
231 | double p[2],scale = 1; |
---|
232 | |
---|
233 | p[0] = x; |
---|
234 | p[1] = y; |
---|
235 | for (i=0;i<n;i++) { |
---|
236 | val = noise2(p); |
---|
237 | sum += val / scale; |
---|
238 | scale *= alpha; |
---|
239 | p[0] *= beta; |
---|
240 | p[1] *= beta; |
---|
241 | } |
---|
242 | return(sum); |
---|
243 | } |
---|
244 | |
---|
245 | double PerlinNoise3D(double x,double y,double z,double alpha,double beta,int n) |
---|
246 | { |
---|
247 | int i; |
---|
248 | double val,sum = 0; |
---|
249 | double p[3],scale = 1; |
---|
250 | |
---|
251 | p[0] = x; |
---|
252 | p[1] = y; |
---|
253 | p[2] = z; |
---|
254 | for (i=0;i<n;i++) { |
---|
255 | val = noise3(p); |
---|
256 | sum += val / scale; |
---|
257 | scale *= alpha; |
---|
258 | p[0] *= beta; |
---|
259 | p[1] *= beta; |
---|
260 | p[2] *= beta; |
---|
261 | } |
---|
262 | return(sum); |
---|
263 | } |
---|