【DX11地形篇】10-地形分块

参考教程

Tutorial 9: Terrain Cells

学习记录

  这篇文章中我们简单介绍对地形进行划分,完整的地形网格分为多块来进行管理,本篇新增类:TerrainCellClass

  首先来看我们 TerrainCellClass 类的声明:

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
////////////////////////////////////////////////////////////////////////////////
// Filename: terraincellclass.h
////////////////////////////////////////////////////////////////////////////////
#ifndef _TERRAINCELLCLASS_H_
#define _TERRAINCELLCLASS_H_


//////////////
// INCLUDES //
//////////////
#include <d3d11.h>
#include <directxmath.h>
using namespace DirectX;


////////////////////////////////////////////////////////////////////////////////
// Class name: TerrainCellClass
////////////////////////////////////////////////////////////////////////////////
class TerrainCellClass
{
private:
struct ModelType
{
float x, y, z;
float tu, tv;
float nx, ny, nz;
float tx, ty, tz;
float bx, by, bz;
float r, g, b;
};

struct VertexType
{
XMFLOAT3 position;
XMFLOAT2 texture;
XMFLOAT3 normal;
XMFLOAT3 tangent;
XMFLOAT3 binormal;
XMFLOAT3 color;
};

struct VectorType
{
float x, y, z;
};

struct ColorVertexType
{
XMFLOAT3 position;
XMFLOAT4 color;
};

public:
TerrainCellClass();
TerrainCellClass(const TerrainCellClass&);
~TerrainCellClass();

bool Initialize(ID3D11Device*, void*, int, int, int, int, int);
void Shutdown();
void Render(ID3D11DeviceContext*);
void RenderLineBuffers(ID3D11DeviceContext*);

int GetVertexCount();
int GetIndexCount();
int GetLineBuffersIndexCount();
void GetCellDimensions(float&, float&, float&, float&, float&, float&);

private:
bool InitializeBuffers(ID3D11Device*, int, int, int, int, int, ModelType*);
void ShutdownBuffers();
void RenderBuffers(ID3D11DeviceContext*);
void CalculateCellDimensions();
bool BuildLineBuffers(ID3D11Device*);
void ShutdownLineBuffers();

public:
VectorType* m_vertexList;

private:
int m_vertexCount, m_indexCount, m_lineIndexCount;
ID3D11Buffer *m_vertexBuffer, *m_indexBuffer, *m_lineVertexBuffer, *m_lineIndexBuffer;;
float m_maxWidth, m_maxHeight, m_maxDepth, m_minWidth, m_minHeight, m_minDepth;
float m_positionX, m_positionY, m_positionZ;
};

#endif

  也是一个模型类,他使用 LINE 作为绘制图元来绘制,用我们传进来的 TerrainClass 的定点属性来生成顶点信息。其部分实现代码如下:

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
bool TerrainCellClass::InitializeBuffers(ID3D11Device* device, int nodeIndexX, int nodeIndexY, int cellHeight, int cellWidth, 
int terrainWidth, ModelType* terrainModel)
{
VertexType* vertices;
unsigned long* indices;
int i, j, modelIndex, index;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;


// Calculate the number of vertices in this terrain cell.
m_vertexCount = (cellHeight - 1) * (cellWidth - 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;
}

// Setup the indexes into the terrain model data and the local vertex/index array.
modelIndex = ((nodeIndexX * (cellWidth - 1)) + (nodeIndexY * (cellHeight - 1) * (terrainWidth - 1))) * 6;
index = 0;

// Load the vertex array and index array with data.
for(j=0; j<(cellHeight - 1); j++)
{
for(i=0; i<((cellWidth - 1) * 6); i++)
{
vertices[index].position = XMFLOAT3(terrainModel[modelIndex].x, terrainModel[modelIndex].y, terrainModel[modelIndex].z);
vertices[index].texture = XMFLOAT2(terrainModel[modelIndex].tu, terrainModel[modelIndex].tv);
vertices[index].normal = XMFLOAT3(terrainModel[modelIndex].nx, terrainModel[modelIndex].ny, terrainModel[modelIndex].nz);
vertices[index].tangent = XMFLOAT3(terrainModel[modelIndex].tx, terrainModel[modelIndex].ty, terrainModel[modelIndex].tz);
vertices[index].binormal = XMFLOAT3(terrainModel[modelIndex].bx, terrainModel[modelIndex].by, terrainModel[modelIndex].bz);
vertices[index].color = XMFLOAT3(terrainModel[modelIndex].r, terrainModel[modelIndex].g, terrainModel[modelIndex].b);
indices[index] = index;
modelIndex++;
index++;
}
modelIndex += (terrainWidth * 6) - (cellWidth * 6);
}

// 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;
}

// Create a public vertex array that will be used for accessing vertex information about this cell.
m_vertexList = new VectorType[m_vertexCount];
if(!m_vertexList)
{
return false;
}

// Keep a local copy of the vertex position data for this cell.
for(i=0; i<m_vertexCount; i++)
{
m_vertexList[i].x = vertices[i].position.x;
m_vertexList[i].y = vertices[i].position.y;
m_vertexList[i].z = vertices[i].position.z;
}

// Release the arrays now that the buffers have been created and loaded.
delete [] vertices;
vertices = 0;

delete [] indices;
indices = 0;

return true;
}

void TerrainCellClass::CalculateCellDimensions()
{
int i;
float width, height, depth;


// Initialize the dimensions of the node.
m_maxWidth = -1000000.0f;
m_maxHeight = -1000000.0f;
m_maxDepth = -1000000.0f;

m_minWidth = 1000000.0f;
m_minHeight = 1000000.0f;
m_minDepth = 1000000.0f;

for(i=0; i<m_vertexCount; i++)
{
width = m_vertexList[i].x;
height = m_vertexList[i].y;
depth = m_vertexList[i].z;

// Check if the width exceeds the minimum or maximum.
if(width > m_maxWidth)
{
m_maxWidth = width;
}
if(width < m_minWidth)
{
m_minWidth = width;
}

// Check if the height exceeds the minimum or maximum.
if(height > m_maxHeight)
{
m_maxHeight = height;
}
if(height < m_minHeight)
{
m_minHeight = height;
}

// Check if the depth exceeds the minimum or maximum.
if(depth > m_maxDepth)
{
m_maxDepth = depth;
}
if(depth < m_minDepth)
{
m_minDepth = depth;
}
}

// Calculate the center position of this cell.
m_positionX = (m_maxWidth - m_minWidth) + m_minWidth;
m_positionY = (m_maxHeight - m_minHeight) + m_minHeight;
m_positionZ = (m_maxDepth - m_minDepth) + m_minDepth;

return;
}

bool TerrainCellClass::BuildLineBuffers(ID3D11Device* device)
{
ColorVertexType* vertices;
unsigned long* indices;
D3D11_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
D3D11_SUBRESOURCE_DATA vertexData, indexData;
HRESULT result;
XMFLOAT4 lineColor;
int index, vertexCount, indexCount;


// Set the color of the lines to orange.
lineColor = XMFLOAT4(1.0f, 0.5f, 0.0f, 1.0f);

// Set the number of vertices in the vertex array.
vertexCount = 24;

// Set the number of indices in the index array.
indexCount = vertexCount;

// Create the vertex array.
vertices = new ColorVertexType[vertexCount];
if(!vertices)
{
return false;
}

// Create the index array.
indices = new unsigned long[indexCount];
if(!indices)
{
return false;
}

// Set up the description of the vertex buffer.
vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
vertexBufferDesc.ByteWidth = sizeof(ColorVertexType) * 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;

// Set up the description of the index buffer.
indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
indexBufferDesc.ByteWidth = sizeof(unsigned long) * 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;

// Load the vertex and index array with data.
index = 0;

// 8 Horizontal lines.
vertices[index].position = XMFLOAT3(m_minWidth, m_minHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_minHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_minHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_minHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_minHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_minHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_minHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_minHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_maxHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_maxHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_maxHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_maxHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_maxHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_maxHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_maxHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_maxHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

// 4 Verticle lines.
vertices[index].position = XMFLOAT3(m_maxWidth, m_maxHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_minHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_maxHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_minHeight, m_maxDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_maxHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_maxWidth, m_minHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_maxHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;
index++;

vertices[index].position = XMFLOAT3(m_minWidth, m_minHeight, m_minDepth);
vertices[index].color = lineColor;
indices[index] = index;

// Create the vertex buffer.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_lineVertexBuffer);
if(FAILED(result))
{
return false;
}

// Create the index buffer.
result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_lineIndexBuffer);
if(FAILED(result))
{
return false;
}

// Store the index count for rendering.
m_lineIndexCount = indexCount;

// 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;
}

  在 TerrainClass 中我们使用 TerrainCellClass 类的对象数组,以及一些方法:

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
////////////////////////////////////////////////////////////////////////////////
// Class name: TerrainClass
////////////////////////////////////////////////////////////////////////////////
class TerrainClass
{
private:
struct VertexType
{
XMFLOAT3 position;
XMFLOAT2 texcoord;
XMFLOAT3 normal;
XMFLOAT3 tangent;
XMFLOAT3 binormal;
XMFLOAT3 color;
};

struct HeightMapType {
float x, y, z;
float nx, ny, nz;
float r, g, b;
};

struct ModelType {
float x, y, z;
float tu, tv;
float nx, ny, nz;
float tx, ty, tz;
float bx, by, bz;
float r, g, b;
};

struct VectorType {
float x, y, z;
};

struct TempVertexType
{
float x, y, z;
float tu, tv;
float nx, ny, nz;
};

public:
TerrainClass();
TerrainClass(const TerrainClass&);
~TerrainClass();

bool Initialize(ID3D11Device* , char*);
void Shutdown();
bool Render(ID3D11DeviceContext*);

int GetIndexCount();

bool RenderCell(ID3D11DeviceContext*, int);
void RenderCellLines(ID3D11DeviceContext*, int);

int GetCellIndexCount(int);
int GetCellLinesIndexCount(int);
int GetCellCount();

private:
bool InitializeBuffers(ID3D11Device*);
void ShutdownBuffers();
void RenderBuffers(ID3D11DeviceContext*);

void CalculateTerrainVectors();
void CalculateTangentBinormal(TempVertexType, TempVertexType, TempVertexType, VectorType&, VectorType&);

bool LoadColorMap();
bool CalculateNormals();
bool LoadSetupFile(CHAR*);
bool LoadBitmapHeightMap();
bool LoadRawHeightMap();
void ShutdownHeightMap();
void SetTerrainCoordinates();
bool BuildTerrainModel();
void ShutdownTerrainModel();

bool LoadTerrainCells(ID3D11Device*);
void ShutdownTerrainCells();

private:
ID3D11Buffer *m_vertexBuffer, *m_indexBuffer;
int m_vertexCount, m_indexCount;

int m_terrainHeight , m_terrainWidth;
float m_heightScale;
LPSTR m_terrainFilename;
LPSTR m_colorMapFilename;
HeightMapType* m_heightMap;
ModelType* m_terrainModel;

TerrainCellClass* m_TerrainCells;
int m_cellCount;
};

  最后在渲染地形的时候,我们选择性的对 TerrainCell 进行渲染,最终效果如下:

1

  源代码:DX11TerrainTutorial-TerrainCells

0%