## Add a heightmap terrain

Adding a heightmap based terrain to the scene is pretty easy. You have to create an instance of the "Terrain" class with several parameters. The first one is the grayscale image that is used as the heightmap. Be aware that the used image files should have the same power of two resolution for both sides. Furthermore you have to specify the path to a ground texture that ought to be applied to texture unit 0 of the terrain mesh. The third parameter represents the resolution to use for creating the heightmap. A resolution of 1 transfers every pixel of the image into a triangle of the ground mesh. A resolution of 2 or 4 transfers every 2nd or 4th pixel into a triangle, resulting in a less detailed look of the terrain. The last paramter specifies, if the engine should calculated smoothing normals for the terrain. If set to "true" the terrain will look smoother at its edges and the effects of illumination on the terrain will become nicer, too. The drawback of this is an increased amount of time that is needed to load the scene.
To actually apply the terrain to the scene its update() and draw() methods need to be called within the according methods of this class. Just take a look at the code example below.

`01 package tutorial; 02 import javax.media.opengl.GL; 03 import javax.media.opengl.GLDrawable; 04 05 import de.rico.engine.game.AbstractBaseGame; 06 import de.rico.engine.geometry.Terrain; 07 08 /** 09 * A class that shows how to add a heightmap terrain to 10 * the 3d environment. 11 * @author Frank Bruns 12 * 13 */ 14 public class AddHeightmapTerrain extends AbstractBaseGame 15 { 16 private Terrain terrain = null; 17 18 /** 19 * Constructor for the new game. 20 * @param title title for the window bar 21 * @param maxFps maximum possible framerate 22 */ 23 public AddHeightmapTerrain(String title, int maxFps) 24 { 25 super(title, maxFps); 26 } 27 28 /* (non-Javadoc) 29 * @see de.rico.engine.game.AbstractBaseGame#initResources(javax.media.opengl.GLDrawable, javax.media.opengl.GL, int, int) 30 */ 31 @Override 32 public void initResources(GLDrawable gld, GL gl, int width, int height) 33 { 34 // create terrain object --> resolution = 1, smooth = true 35 this.terrain = new Terrain("./images/misc/heightmap.jpg","./images/misc/heightmap_tex.jpg",1,true); 36 } 37 38 /* (non-Javadoc) 39 * @see de.rico.engine.game.AbstractBaseGame#update(long) 40 */ 41 @Override 42 public void update(long elapsedTime) 43 { 44 // update the terrain every frame 45 this.terrain.update(elapsedTime); 46 } 47 48 /* (non-Javadoc) 49 * @see de.rico.engine.game.AbstractBaseGame#draw(javax.media.opengl.GL) 50 */ 51 @Override 52 public void draw(GL gl) 53 { 54 // draw the terrain every frame 55 this.terrain.draw(gl); 56 } 57 58 /** 59 * The usual main method. It's the entry point to the application. 60 * @param args argument string 61 */ 62 public static void main(String args[]) 63 { 64 // create a new game 65 AddHeightmapTerrain game = new AddHeightmapTerrain("Test Game", 100); 66 67 // initialise the camera --> needs to be done! 68 game.initCamera(0f, 10f, 20f, 0f, 0f, 0.1f, 200f); 69 70 // show framerate --> this is optional 71 game.setDisplayFPS(true); 72 73 // finally start the game 74 game.start(game); 75 } 76 }`
Java2html

After executing the code you should see a display resolution dialog. Choose a resolution suitable for you and press "OK". If everything is correct there should be a window showing up on the screen that looks like the one in the picture below. Of course the result depends on the specified heighmat image and ground texture.
.