1.2、构建三角形的顶点及索引数据
地形由一个个三角形面构成,可利用顶点索引构建三角形,三角形从左往右构建。
A |----- | B
| / |
C |/___ | D
先构建顶点数据:
float uCoordIncrementSize = 1.0f / (float)_numCellsPerRow;
float vCoordIncrementSize = 1.0f / (float)_numCellsPerCol;
TerrainVertex* v = 0;
_vb->Lock(0, 0, (void**)&v, 0);
for(int i = 0; i <_numvertsperrow i>
{
for(int j = 0; j < _numVertsPerCol; j++)
{
int index = i * _numVertsPerRow + j;
v[index] = TerrainVertex(
(float)j*_cellSpacing + startX,
(float)_heightmap[index],
(float)i*_cellSpacing + startZ,
0.0f,
1.0f,
0.0f,
(float)j * uCoordIncrementSize,
(float)i * vCoordIncrementSize);
}
}
_vb->Unlock();
再构建三角形索引:
WORD* indices = 0;
_ib->Lock(0, 0, (void**)&indices, 0);
int baseIndex = 0;
for(int i = 0; i < _numCellsPerCol; i++)
{
for(int j = 0; j < _numCellsPerRow; j++)
{
indices[baseIndex] = i * _numVertsPerRow + j;
indices[baseIndex + 1] = (i+1) * _numVertsPerRow + j;
indices[baseIndex + 2] = i * _numVertsPerRow + j +1;
indices[baseIndex + 3] = (i+1) * _numVertsPerRow + j;
indices[baseIndex + 4] = (i+1) * _numVertsPerRow + j + 1;
indices[baseIndex + 5] = i * _numVertsPerRow + j + 1;