/* * This file is part of Deliantra, the Roguelike Realtime MMORPG. * * Copyright (©) 2010 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 */ #ifndef NOISE_H_ #define NOISE_H_ #include "global.h" template struct permutation { T pmap [N]; template void seed (generator &rng) { for (int i = 0; i < N; ++i) pmap [i] = i; // fisher-yates to randomly perturb for (int i = N; --i; ) ::swap (pmap [i], pmap [::rmg_rndm (i + 1)]); } T operator ()(T v) { return pmap [v & (N - 1)]; } }; struct noise_gen { permutation<256, uint8_t> rvmap[2]; float rvec [256][2]; // random unit vectors noise_gen (uint32_t seed); float noise (float x, float y); }; struct rotate { float s, c; void set (float angle) { s = sinf (angle); c = cosf (angle); } void operator ()(float &x, float &y) const { float xr = x * c - y * s; float yr = x * s + y * c; x = xr; y = yr; } }; struct frac_gen : noise_gen { enum { MAX_OCTAVES = 64 }; float h, lac, ex [MAX_OCTAVES]; float fbm_mul [MAX_OCTAVES]; rotate rot [MAX_OCTAVES]; frac_gen (float hurst_expo = .5f, float lacunarity = 2.f); float fBm (float x, float y, int octaves); float turbulence (float x, float y, int octaves); float multifractal (float x, float y, int octaves, float offset = 1); float heterofractal (float x, float y, int octaves, float offset = 1); float hybridfractal (float x, float y, int octaves, float offset = 1, float gain = 1); float terrain (float x, float y, int octaves); float terrain2 (float x, float y, int octaves); float ridgedmultifractal (float x, float y, int octaves, float offset = 1, float gain = 8); }; #endif