Game Development Reference
In-Depth Information
give us our x- and z-coordinate, but what about the y-coordinate? The
y-coordinate is easily obtained by finding the corresponding entry in
the loaded heightmap data structure.
Note: This implementation uses one large vertex buffer to hold all
of the vertices for the entire terrain. This can be problematic due to
hardware limitations. For example, there is a maximum primitive count
limit and maximum vertex index limit that is set for the 3D device.
Check the MaxPrimitiveCount and MaxVertexIndex members of the
D3DCAPS9 structure to see what your particular device's limits are. Sec-
tion 13.7 discusses a solution to the problems of using one vertex
buffer.
To compute the texture coordinates, consider Figure 13.5, which gives
us a simple scenario allowing us to see that the ( u , v ) texture coordi-
nate that corresponds to the terrain vertex at ( i , j ) is given by:
Figure 13.5: The correspondence
between the terrain vertices and the
texture vertices
u
j
uCoordIncr
ementSize
v
i
vCoordIncr
ementSize
And where:
1
uCoordIncr
ementSize
numCellCol
s
1
vCoordIncr
ementSize
numCellRow
s
Finally, the code to generate the vertices:
bool Terrain::computeVertices()
{
HRESULT hr = 0;
hr = _device->CreateVertexBuffer(
_numVertices * sizeof(TerrainVertex),
D3DUSAGE_WRITEONLY,
Search WWH ::




Custom Search