-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerlinNoise.cpp
More file actions
145 lines (110 loc) · 2.99 KB
/
PerlinNoise.cpp
File metadata and controls
145 lines (110 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "../Include/defGameEngine.hpp"
using namespace def;
Vector2f RandomGradient(const Vector2i& i)
{
uint32_t a = i.x, b = i.y;
a *= 3284157443u; b ^= a << 16 | a >> 16;
b *= 1911520717u; a ^= b << 16 | b >> 16;
a *= 2048419325u;
float random = (float)a * 3.14159f / float(~(~0u >> 1));
return { cosf(random), sinf(random) };
}
float DotProductGridGradient(const Vector2i& i, const Vector2f& p)
{
return (p - Vector2f(i)).DotProduct(RandomGradient(i));
}
// Returns float value (0.0f - 1.0f)
float PerlinNoise2D(const Vector2f& p)
{
Vector2i i0 = p.Floor();
Vector2i i1 = i0 + 1.0f;
Vector2f w = p - Vector2f(i0);
float ix0 = std::lerp(
DotProductGridGradient(i0, p),
DotProductGridGradient({ i1.x, i0.y }, p),
w.x);
float ix1 = std::lerp(
DotProductGridGradient({ i0.x, i1.y }, p),
DotProductGridGradient(i1, p),
w.x);
return std::lerp(ix0, ix1, w.y);
}
class PerlinNoise : public def::GameEngine
{
public:
PerlinNoise()
{
GetWindow()->SetTitle("Perlin Noise");
}
private:
void UpdateMap()
{
for (int i = 0; i < GetWindow()->GetScreenWidth(); i++)
for (int j = 0; j < GetWindow()->GetScreenHeight(); j++)
{
float n = 0.0f;
float frequency = 1.0f;
float amplitude = 1.0f;
for (int o = 0; o < octaves; o++)
{
n += PerlinNoise2D(Vector2f(i, j) / (Vector2f)GetWindow()->GetScreenSize() * frequency) * amplitude;
frequency *= 2.0f;
amplitude *= 0.5f;
}
map[j * GetWindow()->GetScreenWidth() + i] = std::clamp(n * 1.2f, -1.0f, 1.0f) * 0.5f + 0.5f;
}
}
private:
int octaves = 12;
float* map = nullptr;
protected:
bool OnUserCreate() override
{
int size = GetWindow()->GetScreenWidth() * GetWindow()->GetScreenHeight();
map = new float[size];
memset(map, 0, sizeof(float) * size);
UpdateMap();
return true;
}
bool OnUserUpdate(float deltaTime) override
{
if (GetInput()->GetKeyState(def::Key::LEFT).pressed) octaves--;
if (GetInput()->GetKeyState(def::Key::RIGHT).pressed) octaves++;
octaves = std::clamp(octaves, 0, 32);
if (GetInput()->GetKeyState(def::Key::SPACE).pressed)
UpdateMap();
for (int i = 0; i < GetWindow()->GetScreenWidth(); i++)
for (int j = 0; j < GetWindow()->GetScreenHeight(); j++)
{
float n = map[j * GetWindow()->GetScreenWidth() + i];
uint8_t col = uint8_t(n * 12.0f);
def::Pixel pix;
switch (col)
{
case 0: pix = BLACK; break;
case 1: pix = DARK_BROWN; break;
case 2: pix = BROWN; break;
case 3: pix = DARK_RED; break;
case 4: pix = RED; break;
case 5: pix = DARK_PURPLE; break;
case 6: pix = DARK_BLUE; break;
case 7: pix = PURPLE; break;
case 8: pix = BLUE; break;
case 9: pix = DARK_ORANGE; break;
case 10: pix = ORANGE; break;
case 11: pix = BEIGE; break;
case 12: pix = YELLOW; break;
}
Draw(i, j, pix);
}
DrawString(2, 2, "Octaves = " + std::to_string(octaves), def::CYAN);
return true;
}
};
int main()
{
PerlinNoise demo;
demo.Construct(1280, 720, 1, 1);
demo.Run();
return 0;
}