… | |
… | |
29 | #include "global.h" |
29 | #include "global.h" |
30 | |
30 | |
31 | typedef blitz::TinyVector<float,2> vec2d; |
31 | typedef blitz::TinyVector<float,2> vec2d; |
32 | typedef blitz::TinyVector<float,3> vec3d; |
32 | typedef blitz::TinyVector<float,3> vec3d; |
33 | |
33 | |
|
|
34 | vec2d |
|
|
35 | inline floor (vec2d v) |
|
|
36 | { |
|
|
37 | return vec2d (fastfloor (v[0]), fastfloor (v[1])); |
|
|
38 | } |
|
|
39 | |
|
|
40 | vec3d |
|
|
41 | inline floor (vec3d v) |
|
|
42 | { |
|
|
43 | return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2])); |
|
|
44 | } |
|
|
45 | |
|
|
46 | vec2d |
|
|
47 | inline pow (vec2d v, vec2d p) |
|
|
48 | { |
|
|
49 | return vec2d (pow (v[0], p[0]), pow (v[1], p[1])); |
|
|
50 | } |
|
|
51 | |
|
|
52 | ///////////////////////////////////////////////////////////////////////////// |
|
|
53 | |
|
|
54 | // various s-shaped curves, smooth to, first, or second derivative |
|
|
55 | // used for smooth interpolation from 0..1 |
|
|
56 | |
|
|
57 | // linear |
|
|
58 | template<typename T> |
|
|
59 | inline T |
|
|
60 | sigmoid0 (T x) |
|
|
61 | { |
|
|
62 | return x; |
|
|
63 | } |
|
|
64 | |
|
|
65 | // 3x²-2x³ |
|
|
66 | template<typename T> |
|
|
67 | inline T |
|
|
68 | sigmoid1 (T x) |
|
|
69 | { |
|
|
70 | return (3 - 2 * x) * x * x; |
|
|
71 | } |
|
|
72 | |
|
|
73 | // 6x⁵ - 15x⁴ + 10x³ |
|
|
74 | template<typename T> |
|
|
75 | inline T |
|
|
76 | sigmoid2 (T x) |
|
|
77 | { |
|
|
78 | #ifdef MORE_PARALLELITY |
|
|
79 | float x2 = x * x; |
|
|
80 | float x4 = x2 * x2; |
|
|
81 | |
|
|
82 | return (6 * x4 + 10 * x2) * x - 15 * x4; |
|
|
83 | #endif |
|
|
84 | |
|
|
85 | // simple horner |
|
|
86 | return ((6 * x - 15) * x + 10) * x * x * x; |
|
|
87 | } |
|
|
88 | |
|
|
89 | // blend between a and b |
|
|
90 | // c is the control function - if lower than ca |
|
|
91 | // then return a, if higher than cb, return b |
|
|
92 | template<typename T, typename U> |
|
|
93 | inline T |
|
|
94 | blend (T a, T b, U c, U ca, U cb, U weight (U) = sigmoid1) |
|
|
95 | { |
|
|
96 | if (c <= ca) return a; |
|
|
97 | if (c >= cb) return b; |
|
|
98 | |
|
|
99 | U w = weight ((c - ca) / (cb - ca)); |
|
|
100 | return (U(1) - w) * a + w * b; |
|
|
101 | } |
|
|
102 | |
|
|
103 | // blend between a and b |
|
|
104 | // c is the control function - if lower than -c_w |
|
|
105 | // then return a, if higher than +c_w then b. |
|
|
106 | template<typename T, typename U> |
|
|
107 | inline T |
|
|
108 | blend0 (T a, T b, U c, U c_w, U weight (U) = sigmoid1) |
|
|
109 | { |
|
|
110 | return blend<T,U> (a, b, c, -c_w, c_w, weight); |
|
|
111 | } |
|
|
112 | |
34 | template<class vec_t, int a, int b> |
113 | template<class vec_t, int a, int b> |
35 | struct rotate_nn |
114 | struct rotate_nn |
36 | { |
115 | { |
37 | typename vec_t::T_numtype s, c; |
116 | typename vec_t::T_numtype s, c; |
38 | |
117 | |
… | |
… | |
86 | template<class vec_t> |
165 | template<class vec_t> |
87 | struct noise_gen_base |
166 | struct noise_gen_base |
88 | { |
167 | { |
89 | permutation<256, uint8_t> rvmap[vec_t::numElements]; |
168 | permutation<256, uint8_t> rvmap[vec_t::numElements]; |
90 | |
169 | |
|
|
170 | typedef typename vec_t::T_numtype value_t; |
|
|
171 | |
91 | void seed (seedable_rand_gen &rng); |
172 | void seed (seedable_rand_gen &rng); |
92 | void seed (seed_t seed); |
173 | void seed (seed_t seed); |
93 | |
174 | |
94 | typename vec_t::T_numtype operator ()(vec_t P) func_pure; |
175 | value_t operator ()(vec_t P, uint32_t seed = 0) func_pure; |
95 | }; |
176 | }; |
96 | |
177 | |
97 | template<class vec_t> |
178 | template<class vec_t> |
98 | struct noise_gen; |
179 | struct noise_gen; |
99 | |
180 | |
100 | // modelled after 2d/3d kensler noise without projection |
181 | // modelled after 2d/3d kensler noise without projection |
101 | template<> |
182 | template<> |
102 | struct noise_gen<vec2d> |
183 | struct noise_gen<vec2d> |
103 | : noise_gen_base<vec2d> |
184 | : noise_gen_base<vec2d> |
104 | { |
185 | { |
105 | static vec2d::T_numtype abs_avg() { return 0.2231; } // avg(abs(noise)) |
186 | static value_t abs_avg() { return 0.2231; } // avg(abs(noise)) |
106 | }; |
187 | }; |
107 | |
188 | |
108 | template<> |
189 | template<> |
109 | struct noise_gen<vec3d> |
190 | struct noise_gen<vec3d> |
110 | : noise_gen_base<vec3d> |
191 | : noise_gen_base<vec3d> |
… | |
… | |
112 | static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise)) |
193 | static vec3d::T_numtype abs_avg() { return 0.415; } // avg(abs(noise)) |
113 | |
194 | |
114 | using noise_gen_base<vec3d>::operator (); |
195 | using noise_gen_base<vec3d>::operator (); |
115 | |
196 | |
116 | // noise projected on a surface with normal n |
197 | // noise projected on a surface with normal n |
117 | vec2d::T_numtype operator() (vec3d P, vec3d N) func_pure; |
198 | vec2d::T_numtype operator() (vec3d P, vec3d N, uint32_t seed = 0) func_pure; |
118 | }; |
199 | }; |
119 | |
200 | |
120 | template<class vec_T> |
201 | typedef noise_gen<vec2d> noise2d; |
121 | struct perturb_gen |
202 | typedef noise_gen<vec3d> noise3d; |
122 | { |
203 | |
123 | //TODO |
204 | ///////////////////////////////////////////////////////////////////////////// |
124 | }; |
|
|
125 | |
205 | |
126 | template<class vec_t> |
206 | template<class vec_t> |
127 | struct frac_gen |
207 | struct frac_gen |
128 | : noise_gen<vec_t> |
208 | : noise_gen<vec_t> |
129 | { |
209 | { |
130 | enum { MAX_OCTAVES = 32 }; |
210 | enum { MAX_OCTAVES = 32 }; |
131 | |
211 | |
132 | typedef typename vec_t::T_numtype value_t; |
212 | typedef typename vec_t::T_numtype value_t; |
133 | |
213 | |
|
|
214 | int octaves; |
134 | value_t h, lac, ex[MAX_OCTAVES]; |
215 | value_t h, lac, ex[MAX_OCTAVES]; |
135 | value_t fbm_mul[MAX_OCTAVES]; |
216 | value_t fbm_mul[MAX_OCTAVES]; |
136 | rotate_xy<vec_t> rot[MAX_OCTAVES]; |
217 | rotate_xy<vec_t> rot[MAX_OCTAVES]; |
137 | |
218 | |
138 | frac_gen (value_t hurst_expo = .5, value_t lacunarity = 2, uint32_t seed = 0); |
219 | frac_gen (int octaves = 3, value_t lacunarity = 2, value_t hurst_expo = .5, uint32_t seed = 0); |
139 | |
220 | |
140 | value_t noise (vec_t P) func_pure |
221 | value_t noise (vec_t P, uint32_t seed = 0) func_pure |
141 | { |
222 | { |
142 | return operator() (P); |
223 | return operator() (P, seed); |
143 | } |
224 | } |
144 | |
225 | |
145 | value_t fBm (vec_t P, int octaves) func_pure; |
226 | value_t fBm (vec_t P) func_pure; |
146 | value_t turbulence (vec_t P, int octaves) func_pure; |
227 | value_t turbulence (vec_t P) func_pure; |
147 | value_t multifractal (vec_t P, int octaves, value_t offset = 1) func_pure; |
228 | value_t multifractal (vec_t P, value_t offset = 1) func_pure; |
148 | value_t heterofractal (vec_t P, int octaves, value_t offset = 1) func_pure; |
229 | value_t heterofractal (vec_t P, value_t offset = 1) func_pure; |
149 | value_t hybridfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 1) func_pure; |
230 | value_t hybridfractal (vec_t P, value_t offset = 1, value_t gain = 1) func_pure; |
150 | value_t ridgedmultifractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 8) func_pure; |
231 | value_t ridgedmultifractal (vec_t P, value_t offset = 1, value_t gain = 8) func_pure; |
151 | value_t billowfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 2) func_pure; |
232 | value_t billowfractal (vec_t P, value_t offset = 1, value_t gain = 2) func_pure; |
152 | value_t terrain (vec_t P, int octaves) func_pure; |
233 | value_t terrain (vec_t P) func_pure; |
153 | value_t terrain2 (vec_t P, int octaves) func_pure; |
234 | value_t terrain2 (vec_t P) func_pure; |
154 | }; |
235 | }; |
|
|
236 | |
|
|
237 | typedef frac_gen<vec2d> frac2d; |
|
|
238 | typedef frac_gen<vec3d> frac3d; |
155 | |
239 | |
156 | #endif |
240 | #endif |
157 | |
241 | |