2 |
#include <math.h> |
#include <math.h> |
3 |
|
|
4 |
#define FPARAM(name, default) float name = params->getFloat(#name, default); |
#define FPARAM(name, default) float name = params->getFloat(#name, default); |
5 |
|
#define SFPARAM(name, default) name = params->getFloat(#name, default); |
6 |
|
|
7 |
/** Signed noise, Any number of components, with or without discontinuities. |
typedef float (*FUNC)(float *p); |
|
*/ |
|
|
static void std_noise(TextureParam *params, int width, int height, int depth, int components, float *data) { |
|
|
|
|
|
FPARAM(freq, 5); |
|
|
FPARAM(discfreq, 0); |
|
|
FPARAM(scale, 1); |
|
8 |
|
|
9 |
int ind=0, i, j, k; |
float f_noise(float *p) { |
10 |
for (k = 0; k < depth; k++) { |
return Perlin::noise3(p); |
11 |
for (j = 0; j < height; j++) { |
} |
|
for (i = 0; i < width; i++) { |
|
12 |
|
|
13 |
// 0..1 |
static float turbfreq = 1; // !! nasty global |
14 |
float x = ((float)i)/(width-1); |
float f_turb(float *p) { |
15 |
float y = ((float)j)/(height-1); |
return Perlin::turbulence(p, turbfreq); |
16 |
float z = (depth==1?0:((float)k)/(depth-1)); |
} |
|
|
|
|
float org = 0; |
|
|
if(discfreq) |
|
|
org = Perlin::noise(discfreq*x, discfreq*y, 0.5+discfreq*z); |
|
|
|
|
|
for(int c = 0; c < components; c++) { |
|
|
data[ind+c] = Perlin::noise(freq*x, freq*y, freq*z + |
|
|
8.5+ 50.1* ((c+(org>0))%components))*scale; |
|
|
} |
|
|
ind += components; |
|
|
} |
|
|
} |
|
|
} |
|
17 |
|
|
18 |
|
static int fbmoct = 1; |
19 |
|
static float fbmlacu = 1; |
20 |
|
static float fbmgain = 1; |
21 |
|
float f_fBm(float *p) { |
22 |
|
return Perlin::fBm(p, fbmoct, fbmlacu, fbmgain); |
23 |
|
} |
24 |
|
float f_faBm(float *p) { |
25 |
|
return Perlin::faBm(p, fbmoct, fbmlacu, fbmgain); |
26 |
} |
} |
27 |
|
|
28 |
/** Positive turbulence, Any number of components, with or without discontinuities. |
/** Calculate and add a single cube of data to given array. |
29 |
*/ |
*/ |
30 |
static void std_turb(TextureParam *params, int width, int height, int depth, int components, float *data) { |
static void cube(FUNC f, float scale, TextureParam *params, int width, int height, int depth, int components, float *data) { |
31 |
|
|
32 |
FPARAM(freq, 5); |
FPARAM(freq, 5); |
|
FPARAM(scale, 1); |
|
33 |
|
|
34 |
int ind=0, i, j, k; |
int ind=0, i, j, k; |
35 |
for (k = 0; k < depth; k++) { |
for (k = 0; k < depth; k++) { |
37 |
for (i = 0; i < width; i++) { |
for (i = 0; i < width; i++) { |
38 |
|
|
39 |
// 0..1 |
// 0..1 |
40 |
float x = ((float)i)/(width-1); |
float par[3] = { |
41 |
float y = ((float)j)/(height-1); |
freq * ((float)i)/(width-1)+.3, |
42 |
float z = (depth==1?0:((float)k)/(depth-1)); |
freq * ((float)j)/(height-1)+.3, |
43 |
|
freq * (depth==1?0:((float)k)/(depth-1))+.3 |
44 |
|
}; |
45 |
|
|
46 |
|
|
47 |
for(int c = 0; c < components; c++) { |
for(int c = 0; c < components; c++) { |
48 |
float par[3] = { x, y, 8.5 + 28*c + z }; |
data[ind+c] += f(par) * scale; |
49 |
data[ind+c] = Perlin::turbulence(par, freq)*scale; |
par[2] += 21.419; |
50 |
} |
} |
51 |
ind += components; |
ind += components; |
52 |
} |
} |
55 |
|
|
56 |
} |
} |
57 |
|
|
|
|
|
58 |
inline float mix(float t, float a, float b) { |
inline float mix(float t, float a, float b) { |
59 |
//return t*a + (1-t)*b; |
//return t*a + (1-t)*b; |
60 |
|
|
66 |
} |
} |
67 |
|
|
68 |
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) { |
69 |
int tilewidth = (int)(width * .25); |
int tilewidth = (int)(width * .5); |
70 |
int tileheight = (int)(height * .25); |
int tileheight = (int)(height * .5); |
71 |
int tiledepth = (int)(depth * .25); |
int tiledepth = (int)(depth * .5); |
72 |
|
|
73 |
FPARAM(bias, 0); |
FPARAM(bias, 0); |
74 |
|
FPARAM(scale, 1); |
75 |
|
|
76 |
|
SFPARAM(turbfreq, 100); |
77 |
|
SFPARAM(fbmoct, 5); |
78 |
|
SFPARAM(fbmlacu, 1.94); |
79 |
|
SFPARAM(fbmgain, 0.5); |
80 |
|
|
81 |
float *data0 = new float[(width + tilewidth) * |
float *data0 = new float[(width + tilewidth) * |
82 |
(height + tileheight) * |
(height + tileheight) * |
83 |
(depth + tiledepth) * components]; |
(depth + tiledepth) * components]; |
84 |
|
|
85 |
|
FUNC f; |
86 |
switch(params->getStringEnum("type", 0, |
switch(params->getStringEnum("type", 0, |
87 |
"normal", |
"normal", |
88 |
"turbulence", |
"turbulence", |
90 |
"faBm", |
"faBm", |
91 |
0)) { |
0)) { |
92 |
case 1: |
case 1: |
93 |
std_turb(params, width + tilewidth, height + tileheight, |
f = f_turb; |
|
depth + tiledepth, components, data0); |
|
94 |
break; |
break; |
95 |
case 2: |
case 2: |
96 |
|
f = f_fBm; |
97 |
break; |
break; |
98 |
case 3: |
case 3: |
99 |
|
f = f_faBm; |
100 |
break; |
break; |
101 |
default: |
default: |
102 |
case 0: |
case 0: |
103 |
std_noise(params, width + tilewidth, height + tileheight, |
f = f_noise; |
|
depth + tiledepth, components, data0); |
|
104 |
break; |
break; |
105 |
} |
} |
106 |
|
|
107 |
|
cube(f, scale, params, width + tilewidth, height + tileheight, |
108 |
|
depth + tiledepth, components, data0); |
109 |
|
|
110 |
|
|
111 |
#define D0(x, y, z, c) data0[((x) + (y)*(width+tilewidth) + \ |
#define D0(x, y, z, c) data0[((x) + (y)*(width+tilewidth) + \ |
112 |
(z)*(height+tileheight)*(width+tileheight)) * components + (c)] |
(z)*(height+tileheight)*(width+tileheight)) * components + (c)] |