38#pragma region Constructor and destructor
70#pragma endregion Constructor and destructor
75#pragma region Functions that change noise settings
92 for(
size_t i=0; i<
m_nSize; i++){
93 std::uniform_int_distribution<size_t> d(i,
m_nSize - 1);
118 assert(alpha < 0.0f);
120 std::uniform_real_distribution<float> d(-1.0f, 1.0f);
123 const size_t mid = (i + j)/2;
125 assert(i < mid && mid < j);
153 std::uniform_real_distribution<float> d(-1.0f, 1.0f);
155 for(
size_t i=0; i<
m_nSize; i++){
167 std::uniform_real_distribution<float> d(-1.0f, 1.0f);
169 for(
size_t i=0; i<
m_nSize; i++){
180 std::normal_distribution<float> d(500.0f, 200.0f);
182 for(
size_t i=0; i<
m_nSize; i++){
195 std::uniform_real_distribution<float> d(0.0f, 1.0f);
197 for(
size_t i=0; i<
m_nSize; i++){
209 std::exponential_distribution<float> d(4.0f);
213 for(
size_t i=0; i<half; i++)
216 for(
size_t i=half; i<
m_nSize; i++)
313#pragma endregion Functions that change noise settings
318#pragma region Helper functions
326 assert(-1.0f <= x && x <= 1.0f);
328 float fResult = 0.0f;
331 case eSpline::None: fResult = x;
break;
332 case eSpline::Cubic: fResult =
spline3(x);
break;
333 case eSpline::Quintic: fResult =
spline5(x);
break;
336 assert(-1.0f <= fResult && fResult <= 1.0f);
359 return (std::hash<size_t>{}(x) << 1) ^ std::hash<size_t>{}(y);
378 return std::hash<size_t>{}(x) &
m_nMask;
390 const uint64_t p0 = 11903454645187951493LL;
391 const uint64_t p1 = 2078231835154824277LL;
392 const uint64_t p2 = 5719147207009855033LL;
394 const uint64_t h = (p0*(uint64_t)x + p1*(uint64_t)y)%p2;
396 return size_t(h >> 8) &
m_nMask;
406 case eHash::Permutation:
411 case eHash::LinearCongruential:
413 c[2] =
hash2(x, y + 1); c[3] =
hash2(x + 1, y + 1);
436 assert(-1.0f <= x && x <= 1.0f);
437 assert(-1.0f <= y && y <= 1.0f);
445 assert(-2.0f <= result && result <= 2.0f);
450 assert(-1.0f <= result && result <= 1.0f);
469 assert(-1.0f <= sX && sX <= 1.0f);
470 assert( 0.0f <= fX && fX <= 1.0f);
471 assert(-1.0f <= fY && fY <= 1.0f);
473 const float result =
lerp(sX,
z(c[0], fX, fY, t),
z(c[1], fX - 1, fY, t));
480 assert(-1.5f <= result && result <= 1.5f);
485 assert(-1.0f <= result && result <= 1.0f);
489 default:
return 0.0f;
493#pragma endregion Helper functions
498#pragma region Noise generation functions
507 const size_t nX = (size_t)floorf(x);
508 const size_t nY = (size_t)floorf(y);
510 const float fX = x - floorf(x);
511 const float fY = y - floorf(y);
515 const float sX =
spline(fX);
516 const float sY =
spline(fY);
525 const float a =
Lerp(sX, fX, fY, c, t);
526 const float b =
Lerp(sX, fX, fY - 1, &(c[2]), t);
530 const float result =
lerp(sY, a, b);
531 assert(-1.0f <= result && result <= 1.0f);
549 float alpha,
float beta)
const
551 assert(0.0f <= alpha && alpha < 1.0f);
555 float amplitude = 1.0f;
557 for(
size_t i=0; i<n; i++){
558 sum += amplitude*
noise(x, y, t);
560 x *= beta; y *= beta;
563 assert(amplitude == powf(alpha, (
float)n));
565 float result = (1 - alpha)*sum/(1 - amplitude);
566 if(t == eNoise::Perlin)result *= 4.0f/3.0f;
567 assert(-1.0f <= result && result <= 1.0f);
571#pragma endregion Noise generation functions
576#pragma region Reader functions
627#pragma endregion Reader functions
eDistribution
Distribution.
const float lerp(float t, float a, float b)
Linear interpolation.
const float spline5(float t)
Quintic spline.
const bool isPowerOf2(size_t n)
Power of 2 test.
const float spline3(float t)
Cubic spline.
const float clamp(float a, float x, float b)
Clamp between two values.
Interface for helper functions.
const size_t GetMinTableSize() const
Get minimum table size.
const size_t m_nDefTableSize
Default table size.
CPerlinNoise2D()
Constructor.
const size_t hashstd(size_t) const
std::hash function.
void RandomizeTableExp()
Randomize table using exponential distribution.
void RandomizeTableMidpoint()
Randomize table using midpoint displacement.
const float z(size_t, float, float, eNoise) const
Apply gradients.
const size_t pair(size_t, size_t) const
Perlin pairing function.
const size_t m_nMinTableSize
Min table size.
eDistribution m_eDistribution
Uniform distribution..
const float noise(float, float, eNoise) const
Perlin noise.
const eSpline GetSpline() const
Get spline function type.
void SetHash(eHash)
Set hash function.
eHash m_eHash
Hash function type.
void RandomizeTableUniform()
Randomize table using uniform distribution.
float * m_fTable
Table of gradients or values.
const size_t GetMaxTableSize() const
Get maximum table size.
const size_t hash2(size_t, size_t) const
Hash function.
const size_t m_nMaxTableSize
Max table size.
size_t m_nSize
Table size, must be a power of 2.
const size_t GetTableSize() const
Get table size.
const size_t pairstd(size_t, size_t) const
Std pairing function.
bool DefaultTableSize()
Set table size to default.
void HashCorners(size_t, size_t, size_t[4]) const
Hash grid corners.
void SetSpline(eSpline)
Set spline function.
size_t * m_nPerm
Random permutation, used for hash function.
size_t m_nMask
Mask for values less than m_nSize.
eSpline m_eSpline
Spline function type.
const float generate(float, float, eNoise, size_t, float=0.5f, float=2.0f) const
Generate noise at a point.
void RandomizeTableMaximal()
Randomize table using large magnitude values.
void RandomizeTableNormal()
Randomize table using normal distribution.
std::default_random_engine m_stdRandom
PRNG.
void RandomizeTable(eDistribution)
Randomize table from distribution.
~CPerlinNoise2D()
Destructor.
const float Lerp(float, float, float, size_t *, eNoise) const
Linear interpolation.
const eHash GetHash() const
Get hash function type.
bool DoubleTableSize()
Double table size.
const eDistribution GetDistribution() const
Get distribution type.
bool HalveTableSize()
Halve table size.
const size_t hash(size_t) const
Perlin hash function.
const size_t GetDefTableSize() const
Get default table size.
void SetSeed()
Set seed for PRNG.
void RandomizePermutation()
Randomize permutation.
const float spline(float) const
Spline curve.
void Initialize()
Initialize.
void RandomizeTableCos()
Randomize table using cosine.
Interface for the Perlin and Value noise generators.