LoadTerrain ( heightmap_file$[,parent] )
Parameters
|
heightmap_file$ - filename of the image to use as a height map parent (optional) - parent entity of the terrain |
Description
|
Loads a terrain from a height map image and returns its handle. This is the quick way to get a landscape into a game: paint or generate a greyscale picture where black is the valley floor and white is the mountain top, drop it in your media folder, and load it. The result is exactly the same kind of entity CreateTerrain makes, so everything else on the terrain pages applies to it. The brightest of a pixel's red, green and blue channels sets the height, so a plain greyscale image behaves the way you expect and a coloured one is read by its brightness. If you save the height map as a 16-bit greyscale PNG or TIFF the full 16 bits are used, which is worth doing for gentle slopes - an 8-bit image only has 256 height steps and large flat-ish areas can end up visibly terraced. Loading goes through Windows imaging, so BMP, PNG, JPEG and TIFF all work; JPEG is a poor choice because its compression artefacts turn into bumps. The image must be square and its side must be a power of two, e.g. 128, 256, 512 or 1024, up to a limit of 4096. A non-square image stops the program with "Terrain must be square", a non-power-of-two side with "Illegal terrain size", and anything wider than 4096 with a size-limit error. The loaded terrain's grid size is the image width, which you can read back with TerrainSize. The image's bottom row becomes the terrain's z=0 edge, so the landscape is laid out as you would see it looking down at the picture in a paint package, not mirrored. Straight after loading, the terrain runs from 0,0,0 to grid_size,1,grid_size - one unit per grid square and one unit of total height. Use ScaleEntity to turn that into a landscape, for example ScaleEntity terrain,2,60,2. Some things that make a terrain look better: blur the height map slightly so the slopes are smooth, keep the y scale modest so you get rolling hills instead of spikes, spread the x and z scale out, and set CameraRange so the far plane is not miles beyond the visible ground. Textures are sized in grid squares. A texture at the default scale of 1 covers exactly one grid square, so it tiles across the landscape; scaling it to the grid size instead stretches one copy over the whole terrain, which is how you apply a baked colour map painted from the same source as the height map. The optional parent parameter attaches the terrain to another entity so that moving the parent moves the terrain with it, one way only. The terrain is created at local position 0,0,0, so it starts sitting on top of its parent rather than at the world origin. See also: CreateTerrain, ModifyTerrain, TerrainDetail, TerrainSize, TerrainY. |
Example
; LoadTerrain Example ; ------------------- Graphics3D 640,480,0,2 SetBuffer BackBuffer() camera=CreateCamera() CameraRange camera,1,500 light=CreateLight() RotateEntity light,45,45,0 ; Load a 256x256 greyscale height map as a terrain; the image's red ; channel sets the height of each grid point terrain=LoadTerrain("media/Height_map.bmp") ; Stretch it into a landscape: 2 units per grid square, 60 units tall ScaleEntity terrain,2,60,2 ; Spend around 4000 triangles, with vertex morphing to hide LOD pop-in TerrainDetail terrain,4000,True ; Texture the landscape grass_tex=LoadTexture("media/MossyGround.BMP") EntityTexture terrain,grass_tex ; Start in the middle of the terrain PositionEntity camera,256,30,256 While Not KeyDown(1) ; Drift forward on its own; arrow keys steer and speed up MoveEntity camera,0,0,0.2 If KeyDown(200) Then MoveEntity camera,0,0,1 If KeyDown(208) Then MoveEntity camera,0,0,-1 If KeyDown(203) Then TurnEntity camera,0,1,0 If KeyDown(205) Then TurnEntity camera,0,-1,0 ; Glide at a fixed height above the ground x#=EntityX(camera) z#=EntityZ(camera) y#=TerrainY(terrain,x,EntityY(camera),z)+8 PositionEntity camera,x,y,z RenderWorld Text 0,0,"Arrows: fly over the terrain Esc: exit" Text 0,20,"LoadTerrain built this landscape from media/Height_map.bmp" Flip Wend End
Index