--- deliantra/server/common/noise.C 2011/04/26 03:35:36 1.11 +++ deliantra/server/common/noise.C 2011/04/26 14:41:35 1.12 @@ -228,9 +228,8 @@ ///////////////////////////////////////////////////////////////////////////// template -template void -permutation::seed (random_generator &rng) +permutation::seed (seedable_rand_gen &rng) { for (int i = 0; i < N; ++i) pmap[i] = i; @@ -244,28 +243,99 @@ ///////////////////////////////////////////////////////////////////////////// +// various s-shaped curves, smooth to, first, or second derivative +// used for smooth interpolation from 0..1 + +// linear +template +inline T +sigmoid0 (T x) +{ + return x; +} + +// 3x²-2x³ +template +inline T +sigmoid1 (T x) +{ + return (3 - 2 * x) * x * x; +} + +// 6x⁵ - 15x⁴ + 10x³ +template +inline T +sigmoid2 (T x) +{ +#ifdef MORE_PARALLELITY + float x2 = x * x; + float x4 = x2 * x2; + + return (6 * x4 + 10 * x2) * x - 15 * x4; +#endif + + // simple horner + return ((6 * x - 15) * x + 10) * x * x * x; +} + +// blend between a and b +// c is the control function - if lower than ca +// then return a, if higher than cb, return b +template +inline T +blend (T a, T b, U c, U ca, U cb, T weight (U) = sigmoid1) +{ + if (c <= ca) return a; + if (c >= cb) return b; + + T w = weight (lerp (c, ca, cb, T(0), T(1))); + return (T(1) - w) * a + w * b; +} + +// blend between a and b +// c is the control function - if lower than -c_w +// then return a, if higher than +c_w then b. +template +inline T +blend0 (T a, T b, U c, U c_w, T weight (U) = sigmoid1) +{ + return blend (a, b, c, -c_w, c_w, weight); +} + +///////////////////////////////////////////////////////////////////////////// + static vec2d floor (vec2d v) { - return vec2d (std::floor (v[0]), std::floor (v[1])); + return vec2d (fastfloor (v[0]), fastfloor (v[1])); } static vec3d floor (vec3d v) { - return vec3d (std::floor (v[0]), std::floor (v[1]), std::floor (v[2])); + return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2])); +} + +template +void +noise_gen_base::seed (seedable_rand_gen &rng) +{ + for (int i = 0; i < array_length (rvmap); ++i) + rvmap[i].seed (rng); } -noise_gen::noise_gen (uint32_t seed) +template +void +noise_gen_base::seed (seed_t seed) { seedable_rand_gen rng (seed); - rvmap[0].seed (rng); - rvmap[1].seed (rng); + this->seed (rng); } +template<> vec2d::T_numtype -noise_gen::operator() (vec2d P) +noise_gen_base::operator() (vec2d P) { vec2d I = floor (P); vec2d F = P - I; @@ -294,22 +364,14 @@ } } - return clamp (v, -.9999999, .9999999); + return v; } ///////////////////////////////////////////////////////////////////////////// -noise_gen::noise_gen (uint32_t seed) -{ - seedable_rand_gen rng (seed); - - rvmap [0].seed (rng); - rvmap [1].seed (rng); - rvmap [2].seed (rng); -} - +template<> vec3d::T_numtype -noise_gen::operator() (vec3d P) +noise_gen_base::operator() (vec3d P) { vec3d I = floor (P); vec3d F = P - I; @@ -339,7 +401,7 @@ } } - return clamp (v * 2, -.9999999, .9999999); + return v * 2; } vec3d::T_numtype @@ -383,7 +445,7 @@ } } - return clamp (v, -.9999999, .9999999); + return v; } template class noise_gen; @@ -405,9 +467,11 @@ } template -frac_gen::frac_gen (value_t hurst_expo, value_t lacunarity) -: h (hurst_expo), lac (lacunarity), noise_gen (0) +frac_gen::frac_gen (value_t hurst_expo, value_t lacunarity, seed_t seed) +: h (hurst_expo), lac (lacunarity) { + this->seed (seed); + value_t exsum = 0; value_t phi = noise (vec_t (value_t (0))) * 0.5 + 1; @@ -433,7 +497,7 @@ P *= lac; } - return clamp (v * fbm_mul [octaves - 1] + 0.5, 0, .9999999); + return v * fbm_mul [octaves - 1]; } template @@ -449,7 +513,7 @@ P *= lac; } - return clamp (v * fbm_mul [octaves - 1] * (0.5 / noise_gen::abs_avg ()), 0, .9999999); + return v * fbm_mul [octaves - 1] * (0.5 / noise_gen::abs_avg ()); } template @@ -465,7 +529,7 @@ P *= lac; } - return clamp (v * 0.5, 0, .9999999); + return v * value_t (0.5); } template @@ -503,7 +567,7 @@ weight *= gain * sig; } - return clamp (v * 0.5 + 0.5, 0, .9999999); + return v * value_t (0.5) + value_t (0.5); } template @@ -528,7 +592,7 @@ v += sig * ex [i]; } - return clamp (v * 0.25, 0, .9999999); + return v * value_t (0.25); } template @@ -546,7 +610,7 @@ P *= lac; } - return clamp (v, -.9999999, .9999999); + return v; } // http://www.gamasutra.com/view/feature/3098/a_realtime_procedural_universe_.php?page=2 @@ -599,7 +663,7 @@ void noise_test (); void noise_test () { - frac_gen gen (0.5, 1.9); + frac_gen gen; frac_gen gen3; #if 1 @@ -612,10 +676,12 @@ if (!(y&63))fprintf (stderr, "y %d\n", y);//D for (int x = 0; x < N; ++x) { + vec2d P = vec2d (x, y) * (5000.f / N); + //putc (127 * gen.noise (vec2d (x * 0.01, y * 0.01)) + 128, stdout); //putc (256 * gen.terrain2 (x * 0.004, y * 0.004, 8), stdout); //putc (256 * gen.fBm (vec2d(x * 0.01, y * 0.01), 16), stdout); - putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout); + //putc (256 * gen.turbulence (vec2d (x * 0.004 - 1, y * 0.004 - 1), 10), stdout); //putc (256 * gen.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout); // mountais or somesuch(?) @@ -625,7 +691,30 @@ //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout); // rain - putc (255 * gen.ridgedmultifractal (vec2d (x * 0.01, y * 0.01), 1, 0.9, 1), stdout); + float continent = gen.fBm (P * 0.0004, 12) + 0.1f; + float mountain = gen.ridgedmultifractal (P * 0.004, 8, 0.8, 10) * 2; + float river = gen.ridgedmultifractal (P * 0.004, 3, 0.8, 10); + float sel1 = gen.noise (P * 0.001 + vec2d (5,0)); + + { + const float W = 100; + const float N = 5000 - 1; + + float border = W; // within n places of the border + + min_it (border, P [0]); + min_it (border, N - P [0]); + min_it (border, P [1]); + min_it (border, N - P [1]); + + continent = blend (-1.f, continent, border, 0.f, W); + } + + //float v = blend (mountain, 0.f, river + sel1 * 0.2f, 0.25f, 0.1f); + float v = blend0 (0.f, 1.f, continent, 0.05f); + + + putc (255 * clamp (v, 0.f, 1.f), stdout); //cells //putc (127.49 * gen.billowfractal (vec2d (x * 0.01, y * 0.01), 9) + 128, stdout);