Game Development Reference
In-Depth Information
return true;
}
13.3.1 A Procedural Approach
An alternative way to texture the terrain is to compute the texture pro-
cedurally; that is, we create an “empty” texture and compute the color
of each texel in code based on some defined parameter(s). In our exam-
ple, the parameter will be the height of the terrain.
We generate the texture procedurally in the Terrain::genTex-
ture method. It first creates an empty texture using the D3DXCre-
ateTexture method. Then we lock the top level (remember a texture
has mipmaps and can have multiple levels). From there we iterate
through each texel and color it. We color the texel based on the approx-
imate height of the quad to which it corresponds. The idea is to have
lower altitudes of the terrain colored a sandy beach color, medium alti-
tudes colored as grassy hills, and the high altitudes colored as snowy
mountains. We define the approximate height of the quad as the height
of the upper-left vertex of the quad.
Once we have a color for each texel, we want to darken or brighten
each texel based on the angle at which sunlight (modeled by a direc-
tional light) strikes the cell to which the texel corresponds. This is
done in the Terrain::lightTerrain method, whose implementa-
tion is covered in the next section.
The Terrain::genTexture method concludes by computing the
texels of the lower mipmap levels. This is done using the D3DXFil-
terTexture function. The code to generate the texture:
bool Terrain::genTexture(D3DXVECTOR3* directionToLight)
{
// Method fills the top surface of a texture procedurally. Then
// lights the top surface. Finally, it fills the other mipmap
// surfaces based on the top surface data using
// D3DXFilterTexture.
HRESULT hr = 0;
// texel for each quad cell
int texWidth = _numCellsPerRow;
int texHeight = _numCellsPerCol;
// create an empty texture
hr = D3DXCreateTexture(
_device,
texWidth, texHeight, // dimensions
0,
// create a complete mipmap chain
0,
// usage - none
D3DFMT_X8R8G8B8,
// 32-bit XRGB format
D3DPOOL_MANAGED,
// memory pool
Search WWH ::




Custom Search