Game Development Reference
In-Depth Information
13.2.2 Computing the Indices—Defining the Triangles
To compute the indices of the triangle grid, we simply iterate through
each quad, starting in the upper left and ending in the lower right of
Figure 13.4, and compute the two triangles that make up that quad.
Figure 13.6: A quad's vertices
The trick is to come up with the general formulas to compute the two
triangles of the ij th quad. Using Figure 13.6 to develop our general for-
mulas, we find that for quad ( i , j ):
"
#
$
ABC
i
numVertsPe
rRow
j
i
numVertsPe
rRow
j
1
i
1
numVertsPe
rRow
j
"
1
$
CBD
i
1
numVertsPe
rRow
j
i
numVertsPe
rRow
j
1
i
1
numVertsPe
rRow
j
The code to generate the indices:
bool Terrain::computeIndices()
{
HRESULT hr = 0;
hr = _device->CreateIndexBuffer(
_numTriangles*3*sizeof(WORD), // 3 indices per triangle
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&_ib,
0);
if(FAILED(hr))
return false;
WORD* indices = 0;
_ib->Lock(0, 0, (void**)&indices, 0);
// index to start of a group of 6 indices that describe the
// two triangles that make up a quad
 
Search WWH ::




Custom Search