Game Development Reference
In-Depth Information
down direction. Thus, by Figure 13.9 and knowing the cell spacing is
equal to 1, we can immediately see that the row and column of the cell
we are in is given by:
float col = ::floorf(x);
float row = ::floorf(z);
In other words, column equals the integer part of x , and row equals the
integer part of z . Also recall that the floor ( t) function gives the greatest
integer t .
Now that we know the cell we are in will grab the heights of the
four vertices that form the cell we are in:
// A
B
// *—-*
// |/|
// *—-*
// C
D
float A = getHeightmapEntry(row, col);
float B = getHeightmapEntry(row, col+1);
float C = getHeightmapEntry(row+1, col);
float D = getHeightmapEntry(row+1, col+1);
At this point, we know the cell we are in and we know the heights of
the four vertices of that cell. Now we need to find the height
(y-coordinate) of the cell at the particular x- and z-coordinates at which
the camera is located. This is a little tricky since the cell can be slanted
in a couple of directions; see Figure 13.10.
Figure 13.10: The height y-coordinate of
the cell at the particular x- and
z-coordinates of the camera's position
In order to find the height, we need to know which triangle of the cell
we are in. Recall that our cells are rendered as two triangles. To find
the triangle we are in, we are going to take the cell we are in and trans-
late it so that its upper-left vertex is at the origin.
Since col and row describe the position of the upper-left vertex of
the cell we are in, we must translate by -col on the x-axis and -row
on the z-axis. Translating our x- and z-coordinates gives:
Search WWH ::




Custom Search