Let's assume that the landscape has n different attributes.
For every attribute, there is a corresponding texture, for instance snow or grass.
The mechanism works like this: Iterate through all pixels (x/y) in the heightmap and
read the height-value at that location. Then, depending on the height, fetch the
texture representing the attribute for a height-region and add that color to an
output-texture at the same location (x/y). For example, a landscape that has
height-values ranging from 0 to 255 could be divided like this:
- 196-255: Snow
- 128-195: Rock
- 64-127: Grass
- 0-63: Sand
The problem with this approach would be the very sharp edges on the output texture
when two pixels border two regions. What would be way better is to fade the pixels
from one region to another. To do that, there's need for a fading-function.
float texfactor(float h1, float h2, float w)
{
float percent;
percent = (w - std::abs(h1 - h2)) / w;
if(percent < 0.0f)
percent = 0.0f;
else if(percent > 1.0f)
percent = 1.0f;
return percent;
}
The function shown in the sourcecode is a weighting-function[2] and has two parameteres
h1 and h2, which represent two
height-values, and a third parameter w that represents the
absolute height of one region. What this function does is to return a percentage value
that indicates wheter h2 is in a region defined by a
representative value in that region h1, for instance the
upper border. The last parameter is to adjust the height of one region, so in the case
of four height-regions that equally divide a range of 256 values,
w would be set to 64.
Moving further with the above example: The percentage of visibility for grass for a
height value of 200 would yield 0%, since at that height there's only rocks and snow
left.