Game Development Reference
In-Depth Information
Thus, the conclusion of the Terrian::getHeight code is:
if(dz < 1.0f - dx) // upper triangle ABC
{
float uy=B-A;//A->B
float vy=C-A;//A->C
height=A+d3d::Lerp(0.0f, uy, dx) +
d3d::Lerp(0.0f, vy, dz);
}
else // lower triangle DCB
{
float uy=C-D;//D->C
float vy=B-D;//D->B
height=D+d3d::Lerp(0.0f, uy, 1.0f - dx) +
d3d::Lerp(0.0f, vy, 1.0f - dz);
}
return height;
}
The Lerp function is basic linear interpolation along a 1D line and is
implemented as:
float d3d::Lerp(float a, float b, float t)
{
return a - (a*t) + (b*t);
}
13.6 Sample Application: Terrain
The sample for this chapter creates a terrain given a RAW file contain-
ing the heightmap data, textures the terrain, and lights it. In addition,
we can walk on the terrain using the arrow keys. Note that in the fol-
lowing functions, non-relevant code has been omitted; a place where
code has been omitted is denoted by ellipses (…). Also, depending on
your hardware, the sample may run slow; try using a smaller terrain.
First we add the following global variables representing our terrain,
camera, and frames per second counter:
Terrain* TheTerrain = 0;
Camera
TheCamera(Camera::LANDOBJECT);
FPSCounter* FPS = 0;
Then the framework functions:
bool Setup()
{
D3DXVECTOR3 lightDirection(0.0f, -1.0f, 0.0f);
TheTerrain = new Terrain(Device, "coastMountain256.raw",
256, 256, 10, 1.0f);
TheTerrain->genTexture();
TheTerrain->lightTerrain(&directionToLight);
Search WWH ::




Custom Search