-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterrainGenerator.cpp
292 lines (254 loc) · 8.45 KB
/
terrainGenerator.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include "terrainGenerator.h"
#include "perlin.h"
#include "structs.h"
#include "engine.h"
namespace App::TerrainGenerator
{
void run(
Excal::Engine::EngineConfig& config
) {
config.appName = "vkTerrainGenerator";
config.windowWidth = 1440*0.7;
config.windowHeight = 900 *0.7;
config.vertShaderPath = "../shaders/terrainShader.vert.spv";
config.fragShaderPath = "../shaders/terrainShader.frag.spv";
config.frontFace = "clockwise";
config.clearColor = glm::vec4(0.53, 0.81, 0.92, 1.0);
config.farClipPlane = 512.0;
config.camera.pos = glm::vec3(192, 70, 320);
// App params
const int xMapChunks = 3;
const int yMapChunks = 3;
const int chunkWidth = 128;
const int chunkHeight = 128;
const float waterHeight = 0.1;
const float meshHeight = 32; // Vertical scaling
// Noise params
int octaves = 5;
float noiseScale = 64; // Horizontal scaling
float persistence = 0.5;
float lacunarity = 2;
// Generate map chunks
for (int yPos = 0; yPos < yMapChunks; yPos++) {
for (int xPos = 0; xPos < xMapChunks; xPos++) {
config.models.push_back(
generateMapChunk(
xPos, yPos,
xMapChunks, yMapChunks,
chunkWidth, chunkHeight,
waterHeight, meshHeight,
octaves, noiseScale,
persistence, lacunarity
)
);
}
}
}
glm::vec3 getColor(
const int r,
const int g,
const int b
) {
return glm::vec3(r/255.0, g/255.0, b/255.0);
}
std::vector<float> generateNoiseMap(
const int xOffset,
const int yOffset,
const int chunkWidth,
const int chunkHeight,
const int octaves,
const float noiseScale,
const float persistence,
const float lacunarity
) {
std::vector<float> noiseValues;
std::vector<float> normalizedNoiseValues;
std::vector<int> p = Perlin::get_permutation_vector();
float amp = 1;
float freq = 1;
float maxPossibleHeight = 0;
for (int i = 0; i < octaves; i++) {
maxPossibleHeight += amp;
amp *= persistence;
}
for (int y = 0; y < chunkHeight; y++) {
for (int x = 0; x < chunkWidth; x++) {
amp = 1;
freq = 1;
float noiseHeight = 0;
for (int i = 0; i < octaves; i++) {
float xSample = (x + xOffset * (chunkWidth-1)) / noiseScale * freq;
float ySample = (y + yOffset * (chunkHeight-1)) / noiseScale * freq;
float perlinValue = Perlin::perlin_noise(xSample, ySample, p);
noiseHeight += perlinValue * amp;
// Lacunarity --> Increase in frequency of octaves
// Persistence --> Decrease in amplitude of octaves
amp *= persistence;
freq *= lacunarity;
}
noiseValues.push_back(noiseHeight);
}
}
for (int y = 0; y < chunkHeight; y++) {
for (int x = 0; x < chunkWidth; x++) {
// Inverse lerp and scale values to range from 0 to 1
normalizedNoiseValues.push_back(
(noiseValues[x + y*chunkWidth] + 1) / maxPossibleHeight
);
}
}
return normalizedNoiseValues;
}
std::vector<float> generateVertices(
const std::vector<float>& noise_map,
const float waterHeight,
const int xOffset,
const int yOffset,
const int chunkWidth,
const int chunkHeight,
const float meshHeight
) {
std::vector<float> vertices;
for (int z = 0; z < chunkHeight; z++) {
for (int x = 0; x < chunkWidth; x++) {
// Apply cubic easing to the noise
float easedNoise = std::pow(noise_map[x + z*chunkWidth] * 1.1, 3);
// Scale noise to match meshHeight
// Pervent vertex height from being below waterHeight
float y = std::fmax(easedNoise * meshHeight, waterHeight * 0.5 * meshHeight);
vertices.push_back(x + xOffset * (chunkWidth - 1));
vertices.push_back(y);
vertices.push_back(z + yOffset * (chunkHeight - 1));
}
}
return vertices;
}
std::vector<uint32_t> generateIndices(
const int chunkWidth,
const int chunkHeight
) {
std::vector<uint32_t> indices;
for (int y = 0; y < chunkHeight; y++) {
for (int x = 0; x < chunkWidth; x++) {
uint32_t pos = x + y*chunkWidth;
if (x == chunkWidth - 1 || y == chunkHeight - 1) {
// Don't create indices for right or top edge
continue;
} else {
// Top left triangle of square
indices.push_back(pos + chunkWidth);
indices.push_back(pos);
indices.push_back(pos + chunkWidth + 1);
// Bottom right triangle of square
indices.push_back(pos + 1);
indices.push_back(pos + 1 + chunkWidth);
indices.push_back(pos);
}
}
}
return indices;
}
std::vector<float> generateNormals(
const std::vector<uint32_t>& indices,
const std::vector<float>& vertices
) {
std::vector<float> normals;
std::vector<glm::vec3> verts;
// Get the vertices of each triangle in mesh
// For each group of indices
for (uint32_t i = 0; i < indices.size(); i += 3) {
// Get the vertices (point) for each index
for (uint32_t j = 0; j < 3; j++) {
uint32_t pos = indices[i+j]*3;
verts.push_back(glm::vec3(vertices[pos], vertices[pos+1], vertices[pos+2]));
}
// Get vectors of two edges of triangle
glm::vec3 U = verts[i+1] - verts[i];
glm::vec3 V = verts[i+2] - verts[i];
// Calculate normal
glm::vec3 normal = glm::normalize(-glm::cross(U, V));
normals.push_back(normal.x);
normals.push_back(normal.y);
normals.push_back(normal.z);
}
return normals;
}
std::vector<float> generateBiome(
const std::vector<float> &vertices,
const float waterHeight,
const int xOffset,
const int yOffset,
const float meshHeight
) {
std::vector<float> colors;
std::vector<terrainColor> biomeColors;
glm::vec3 color = getColor(255, 255, 255);
// NOTE: Terrain color height is a value between 0 and 1
auto wh = waterHeight;
biomeColors.push_back(terrainColor(wh * 0.5, getColor(60, 95, 190))); // Deep water
biomeColors.push_back(terrainColor(wh, getColor(60, 100, 190))); // Shallow water
biomeColors.push_back(terrainColor(0.15, getColor(210, 215, 130))); // Sand
biomeColors.push_back(terrainColor(0.30, getColor( 95, 165, 30))); // Grass 1
biomeColors.push_back(terrainColor(0.40, getColor( 65, 115, 20))); // Grass 2
biomeColors.push_back(terrainColor(0.50, getColor( 90, 65, 60))); // Rock 1
biomeColors.push_back(terrainColor(0.80, getColor( 75, 60, 55))); // Rock 2
biomeColors.push_back(terrainColor(1.00, getColor(255, 255, 255))); // Snow
// Determine which color to assign each vertex by its y-coord
// Iterate through vertex y values
for (int i = 1; i < vertices.size(); i += 3) {
for (int j = 0; j < biomeColors.size(); j++) {
// NOTE: The max height of a vertex is "meshHeight"
if (vertices[i] <= biomeColors[j].height * meshHeight) {
color = biomeColors[j].color;
break;
}
}
colors.push_back(color.r);
colors.push_back(color.g);
colors.push_back(color.b);
}
return colors;
}
Excal::Model::Model generateMapChunk(
const int xOffset,
const int yOffset,
const int xMapChunks,
const int yMapChunks,
const int chunkWidth,
const int chunkHeight,
const float waterHeight,
const float meshHeight,
const int octaves,
const float noiseScale,
const float persistence,
const float lacunarity
) {
// Generate map chunk data
auto noiseMap = generateNoiseMap(
xOffset, yOffset,
chunkWidth, chunkHeight,
octaves, noiseScale,
persistence, lacunarity
);
auto indices = generateIndices(chunkWidth, chunkHeight);
auto positions = generateVertices(noiseMap, waterHeight, xOffset, yOffset, chunkWidth, chunkHeight, meshHeight);
auto normals = generateNormals(indices, positions);
auto colors = generateBiome(positions, waterHeight, xOffset, yOffset, meshHeight);
// Assemble vertices
std::vector<Vertex> vertices;
for (int i=0; i < positions.size() / 3; i++) {
Vertex vertex = {
glm::vec3(positions[i*3 + 0], positions[i*3 + 1], positions[i*3 + 2]),
glm::vec3(colors[i*3 + 0], colors[i*3 + 1], colors[i*3 + 2]),
glm::vec3(normals[i*3 + 0], normals[i*3 + 1], normals[i*3 + 2]),
glm::vec3(0) // TexCoord isn't used
};
vertices.push_back(vertex);
}
Excal::Model::Model mapChunk;
mapChunk.indices = indices;
mapChunk.vertices = vertices;
mapChunk.position = glm::vec3(0.0);
return mapChunk;
}
}