# Introduction to Terrains

Terrain entities are used to draw very large landscapes made up of many, many triangles - even up to a million or more!
To actually draw this many triangles would be a very slow process, so terrains use a technique known as 'dynamic level of detail' to speed things up.
This basically means that instead of drawing all of the triangles in a terrain, a smaller number of triangles is used to render an approximation of the terrain.
Terrains have a few limitations:

- The vertices of a terrain are laid out in a square grid pattern, with the same number of vertices along each edge of the grid.
- Only the height of a vertex may be modified.
- The size of a terrain must be a 'power of 2' value, eg: 2,4,8,16,32...

## Creating Terrains

You can create terrains either by loading in a heightmap image, or by creating an 'empty' terrain and setting the height of each vertex yourself.
A heightmap image is simply a grayscale image, where black pixels represent 'low' vertices, and white pixels represent 'high' vertices. A heightmap image must be of a valid terrain size - ie: square, and a power of 2 wide/high.
When a terrain is created, its width and depth are the same as the size of the terrain, and its height is 1. For example, if you create a terrain using:
terrain=CreateTerrain( 32 )
The terrain will extend from 0 to 32 along the x and z axis', and from 0 to 1 along the y axis.
However, you are free to position and scale the terrain as you see fit using entity manipulation commands such as ScaleEntity, PositionEntity. For example:

| ;terrain is 32 x 1 x 32 terrain=CreateTerrain( 32 ) ;terrain is now 320 x 100 x 320 ScaleEntity terrain,10,100,10 ;and centred (on x/z axis') at 0,0,0 PositionEntity terrain,-160,0,-160 |

## Terrrain Detail Level

You can directly control how many triangles are used to approximate a terrain using the TerrainDetail command.
Using less triangles will naturally result in better speed, but beware that below a certain threshold your terrain will start to behave very strangely!
For example, if you ask Blitz3D to render a detailed 1,000,000 triangle terrain using only 100 triangles, chances are it just wont be able to do a decent job.
This usually leads to a phenomenon known as 'pop-in', where Blitz3D just isn't able to make its mind about about how to draw the terrain, and you end up with vertices abruptly changing position, or 'popping', when the camera view changes.
So what number of triangles should be used? Well, this is definitely a 'trial and error' kind of thing and depends on a number of factors:

- The variation in the terrain - ie: how 'bumpy' it is.
- The scale of the terrain - ie: how much of it is visible to the camera.
- The range of the camera.

You can reduce the number of triangles needed to represent a terrain using the following tricks:

- 'Blur' or smooth out the terrain. For example, if you're using a heightmap image, just apply a 'blur' filter or equivalent to the heightmap.
- Increase the scale of the terrain so less of it is visible to the camera.
- Decrease the camera range, again meaning less of the terrain is visible to the camera.

If all of this sounds a bit wishy-washy, well, it is!
Generating nice looking terrain with minimum pop-in is as much an artform as it is a science.
Blitz3D also provides a technique called 'vertex morphing' to help reduce pop-in. Vertex morphing involves Blitz3D automatically smoothing out the terrain a little to reduce pop-in. The TerrainDetail command is also used to enable vertex morphing.

## Modifying Terrain

The ModifyTerrain command is used to change the height of a terrain vertex, for example:

| ;create a terrain of size 32. terrain=CreateTerrain( 32 ) ;push the centre vertex up half-way. ModifyTerrain terrain,16,16,.5 |

The parameters used with ModifyTerrain must be given in 'terrain coordinates'. This means that the x and z values should always be from 0 to the size of the terrain, and the height value should always be from 0 to 1. In other words, any scale, position or rotation applied to the terrain will be ignored.

## Texturing Terrains

By default the terrain you create will be just a plain colour, but by using the EntityTexture command it is possible to apply a texture brush to the whole landscape. Unfortunately at this time, it is not possible to texture different parts of the terrain with different textures. But fear not !, remember a brush can have 8 multitextures applied to it. By carefully building up these textures - it is possible to have different texturemaps applied onto the same landscape.

Copyright 2001 (c) Paul Gerfen - GamecodingUK