-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathPerlinDemo_01.cpp
43 lines (34 loc) · 1.06 KB
/
PerlinDemo_01.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
#include <cmath>
#include "ppm.h"
#include "PerlinNoise.h"
int main() {
// Define the size of the image
unsigned int width = 600, height = 450;
// Create an empty PPM image
ppm image(width, height);
// Create a PerlinNoise object with a random permutation vector generated with seed
unsigned int seed = 237;
PerlinNoise pn(seed);
unsigned int kk = 0;
// Visit every pixel of the image and assign a color generated with Perlin noise
for(unsigned int i = 0; i < height; ++i) { // y
for(unsigned int j = 0; j < width; ++j) { // x
double x = (double)j/((double)width);
double y = (double)i/((double)height);
// Typical Perlin noise
double n = pn.noise(10 * x, 10 * y, 0.8);
// Wood like structure
//n = 20 * pn.noise(x, y, 0.8);
//n = n - floor(n);
// Map the values to the [0, 255] interval, for simplicity we use
// tones of grey
image.r[kk] = floor(255 * n);
image.g[kk] = floor(255 * n);
image.b[kk] = floor(255 * n);
kk++;
}
}
// Save the image in a binary PPM file
image.write("figure_7_P.ppm");
return 0;
}