Skip to content

Commit 60a33d6

Browse files
committed
Add map smooth and coastline
1 parent 478e4be commit 60a33d6

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

Classes/terrainGenerator.cpp

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ void terrainGenerator::Terrain::createTexture() {
3030

3131
//smoothHeights(4);
3232

33+
smoothFromBuffer();
34+
3335
// generate height map chunks
3436
for (int chunkRow = 0; chunkRow < lineChunks; chunkRow++) {
3537
for (int chunkCol = 0; chunkCol < lineChunks; chunkCol++) {
@@ -41,9 +43,10 @@ void terrainGenerator::Terrain::createTexture() {
4143
int jDraw = 0;
4244
for (int j = chunkCol * chunkSize; j < chunkCol * chunkSize + chunkSize; j++, jDraw++) {
4345

44-
terrainMap[i][n - j - 1].height = linearNormalize(terrainMap[i][n - j - 1].height,
45-
minHeight,
46-
maxHeight);
46+
//terrainMap[i][n - j - 1].height = linearNormalize(terrainMap[i][n - j - 1].height,
47+
// minHeight,
48+
// maxHeight);
49+
4750
double height = terrainMap[i][n - j - 1].height;
4851

4952
double heightColor[3] = {height * 255, height * 255, height * 255};
@@ -124,7 +127,7 @@ void terrainGenerator::Terrain::createTexture() {
124127
terrainGenerator::Terrain::Terrain(std::size_t _n, int _seed, size_t _tileSize) :
125128
n(_n), seed(_seed), tileSize(_tileSize) {
126129
terrainMap = std::vector<std::vector<Tile> >(_n, std::vector<Tile>(_n));
127-
130+
bufferHeights = std::vector<std::vector<double> >(_n, std::vector<double>(_n));
128131
createTexture();
129132
}
130133

@@ -215,7 +218,7 @@ void terrainGenerator::Terrain::generateHeightMap() {
215218

216219
double heightColor[3] = {height * 255, height * 255, height * 255};
217220

218-
terrainMap[i][n - j - 1].height = height;
221+
bufferHeights[i][n - j - 1] = height;
219222
}
220223
}
221224
}
@@ -229,13 +232,13 @@ terrainGenerator::TileType terrainGenerator::Terrain::tileByHeight(double height
229232
return TileType::grass;
230233
}
231234

232-
void terrainGenerator::Terrain::smoothHeights(double a) {
233-
for (int i = 0; i < n; i++) {
234-
for (int j = 0; j < n; j++) {
235-
terrainMap[i][j].height = std::round(terrainMap[i][j].height * a) / a;
236-
}
237-
}
238-
}
235+
//void terrainGenerator::Terrain::smoothHeights(double a) {
236+
// for (int i = 0; i < n; i++) {
237+
// for (int j = 0; j < n; j++) {
238+
// terrainMap[i][j].height = std::round(terrainMap[i][j].height * a) / a;
239+
// }
240+
// }
241+
//}
239242

240243
void terrainGenerator::Terrain::multiptyImages(cimg_library::CImg<double> &A, cimg_library::CImg<double> &mul,
241244
float coef) {
@@ -250,6 +253,38 @@ void terrainGenerator::Terrain::multiptyImages(cimg_library::CImg<double> &A, ci
250253
}
251254
}
252255

256+
void terrainGenerator::Terrain::smoothFromBuffer() {
257+
for (int i = 0; i < n; i++) {
258+
for (int j = 0; j < n; j++) {
259+
if (i == 0 || j == 0 || i == n - 1 || j == n - 1)
260+
bufferHeights[i][j] = 0.0;
261+
else
262+
bufferHeights[i][j] = linearNormalize(bufferHeights[i][j],
263+
minHeight,
264+
maxHeight);
265+
}
266+
}
267+
268+
for (int i = 1; i < n - 1; i++) {
269+
for (int j = 1; j < n - 1; j++) {
270+
// median in i j pos
271+
double median = 0.0;
272+
for (int mI = i - 1; mI <= i + 1; mI++)
273+
for (int mJ = j - 1; mJ <= j + 1; mJ++)
274+
median += bufferHeights[mI][mJ];
275+
median = median / 9.0;
276+
277+
terrainMap[i][j].height = median;
278+
}
279+
}
280+
281+
for (int i = 1; i < n - 1; i++) {
282+
for (int j = 1; j < n - 1; j++) {
283+
bufferHeights[i][j] = terrainMap[i][j].height;
284+
}
285+
}
286+
}
287+
253288

254289
void terrainGenerator::Tile::addCreature(Tribe *tribe) {
255290
tribes.push_back(tribe);

Classes/terrainGenerator.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "random.h"
1313
#include "FastNoise.h"
1414

15+
1516
#include <string>
1617
#include <vector>
1718
#include <random>
@@ -126,6 +127,8 @@ namespace terrainGenerator {
126127
TileType tileByHeight(double height);
127128

128129
void multiptyImages(cl::CImg<double> &A, cl::CImg<double> &mul, float coef);
130+
131+
void smoothFromBuffer();
129132
};
130133
}
131134

0 commit comments

Comments
 (0)