-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
example.cpp
218 lines (178 loc) · 4.81 KB
/
example.cpp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# include <cassert>
# include <iostream>
# include <fstream>
# include <sstream>
# include "PerlinNoise.hpp"
# pragma pack (push, 1)
struct BMPHeader
{
std::uint16_t bfType;
std::uint32_t bfSize;
std::uint16_t bfReserved1;
std::uint16_t bfReserved2;
std::uint32_t bfOffBits;
std::uint32_t biSize;
std::int32_t biWidth;
std::int32_t biHeight;
std::uint16_t biPlanes;
std::uint16_t biBitCount;
std::uint32_t biCompression;
std::uint32_t biSizeImage;
std::int32_t biXPelsPerMeter;
std::int32_t biYPelsPerMeter;
std::uint32_t biClrUsed;
std::uint32_t biClrImportant;
};
static_assert(sizeof(BMPHeader) == 54);
# pragma pack (pop)
struct RGB
{
double r = 0.0;
double g = 0.0;
double b = 0.0;
constexpr RGB() = default;
explicit constexpr RGB(double _rgb) noexcept
: r{ _rgb }, g{ _rgb }, b{ _rgb } {}
constexpr RGB(double _r, double _g, double _b) noexcept
: r{ _r }, g{ _g }, b{ _b } {}
};
class Image
{
public:
Image() = default;
Image(std::size_t width, std::size_t height)
: m_data(width* height)
, m_width{ static_cast<std::int32_t>(width) }
, m_height{ static_cast<std::int32_t>(height) } {}
void set(std::int32_t x, std::int32_t y, const RGB& color)
{
if (not inBounds(y, x))
{
return;
}
m_data[static_cast<std::size_t>(y) * m_width + x] = color;
}
std::int32_t width() const noexcept { return m_width; }
std::int32_t height() const noexcept { return m_height; }
bool saveBMP(const std::string& path)
{
const std::int32_t rowSize = m_width * 3 + m_width % 4;
const std::uint32_t bmpsize = rowSize * m_height;
const BMPHeader header =
{
0x4d42,
static_cast<std::uint32_t>(bmpsize + sizeof(BMPHeader)),
0, 0, sizeof(BMPHeader), 40,
m_width, m_height, 1, 24,
0, bmpsize, 0, 0, 0, 0
};
if (std::ofstream ofs{ path, std::ios_base::binary })
{
ofs.write(reinterpret_cast<const char*>(&header), sizeof(header));
std::vector<std::uint8_t> line(rowSize);
for (std::int32_t y = m_height - 1; -1 < y; --y)
{
size_t pos = 0;
for (std::int32_t x = 0; x < m_width; ++x)
{
const RGB& col = m_data[static_cast<std::size_t>(y) * m_width + x];
line[pos++] = ToUint8(col.b);
line[pos++] = ToUint8(col.g);
line[pos++] = ToUint8(col.r);
}
ofs.write(reinterpret_cast<const char*>(line.data()), line.size());
}
return true;
}
else
{
return false;
}
}
private:
std::vector<RGB> m_data;
std::int32_t m_width = 0, m_height = 0;
bool inBounds(std::int32_t y, std::int32_t x) const noexcept
{
return (0 <= y) && (y < m_height) && (0 <= x) && (x < m_width);
}
static constexpr std::uint8_t ToUint8(double x) noexcept
{
return (x <= 0.0) ? 0 : (1.0 <= x) ? 255 : static_cast<std::uint8_t>(x * 255.0 + 0.5);
}
};
void Test()
{
siv::PerlinNoise perlinA{ std::random_device{} };
siv::PerlinNoise perlinB;
perlinB.deserialize(perlinA.serialize());
assert(perlinA.octave3D(0.1, 0.2, 0.3, 4)
== perlinB.octave3D(0.1, 0.2, 0.3, 4));
perlinA.reseed(12345u);
perlinB.reseed(12345u);
assert(perlinA.octave3D(0.1, 0.2, 0.3, 4)
== perlinB.octave3D(0.1, 0.2, 0.3, 4));
perlinA.reseed(std::mt19937{ 67890u });
perlinB.reseed(std::mt19937{ 67890u });
assert(perlinA.octave3D(0.1, 0.2, 0.3, 4)
== perlinB.octave3D(0.1, 0.2, 0.3, 4));
for (std::int32_t y = 0; y < 20; ++y)
{
for (std::int32_t x = 0; x < 20; ++x)
{
const double noise = perlinA.octave2D_01(x * 0.1, y * 0.1, 6);
std::cout << static_cast<int>(std::floor(noise * 10) - 0.5);
}
std::cout << '\n';
}
}
int main()
{
Test();
Image image{ 512, 512 };
std::cout << "---------------------------------\n";
std::cout << "* frequency [0.1 .. 8.0 .. 64.0] \n";
std::cout << "* octaves [1 .. 8 .. 16] \n";
std::cout << "* seed [0 .. 2^32-1] \n";
std::cout << "---------------------------------\n";
for (;;)
{
double frequency;
std::cout << "double frequency = ";
std::cin >> frequency;
frequency = std::clamp(frequency, 0.1, 64.0);
std::int32_t octaves;
std::cout << "int32 octaves = ";
std::cin >> octaves;
octaves = std::clamp(octaves, 1, 16);
std::uint32_t seed;
std::cout << "uint32 seed = ";
std::cin >> seed;
const siv::PerlinNoise perlin{ seed };
const double fx = (frequency / image.width());
const double fy = (frequency / image.height());
for (std::int32_t y = 0; y < image.height(); ++y)
{
for (std::int32_t x = 0; x < image.width(); ++x)
{
const RGB color(perlin.octave2D_01((x * fx), (y * fy), octaves));
image.set(x, y, color);
}
}
std::stringstream ss;
ss << 'f' << frequency << 'o' << octaves << '_' << seed << ".bmp";
if (image.saveBMP(ss.str()))
{
std::cout << "...saved \"" << ss.str() << "\"\n";
}
else
{
std::cout << "...failed\n";
}
char c;
std::cout << "continue? [y/n] >";
std::cin >> c;
if (c != 'y') break;
std::cout << '\n';
}
}