forked from vixorien/AdvancedDX11Starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerrainMesh.cpp
270 lines (217 loc) · 7.38 KB
/
TerrainMesh.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
#include "TerrainMesh.h"
#include <DirectXMath.h>
#include <vector>
#include <fstream>
using namespace DirectX;
TerrainMesh::TerrainMesh(
Microsoft::WRL::ComPtr<ID3D11Device> device,
const char* heightmap,
unsigned int heightmapWidth,
unsigned int heightmapHeight,
TerrainBitDepth bitDepth,
float yScale,
float xzScale,
float uvScale)
: Mesh()
{
unsigned int numVertices = heightmapWidth * heightmapHeight;
unsigned int numIndices = (heightmapWidth - 1) * (heightmapHeight - 1) * 6;
Vertex* verts = new Vertex[numVertices];
if (bitDepth == BitDepth_8)
Load8bitRaw(heightmap, heightmapWidth, heightmapHeight, yScale, xzScale, verts);
else
Load16bitRaw(heightmap, heightmapWidth, heightmapHeight, yScale, xzScale, verts);
// Create indices and, while we're at it, calculate the normal
// of each triangle (as we'll need those for vertex normals)
unsigned int* indices = new unsigned int[numIndices];
std::vector<XMFLOAT3> triangleNormals;
int indexCounter = 0;
for (int z = 0; z < heightmapHeight - 1; z++)
for (int x = 0; x < heightmapWidth - 1; x++)
{
// Calc the vertex index
int vertIndex = z * heightmapWidth + x;
// Calculate the indices for these two triangles
int i0 = vertIndex;
int i1 = vertIndex + heightmapWidth;
int i2 = vertIndex + 1 + heightmapWidth;
int i3 = vertIndex;
int i4 = vertIndex + 1 + heightmapWidth;
int i5 = vertIndex + 1;
// Put these in the index array
indices[indexCounter++] = i0;
indices[indexCounter++] = i1;
indices[indexCounter++] = i2;
indices[indexCounter++] = i3;
indices[indexCounter++] = i4;
indices[indexCounter++] = i5;
// Get the positions of the three verts of each triangle
XMVECTOR pos0 = XMLoadFloat3(&verts[i0].Position);
XMVECTOR pos1 = XMLoadFloat3(&verts[i1].Position);
XMVECTOR pos2 = XMLoadFloat3(&verts[i2].Position);
XMVECTOR pos3 = XMLoadFloat3(&verts[i3].Position);
XMVECTOR pos4 = XMLoadFloat3(&verts[i4].Position);
XMVECTOR pos5 = XMLoadFloat3(&verts[i5].Position);
// Calculate the normal of each triangle
XMFLOAT3 normal0;
XMFLOAT3 normal1;
// Cross the edges of the triangle
XMStoreFloat3(&normal0,
XMVector3Normalize(XMVector3Cross(pos1 - pos0, pos2 - pos0)));
XMStoreFloat3(&normal1,
XMVector3Normalize(XMVector3Cross(pos4 - pos3, pos5 - pos3)));
// Push the normals into the list
triangleNormals.push_back(normal0);
triangleNormals.push_back(normal1);
}
// Calculate normals!
for (int z = 0; z < heightmapHeight; z++)
{
for (int x = 0; x < heightmapWidth; x++)
{
// Get the index of this vertex, and triangle-related indices
int index = z * heightmapWidth + x;
int triIndex = index * 2 - (2 * z);
int triIndexPrevRow = triIndex - (heightmapWidth * 2 - 1);
// Running total of normals
int normalCount = 0;
XMVECTOR normalTotal = XMVectorSet(0, 0, 0, 0);
// Normals
//XMVECTOR upLeft = XMLoadFloat3(&triangleNormals[triIndexPrevRow - 1]);
//XMVECTOR up = XMLoadFloat3(&triangleNormals[triIndexPrevRow]);
//XMVECTOR upRight = XMLoadFloat3(&triangleNormals[triIndexPrevRow + 1]);
//XMVECTOR downLeft = XMLoadFloat3(&triangleNormals[triIndex - 1]);
//XMVECTOR down = XMLoadFloat3(&triangleNormals[triIndex]);
//XMVECTOR downRight = XMLoadFloat3(&triangleNormals[triIndex + 1]);
// x-----x-----x
// |\ |\ |
// | \ u | \ |
// | \ | \ | ul = up left
// | \ | \ | u = up
// | ul \| ur \| ur = up right
// x-----O-----x
// |\ dl |\ dr | dl = down left
// | \ | \ | d = down
// | \ | \ | dr = down right
// | \ | d \ |
// | \| \|
// x-----x-----x
// If not top row and not first column
if (z > 0 && x > 0)
{
// "Up left" and "up"
normalTotal += XMLoadFloat3(&triangleNormals[triIndexPrevRow - 1]);
normalTotal += XMLoadFloat3(&triangleNormals[triIndexPrevRow]);
normalCount += 2;
}
// If not top row and not last column
if (z > 0 && x < heightmapWidth - 1)
{
// "Up right"
normalTotal += XMLoadFloat3(&triangleNormals[triIndexPrevRow + 1]);
normalCount++;
}
// If not bottom row and not first column
if (z < heightmapHeight - 1 && x > 0)
{
// "Down left"
normalTotal += XMLoadFloat3(&triangleNormals[triIndex - 1]);
normalCount++;
}
// If not bottom row and not last column
if (z < heightmapHeight - 1 && x < heightmapWidth - 1)
{
// "Down right" and "down"
normalTotal += XMLoadFloat3(&triangleNormals[triIndex]);
normalTotal += XMLoadFloat3(&triangleNormals[triIndex + 1]);
normalCount += 2;
}
// Average normal
normalTotal /= normalCount;
XMStoreFloat3(&verts[index].Normal, normalTotal);
}
}
// Create the buffers and clean up arrays
this->CreateBuffers(verts, numVertices, indices, numIndices, device);
delete[] verts;
delete[] indices;
}
TerrainMesh::~TerrainMesh()
{
}
void TerrainMesh::Load8bitRaw(const char* heightmap, unsigned int width, unsigned int height, float yScale, float xzScale, Vertex* verts)
{
unsigned int numVertices = width * height;
float halfWidth = width / 2.0f;
float halfHeight = height / 2.0f;
// Vector to hold heights
std::vector<unsigned char> heights(numVertices);
// Open the file (remember to #include <fstream>)
std::ifstream file;
file.open(heightmap, std::ios_base::binary);
if (!file)
return;
// Read raw bytes to vector
file.read((char*)&heights[0], numVertices); // Same size
file.close();
// Create the initial mesh data
for (int z = 0; z < height; z++)
{
for (int x = 0; x < width; x++)
{
// This vert index
int index = z * width + x;
// Set up this vertex
verts[index] = {};
// Position on a regular grid, heights from heightmap
verts[index].Position.x = (x - halfWidth) * xzScale;
verts[index].Position.y = (heights[index] / 255.0f) * yScale;
verts[index].Position.z = (z - halfHeight) * xzScale;
// Assume we're starting flat
verts[index].Normal.x = 0.0f;
verts[index].Normal.y = 1.0f;
verts[index].Normal.z = 0.0f;
// Simple UV (0-1)
verts[index].UV.x = x / (float)width;
verts[index].UV.y = z / (float)height;
}
}
}
void TerrainMesh::Load16bitRaw(const char* heightmap, unsigned int width, unsigned int height, float yScale, float xzScale, Vertex* verts)
{
unsigned int numVertices = width * height;
float halfWidth = width / 2.0f;
float halfHeight = height / 2.0f;
// Vector to hold heights
std::vector<unsigned short> heights(numVertices);
// Open the file (remember to #include <fstream>)
std::ifstream file;
file.open(heightmap, std::ios_base::binary);
if (!file)
return;
// Read raw bytes to vector
file.read((char*)&heights[0], numVertices * 2); // Double the size, since each pixel is 16-bit
file.close();
// Create the initial mesh data
for (int z = 0; z < height; z++)
{
for (int x = 0; x < width; x++)
{
// This vert index
int index = z * width + x;
// Set up this vertex
verts[index] = {};
// Position on a regular grid, heights from heightmap
verts[index].Position.x = (x - halfWidth) * xzScale;
verts[index].Position.y = (heights[index] / 65535.0f) * yScale; // 16-bit, so max value is 65535
verts[index].Position.z = (z - halfHeight) * xzScale;
// Assume we're starting flat
verts[index].Normal.x = 0.0f;
verts[index].Normal.y = 1.0f;
verts[index].Normal.z = 0.0f;
// Simple UV (0-1)
verts[index].UV.x = x / (float)width;
verts[index].UV.y = z / (float)height;
}
}
}