Game Development Reference
In-Depth Information
hr = DXUTGetD3D11Device()->CreateBuffer( &bd, &InitData, &m_pVertexBuffer );
if( FAILED( hr ) )
return hr;
// The number of indices equals the number of polygons times 3
// since there are 3 indices per polygon. Each grid square contains
// two polygons. The indices are 16 bit, since our grids won
'
t
// be that big!
m_numPolys = m_squares * m_squares * 2;
WORD *pIndices = GCC_NEW WORD[m_numPolys * 3];
GCC_ASSERT(pIndices &&
Out of memory!
);
// Loop through the grid squares and calc the values
// of each index. Each grid square has two triangles:
//
//
B
// | / |
// C − D
WORD *current = pIndices;
for( int j=0; j<m_squares; j++ )
{
A
for (int i=0; i<m_squares; i++)
{
// Triangle #1 ACB
*(current) = WORD(i + (j*(m_squares+1)));
*(current+1) = WORD(i + ((j+1)*(m_squares+1)));
*(current+2) = WORD((i+1) + (j*(m_squares+1)));
// Triangle #2 BCD
*(current+3) = WORD((i+1) + (j*(m_squares+1)));
*(current+4) = WORD(i + ((j+1)*(m_squares+1)));
*(current+5) = WORD((i+1) + ((j+1)*(m_squares+1)));
current+=6;
}
}
I
s going on. When
the code is executed, pVerts holds the list of vertices, and pIndices holds the
indexes into that list that defines the triangles of the grid mesh. Take a few minutes
to stare at the code that assigns the index numbers
'
ve commented the code pretty heavily to help you understand what
'
s the last nested for loop. If
you have trouble figuring it out, trace the code with a 2 × 2 grid, and you
it
'
'
ll get it.
Once these two data structures are defined, you have to create the vertex buffer and
index buffer that can be consumed by Direct3D 11. Here
'
s the code to do that:
Search WWH ::




Custom Search