-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProceduralLandscape.cpp
252 lines (212 loc) · 5.66 KB
/
ProceduralLandscape.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
#include "ProceduralLandscape.h"
#include "noise/noise.h"
#include <fstream>
#include "Utilities.h"
using namespace noise;
void DiamondSquareStep(float** array, int chunk_x, int chunk_z, int size);
void SquareStep(float** array, int chunk_x, int chunk_z, int x, int z, int reach);
void DiamondStep(float** array, int chunk_x, int chunk_z, int x, int z, int reach);
std::vector<float> ProceduralLandscape::PerlinNoise()
{
//module::Perlin myModule;
//utils::NoiseMap heightMap;
//utils::NoiseMapBuilderPlane heightMapBuilder;
//heightMapBuilder.SetSourceModule(myModule);
//heightMapBuilder.SetDestNoiseMap(heightMap);
//heightMapBuilder.SetDestSize(256, 256);
return std::vector<float>();
}
std::vector<float> ProceduralLandscape::DiamondSquare(Terrain::InitInfo tii)
{
//initialise float array
float** array = new float*[tii.HeightMapHeight];
for (int i = 0; i < tii.HeightMapWidth; i++)
{
array[i] = new float[tii.HeightMapWidth];
for (int j = 0; j < tii.HeightMapWidth; j++)
{
array[i][j] = Random::FloatInRange(0.0f, tii.HeightScale);
}
}
//define the first four corners based on heightmap scale
array[0][0] = Random::FloatInRange(0.0f, tii.HeightScale);
array[0][tii.HeightMapHeight-1] = Random::FloatInRange(0.0f, tii.HeightScale);
array[tii.HeightMapWidth-1][0] = Random::FloatInRange(0.0f, tii.HeightScale);
array[tii.HeightMapWidth-1][tii.HeightMapHeight-1] = Random::FloatInRange(0.0f, tii.HeightScale);
DiamondSquareStep(array, tii.HeightMapWidth, tii.HeightMapHeight, tii.HeightMapHeight / 2);
//convert float array into float vector
std::vector<float> returnHeightMap;
for (int i = 0; i < tii.HeightMapHeight; i++)
{
for (int j = 0; j < tii.HeightMapWidth; j++)
{
returnHeightMap.push_back(array[i][j]);
}
}
return returnHeightMap;
}
std::vector<float> ProceduralLandscape::FaultLine(Terrain::InitInfo tii)
{
//initialise float array
float** array = new float*[tii.HeightMapHeight];
for (int i = 0; i < tii.HeightMapWidth; i++)
{
array[i] = new float[tii.HeightMapWidth];
for (int j = 0; j < tii.HeightMapWidth; j++)
{
array[i][j] = 0.0f;
}
}
for (int k = 0; k < 4000; k++)
{
float x1 = Random::FloatInRange(0.0f, tii.HeightMapWidth);
float z1 = Random::FloatInRange(0.0f, tii.HeightMapHeight);
float x2 = Random::FloatInRange(0.0f, tii.HeightMapWidth);
float z2 = Random::FloatInRange(0.0f, tii.HeightMapHeight);
float displacement = 0.5f + (k / 4000) * (tii.HeightScale - 0.5f);
for (int i = 0; i < tii.HeightMapHeight; i++)
{
for (int j = 0; j < tii.HeightMapWidth; j++)
{
float dist = ((x2 - x1) * (i - z1) - (z2 - z1) * (j - x1));
if (dist > 0 && dist < XM_PIDIV2)
{
array[i][j] += displacement * sin(dist);
}
else if(dist <0 && dist > - XM_PIDIV2)
{
array[i][j] -= displacement *sin(dist);
}
else if (dist > 0)
{
array[i][j] += displacement;
}
else
{
array[i][j] -= displacement;
}
}
}
}
std::vector<float> returnHeightMap;
for (int i = 0; i < tii.HeightMapHeight; i++)
{
for (int j = 0; j < tii.HeightMapWidth; j++)
{
returnHeightMap.push_back(array[i][j]);
}
}
return returnHeightMap;
}
std::vector<float> ProceduralLandscape::LoadHeightMap(Terrain::InitInfo tii)
{
std::vector<float> heightMap;
//A height for each vertex
std::vector<unsigned char> in(tii.HeightMapWidth * tii.HeightMapHeight);
//open the file
std::ifstream inFile;
inFile.open(tii.HeightMapFilename, std::ios_base::binary);
if (inFile)
{
//Read the Raw bytes
inFile.read((char*)&in[0], (std::streamsize)in.size());
inFile.close();
}
//Copy the array data into a float array and scale it.
heightMap.resize(tii.HeightMapHeight * tii.HeightMapWidth, 0);
for (unsigned int i = 0; i < tii.HeightMapHeight * tii.HeightMapWidth; ++i)
{
heightMap[i] = (in[i] / 255.0f) * tii.HeightScale;
}
return heightMap;
}
void DiamondSquareStep(float ** array, int chunk_x, int chunk_z, int size)
{
int half = size / 2;
if (half < 1)
return;
//square steps
for (int z = half; z < chunk_z; z += size)
{
for (int x = half; x < chunk_x; x += size)
SquareStep(array, chunk_x, chunk_z, x % chunk_x, z % chunk_z, half);
}
//diamond steps
int col = 0;
for (int x = 0; x < chunk_x; x += half)
{
col++;
//if this is an odd column
if (col % 2 == 1)
{
for (int z = half; z < chunk_z; z += size)
{
DiamondStep(array, chunk_x, chunk_z, x % chunk_x, z % chunk_z, half);
}
}
else
{
for (int z = 0; z < chunk_z; z += size)
{
DiamondStep(array, chunk_x, chunk_z, x % chunk_x, z % chunk_z, half);
}
}
}
DiamondSquareStep(array,chunk_x, chunk_z, size / 2);
}
void SquareStep(float ** array, int chunk_x, int chunk_z, int x, int z, int reach)
{
int count = 0;
float avg = 0.0f;
if (x - reach >= 0 && z - reach >= 0)
{
avg += array[x - reach][z - reach];
count++;
}
if (x - reach >= 0 && z + reach < chunk_z)
{
avg += array[x - reach][z + reach];
count++;
}
if (x + reach < chunk_x && z - reach >= 0)
{
avg += array[x + reach][z - reach];
count++;
}
if (x + reach < chunk_x && z + reach < chunk_z)
{
avg += array[x + reach][z + reach];
count++;
}
avg += Random::FloatInRange(-reach, reach);
avg /= count;
array[x][z] = avg;
}
void DiamondStep(float ** array, int chunk_x, int chunk_z, int x, int z, int reach)
{
int count = 0;
float avg = 0.0f;
if (x - reach >= 0)
{
avg += array[x - reach][z];
count++;
}
if (x + reach < chunk_x)
{
avg += array[x + reach][z];
count++;
}
if (z - reach >= 0)
{
avg += array[x][z - reach];
count++;
}
if (z + reach < chunk_z)
{
avg += array[x][z + reach];
count++;
}
avg += Random::FloatInRange(-reach, reach);
avg /= count;
array[x][z] = avg;
}