-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoiseMap.h
88 lines (76 loc) · 2.6 KB
/
NoiseMap.h
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
//
// NoiseMap.h
// Perlin
//
// Created by Kyle Burns on 1/14/20.
// Copyright © 2020 Kyle Burns. All rights reserved.
//
#ifndef NoiseMap_h
#define NoiseMap_h
#include "Noise.h"
#include <random>
#include <iostream>
class NoiseMap
{
int h, w;
public:
float * noisemap;
float ** tempmap;
Noise* n;
NoiseMap(int width, int height)
{
srand( (unsigned)time(NULL) );
tempmap = new float* [height + 2];
for ( int i = 0; i < height + 2; i++) tempmap[i] = new float[width + 2];
noisemap = new float[height * width];
h = height;
w = width;
n = new Noise;
for ( int y = 0; y < height + 2; y++ ) // + 2 allows for gaussian blur and clipping the egdes off later.
for ( int x = 0; x < width + 2; x++)
{
tempmap[y][x] = n->OctavePerlin( x * 50.34f, y * 50.34f, 0, 8, .50, .00010f); // ~.00024 appears to be optimal
//std::cout << "here: " << *noisemap << "\n";
}
// apply gaussian blur to plus sized tempmap
blur(); blur(); blur();
// copy only blurred region of plus sized tempmap to appropriately sized noisemap
for ( int y = 1; y < height + 1; y++ )
for ( int x = 1; x < width + 1; x++ )
noisemap[(y-1) * width + (x-1)] = tempmap[y][x];
//for ( int i = 0; i < h + 2; i++) delete[] tempmap[i];
delete[] tempmap;
}
~NoiseMap()
{
delete n;
delete[] noisemap;
}
void correct()
{
for ( int y = 0; y < h; y ++)
for ( int x = 0; x < w; x++) noisemap[y * h + x] *= 0.65f;
}
void blur()
{
float sum = 0;
for (int y = 1; y < h + 1 ; y ++)
for (int x = 1; x < w + 1; x++)
{
sum = 0;
// 3x3 gaussian kernel
sum += tempmap[y][x] * .25; // current cell
sum += tempmap[y-1][x] * .125; // left cell
sum += tempmap[y+1][x] * .125; // right cell
sum += tempmap[y][x-1] * .125; // bottom cell
sum += tempmap[y][x+1] * .125; // upper cell
sum += tempmap[y-1][x+1] * .0625; // upper left cell
sum += tempmap[y+1][x+1] * .0625; // upper right cell
sum += tempmap[y-1][x-1] * .0625; // lower left cell
sum += tempmap[y+1][x-1] * 0.625; // lower right cell
if (0.65f * sum < .455) tempmap[y][x] = 0.455f;
else tempmap[y][x] = 0.645f * sum;
}
}
};
#endif /* NoiseMap_h */