ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/include/noise.h
(Generate patch)

Comparing deliantra/server/include/noise.h (file contents):
Revision 1.8 by root, Tue Apr 26 03:18:07 2011 UTC vs.
Revision 1.13 by root, Sun May 8 21:51:27 2011 UTC

29#include "global.h" 29#include "global.h"
30 30
31typedef blitz::TinyVector<float,2> vec2d; 31typedef blitz::TinyVector<float,2> vec2d;
32typedef blitz::TinyVector<float,3> vec3d; 32typedef blitz::TinyVector<float,3> vec3d;
33 33
34///////////////////////////////////////////////////////////////////////////// 34vec2d
35inline floor (vec2d v)
36{
37 return vec2d (fastfloor (v[0]), fastfloor (v[1]));
38}
35 39
40vec3d
41inline floor (vec3d v)
42{
43 return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2]));
44}
45
46vec2d
47inline 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
58template<typename T>
59inline T
60sigmoid0 (T x)
61{
62 return x;
63}
64
65// 3x²-2x³
66template<typename T>
67inline T
68sigmoid1 (T x)
69{
70 return (3 - 2 * x) * x * x;
71}
72
73// 6x⁵ - 15x⁴ + 10x³
74template<typename T>
75inline T
76sigmoid2 (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
92template<typename T, typename U>
93inline T
94blend (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.
106template<typename T, typename U>
107inline T
108blend0 (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
113template<class vec_t, int a, int b>
114struct rotate_nn
115{
116 typename vec_t::T_numtype s, c;
117
118 void set (typename vec_t::T_numtype angle)
119 {
120 s = sin (angle);
121 c = cos (angle);
122 }
123
124 void operator ()(vec_t &P) const
125 {
126 vec_t o = P;
127
128 P[a] = o[a] * c - o[b] * s;
129 P[b] = o[a] * s + o[b] * c;
130 }
131};
132
133template<class vec_t>
134struct rotate_xy : rotate_nn<vec_t, 0, 1>
135{
136};
137
138template<class vec_t>
139struct rotate_xz : rotate_nn<vec_t, 0, 2>
140{
141};
142
143template<class vec_t>
144struct rotate_yz : rotate_nn<vec_t, 1, 2>
145{
146};
147
148/////////////////////////////////////////////////////////////////////////////
149
36template<int N, typename T = uint8_t> 150template< int N, typename T>
37struct permutation 151struct permutation
38{ 152{
39 T pmap[N]; 153 T pmap[N];
40 154
41 template<class random_generator>
42 void seed (random_generator &rng); 155 void seed (seedable_rand_gen &rng);
43 156
44 T operator ()(T v) 157 T operator ()(T v) func_pure
45 { 158 {
46 return pmap[v & (N - 1)]; 159 return pmap[v & T(N - 1)];
47 } 160 }
161};
162
163/////////////////////////////////////////////////////////////////////////////
164
165template<class vec_t>
166struct noise_gen_base
167{
168 permutation<256, uint8_t> rvmap[vec_t::numElements + 1];
169
170 typedef typename vec_t::T_numtype value_t;
171
172 void seed (seedable_rand_gen &rng);
173 void seed (seed_t seed);
174
175 value_t operator ()(vec_t P, uint32_t seed = 0) func_pure;
48}; 176};
49 177
50template<class vec_t> 178template<class vec_t>
51struct noise_gen; 179struct noise_gen;
52 180
53// modelled after 2d/3d kensler noise without projection 181// modelled after 2d/3d kensler noise without projection
54template<> 182template<>
55struct noise_gen<vec2d> 183struct noise_gen<vec2d>
184: noise_gen_base<vec2d>
56{ 185{
57 static vec2d::T_numtype abs_avg() { return 0.2231; } // avg(abs(noise)) 186 static value_t abs_avg() { return 0.2231; } // avg(abs(noise))
58
59 permutation<256, uint8_t> rvmap[2];
60
61 noise_gen<vec2d> (uint32_t seed);
62 vec2d::T_numtype operator() (vec2d P);
63}; 187};
64 188
65template<> 189template<>
66struct noise_gen<vec3d> 190struct noise_gen<vec3d>
191: noise_gen_base<vec3d>
67{ 192{
68 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))
69 194
70 permutation<256, uint8_t> rvmap[3]; 195 using noise_gen_base<vec3d>::operator ();
71
72 noise_gen<vec3d> (uint32_t seed);
73 vec2d::T_numtype operator() (vec3d P);
74 196
75 // noise projected on a surface with normal n 197 // noise projected on a surface with normal n
76 vec2d::T_numtype operator() (vec3d P, vec3d N); 198 vec2d::T_numtype operator() (vec3d P, vec3d N, uint32_t seed = 0) func_pure;
77}; 199};
78 200
79template<class vec_t, int a, int b> 201typedef noise_gen<vec2d> noise2d;
80struct rotate_nn 202typedef noise_gen<vec3d> noise3d;
81{
82 typename vec_t::T_numtype s, c;
83 203
84 void set (typename vec_t::T_numtype angle) 204/////////////////////////////////////////////////////////////////////////////
85 {
86 s = sin (angle);
87 c = cos (angle);
88 }
89
90 void operator ()(vec_t &P) const
91 {
92 vec_t o = P;
93
94 P[a] = o[a] * c - o[b] * s;
95 P[b] = o[a] * s + o[b] * c;
96 }
97};
98
99template<class vec_t>
100struct rotate_xy : rotate_nn<vec_t, 0, 1>
101{
102};
103
104template<class vec_t>
105struct rotate_xz : rotate_nn<vec_t, 0, 2>
106{
107};
108
109template<class vec_t>
110struct rotate_yz : rotate_nn<vec_t, 1, 2>
111{
112};
113 205
114template<class vec_t> 206template<class vec_t>
115struct frac_gen 207struct frac_gen
116: noise_gen<vec_t> 208: noise_gen<vec_t>
117{ 209{
118 enum { MAX_OCTAVES = 32 }; 210 enum { MAX_OCTAVES = 32 };
119 211
120 typedef typename vec_t::T_numtype value_t; 212 typedef typename vec_t::T_numtype value_t;
121 213
214 int octaves;
122 value_t h, lac, ex[MAX_OCTAVES]; 215 value_t h, lac, ex[MAX_OCTAVES];
123 value_t fbm_mul[MAX_OCTAVES]; 216 value_t fbm_mul[MAX_OCTAVES];
124 rotate_xy<vec_t> rot[MAX_OCTAVES]; 217 rotate_xy<vec_t> rot[MAX_OCTAVES];
125 218
126 value_t noise (vec_t P) 219 frac_gen (int octaves = 3, value_t lacunarity = 2, value_t hurst_expo = .5, uint32_t seed = 0);
220
221 value_t noise (vec_t P, uint32_t seed = 0) func_pure
127 { 222 {
128 return operator() (P); 223 return operator() (P, seed);
129 } 224 }
130 225
131 frac_gen (value_t hurst_expo = .5, value_t lacunarity = 2); 226 value_t fBm (vec_t P) func_pure;
132
133 value_t fBm (vec_t P, int octaves);
134 value_t turbulence (vec_t P, int octaves); 227 value_t turbulence (vec_t P) func_pure;
135 value_t multifractal (vec_t P, int octaves, value_t offset = 1); 228 value_t multifractal (vec_t P, value_t offset = 1) func_pure;
136 value_t heterofractal (vec_t P, int octaves, value_t offset = 1); 229 value_t heterofractal (vec_t P, value_t offset = 1) func_pure;
137 value_t hybridfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 1); 230 value_t hybridfractal (vec_t P, value_t offset = 1, value_t gain = 1) func_pure;
138 value_t ridgedmultifractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 8); 231 value_t ridgedmultifractal (vec_t P, value_t offset = 1, value_t gain = 8) func_pure;
139 value_t billowfractal (vec_t P, int octaves, value_t offset = 1, value_t gain = 2); 232 value_t billowfractal (vec_t P, value_t offset = 1, value_t gain = 2) func_pure;
140 value_t terrain (vec_t P, int octaves); 233 value_t terrain (vec_t P) func_pure;
141 value_t terrain2 (vec_t P, int octaves); 234 value_t terrain2 (vec_t P) func_pure;
142}; 235};
236
237typedef frac_gen<vec2d> frac2d;
238typedef frac_gen<vec3d> frac3d;
239
240/////////////////////////////////////////////////////////////////////////////
241
242template<typename T, typename U>
243inline T
244border_blend (T a, T b, vec2d P, U N, U W)
245{
246 U border = W; // within n places of the border
247
248 min_it (border, P [0]);
249 min_it (border, N - P [0]);
250 min_it (border, P [1]);
251 min_it (border, N - P [1]);
252
253 return blend (a, b, border, U(0), W);
254}
143 255
144#endif 256#endif
145 257

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines