/*
* 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 gen_vec_base
{
enum { D = N };
T v[N];
T &operator [](int n)
{
return v[n];
}
template
const gen_vec_base &operator =(gen_vec_base v)
{
for (int i = 0; i < N; ++i)
this->v[i] = v.v[i];
return *this;
}
};
template
gen_vec_base floor (gen_vec_base v)
{
for (int i = 0; i < N; ++i)
v[i] = floor (v[i]);
return v;
}
template
gen_vec_base operator +(gen_vec_base a, gen_vec_base b)
{
gen_vec_base c;
for (int i = 0; i < N; ++i)
c[i] = a[i] + b[i];
return c;
}
template
gen_vec_base operator -(gen_vec_base a, gen_vec_base b)
{
gen_vec_base c;
for (int i = 0; i < N; ++i)
c[i] = a[i] - b[i];
return c;
}
template
T operator *(gen_vec_base a, gen_vec_base b)
{
T v = 0;
for (int i = 0; i < N; ++i)
v += a[i] * b[i];
return v;
}
template
struct gen_vec;
template
struct gen_vec<2,T> : gen_vec_base<2,T>
{
using gen_vec_base<2,T>::operator =;
gen_vec ()
{
}
gen_vec (T x, T y)
{
this->v[0] = x;
this->v[1] = y;
}
template
gen_vec (gen_vec_base<2,U> v)
{
*this = v;
}
};
template
struct gen_vec<3,T> : gen_vec_base<3,T>
{
gen_vec ()
{
}
gen_vec (T x, T y, T z)
{
this->v[0] = x;
this->v[1] = y;
this->v[2] = z;
}
template
gen_vec (gen_vec_base<3,U> v)
{
*this = v;
}
};
typedef gen_vec<2> vec2d;
typedef gen_vec<3> vec3d;
template
struct space
{
enum { D = N };
typedef gen_vec vec;
};
typedef space<2> space2d;
typedef space<3> space3d;
/////////////////////////////////////////////////////////////////////////////
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)];
}
};
// modelled after 2d/3d kensler noise without projection
struct noise2d
{
permutation<256, uint8_t> rvmap[2];
float rvec [256][2]; // random unit vectors
noise2d (uint32_t seed);
float noise (float x, float y);
};
struct noise3d
{
permutation<256, uint8_t> rvmap[3];
float rvec [256][3]; // random unit vectors
noise3d (uint32_t seed);
float noise (float x, float y, float z);
// noise projected on a surface with normal n
float noise (float x, float y, float z, float nx, float ny, float nz);
};
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
: noise2d
{
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