• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2010 jMonkeyEngine
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  *   notice, this list of conditions and the following disclaimer in the
14  *   documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
17  *   may be used to endorse or promote products derived from this software
18  *   without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 package jme3test.helloworld;
34 
35 import com.jme3.app.SimpleApplication;
36 import com.jme3.material.Material;
37 import com.jme3.terrain.geomipmap.TerrainLodControl;
38 import com.jme3.terrain.geomipmap.TerrainQuad;
39 import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
40 import com.jme3.terrain.heightmap.AbstractHeightMap;
41 import com.jme3.terrain.heightmap.ImageBasedHeightMap;
42 import com.jme3.texture.Texture;
43 import com.jme3.texture.Texture.WrapMode;
44 
45 public class HelloTerrain extends SimpleApplication {
46 
47   private TerrainQuad terrain;
48   Material mat_terrain;
49 
main(String[] args)50   public static void main(String[] args) {
51     HelloTerrain app = new HelloTerrain();
52     app.start();
53   }
54 
55   @Override
simpleInitApp()56   public void simpleInitApp() {
57     flyCam.setMoveSpeed(50);
58 
59     /** 1. Create terrain material and load four textures into it. */
60     mat_terrain = new Material(assetManager,
61             "Common/MatDefs/Terrain/Terrain.j3md");
62 
63     /** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
64     mat_terrain.setTexture("Alpha", assetManager.loadTexture(
65             "Textures/Terrain/splat/alphamap.png"));
66 
67     /** 1.2) Add GRASS texture into the red layer (Tex1). */
68     Texture grass = assetManager.loadTexture(
69             "Textures/Terrain/splat/grass.jpg");
70     grass.setWrap(WrapMode.Repeat);
71     mat_terrain.setTexture("Tex1", grass);
72     mat_terrain.setFloat("Tex1Scale", 64f);
73 
74     /** 1.3) Add DIRT texture into the green layer (Tex2) */
75     Texture dirt = assetManager.loadTexture(
76             "Textures/Terrain/splat/dirt.jpg");
77     dirt.setWrap(WrapMode.Repeat);
78     mat_terrain.setTexture("Tex2", dirt);
79     mat_terrain.setFloat("Tex2Scale", 32f);
80 
81     /** 1.4) Add ROAD texture into the blue layer (Tex3) */
82     Texture rock = assetManager.loadTexture(
83             "Textures/Terrain/splat/road.jpg");
84     rock.setWrap(WrapMode.Repeat);
85     mat_terrain.setTexture("Tex3", rock);
86     mat_terrain.setFloat("Tex3Scale", 128f);
87 
88     /** 2. Create the height map */
89     AbstractHeightMap heightmap = null;
90     Texture heightMapImage = assetManager.loadTexture(
91             "Textures/Terrain/splat/mountains512.png");
92     heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
93     heightmap.load();
94 
95     /** 3. We have prepared material and heightmap.
96      * Now we create the actual terrain:
97      * 3.1) Create a TerrainQuad and name it "my terrain".
98      * 3.2) A good value for terrain tiles is 64x64 -- so we supply 64+1=65.
99      * 3.3) We prepared a heightmap of size 512x512 -- so we supply 512+1=513.
100      * 3.4) As LOD step scale we supply Vector3f(1,1,1).
101      * 3.5) We supply the prepared heightmap itself.
102      */
103     int patchSize = 65;
104     terrain = new TerrainQuad("my terrain", patchSize, 513, heightmap.getHeightMap());
105 
106     /** 4. We give the terrain its material, position & scale it, and attach it. */
107     terrain.setMaterial(mat_terrain);
108     terrain.setLocalTranslation(0, -100, 0);
109     terrain.setLocalScale(2f, 1f, 2f);
110     rootNode.attachChild(terrain);
111 
112     /** 5. The LOD (level of detail) depends on were the camera is: */
113     TerrainLodControl control = new TerrainLodControl(terrain, getCamera());
114     control.setLodCalculator( new DistanceLodCalculator(patchSize, 2.7f) ); // patch size, and a multiplier
115     terrain.addControl(control);
116   }
117 }
118