Professional Documents
Culture Documents
Generate 3D Terrain
Generate a heightmap to be
used as the “blueprint” for the
terrain.
Fault Formation
Midpoint Displacement
Genetic Algorithm
Etc.
Fault Formation
process of generating
“faults” in terrain
add a random line to a
blank height field
add a random height
to one of the sides.
Fault Formation
How to create Random Line?
//check to make sure that the points are not the same
do
{
iRandX2= rand( )%m_iSize;
iRandZ2= rand( )%m_iSize;
} while ( iRandX2==iRandX1 && iRandZ2==iRandZ1 );
Fault Formation
we want to elevate one side of the line,
and we want to raise the height value of
every point on that side of the line.
How?
Scan all pixels of the entire heightmap, if
the pixel is in the right side, elevate it.
Fault Formation
Create a vector from (iRandX1, iRandZ1)
to (iRandX1, iRandZ1).
- Calculate the midpoint for all four lines (AB, BD, DC, CA). The resulting point, E,
should be directly in the middle of the square.
- Displace E by taking the average of A, B, C, and D’s height values, and then we
add a random value in the range of –fHeight/2 to fHeight/2 .
Midpoint Displacement
Calculate the height values for each of the
midpoints that is found earlier.
Average the height values of the surrounding vertices and
add a random height value in the range of –fHeight/2 to
fHeight/2
Texturing Terrain
Simple Texture Mapping
“stretch” one texture over an entire terrain mesh.
Simple Texture Mapping
LoadTexture( "../Data/grass_1.tga" );
glEnable( GL_TEXTURE_2D );
glBindTexture( GL_TEXTURE_2D, m_texture.GetID( ) );
struct STRN_TEXTURE_REGIONS {
int m_iLowHeight; //lowest possible height (0%)
int m_iOptimalHeight; //optimal height (100%)
int m_iHighHeight; //highest possible height (0%)
};
The Region System
How it work?
//set our terrain's texture color to the one that we previously calculated
m_texture.SetColor( x, z, ( unsigned char )fTotalRed,
( unsigned char )fTotalGreen,
( unsigned char )fTotalBlue );
}
}
InterpolateHeight() -> Interpolate the heights in the height map so that the generated texture
map does not look incredibly blocky