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