-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterrain_map.cpp
298 lines (252 loc) · 9.94 KB
/
terrain_map.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
293
294
295
296
297
298
#include "terrain_map.h"
#include "iostream"
#include <chrono>
#include <omp.h>
#include "frustum.h"
#include <jsoncpp/json/json.h>
TerrainMap::TerrainMap(Json::Value settings, vec3 cameraPosition, const Frustum &f)
{
cameraChunkX = cameraPosition.x / terrainWidth;
cameraChunkZ = cameraPosition.z / terrainHeight;
frustum_obj = f;
// Calculate the number of vertices and triangles
vertexCount = terrainWidth * terrainHeight;
triangleCount = (terrainWidth - 1) * (terrainHeight - 1) * 2;
frequency = settings["frequency"].asFloat();
amplitude = settings["amplitude"].asFloat();
snow = -amplitude + settings["snow_level"].asFloat() * amplitude * 2;
rock = -amplitude + settings["rock_level"].asFloat() * amplitude * 2;
grass = -amplitude + settings["grass_level"].asFloat() * amplitude * 2;
sand = -amplitude + settings["sand_level"].asFloat() * amplitude * 2;
water = sand;
rock_size = (settings["snow_level"].asFloat() - settings["rock_level"].asFloat()) * amplitude * 2;
grass_size = (settings["rock_level"].asFloat() - settings["grass_level"].asFloat()) * amplitude * 2;
sand_size = (settings["grass_level"].asFloat() - settings["sand_level"].asFloat()) * amplitude * 2;
snow_inter = settings["snow_inter"].asFloat() * rock_size;
rock_inter = settings["rock_inter"].asFloat() * grass_size;
grass_inter = settings["grass_inter"].asFloat() * sand_size;
water_to_sand = settings["water_to_sand"].asFloat() * sand_size;
noise = new SimplexNoise(1.0, 1.0);
}
TerrainMap::~TerrainMap()
{
for (auto &chunk : chunks)
{
delete chunk.second;
chunk.second = nullptr;
}
chunks.clear();
}
std::pair<int, int> TerrainMap::getChunk(int x, int z)
{
int chunkX;
int chunkZ;
if (x < 0) {
chunkX = (x - terrainWidth) / (terrainWidth);
}
else {
chunkX = x / (terrainWidth);
}
if (z < 0) {
chunkZ = (z - terrainHeight) / (terrainHeight);
}
else {
chunkZ = z / (terrainHeight);
}
return {chunkX, chunkZ};
}
bool TerrainMap::collision(std::map<std::pair<int, int>, int> points)
{
for (auto &point : points){
int x = point.first.first;
int z = point.first.second;
int y = point.second;
std::pair<int, int> chunk = getChunk(x, z);
// Check if the chunk exists
if (chunks.find(chunk) == chunks.end())
{
continue;
}
// Calculate the position of the vertex in the chunk
if (x < 0) {
x = -x;
x = (terrainWidth-1) - x % (terrainWidth);
}
else {
x = x - chunk.first * (terrainWidth);
}
if (z < 0) {
z = -z;
z = (terrainHeight-1) - z % (terrainHeight);
}
else {
z = z - chunk.second * (terrainHeight);
}
Model *chunkModel = chunks[chunk];
if (y < chunkModel -> vertexArray[x + z * terrainWidth].y)
{
return true;
}
}
return false;
}
void TerrainMap::update(vec3 cameraPosition, const mat4 &world2view)
{
float rad_sq = (CHUNKS * terrainWidth) * (CHUNKS * terrainWidth);
cameraChunkX = cameraPosition.x / terrainWidth;
cameraChunkZ = cameraPosition.z / terrainHeight;
for (int x = cameraChunkX - CHUNKS; x <= cameraChunkX + CHUNKS; ++x)
{
float chunkX = ((x * (terrainWidth)) - cameraPosition.x) * ((x * (terrainWidth)) - cameraPosition.x);
for (int z = cameraChunkZ - CHUNKS; z <= cameraChunkZ + CHUNKS; ++z)
{
float chunkZ = (z * (terrainHeight) - cameraPosition.z) * (z * (terrainHeight) - cameraPosition.z);
if (chunkX + chunkZ > rad_sq)
{
continue;
}
if (chunks.find({x, z}) == chunks.end())
{
chunks[{x, z}] = GeneratePerlinTerrain(x * (terrainWidth - 2), z * (terrainHeight - 2));
}
}
}
// Iterate over all chunks
for (auto it = chunks.begin(); it != chunks.end();)
{
// Calculate the distance from the chunk to the camera
int dx = it->first.first - cameraChunkX;
int dz = it->first.second - cameraChunkZ;
int distanceSquared = dx * dx + dz * dz;
// If the chunk is too far from the camera, offload it
if (distanceSquared > MAX_CHUNK_DISTANCE * MAX_CHUNK_DISTANCE)
{
// Delete the model
delete it->second;
it->second = nullptr;
// Remove the chunk from the map
it = chunks.erase(it);
}
else
{
++it;
}
}
}
void TerrainMap::display(const GLuint &program, const mat4 &world2view, const mat4 &projection, vec3 cameraPosition)
{
glUniform1f(glGetUniformLocation(program, "snow"), snow);
glUniform1f(glGetUniformLocation(program, "rock"), rock);
glUniform1f(glGetUniformLocation(program, "grass"), grass);
glUniform1f(glGetUniformLocation(program, "sand"), sand);
glUniform1f(glGetUniformLocation(program, "snow_inter"), snow_inter);
glUniform1f(glGetUniformLocation(program, "rock_inter"), rock_inter);
glUniform1f(glGetUniformLocation(program, "grass_inter"), grass_inter);
glUniform1f(glGetUniformLocation(program, "water_to_sand"), water_to_sand);
glUniform1f(glGetUniformLocation(program, "far"), frustum_obj.far);
glUniform1f(glGetUniformLocation(program, "width"), terrainWidth);
// Render all chunks
for (const auto &pair : chunks)
{
// Calculate the chunk position
float chunkX = pair.first.first * (terrainWidth - 2);
float chunkZ = pair.first.second * (terrainHeight - 2);
if (frustum_obj.side_culling(vec3(chunkX, 0, chunkZ), terrainWidth * 2, world2view))
{
continue;
}
// Pass the chunk position to the shader
glUniform3f(glGetUniformLocation(program, "chunkPosition"), chunkX, 0, chunkZ);
// Draw the chunk
DrawModel(pair.second, program, "in_Position", "in_Normal", NULL);
}
}
Model *TerrainMap::GeneratePerlinTerrain(int offsetX, int offsetZ)
{
// Allocate memory for the vertex, normal, texture coordinate, and index arrays
vec3 *vertexArray;
vec3 *normalArray;
vec2 *texCoordArray;
GLuint *indexArray;
// Allocate memory for the vertex, normal, texture coordinate, and index arrays
vertexArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount);
normalArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount);
texCoordArray = (vec2 *)malloc(sizeof(GLfloat) * 2 * vertexCount);
indexArray = (GLuint *)malloc(sizeof(GLuint) * triangleCount * 3);
// Generate the vertices, normals, and texture coordinates using Perlin noise
#pragma omp parallel for collapse(2)
for (int x = 0; x < terrainWidth; x++)
for (int z = 0; z < terrainHeight; z++)
{
// Calculate the Perlin noise value for the current vertex
float perlin_noise = noise->fractal(4, (x + offsetX) * frequency, (z + offsetZ) * frequency) * amplitude;
if (perlin_noise < water)
{
perlin_noise = water;
}
// Set the vertex position, normal, and texture coordinate
vertexArray[x + z * terrainWidth] = vec3(x, perlin_noise, z);
texCoordArray[x + z * terrainWidth] = vec2(x + offsetX, z + offsetZ);
}
// Calculate the normals for the vertices
#pragma omp parallel for collapse(2)
for (int x = 0; x < terrainWidth; x++)
{
for (int z = 0; z < terrainHeight; z++)
{
// If the vertex is not on the edge of the terrain, calculate the normal
if ((x != 0) && (x != (terrainWidth - 1)) && (z != 0) && (z != (terrainHeight - 1)))
{
vec3 left = vertexArray[(x - 1 + z * terrainWidth)];
vec3 right = vertexArray[(x + 1 + z * terrainWidth)];
vec3 up = vertexArray[(x + (z - 1) * terrainWidth)];
vec3 down = vertexArray[(x + (z + 1) * terrainWidth)];
vec3 normal = normalize(CrossProduct(up - down, right - left));
if (normal.y < 0)
{
normal = -normal;
}
normalArray[(x + z * terrainWidth)] = normal;
}
}
}
// Copy the normals from the neighboring vertices for the edge vertices
for (int x = 0; x < terrainWidth; x++)
{
normalArray[x] = normalArray[x + terrainWidth]; // Top edge
normalArray[x + (terrainHeight - 1) * terrainWidth] = normalArray[x + (terrainHeight - 2) * terrainWidth]; // Bottom edge
}
for (int z = 0; z < terrainHeight; z++)
{
normalArray[z * terrainWidth] = normalArray[1 + z * terrainWidth]; // Left edge
normalArray[(terrainWidth - 1) + z * terrainWidth] = normalArray[(terrainWidth - 2) + z * terrainWidth]; // Right edge
}
// Generate the triangle indices
#pragma omp parallel for collapse(2)
for (int x = 0; x < terrainWidth - 1; x++)
{
for (int z = 0; z < terrainHeight - 1; z++)
{
// Calculate the index once and reuse it
int index = (x + z * (terrainWidth - 1)) * 6;
// Triangle 1
indexArray[index + 0] = x + z * terrainWidth;
indexArray[index + 1] = x + (z + 1) * terrainWidth;
indexArray[index + 2] = x + 1 + z * terrainWidth;
// Triangle 2
indexArray[index + 3] = x + 1 + z * terrainWidth;
indexArray[index + 4] = x + (z + 1) * terrainWidth;
indexArray[index + 5] = x + 1 + (z + 1) * terrainWidth;
}
}
// Load the data into a model and return it
Model *model = LoadDataToModel(
vertexArray,
normalArray,
texCoordArray,
NULL,
indexArray,
vertexCount,
triangleCount * 3);
return model;
}