-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTerrain.cpp
739 lines (609 loc) · 22 KB
/
Terrain.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#include "pch.h"
#include "Terrain.h"
#include <iostream>
Terrain::Terrain()
{
m_terrainGeneratedToggle = false;
}
Terrain::~Terrain()
{
}
bool Terrain::Initialize(ID3D11Device* device, int terrainWidth, int terrainHeight)
{
int index;
float height = 0.0;
bool result;
// Save the dimensions of the terrain.
m_terrainWidth = terrainWidth;
m_terrainHeight = terrainHeight;
m_frequency = m_terrainWidth / 20;
m_amplitude = 3.0;
m_wavelength = 1;
// Create the structure to hold the terrain data.
m_heightMap = new HeightMapType[m_terrainWidth * m_terrainHeight];
if (!m_heightMap)
{
return false;
}
//this is how we calculate the texture coordinates first calculate the step size there will be between vertices.
// 5.0f tile 5 times across the terrain.
// tile 1.0f across terrain 512x512
float textureCoordinatesStep = 1.0f / m_terrainWidth;
// Initialise the data in the height map (flat).
for (int j = 0; j<m_terrainHeight; j++)
{
for (int i = 0; i<m_terrainWidth; i++)
{
index = (m_terrainHeight * j) + i;
m_heightMap[index].x = (float)i;
m_heightMap[index].y = (float)height;
m_heightMap[index].z = (float)j;
//and use this step to calculate the texture coordinates for this point on the terrain.
m_heightMap[index].u = (float)i * textureCoordinatesStep;
m_heightMap[index].v = (float)j * textureCoordinatesStep;
}
}
//even though we are generating a flat terrain, we still need to normalise it.
// Calculate the normals for the terrain data.
result = CalculateNormals();
if (!result)
{
return false;
}
// Initialize the vertex and index buffer that hold the geometry for the terrain. // second step - transfers array info into vertex buffers for DX Rendering
result = InitializeBuffers(device);
if (!result)
{
return false;
}
return true;
}
void Terrain::Render(ID3D11DeviceContext * deviceContext)
{
// Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
RenderBuffers(deviceContext);
deviceContext->DrawIndexed(m_indexCount, 0, 0);
return;
}
bool Terrain::CalculateNormals()
{
int i, j, index1, index2, index3, index, count;
float vertex1[3], vertex2[3], vertex3[3], vector1[3], vector2[3], sum[3], length;
DirectX::SimpleMath::Vector3* normals;
// Create a temporary array to hold the un-normalized normal vectors.
normals = new DirectX::SimpleMath::Vector3[(m_terrainHeight - 1) * (m_terrainWidth - 1)];
if (!normals)
{
return false;
}
// Go through all the faces in the mesh and calculate their normals.
for (j = 0; j<(m_terrainHeight - 1); j++)
{
for (i = 0; i<(m_terrainWidth - 1); i++)
{
index1 = (j * m_terrainHeight) + i;
index2 = (j * m_terrainHeight) + (i + 1);
index3 = ((j + 1) * m_terrainHeight) + i;
// Get three vertices from the face.
vertex1[0] = m_heightMap[index1].x;
vertex1[1] = m_heightMap[index1].y;
vertex1[2] = m_heightMap[index1].z;
vertex2[0] = m_heightMap[index2].x;
vertex2[1] = m_heightMap[index2].y;
vertex2[2] = m_heightMap[index2].z;
vertex3[0] = m_heightMap[index3].x;
vertex3[1] = m_heightMap[index3].y;
vertex3[2] = m_heightMap[index3].z;
// Calculate the two vectors for this face.
vector1[0] = vertex1[0] - vertex3[0];
vector1[1] = vertex1[1] - vertex3[1];
vector1[2] = vertex1[2] - vertex3[2];
vector2[0] = vertex3[0] - vertex2[0];
vector2[1] = vertex3[1] - vertex2[1];
vector2[2] = vertex3[2] - vertex2[2];
index = (j * (m_terrainHeight - 1)) + i;
// Calculate the cross product of those two vectors to get the un-normalized value for this face normal.
normals[index].x = (vector1[1] * vector2[2]) - (vector1[2] * vector2[1]);
normals[index].y = (vector1[2] * vector2[0]) - (vector1[0] * vector2[2]);
normals[index].z = (vector1[0] * vector2[1]) - (vector1[1] * vector2[0]);
}
}
// Now go through all the vertices and take an average of each face normal
// that the vertex touches to get the averaged normal for that vertex.
for (j = 0; j<m_terrainHeight; j++)
{
for (i = 0; i<m_terrainWidth; i++)
{
// Initialize the sum.
sum[0] = 0.0f;
sum[1] = 0.0f;
sum[2] = 0.0f;
// Initialize the count.
count = 0;
// Bottom left face.
if (((i - 1) >= 0) && ((j - 1) >= 0))
{
index = ((j - 1) * (m_terrainHeight - 1)) + (i - 1);
sum[0] += normals[index].x;
sum[1] += normals[index].y;
sum[2] += normals[index].z;
count++;
}
// Bottom right face.
if ((i < (m_terrainWidth - 1)) && ((j - 1) >= 0))
{
index = ((j - 1) * (m_terrainHeight - 1)) + i;
sum[0] += normals[index].x;
sum[1] += normals[index].y;
sum[2] += normals[index].z;
count++;
}
// Upper left face.
if (((i - 1) >= 0) && (j < (m_terrainHeight - 1)))
{
index = (j * (m_terrainHeight - 1)) + (i - 1);
sum[0] += normals[index].x;
sum[1] += normals[index].y;
sum[2] += normals[index].z;
count++;
}
// Upper right face.
if ((i < (m_terrainWidth - 1)) && (j < (m_terrainHeight - 1)))
{
index = (j * (m_terrainHeight - 1)) + i;
sum[0] += normals[index].x;
sum[1] += normals[index].y;
sum[2] += normals[index].z;
count++;
}
// Take the average of the faces touching this vertex.
sum[0] = (sum[0] / (float)count);
sum[1] = (sum[1] / (float)count);
sum[2] = (sum[2] / (float)count);
// Calculate the length of this normal.
length = sqrt((sum[0] * sum[0]) + (sum[1] * sum[1]) + (sum[2] * sum[2]));
// Get an index to the vertex location in the height map array.
index = (j * m_terrainHeight) + i;
// Normalize the final shared normal for this vertex and store it in the height map array.
m_heightMap[index].nx = (sum[0] / length);
m_heightMap[index].ny = (sum[1] / length);
m_heightMap[index].nz = (sum[2] / length);
}
}
// Release the temporary normals.
delete[] normals;
normals = 0;
return true;
}
DirectX::SimpleMath::Vector3 Terrain::GetDimensions()
{
DirectX::SimpleMath::Vector3 m_terrainDimensions;
m_terrainDimensions.x = m_terrainWidth;
m_terrainDimensions.z = m_terrainHeight;
return m_terrainDimensions;
}
void Terrain::Shutdown()
{
// Release the index buffer.
if (m_indexBuffer)
{
m_indexBuffer->Release();
m_indexBuffer = 0;
}
// Release the vertex buffer.
if (m_vertexBuffer)
{
m_vertexBuffer->Release();
m_vertexBuffer = 0;
}
return;
}
bool Terrain::InitializeBuffers(ID3D11Device * device )
{
VertexType* vertices;
unsigned long* indices;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;
int index, i, j;
int index1, index2, index3, index4; //geometric indices.
// Calculate the number of vertices in the terrain mesh. // two squares
m_vertexCount = (m_terrainWidth - 1) * (m_terrainHeight - 1) * 6;
// Set the index count to the same as the vertex count.
m_indexCount = m_vertexCount;
// Create the vertex array.
vertices = new VertexType[m_vertexCount];
if (!vertices)
{
return false;
}
// Create the index array.
indices = new unsigned long[m_indexCount];
if (!indices)
{
return false;
}
// Initialize the index to the vertex buffer.
index = 0;
bool inverted = false;
for (j = 0; j<(m_terrainHeight - 1); j++)
{
for (i = 0; i<(m_terrainWidth - 1); i++)
{
if (i == 0) {
if (j % 2 == 0) {
inverted = false;
}
else {
inverted = true;
}
}
if (inverted) {
index1 = (m_terrainHeight * j) + (i + 1); // Bottom right.
index2 = (m_terrainHeight * (j + 1)) + (i + 1); // Upper right.
index3 = (m_terrainHeight * j) + i; // Bottom left.
index4 = (m_terrainHeight * (j + 1)) + i; // Upper left.
}
else {
index1 = (m_terrainHeight * j) + i; // Bottom left.
index2 = (m_terrainHeight * j) + (i + 1); // Bottom right.
index3 = (m_terrainHeight * (j + 1)) + i; // Upper left.
index4 = (m_terrainHeight * (j + 1)) + (i + 1); // Upper right.
}
inverted = !inverted;
// Upper left.
vertices[index].position = DirectX::SimpleMath::Vector3(m_heightMap[index3].x, m_heightMap[index3].y, m_heightMap[index3].z);
vertices[index].normal = DirectX::SimpleMath::Vector3(m_heightMap[index3].nx, m_heightMap[index3].ny, m_heightMap[index3].nz);
vertices[index].texture = DirectX::SimpleMath::Vector2(m_heightMap[index3].u, m_heightMap[index3].v);
indices[index] = index;
index++;
// Upper right.
vertices[index].position = DirectX::SimpleMath::Vector3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z);
vertices[index].normal = DirectX::SimpleMath::Vector3(m_heightMap[index4].nx, m_heightMap[index4].ny, m_heightMap[index4].nz);
vertices[index].texture = DirectX::SimpleMath::Vector2(m_heightMap[index4].u, m_heightMap[index4].v);
indices[index] = index;
index++;
// Bottom left.
vertices[index].position = DirectX::SimpleMath::Vector3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z);
vertices[index].normal = DirectX::SimpleMath::Vector3(m_heightMap[index1].nx, m_heightMap[index1].ny, m_heightMap[index1].nz);
vertices[index].texture = DirectX::SimpleMath::Vector2(m_heightMap[index1].u, m_heightMap[index1].v);
indices[index] = index;
index++;
// Bottom left.
vertices[index].position = DirectX::SimpleMath::Vector3(m_heightMap[index1].x, m_heightMap[index1].y, m_heightMap[index1].z);
vertices[index].normal = DirectX::SimpleMath::Vector3(m_heightMap[index1].nx, m_heightMap[index1].ny, m_heightMap[index1].nz);
vertices[index].texture = DirectX::SimpleMath::Vector2(m_heightMap[index1].u, m_heightMap[index1].v);
indices[index] = index;
index++;
// Upper right.
vertices[index].position = DirectX::SimpleMath::Vector3(m_heightMap[index4].x, m_heightMap[index4].y, m_heightMap[index4].z);
vertices[index].normal = DirectX::SimpleMath::Vector3(m_heightMap[index4].nx, m_heightMap[index4].ny, m_heightMap[index4].nz);
vertices[index].texture = DirectX::SimpleMath::Vector2(m_heightMap[index4].u, m_heightMap[index4].v);
indices[index] = index;
index++;
// Bottom right.
vertices[index].position = DirectX::SimpleMath::Vector3(m_heightMap[index2].x, m_heightMap[index2].y, m_heightMap[index2].z);
vertices[index].normal = DirectX::SimpleMath::Vector3(m_heightMap[index2].nx, m_heightMap[index2].ny, m_heightMap[index2].nz);
vertices[index].texture = DirectX::SimpleMath::Vector2(m_heightMap[index2].u, m_heightMap[index2].v);
indices[index] = index;
index++;
}
}
// Set up the description of the static vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(VertexType) * m_vertexCount;
vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexBufferDesc.CPUAccessFlags = 0;
vertexBufferDesc.MiscFlags = 0;
vertexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the vertex data.
vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;
// Now create the vertex buffer.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
if (FAILED(result))
{
return false;
}
// Set up the description of the static index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_indexCount;
indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
indexBufferDesc.CPUAccessFlags = 0;
indexBufferDesc.MiscFlags = 0;
indexBufferDesc.StructureByteStride = 0;
// Give the subresource structure a pointer to the index data.
indexData.pSysMem = indices;
indexData.SysMemPitch = 0;
indexData.SysMemSlicePitch = 0;
// Create the index buffer.
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
if (FAILED(result))
{
return false;
}
// Release the arrays now that the vertex and index buffers have been created and loaded.
delete[] vertices;
vertices = 0;
delete[] indices;
indices = 0;
return true;
}
void Terrain::RenderBuffers(ID3D11DeviceContext * deviceContext)
{
unsigned int stride;
unsigned int offset;
// Set vertex buffer stride and offset.
stride = sizeof(VertexType);
offset = 0;
// Set the vertex buffer to active in the input assembler so it can be rendered.
deviceContext->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);
// Set the index buffer to active in the input assembler so it can be rendered.
deviceContext->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);
// Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
deviceContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
return;
}
bool Terrain::GenerateHeightMap(ID3D11Device* device)
{
bool result;
int index;
float height = 0.0;
m_frequency = (6.283/m_terrainHeight) / m_wavelength; //we want a wavelength of 1 to be a single wave over the whole terrain. A single wave is 2 pi which is about 6.283
// m_terrainHeight is actually the z axis
//loop through the terrain and set the hieghts how we want. This is where we generate the terrain
//in this case I will run a sin-wave through the terrain in one axis.
for (int j = 0; j<m_terrainHeight; j++)
{
for (int i = 0; i<m_terrainWidth; i++)
{
index = (m_terrainHeight * j) + i;
m_heightMap[index].x = (float)i;
m_heightMap[index].y = GenerateRandomNumber(0.5f) * m_amplitude *2.f;
m_heightMap[index].z = (float)j;
}
}
result = CalculateNormals();
if (!result)
{
return false;
}
result = InitializeBuffers(device);
if (!result)
{
return false;
}
}
bool Terrain::SmoothenHeightMap(ID3D11Device* device)
{
bool result;
int index, nx, nz;
float height = 0.0;
int neighbours[8] = {}; // array starts at 0, inclusive
int n = 8;
/* Initialise corner of height map */ // 1.
int bottomLeftCorner = 0;
int bottomRightCorner = (m_terrainHeight * (m_terrainHeight - 1));
int topLeftCorner = (m_terrainWidth - 1);
int topRightCorner = m_terrainHeight * (m_terrainHeight - 1) + (m_terrainWidth - 1);
//we want a wavelength of 1 to be a single wave over the whole terrain. A single wave is 2 pi which is about 6.283
m_frequency = (6.283 / m_terrainHeight) / m_wavelength;
// m_terrainHeight is actually the z axis
for (int j = 0; j < m_terrainHeight; j++)
{
for (int i = 0; i < m_terrainWidth; i++)
{
index = (m_terrainHeight * j) + i;
float sum = m_heightMap[index].y;
// with more than 128 square dimensions, initial neighbours on bottom row might not exist
// can refractor this better if it works
if (m_heightMap[index].z + 1 >= m_terrainHeight || m_heightMap[index].z <= 0) {
// if no neighbours, set height to that of current index/element
neighbours[0], neighbours [1], neighbours[2] = m_heightMap[index].y;
}
else {
neighbours[0] = m_heightMap[(m_terrainHeight * (j + 1)) + (i - 1)].y; // top left
neighbours[1] = m_heightMap[(m_terrainHeight * (j + 1)) + (i)].y; // top middle
neighbours[2] = m_heightMap[(m_terrainHeight * (j + 1)) + (i + 1)].y; // top right
}
neighbours[3] = m_heightMap[(m_terrainHeight * (j)) + (i - 1)].y; // middle left
neighbours[4] = m_heightMap[(m_terrainHeight * (j)) + (i + 1)].y; // middle right
if (m_heightMap[index].z <= 0 || m_heightMap[index].z + 1 >= m_terrainHeight) {
// if no neighbours, set height to that of current index/element
neighbours[5], neighbours[6], neighbours[7] = m_heightMap[index].y;
}
else {
neighbours[5] = m_heightMap[(m_terrainHeight * (j - 1)) + (i - 1)].y; // bottom left
neighbours[6] = m_heightMap[(m_terrainHeight * (j - 1)) + (i)].y; // bottom middle
neighbours[7] = m_heightMap[(m_terrainHeight * (j - 1)) + (i + 1)].y; // bottom right
}
for (int z = 0; z < n; z++)
{
// if out of map, take y of current index for sum
if (neighbours[z] < 0 || neighbours[z] >= m_terrainHeight * m_terrainWidth)
{
sum += m_heightMap[index].y;
}
else
{
sum += neighbours[z]; // if exists, include in sum
}
}
// smoothen based on neighbours
m_heightMap[index].y = sum / 9.0f; // current point n is no. of neighbours +1 for current vertex point// total of 9 points in a 3*3 grid
}
}
result = CalculateNormals();
if (!result)
{
return false;
}
result = InitializeBuffers(device);
if (!result)
{
return false;
}
}
bool Terrain::GenerateMidpointHeightMap(ID3D11Device* device)
{
bool result;
int iter = 0; // iter for looping through midpoint passes
float height = 0.0;
//we want a wavelength of 1 to be a single wave over the whole terrain. A single wave is 2 pi which is about 6.283
m_frequency = (6.283 / m_terrainHeight) / m_wavelength;
// m_terrainHeight is actually the z axis
//loop through the terrain and set the heights how we want. This is where we generate the terrain
/* Initialise corner of height map */ // 1.
int bottomLeftCorner = 0;
int bottomRightCorner = (m_terrainHeight * (m_terrainHeight - 1));
int topLeftCorner = (m_terrainWidth - 1);
int topRightCorner = m_terrainHeight * (m_terrainHeight - 1) + (m_terrainWidth - 1);
/* Set height on initial corners*/
m_heightMap[bottomLeftCorner].y = GenerateRandomNumber(0.1f) * m_amplitude * 5; // 0, 0 (x,z) // bottom left
m_heightMap[bottomRightCorner].y = GenerateRandomNumber(0.1f) * m_amplitude * 5; // (129 , 0) // bottom right
m_heightMap[topRightCorner].y = GenerateRandomNumber(0.1f) * m_amplitude * 5; // 129 , 129 // top right
m_heightMap[topLeftCorner].y = GenerateRandomNumber(0.1f) * m_amplitude * 5; // 0 , 129 // top left
/* End of Initialise corners of heightmap */
float leftX, rightX, bottomZ, topZ = 0.f;
//int test = (int)sqrt((double)m_terrainHeight - 1.0f);
int heightmapExponent = log(m_terrainWidth - 1) / log(2);
while (iter < heightmapExponent) // 2. iter < heightmap.exponent
{
int chunks = pow(2, iter);
int chunk_width = (m_terrainHeight - 1) / chunks;
for (int xChunk = 0; xChunk < chunks; xChunk++)
{
for (int yChunk = 0; yChunk < chunks; yChunk++)
{
leftX = chunk_width * xChunk;
rightX = leftX + chunk_width;
bottomZ = chunk_width * yChunk;
topZ = bottomZ + chunk_width;
MidpointDisplace(leftX, rightX, bottomZ, topZ); // coordinates
}
}
iter++;
}
result = CalculateNormals();
if (!result)
{
return false;
}
result = InitializeBuffers(device);
if (!result)
{
return false;
}
}
void Terrain::MidpointDisplace(float lx, float rx, float bz, float tz)
{
// let
int index;
float cx = (lx + rx) / 2.f;
float cz = (bz + tz) / 2.f;
// Heights at corners
float bottomLeftY, bottomRightY, topLeftY, topRightY = 0.f;
float topMidpointY, leftMidpointY, bottomMidpointY, rightMidpointY, centreMidpointY = 0.f;
// height value at that point // corners
// We are using this to find the points on the height map
for (int j = 0; j < m_terrainHeight; j++)
{
for (int i = 0; i < m_terrainWidth; i++)
{
index = (m_terrainHeight * j) + i;
// Bottom Left Corner // Getting Y at this position
if (m_heightMap[index].x == lx && m_heightMap[index].z == bz)
{
// Get
bottomLeftY = m_heightMap[index].y;
}
// Bottom Right Corner
if (m_heightMap[index].x == rx && m_heightMap[index].z == bz)
{
bottomRightY = m_heightMap[index].y;
}
// Top Left Corner
if (m_heightMap[index].x == lx && m_heightMap[index].z == tz)
{
topLeftY = m_heightMap[index].y;
}
// Top Right Corner
if (m_heightMap[index].x == rx && m_heightMap[index].z == tz)
{
topRightY = m_heightMap[index].y;
}
}
}
topMidpointY = CalculateAverage(topLeftY, topRightY);
leftMidpointY = CalculateAverage(bottomLeftY, topLeftY);
bottomMidpointY = CalculateAverage(bottomLeftY, bottomRightY);
rightMidpointY = CalculateAverage(bottomRightY, topRightY);
centreMidpointY = CalculateAverage(topMidpointY, leftMidpointY, bottomMidpointY, rightMidpointY);
// Once these are done push to midpoint **
// Set Midpoints
for (int j = 0; j < m_terrainHeight; j++)
{
for (int i = 0; i < m_terrainWidth; i++)
{
index = (m_terrainHeight * j) + i;
// Bottom Midpoint
if (m_heightMap[index].x == cx && m_heightMap[index].z == bz)
{
m_heightMap[index].y = bottomMidpointY * GenerateRandomNumber(0.1f); // jitter spread
}
// Top Midpoint
if (m_heightMap[index].x == cx && m_heightMap[index].z == tz)
{
m_heightMap[index].y = topMidpointY * GenerateRandomNumber(0.1f);
}
// Left Midpoint
if (m_heightMap[index].x == lx && m_heightMap[index].z == cz)
{
m_heightMap[index].y = leftMidpointY * GenerateRandomNumber(0.1f);
}
// Right Midpoint
if (m_heightMap[index].x == rx && m_heightMap[index].z == cz)
{
m_heightMap[index].y = rightMidpointY * GenerateRandomNumber(0.1f);
}
// Centre Midpoint
if (m_heightMap[index].x == cx && m_heightMap[index].z == cz)
{
m_heightMap[index].y = centreMidpointY * GenerateRandomNumber(0.1f);
}
}
}
}
float Terrain::CalculateAverage(float a, float b)
{
float avg = (a + b) / 2.f;
return avg;
}
float Terrain::CalculateAverage(float a, float b, float c)
{
float avg = (a + b + c) / 3.f;
return avg;
}
float Terrain::CalculateAverage(float a, float b, float c, float d)
{
float avg = (a + b + c + d) / 4.f;
return avg;
}
float Terrain::GenerateRandomNumber(float spread)
{
float x = ((float)rand() / (RAND_MAX));
return x;
}
bool Terrain::Update()
{
return true;
}
float* Terrain::GetWavelength()
{
return &m_wavelength;
}
float* Terrain::GetAmplitude()
{
return &m_amplitude;
}