@@ -35,7 +35,11 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
35
35
depth, vector<vector<int >>(width, vector<int >(height, 0 )));
36
36
37
37
vector<vector<int >> elevationMap (width, vector<int >(height, 0 ));
38
- vector<vector<int >> elevationMapSmooth (width, vector<int >(height, 0 ));
38
+
39
+ int blockSize = 4 ;
40
+ int reducedX = floor (width / blockSize);
41
+ int reducedY = floor (height / blockSize);
42
+ vector<vector<int >> lowResMap (reducedX, vector<int >(reducedY, 0 ));
39
43
40
44
int dx[4 ] = { 1 , -1 , 0 , 0 };
41
45
int dy[4 ] = { 0 , 0 , 1 , -1 };
@@ -57,61 +61,29 @@ void TerrainGen::generate(GridMap *myGridMap, int height, int width, int depth,
57
61
}
58
62
}
59
63
60
- // Apply a Median Filter
61
- for (int x = 0 ; x < width; x++) {
62
- for (int y = 0 ; y < height; y++) {
63
- vector<int > neighbors;
64
-
65
- for (int dx = -1 ; dx <= 1 ; dx++) {
66
- for (int dy = -1 ; dy <= 1 ; dy++) {
67
- int nx = x + dx;
68
- int ny = y + dy;
69
-
70
- // Ensure indices are valid before adding to the list
71
- if (nx >= 0 && nx < width && ny >= 0 && ny < height) {
72
- neighbors.push_back (elevationMap[nx][ny]);
73
- }
74
- }
75
- }
64
+ // Downsample by sampling one pixel per block.
65
+ for (int x = 0 ; x < reducedX; x++) {
66
+ for (int y = 0 ; y < reducedY; y++) {
67
+ int sampleX = x * blockSize;
68
+ int sampleY = y * blockSize;
76
69
77
- if (!neighbors.empty ()) {
78
- sort (neighbors.begin (), neighbors.end ());
79
- elevationMapSmooth[x][y] = max (0 , static_cast <int >(neighbors[neighbors.size () / 2 ]));
80
- }
70
+ lowResMap[y][x] = (elevationMap[x][y] + 1 .0f ) / 2 .0f ;
81
71
}
82
72
}
83
73
84
- // Apply the smoothed elevation map
85
- elevationMap = elevationMapSmooth;
86
-
87
- /* ****************************************************
88
-
89
- Elevation Spacing
90
-
91
- The elevation map generates elevation changes to close together
92
- create a buffer that smooths out double edges
93
-
94
- *****************************************************/
95
-
96
- for (int x = 1 ; x < width - 1 ; x++) {
97
- for (int y = 1 ; y < height - 1 ; y++) {
98
- int elevation = elevationMap[x][y];
99
-
100
- bool hasElevationChange = false ;
101
- for (int d = 0 ; d < 4 ; ++d) {
102
- int nx = x + dx[d];
103
- int ny = y + dy[d];
104
-
105
- if (nx >= 0 && ny >= 0 && nx < width && ny < height) {
106
- if (abs (elevationMap[nx][ny] - elevation) > 1 ) {
107
- hasElevationChange = true ;
108
- }
109
- }
110
- }
74
+ // Posterize
75
+ for (int x = 0 ; x < reducedX; x++) {
76
+ for (int y = 0 ; y < reducedY; y++) {
77
+ int quantizedLevel = min (depth - 1 , static_cast <int >(lowResMap[x][y] * depth));
78
+ lowResMap[x][x] = quantizedLevel / static_cast <float >(depth - 1 );
79
+ }
80
+ }
111
81
112
- if (hasElevationChange) {
113
- elevationMap[x][y] = elevationMap[x - 1 ][y]; // Force buffer using prior elevation
114
- }
82
+ for (int x = 0 ; x < width; x++) {
83
+ for (int y = 0 ; y < height; y++) {
84
+ int srcX = x / blockSize;
85
+ int srcY = y / blockSize;
86
+ elevationMap[x][y] = lowResMap[srcX][srcY];
115
87
}
116
88
}
117
89
0 commit comments