00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <math.h>
00026 #include <stdlib.h>
00027
00028
00029
00030
00031 #define MAXB 0x100
00032 #define N 0x1000
00033 #define NP 12
00034 #define NM 0xfff
00035
00036 #define s_curve(t) ( t * t * (3. - 2. * t) )
00037 #define lerp(t, a, b) ( a + t * (b - a) )
00038 #define setup(i,b0,b1,r0,r1)\
00039 t = vec[i] + N;\
00040 b0 = ((int)t) & BM;\
00041 b1 = (b0+1) & BM;\
00042 r0 = t - (int)t;\
00043 r1 = r0 - 1.;
00044 #define at2(rx,ry) ( rx * q[0] + ry * q[1] )
00045 #define at3(rx,ry,rz) ( rx * q[0] + ry * q[1] + rz * q[2] )
00046
00047 static void initNoise(void);
00048
00049 static int p[MAXB + MAXB + 2];
00050 static double g3[MAXB + MAXB + 2][3];
00051 static double g2[MAXB + MAXB + 2][2];
00052 static double g1[MAXB + MAXB + 2];
00053
00054 int start;
00055 int B;
00056 int BM;
00057
00058
00059 void SetNoiseFrequency(int frequency)
00060 {
00061 start = 1;
00062 B = frequency;
00063 BM = B-1;
00064 }
00065
00066 double noise1(double arg)
00067 {
00068 int bx0, bx1;
00069 double rx0, rx1, sx, t, u, v, vec[1];
00070
00071 vec[0] = arg;
00072 if (start) {
00073 start = 0;
00074 initNoise();
00075 }
00076
00077 setup(0,bx0,bx1,rx0,rx1);
00078
00079 sx = s_curve(rx0);
00080 u = rx0 * g1[ p[ bx0 ] ];
00081 v = rx1 * g1[ p[ bx1 ] ];
00082
00083 return(lerp(sx, u, v));
00084 }
00085
00086 double noise2(double vec[2])
00087 {
00088 int bx0, bx1, by0, by1, b00, b10, b01, b11;
00089 double rx0, rx1, ry0, ry1, *q, sx, sy, a, b, t, u, v;
00090 int i, j;
00091
00092 if (start) {
00093 start = 0;
00094 initNoise();
00095 }
00096
00097 setup(0, bx0,bx1, rx0,rx1);
00098 setup(1, by0,by1, ry0,ry1);
00099
00100 i = p[ bx0 ];
00101 j = p[ bx1 ];
00102
00103 b00 = p[ i + by0 ];
00104 b10 = p[ j + by0 ];
00105 b01 = p[ i + by1 ];
00106 b11 = p[ j + by1 ];
00107
00108 sx = s_curve(rx0);
00109 sy = s_curve(ry0);
00110
00111 q = g2[ b00 ] ; u = at2(rx0,ry0);
00112 q = g2[ b10 ] ; v = at2(rx1,ry0);
00113 a = lerp(sx, u, v);
00114
00115 q = g2[ b01 ] ; u = at2(rx0,ry1);
00116 q = g2[ b11 ] ; v = at2(rx1,ry1);
00117 b = lerp(sx, u, v);
00118
00119 return lerp(sy, a, b);
00120 }
00121
00122 double noise3(double vec[3])
00123 {
00124 int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
00125 double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
00126 int i, j;
00127
00128 if (start) {
00129 start = 0;
00130 initNoise();
00131 }
00132
00133 setup(0, bx0,bx1, rx0,rx1);
00134 setup(1, by0,by1, ry0,ry1);
00135 setup(2, bz0,bz1, rz0,rz1);
00136
00137 i = p[ bx0 ];
00138 j = p[ bx1 ];
00139
00140 b00 = p[ i + by0 ];
00141 b10 = p[ j + by0 ];
00142 b01 = p[ i + by1 ];
00143 b11 = p[ j + by1 ];
00144
00145 t = s_curve(rx0);
00146 sy = s_curve(ry0);
00147 sz = s_curve(rz0);
00148
00149 q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
00150 q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
00151 a = lerp(t, u, v);
00152
00153 q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
00154 q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
00155 b = lerp(t, u, v);
00156
00157 c = lerp(sy, a, b);
00158
00159 q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
00160 q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
00161 a = lerp(t, u, v);
00162
00163 q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
00164 q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
00165 b = lerp(t, u, v);
00166
00167 d = lerp(sy, a, b);
00168
00169
00170
00171 return lerp(sz, c, d);
00172 }
00173
00174 void normalize2(double v[2])
00175 {
00176 double s;
00177
00178 s = sqrt(v[0] * v[0] + v[1] * v[1]);
00179 v[0] = v[0] / s;
00180 v[1] = v[1] / s;
00181 }
00182
00183 void normalize3(double v[3])
00184 {
00185 double s;
00186
00187 s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
00188 v[0] = v[0] / s;
00189 v[1] = v[1] / s;
00190 v[2] = v[2] / s;
00191 }
00192
00193 void initNoise(void)
00194 {
00195 int i, j, k;
00196
00197 srand(30757);
00198 for (i = 0 ; i < B ; i++) {
00199 p[i] = i;
00200 g1[i] = (double)((rand() % (B + B)) - B) / B;
00201
00202 for (j = 0 ; j < 2 ; j++)
00203 g2[i][j] = (double)((rand() % (B + B)) - B) / B;
00204 normalize2(g2[i]);
00205
00206 for (j = 0 ; j < 3 ; j++)
00207 g3[i][j] = (double)((rand() % (B + B)) - B) / B;
00208 normalize3(g3[i]);
00209 }
00210
00211 while (--i) {
00212 k = p[i];
00213 p[i] = p[j = rand() % B];
00214 p[j] = k;
00215 }
00216
00217 for (i = 0 ; i < B + 2 ; i++) {
00218 p[B + i] = p[i];
00219 g1[B + i] = g1[i];
00220 for (j = 0 ; j < 2 ; j++)
00221 g2[B + i][j] = g2[i][j];
00222 for (j = 0 ; j < 3 ; j++)
00223 g3[B + i][j] = g3[i][j];
00224 }
00225 }
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235 double PerlinNoise1D(double x,double alpha,double beta,int n)
00236 {
00237 int i;
00238 double val,sum = 0;
00239 double p,scale = 1;
00240
00241 p = x;
00242 for (i=0;i<n;i++) {
00243 val = noise1(p);
00244 sum += val / scale;
00245 scale *= alpha;
00246 p *= beta;
00247 }
00248 return(sum);
00249 }
00250
00251 double PerlinNoise2D(double x,double y,double alpha,double beta,int n)
00252 {
00253 int i;
00254 double val,sum = 0;
00255 double p[2],scale = 1;
00256
00257 p[0] = x;
00258 p[1] = y;
00259 for (i=0;i<n;i++) {
00260 val = noise2(p);
00261 sum += val / scale;
00262 scale *= alpha;
00263 p[0] *= beta;
00264 p[1] *= beta;
00265 }
00266 return(sum);
00267 }
00268
00269 double PerlinNoise3D(double x,double y,double z,double alpha,double beta,int n)
00270 {
00271 int i;
00272 double val,sum = 0;
00273 double p[3],scale = 1;
00274
00275 p[0] = x;
00276 p[1] = y;
00277 p[2] = z;
00278 for (i=0;i<n;i++) {
00279 val = noise3(p);
00280 sum += val / scale;
00281 scale *= alpha;
00282 p[0] *= beta;
00283 p[1] *= beta;
00284 p[2] *= beta;
00285 }
00286 return(sum);
00287 }