6 |
#include <math.h> |
#include <math.h> |
7 |
#define FPARAM(name, default) float name = params->getFloat(#name, default); |
#define FPARAM(name, default) float name = params->getFloat(#name, default); |
8 |
|
|
|
#define a b |
|
|
|
|
9 |
static double drand() { |
static double drand() { |
10 |
return random() / (double)RAND_MAX; |
return random() / (double)RAND_MAX; |
11 |
} |
} |
21 |
/* Add a fourier noise to the data. |
/* Add a fourier noise to the data. |
22 |
*/ |
*/ |
23 |
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, |
24 |
float freq, float df) { |
float freq, float df, float amp, float freq2, float df2, float amp2) { |
25 |
int nf = (int)(2 * freq); |
int nf = (int)(2 * ((freq > freq2) ? freq : freq2)); |
26 |
float xsin[2][nf][width]; |
float xsin[4][nf][width]; |
27 |
for(int i=0; i<width; i++) |
for(int i=0; i<width; i++) |
28 |
for(int f = 0; f < nf; f++) { |
for(int f = 0; f < nf; f++) { |
29 |
xsin[0][f][i] = saw_sin(i/(float)width * f * M_PI * 2); |
xsin[0][f][i] = sin(i/(float)width * f * M_PI * 2); |
30 |
xsin[1][f][i] = saw_cos(i/(float)width * f * M_PI * 2); |
xsin[1][f][i] = cos(i/(float)width * f * M_PI * 2); |
31 |
} |
xsin[2][f][i] = saw_sin(i/(float)width * f * M_PI * 2); |
32 |
float ysin[2][nf][height]; |
xsin[3][f][i] = saw_cos(i/(float)width * f * M_PI * 2); |
33 |
|
} |
34 |
|
float ysin[4][nf][height]; |
35 |
for(int j=0; j<width; j++) |
for(int j=0; j<width; j++) |
36 |
for(int f = 0; f < nf; f++) { |
for(int f = 0; f < nf; f++) { |
37 |
ysin[0][f][j] = saw_sin(j/(float)height * f * M_PI * 2); |
ysin[0][f][j] = sin(j/(float)height * f * M_PI * 2); |
38 |
ysin[1][f][j] = saw_cos(j/(float)height * f * M_PI * 2); |
ysin[1][f][j] = cos(j/(float)height * f * M_PI * 2); |
39 |
} |
ysin[2][f][j] = saw_sin(j/(float)height * f * M_PI * 2); |
40 |
|
ysin[3][f][j] = saw_cos(j/(float)height * f * M_PI * 2); |
41 |
|
} |
42 |
|
|
43 |
|
|
44 |
if(depth < 2) { |
if(depth < 2) { |
47 |
for(int yf = 0; yf < nf; yf++) { |
for(int yf = 0; yf < nf; yf++) { |
48 |
|
|
49 |
double f = sqrt(xf*xf + yf*yf); |
double f = sqrt(xf*xf + yf*yf); |
50 |
if(f < freq-df || f > freq+df) continue; |
int b; |
51 |
|
float a; |
52 |
for(int xsc = 0; xsc < 2; xsc++) { |
for (b = 0; b < 4; b += 2) { |
53 |
for(int ysc = 0; ysc < 2; ysc++) { |
if (b == 0 && fabs(f - freq) < df) a = amp; // Sine noise |
54 |
float coeff[components]; |
else if (b == 2 && fabs(f - freq2) < df2) a = amp2; // Saw noise |
55 |
for(int co = 0; co < components; co++) |
else continue; |
56 |
coeff[co] = drand()-0.5; |
|
57 |
int ind = 0; |
for(int xsc = 0; xsc < 2; xsc++) { |
58 |
for (int j = 0; j < height; j++) { |
for(int ysc = 0; ysc < 2; ysc++) { |
59 |
for (int i = 0; i < width; i++) { |
float coeff[components]; |
60 |
for(int co = 0; co < components; co++) |
for(int co = 0; co < components; co++) |
61 |
data[ind++] += xsin[xsc][xf][i] * ysin[ysc][yf][j] * coeff[co]; |
coeff[co] = a * (drand()-0.5); |
62 |
|
int ind = 0; |
63 |
|
for (int j = 0; j < height; j++) { |
64 |
|
for (int i = 0; i < width; i++) { |
65 |
|
for(int co = 0; co < components; co++) |
66 |
|
data[ind++] += xsin[xsc+b][xf][i] * ysin[ysc+b][yf][j] * coeff[co]; |
67 |
|
} |
68 |
} |
} |
69 |
} |
} |
70 |
} |
} |
79 |
|
|
80 |
void GENERATE(TextureParam *params, int width, int height, int depth, int components, float *data) { |
void GENERATE(TextureParam *params, int width, int height, int depth, int components, float *data) { |
81 |
FPARAM(bias, 0); |
FPARAM(bias, 0); |
82 |
FPARAM(scale, 1); |
|
83 |
FPARAM(freq, 5); |
FPARAM(freq, 5); |
84 |
FPARAM(df, 2); |
FPARAM(df, 2); |
85 |
|
FPARAM(scale, 1); |
86 |
|
|
87 |
|
FPARAM(freq2, 5); |
88 |
|
FPARAM(df2, 0); |
89 |
|
FPARAM(scale2, 1); |
90 |
|
|
91 |
int d = (depth==0 ? 1 : depth); |
int d = (depth==0 ? 1 : depth); |
92 |
|
|
93 |
for(int i = 0; i<width*height*d*components; i++) |
for(int i = 0; i<width*height*d*components; i++) |
94 |
data[i] = 0; |
data[i] = 0; |
95 |
|
|
96 |
fourier_noise(width,height,d,components,data, freq, df); |
fourier_noise(width,height,d,components,data, freq, df, scale, freq2, df2, scale2); |
97 |
for(int i = 0; i<width*height*d*components; i++) { |
for(int i = 0; i<width*height*d*components; i++) { |
|
data[i] *= scale; |
|
98 |
data[i] += bias; |
data[i] += bias; |
99 |
} |
} |
100 |
} |
} |