Graphics Reference
In-Depth Information
float fHeight = static_cast< float >( TERRAIN_Z_LEN );
for( int x = 0; x < TERRAIN_X_LEN + lj ++x )
{
for( int Z = 0; z < TERRAIN_Z_LEN + 1; ++Z )
{
float fX = static_cast<float>(x) / fwidth - 0.5f;
float fZ = static_cast<float>(z) / fHeight - 0.5f;
pPosDataf x + z * (TERRAIN_X_LEN + 1) ] = Vector3f( fX, 0.0f, fZ );
pTCData[ x + z * (TERRAIN_X_LEN + 1) ] = Vector2f( fX + 0.5f, fZ + 0.5f );
}
}
m_pTerrainGeometry->AddElement( pPositions );
m_pTerrainGeometry->AddElement( pTexCoords );
Listing 9.1. Creating the vertex buffer.
The TERRAIN_X_LEN and TERRAIN_Z_LEN constants are defined in the context of how
many tiles should be generated. Therefore, 1 is added to each of these constants in various
places—for example, a 3x3 grid of tiles will require a 4x4 grid of vertices.
It is also worth noting that the raw geometry is defined as being a flat grid on the XZ
plane in 3D space, and that the height of the terrain is defined entirely along the Y axis.
Listing 9.1 initializes all height points to 0.0 at this stage.
Listing 9.2, shown below, builds up the index buffer data that defines each patch. This
is by far the most complex and most important resource that this algorithm must initialize.
The input assembler will use this data to index into the vertex buffer and map all the neces-
sary control points that the hull shader and domain shader need to do their job.
// Code below makes reference to the following macro:
#define clamp(value,minimum,maximum) (max(min( (value) , (maximum) ), (minimum)))
for( int x = 0; x < TERRAIN_X_LEN; ++x )
{
for( int z = 0; z < TERRAIN_Z_LEN; ++z )
{
// Define 12 control points per terrain quad
// 0-3 are the actual quad vertices
m_pTerrainGeontetry->AddIndex( (z + 0) + (x + 0) * (TERRAIN_X_LEN + 1) );
m_pTerrainGeometry->AddIndex( (z + 1) + (x + 0) * (TERRAIN_X_LEN + 1) );
m_pTerrainGeometry->AddIndex( (z + 0) + (x + 1) * (TERRAIN_X_LEN + 1) );
m_pTerrainGeometry->AddIndex( (z + 1) + (x + 1) * (TERRAIN_X_LEN + 1) );
// 4-5 are +x
m_pTerrainGeometry->AddIndex
(
clamp(z + 0, 0, TERRAIN_Z_LEN)
Search WWH ::




Custom Search