13 |
static void fourier_noise(int width, int height, int depth, int components, float *data, |
static void fourier_noise(int width, int height, int depth, int components, float *data, |
14 |
float freq, float df) { |
float freq, float df) { |
15 |
int nf = (int)(2 * freq); |
int nf = (int)(2 * freq); |
16 |
float xsin[nf][width]; |
float xsin[2][nf][width]; |
17 |
for(int i=0; i<width; i++) |
for(int i=0; i<width; i++) |
18 |
for(int f = 0; f < nf; f++) |
for(int f = 0; f < nf; f++) { |
19 |
xsin[f][i] = sin(i/(float)width * f * M_PI); |
xsin[0][f][i] = sin(i/(float)width * f * M_PI * 2); |
20 |
float ysin[nf][height]; |
xsin[1][f][i] = cos(i/(float)width * f * M_PI * 2); |
21 |
|
} |
22 |
|
float ysin[2][nf][height]; |
23 |
for(int j=0; j<width; j++) |
for(int j=0; j<width; j++) |
24 |
for(int f = 0; f < nf; f++) |
for(int f = 0; f < nf; f++) { |
25 |
ysin[f][j] = sin(j/(float)height * f * M_PI); |
ysin[0][f][j] = sin(j/(float)height * f * M_PI * 2); |
26 |
|
ysin[1][f][j] = cos(j/(float)height * f * M_PI * 2); |
27 |
|
} |
28 |
|
|
29 |
|
|
30 |
if(depth < 2) { |
if(depth < 2) { |
31 |
// 2D |
// 2D |
32 |
for(int xf = 0; xf < nf; xf++) { |
for(int xf = 0; xf < nf; xf++) { |
33 |
for(int yf = 0; yf < nf; yf++) { |
for(int yf = 0; yf < nf; yf++) { |
|
float coeff[components]; |
|
34 |
|
|
35 |
double f = sqrt(xf*xf + yf*yf); |
double f = sqrt(xf*xf + yf*yf); |
36 |
if(f < freq-df || f > freq+df) continue; |
if(f < freq-df || f > freq+df) continue; |
37 |
|
|
38 |
for(int co = 0; co < components; co++) |
for(int xsc = 0; xsc < 2; xsc++) { |
39 |
coeff[co] = drand()-0.5; |
for(int ysc = 0; ysc < 2; ysc++) { |
40 |
int ind = 0; |
float coeff[components]; |
|
for (int j = 0; j < height; j++) { |
|
|
for (int i = 0; i < width; i++) { |
|
41 |
for(int co = 0; co < components; co++) |
for(int co = 0; co < components; co++) |
42 |
data[ind++] += xsin[xf][i] * ysin[yf][j] * coeff[co]; |
coeff[co] = drand()-0.5; |
43 |
|
int ind = 0; |
44 |
|
for (int j = 0; j < height; j++) { |
45 |
|
for (int i = 0; i < width; i++) { |
46 |
|
for(int co = 0; co < components; co++) |
47 |
|
data[ind++] += xsin[xsc][xf][i] * ysin[ysc][yf][j] * coeff[co]; |
48 |
|
} |
49 |
|
} |
50 |
} |
} |
51 |
} |
} |
52 |
} |
} |