Smooth 2D Noise Viewer
Perlin and Value Noise
perlin.h
Go to the documentation of this file.
1
4
5// MIT License
6//
7// Copyright (c) 2022 Ian Parberry
8//
9// Permission is hereby granted, free of charge, to any person obtaining a copy
10// of this software and associated documentation files (the "Software"), to
11// deal in the Software without restriction, including without limitation the
12// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
13// sell copies of the Software, and to permit persons to whom the Software is
14// furnished to do so, subject to the following conditions:
15//
16// The above copyright notice and this permission notice shall be included in
17// all copies or substantial portions of the Software.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25// IN THE SOFTWARE.
26
27#ifndef __PERLIN_H__
28#define __PERLIN_H__
29
30#include <windows.h>
31#include <windowsx.h>
32#include <random>
33
34#include "Defines.h"
35
47
49 private:
50 eHash m_eHash = eHash::Permutation;
51 eSpline m_eSpline = eSpline::Cubic;
52 eDistribution m_eDistribution = eDistribution::Uniform;
53
54 size_t* m_nPerm = nullptr;
55 float* m_fTable = nullptr;
56
57 std::default_random_engine m_stdRandom;
58 UINT m_nSeed = 0;
59
60 const size_t m_nDefTableSize = 256;
61 const size_t m_nMinTableSize = 16;
62 const size_t m_nMaxTableSize = 1024;
63
65 size_t m_nMask = m_nDefTableSize - 1;
66
67 inline const size_t pair(size_t, size_t) const;
68 inline const size_t pairstd(size_t, size_t) const;
69
70 inline const size_t hash(size_t) const;
71 inline const size_t hashstd(size_t) const;
72 inline const size_t hash2(size_t, size_t) const;
73
74 void HashCorners(size_t, size_t, size_t[4]) const;
75
77 void RandomizeTableCos();
79 void RandomizeTableExp();
81
82 void RandomizeTableMidpoint(size_t, size_t, float);
84
85 inline const float spline(float) const;
86 inline const float z(size_t, float, float, eNoise) const;
87 const float Lerp(float, float, float, size_t*, eNoise) const;
88 const float noise(float, float, eNoise) const;
89
91 void Initialize();
92
93 public:
96
97 const float generate(float, float, eNoise, size_t, float=0.5f, float=2.0f)
98 const;
99
100 //functions that change the noise properties
101
102 void SetSeed();
104
105 bool DoubleTableSize();
106 bool HalveTableSize();
107 bool DefaultTableSize();
108
109 void SetSpline(eSpline);
110 void SetHash(eHash);
111
112 //reader functions
113
114 const size_t GetTableSize() const;
115 const size_t GetMinTableSize() const;
116 const size_t GetMaxTableSize() const;
117 const size_t GetDefTableSize() const;
118
119 const eHash GetHash() const;
120 const eSpline GetSpline() const;
121 const eDistribution GetDistribution() const;
122}; //CPerlinNoise2D
123
124#endif //__PERLIN_H__
Useful defines, constants, and types.
eDistribution
Distribution.
Definition: Defines.h:54
eSpline
Spline type.
Definition: Defines.h:62
eHash
Hash function type.
Definition: Defines.h:46
eNoise
Perlin noise type.
Definition: Defines.h:38
2D Perlin and Value noise generator.
Definition: perlin.h:48
const size_t GetMinTableSize() const
Get minimum table size.
Definition: perlin.cpp:588
const size_t m_nDefTableSize
Default table size.
Definition: perlin.h:60
CPerlinNoise2D()
Constructor.
Definition: perlin.cpp:42
const size_t hashstd(size_t) const
std::hash function.
Definition: perlin.cpp:377
void RandomizeTableExp()
Randomize table using exponential distribution.
Definition: perlin.cpp:208
void RandomizeTableMidpoint()
Randomize table using midpoint displacement.
Definition: perlin.cpp:141
const float z(size_t, float, float, eNoise) const
Apply gradients.
Definition: perlin.cpp:435
const size_t pair(size_t, size_t) const
Perlin pairing function.
Definition: perlin.cpp:346
const size_t m_nMinTableSize
Min table size.
Definition: perlin.h:61
eDistribution m_eDistribution
Uniform distribution..
Definition: perlin.h:52
const float noise(float, float, eNoise) const
Perlin noise.
Definition: perlin.cpp:506
const eSpline GetSpline() const
Get spline function type.
Definition: perlin.cpp:616
void SetHash(eHash)
Set hash function.
Definition: perlin.cpp:309
eHash m_eHash
Hash function type.
Definition: perlin.h:50
void RandomizeTableUniform()
Randomize table using uniform distribution.
Definition: perlin.cpp:152
float * m_fTable
Table of gradients or values.
Definition: perlin.h:55
const size_t GetMaxTableSize() const
Get maximum table size.
Definition: perlin.cpp:595
const size_t hash2(size_t, size_t) const
Hash function.
Definition: perlin.cpp:389
const size_t m_nMaxTableSize
Max table size.
Definition: perlin.h:62
size_t m_nSize
Table size, must be a power of 2.
Definition: perlin.h:64
const size_t GetTableSize() const
Get table size.
Definition: perlin.cpp:581
const size_t pairstd(size_t, size_t) const
Std pairing function.
Definition: perlin.cpp:358
bool DefaultTableSize()
Set table size to default.
Definition: perlin.cpp:278
void HashCorners(size_t, size_t, size_t[4]) const
Hash grid corners.
Definition: perlin.cpp:404
void SetSpline(eSpline)
Set spline function.
Definition: perlin.cpp:302
size_t * m_nPerm
Random permutation, used for hash function.
Definition: perlin.h:54
size_t m_nMask
Mask for values less than m_nSize.
Definition: perlin.h:65
eSpline m_eSpline
Spline function type.
Definition: perlin.h:51
const float generate(float, float, eNoise, size_t, float=0.5f, float=2.0f) const
Generate noise at a point.
Definition: perlin.cpp:548
void RandomizeTableMaximal()
Randomize table using large magnitude values.
Definition: perlin.cpp:166
void RandomizeTableNormal()
Randomize table using normal distribution.
Definition: perlin.cpp:179
std::default_random_engine m_stdRandom
PRNG.
Definition: perlin.h:57
void RandomizeTable(eDistribution)
Randomize table from distribution.
Definition: perlin.cpp:227
~CPerlinNoise2D()
Destructor.
Definition: perlin.cpp:49
const float Lerp(float, float, float, size_t *, eNoise) const
Linear interpolation.
Definition: perlin.cpp:466
const eHash GetHash() const
Get hash function type.
Definition: perlin.cpp:609
bool DoubleTableSize()
Double table size.
Definition: perlin.cpp:245
const eDistribution GetDistribution() const
Get distribution type.
Definition: perlin.cpp:623
bool HalveTableSize()
Halve table size.
Definition: perlin.cpp:262
UINT m_nSeed
PRNG seed.
Definition: perlin.h:58
const size_t hash(size_t) const
Perlin hash function.
Definition: perlin.cpp:367
const size_t GetDefTableSize() const
Get default table size.
Definition: perlin.cpp:602
void SetSeed()
Set seed for PRNG.
Definition: perlin.cpp:295
void RandomizePermutation()
Randomize permutation.
Definition: perlin.cpp:86
const float spline(float) const
Spline curve.
Definition: perlin.cpp:325
void Initialize()
Initialize.
Definition: perlin.cpp:58
void RandomizeTableCos()
Randomize table using cosine.
Definition: perlin.cpp:194