# Geometrical MipMapping¶

The GeoMipTerrain generates terrain geometry from a heightfield image, but it does more than any bruteforce terrain generator: the GeoMipTerrain divides the terrain into multiple chunks, where each of them can have a different level of detail. The advantage of this approach is that, when the focal point (the place where the terrain has the highest quality, probably the camera) moves, not the entire terrain has to be regenerated to match the correct detail level, like the HeightfieldTesselator, but only the chunks that have a different LOD can be regenerated. Also, it improves culling and collision detection.

## Basic Usage¶

Using the GeoMipTerrain is quite easy, it does not require to write many complicated calculations:

terrain = GeoMipTerrain("mySimpleTerrain")
terrain.setHeightfield("yourHeightField.png")
#terrain.setBruteforce(True)
terrain.getRoot().reparentTo(render)
terrain.generate()


First, the code creates a GeoMipTerrain instance. The setHeightfield() call loads in a heightfield image. Preferably this is a size of a power of two plus one (like 129, 257, 513, 1025, etc.), but if it is not, the GeoMipTerrain will automatically scale it up to the nearest correct size (which is quite slow). setHeightfield() can take a PNMImage, Texture or Filename instance.

The setBruteforce(True) call sets the terrain to bruteforce rendering – this means that the terrain is created at the highest quality (the lowest detail level), and LOD is not applied. In the next section we will explain how to set a LOD level and a Focal Point. The getRoot() call returns the NodePath of the terrain. It is then reparented to render to be a part of the scene graph. You can apply Common State Changes to this NodePath. Finally, the generate() call generates the terrain geometry. Note that if the terrain is still quite flat, you will have to scale the terrain NodePath in the Z direction, because by default, the Z positions are between 0 and 1. To fix this, scale the terrain up in the Z direction (before generating it, otherwise it might require you to regenerate it):

terrain.getRoot().setSz(100)


## Dynamic Terrains¶

This code shows a dynamically updated terrain with LOD:

# Set up the GeoMipTerrain
terrain = GeoMipTerrain("myDynamicTerrain")
terrain.setHeightfield("yourHeightField.png")

# Set terrain properties
terrain.setBlockSize(32)
terrain.setNear(40)
terrain.setFar(100)
terrain.setFocalPoint(base.camera)

# Store the root NodePath for convenience
root = terrain.getRoot()
root.reparentTo(render)
root.setSz(100)

# Generate it.
terrain.generate()

terrain.update()



This code shows a dynamically updated terrain, which is updated every frame with the camera as focal point. You see that a few functions are called: The blocksize is set to 32. This means that GeoMipTerrain has to divide the terrain in chunks of 32x32 quads. Then, the near and far distances are set. The Near distance is the distance from the focal point to where the terrain starts to decrease in quality. The far clip is the distance where the terrain is lowest quality. Also, the focal point is set to the Camera’s NodePath; you can specify any NodePath you want, but also a Point2 or Point3. If you specify the latter, please note that only the X and Y positions are used to calculate the distance; the Z position is disregarded. Note that you need to experiment with those values to get a good quality terrain while still maintaining a good performance.

Next, for convenience, the terrain root is stored in a separate variable, which is scaled and placed in the scene graph. The terrain is then initially generated, and a task is created which calls terrain.update() every frame. This function calculates the new LOD levels based on the movement of the focal point and updates the chunks which have got a new LOD level.

The GeoMipTerrain provides some advanced features over the terrain:

### Minimum Level¶

You can specify a minimum LOD level to GeoMipTerrain. You can do this if you find the terrain a bit too high quality near the focal point, and this could waste your performance. If you set a minimum LOD level, you can prevent this and force the chunks to have a minimum level of detail:

terrain.setMinLevel(2)


If you make the value higher, it will decrease the quality level near the focal point.

### Automatic Flattening¶

Since flattening the terrain root might interfere with the updating system, GeoMipTerrain provides an auto-flattening function, which can be really useful if you have too many meshes in your scene. This function calls one of NodePath’s flattening functions every time the terrain is regenerated, and each time before the chunks are modified the terrain is restored from a backup node:

terrain.setAutoFlatten(GeoMipTerrain.AFMStrong)


There are multiple options: AFM_strong for flattenStrong(), AFM_medium for flattenMedium(), AFM_light for flattenLight(), and AFM_off for no flattening at all. After setting the AutoFlattenMode, GeoMipTerrain will automatically take care of it at the next update() call.

## Notes¶

• For a full function overview, see the GeoMipTerrain page in the API Reference.

• The GeoMipTerrain generates texture coordinates between 0 and 1, making the texture stretched over the entire terrain. If you are using a shader, please do not directly base the coordinates on the vtx_position, because since the terrain can have multiple chunks the vertex position is relative to the chunk. Instead, base your shader calculations on the vtx_texcoord0 generated by the GeoMipTerrain.

• The GeoMipTerrain class implements part of the GeoMipMapping algorithm, described in this paper by Willem H. de Boer.