--- deliantra/server/common/noise.C 2011/04/26 14:41:35 1.12 +++ deliantra/server/common/noise.C 2018/11/17 23:40:00 1.23 @@ -1,22 +1,23 @@ /* * This file is part of Deliantra, the Roguelike Realtime MMORPG. - * - * Copyright (©) 2010,2011 Marc Alexander Lehmann / Robin Redeker / the Deliantra team - * + * + * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team + * Copyright (©) 2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * * Deliantra is free software: you can redistribute it and/or modify it under * the terms of the Affero GNU General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. - * + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the Affero GNU General Public License * and the GNU General Public License along with this program. If not, see * . - * + * * The authors can be reached via e-mail to */ @@ -243,79 +244,6 @@ ///////////////////////////////////////////////////////////////////////////// -// 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 (fastfloor (v[0]), fastfloor (v[1])); -} - -static vec3d -floor (vec3d v) -{ - return vec3d (fastfloor (v[0]), fastfloor (v[1]), fastfloor (v[2])); -} - template void noise_gen_base::seed (seedable_rand_gen &rng) @@ -335,115 +263,154 @@ template<> vec2d::T_numtype -noise_gen_base::operator() (vec2d P) +noise_gen_base::operator() (vec2d P, uint32_t seed) { vec2d I = floor (P); vec2d F = P - I; - float v = 0; + value_t v = 0; + + seed *= 3039177861; + uint8_t i1 = uint8_t (I[1]) + (seed >> 16); + uint8_t i0 = uint8_t (I[0]) + (seed >> 8); + + uint8_t h2 = rvmap[2](seed); for (int j = -1; j <= 2; ++j) - for (int i = -1; i <= 2; ++i) - { - vec2d A = F - vec2d (i, j); - - float d = dot (A, A); - - if (d < 4) - { - float t1 = 1 - d / 4; - float t2 = t1 * t1; - float t4 = t2 * t2; - - float p = (4 * t1 - 3) * t4; - - int h = rvmap[0](I[0] + i) ^ rvmap[1](I[1] + j); - vec2d G = charges2[h & 0xff]; - - v += dot (A, G) * p; - } - } + { + uint8_t h1 = rvmap[1](i1 + j) ^ h2; + + for (int i = -1; i <= 2; ++i) + { + vec2d A = F - vec2d (i, j); + value_t d = dot (A, A); + + if (d < 4) + { + value_t t1 = 1 - d / 4; + value_t t2 = t1 * t1; + value_t t4 = t2 * t2; + + value_t p = (4 * t1 - 3) * t4; + + uint8_t h = rvmap[0](i0 + i) ^ h1; + vec2d G = charges2[h & 0xff]; + + v += dot (A, G) * p; + } + } + } return v; -} +} ///////////////////////////////////////////////////////////////////////////// template<> vec3d::T_numtype -noise_gen_base::operator() (vec3d P) +noise_gen_base::operator() (vec3d P, uint32_t seed) { vec3d I = floor (P); vec3d F = P - I; - float v = 0; + seed *= 3039177861; + uint8_t i2 = uint8_t (I[2]) + (seed >> 24); + uint8_t i1 = uint8_t (I[1]) + (seed >> 16); + uint8_t i0 = uint8_t (I[0]) + (seed >> 8); - for (int j = -1; j <= 2; ++j) - for (int i = -1; i <= 2; ++i) - for (int k = -1; k <= 2; ++k) + uint8_t h3 = rvmap[3](seed); + + value_t v = 0; + + for (int k = -1; k <= 2; ++k) + { + uint8_t h2 = rvmap[2](i2 + k) ^ h3; + + for (int j = -1; j <= 2; ++j) { - vec3d A = F - vec3d (i, j, k); - float d = dot (A, A); + uint8_t h1 = rvmap[1](i1 + j) ^ h2; - if (d < 4) + for (int i = -1; i <= 2; ++i) { - float t1 = 1 - d / 4; - float t2 = t1 * t1; - float t4 = t2 * t2; + vec3d A = F - vec3d (i, j, k); + value_t d = dot (A, A); - // (4t⁵ - 3t⁴) - float p = (4 * t1 - 3) * t4; + if (d < 4) + { + value_t t1 = 1 - d / 4; + value_t t2 = t1 * t1; + value_t t4 = t2 * t2; - int h = rvmap[0](I[0] + i) ^ rvmap[1](I[1] + j) ^ rvmap[2](I[2] + k); - const float *G = charges3[h & 0xff]; + // (4t⁵ - 3t⁴) + value_t p = (4 * t1 - 3) * t4; - v += dot (A, vec3d (G[0], G[1], G[2])) * p; + uint8_t h = rvmap[0](i0 + i) ^ h1; + const value_t *G = charges3[h & 0xff]; + + v += dot (A, vec3d (G[0], G[1], G[2])) * p; + } } } + } return v * 2; } vec3d::T_numtype -noise_gen::operator() (vec3d P, vec3d N) +noise_gen::operator() (vec3d P, vec3d N, uint32_t seed) { vec3d I = floor (P); vec3d F = P - I; - float v = 0; + seed *= 3039177861; + uint8_t i2 = uint8_t (I[2]) + (seed >> 24); + uint8_t i1 = uint8_t (I[1]) + (seed >> 16); + uint8_t i0 = uint8_t (I[0]) + (seed >> 8); - for (int j = -1; j <= 2; ++j) - for (int i = -1; i <= 2; ++i) - for (int k = -1; k <= 2; ++k) + uint8_t h3 = rvmap[3](seed); + + value_t v = 0; + + for (int k = -1; k <= 2; ++k) + { + uint8_t h2 = rvmap[2](i2 + k) ^ h3; + + for (int j = -1; j <= 2; ++j) { - vec3d D = F - vec3d (i, j, k); - float e = dot (D, N); - float o = 1 - abs (e); + uint8_t h1 = rvmap[1](i1 + j) ^ h2; - if (o > 0) + for (int i = -1; i <= 2; ++i) { - vec3d A = D - e * N; - float d = dot (A, A); + vec3d D = F - vec3d (i, j, k); + value_t e = dot (D, N); + value_t o = 1 - abs (e); - if (d < 4) + if (o > 0) { - float t1 = 1 - d / 4; - float t2 = t1 * t1; - float t4 = t2 * t2; - - float o2 = o * o; - - // (4t⁵ - 3t⁴) * (3o³ - 2o²) - // alternative? ((o * 6 - 15) * o + 10) * o³ - float p = (4 * t1 - 3) * t4 * (3 * o2 - 2 * o2 * o); + vec3d A = D - e * N; + value_t d = dot (A, A); - int h = rvmap[0](I[0] + i) ^ rvmap[1](I[1] + j) ^ rvmap[2](I[2] + k); - const float *G = charges3[h & 0xff]; + if (d < 4) + { + value_t t1 = 1 - d / 4; + value_t t2 = t1 * t1; + value_t t4 = t2 * t2; + + value_t o2 = o * o; + + // (4t⁵ - 3t⁴) * (3o³ - 2o²) + // alternative? ((o * 6 - 15) * o + 10) * o³ + value_t p = (4 * t1 - 3) * t4 * (3 * o2 - 2 * o2 * o); - v += dot (A, vec3d (G[0], G[1], G[2])) * p; + uint8_t h = rvmap[0](i0 + i) ^ h1; + const value_t *G = charges3[h & 0xff]; + + v += dot (A, vec3d (G[0], G[1], G[2])) * p; + } } } } + } return v; } @@ -453,29 +420,16 @@ ///////////////////////////////////////////////////////////////////////////// -// find some uncorrelated spot - we assume that noise 2 units away -// is completely uncorrelated - this is not true for other coordinates, -// but generally works well. -template -static vec_t -ith_octave (vec_t P, int i) -{ - vec_t r = P; - r[0] += i; - r[1] += i * 2; - return r; -} - template -frac_gen::frac_gen (value_t hurst_expo, value_t lacunarity, seed_t seed) -: h (hurst_expo), lac (lacunarity) +frac_gen::frac_gen (int octaves, value_t lacunarity, value_t hurst_expo, seed_t seed) +: octaves (octaves), lac (lacunarity), h (hurst_expo) { this->seed (seed); value_t exsum = 0; value_t phi = noise (vec_t (value_t (0))) * 0.5 + 1; - for (int i = 0; i < MAX_OCTAVES; ++i) + for (int i = 0; i < octaves; ++i) { ex [i] = pow (lac, -h * i); exsum += ex [i]; @@ -486,14 +440,14 @@ template typename frac_gen::value_t -frac_gen::fBm (vec_t P, int octaves) +frac_gen::fBm (vec_t P) { value_t v = 0; for (int i = 0; i < octaves; ++i) { rot [i](P); - v += noise (ith_octave (P, i)) * ex [i]; + v += noise (P, i) * ex [i]; P *= lac; } @@ -502,14 +456,14 @@ template typename frac_gen::value_t -frac_gen::turbulence (vec_t P, int octaves) +frac_gen::turbulence (vec_t P) { value_t v = 0; for (int i = 0; i < octaves; ++i) { rot [i](P); - v += abs (noise (ith_octave (P, i))) * ex [i]; + v += abs (noise (P, i)) * ex [i]; P *= lac; } @@ -518,7 +472,7 @@ template typename frac_gen::value_t -frac_gen::multifractal (vec_t P, int octaves, value_t offset) +frac_gen::multifractal (vec_t P, value_t offset) { value_t v = 1; @@ -534,7 +488,7 @@ template typename frac_gen::value_t -frac_gen::heterofractal (vec_t P, int octaves, value_t offset) +frac_gen::heterofractal (vec_t P, value_t offset) { value_t v = noise (P) + offset; @@ -550,7 +504,7 @@ template typename frac_gen::value_t -frac_gen::hybridfractal (vec_t P, int octaves, value_t offset, value_t gain) +frac_gen::hybridfractal (vec_t P, value_t offset, value_t gain) { value_t v = (noise (P) + offset) * ex [0]; value_t weight = v; @@ -572,7 +526,7 @@ template typename frac_gen::value_t -frac_gen::ridgedmultifractal (vec_t P, int octaves, value_t offset, value_t gain) +frac_gen::ridgedmultifractal (vec_t P, value_t offset, value_t gain) { value_t sig = offset - abs (noise (P)); sig *= sig; @@ -583,6 +537,7 @@ rot [i](P); P *= lac; + // weight higher octaves by previous signal value_t w = clamp (sig * gain, 0, 1); sig = offset - abs (noise (P)); @@ -597,7 +552,7 @@ template typename frac_gen::value_t -frac_gen::billowfractal (vec_t P, int octaves, value_t offset, value_t gain) +frac_gen::billowfractal (vec_t P, value_t offset, value_t gain) { value_t v = 0; @@ -606,7 +561,7 @@ for (int i = 0; i < octaves; ++i) { rot [i](P); - v += (abs (noise (ith_octave (P, i))) * gain - offset) * ex [i]; + v += (abs (noise (P, i)) * gain - offset) * ex [i]; P *= lac; } @@ -616,7 +571,7 @@ // http://www.gamasutra.com/view/feature/3098/a_realtime_procedural_universe_.php?page=2 template typename frac_gen::value_t -frac_gen::terrain (vec_t P, int octaves) +frac_gen::terrain (vec_t P) { value_t v = 0; @@ -634,11 +589,11 @@ template typename frac_gen::value_t -frac_gen::terrain2 (vec_t P, int octaves) +frac_gen::terrain2 (vec_t P) { - value_t a = fBm (P, octaves); - value_t b = ridgedmultifractal (P, octaves, 1, 8); - value_t fade = fBm (P + vec_t(10.3), octaves); + value_t a = fBm (P); + value_t b = ridgedmultifractal (P, 1, 8); + value_t fade = fBm (P + vec_t(10.3)); const value_t width = 0.05; @@ -658,108 +613,3 @@ template class frac_gen; template class frac_gen; -///////////////////////////////////////////////////////////////////////////// - -void noise_test (); -void noise_test () -{ - frac_gen gen; - frac_gen gen3; - -#if 1 - int N = 1024; - - printf ("P5 %d %d 255\n", N, N); - // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm - for (int y = 0; y < N; ++y) - { - 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.heterofractal (vec2d (x * 0.008, y * 0.008), 8, 0.9), stdout); - - // mountais or somesuch(?) - //putc (256 * gen.hybridfractal (vec2d (x * 0.01, y * 0.01), 8, -.4, -4), stdout); - - // temperature - //putc (256 * gen.fBm (vec2d (x * 0.002, y * 0.002), 2), stdout); - // rain - - 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); - } - } -#else - int N = 512; - - //printf ("P6 %d %d 255\n", N, N); - // pmake&&server/deliantra-server >x&&convert -depth 8 -size 512xx512 gray:x x.ppm&& cv x.ppm - for (int z = 0; z < N; ++z) - { - if (!(z&7))fprintf (stderr, "z %d\n", z);//D - for (int y = 0; y < N; ++y) - for (int x = 0; x < N; ++x) - { - float v = gen3.ridgedmultifractal (vec3d (x * 0.01 + 0.2, y * 0.01 + 0.2, z * 0.01 + 0.2), 3, 1.03, 2) * 2; - -#if 0 - if (z < 64) - v = v * (z * z) / (64 * 64); -#endif - - if (v <= 0.9999) - continue; - - float r[4]; - int i[4]; - - r[0] = x; - r[1] = y; - r[2] = z; - r[3] = v; - - memcpy (i, r, 16); - - i[0] = htonl (i[0]); - i[1] = htonl (i[1]); - i[2] = htonl (i[2]); - i[3] = htonl (i[3]); - - fwrite (i, 4*4, 1, stdout); - } - } -#endif - - exit (0); -}