• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * To change this template, choose Tools | Templates
3  * and open the template in the editor.
4  */
5 package com.jme3.terrain.geomipmap.grid;
6 
7 import com.jme3.asset.AssetManager;
8 import com.jme3.asset.AssetNotFoundException;
9 import com.jme3.asset.TextureKey;
10 import com.jme3.export.JmeExporter;
11 import com.jme3.export.JmeImporter;
12 import com.jme3.math.Vector3f;
13 import com.jme3.terrain.geomipmap.TerrainGridTileLoader;
14 import com.jme3.terrain.geomipmap.TerrainQuad;
15 import com.jme3.terrain.heightmap.*;
16 import com.jme3.texture.Texture;
17 import java.io.IOException;
18 import java.util.logging.Level;
19 import java.util.logging.Logger;
20 
21 /**
22  *
23  * @author Anthyon, normenhansen
24  */
25 public class ImageTileLoader implements TerrainGridTileLoader{
26     private static final Logger logger = Logger.getLogger(ImageTileLoader.class.getName());
27     private final AssetManager assetManager;
28     private final Namer namer;
29     private int patchSize;
30     private int quadSize;
31     private float heightScale = 1;
32     //private int imageType = BufferedImage.TYPE_USHORT_GRAY; // 16 bit grayscale
33     //private ImageHeightmap customImageHeightmap;
34 
ImageTileLoader(final String textureBase, final String textureExt, AssetManager assetManager)35     public ImageTileLoader(final String textureBase, final String textureExt, AssetManager assetManager) {
36         this(assetManager, new Namer() {
37 
38             public String getName(int x, int y) {
39                 return textureBase + "_" + x + "_" + y + "." + textureExt;
40             }
41         });
42     }
43 
ImageTileLoader(AssetManager assetManager, Namer namer)44     public ImageTileLoader(AssetManager assetManager, Namer namer) {
45         this.assetManager = assetManager;
46         this.namer = namer;
47     }
48 
49     /**
50      * Effects vertical scale of the height of the terrain when loaded.
51      */
setHeightScale(float heightScale)52     public void setHeightScale(float heightScale) {
53         this.heightScale = heightScale;
54     }
55 
56 
57     /**
58      * Lets you specify the type of images that are being loaded. All images
59      * must be the same type.
60      * @param imageType eg. BufferedImage.TYPE_USHORT_GRAY
61      */
62     /*public void setImageType(int imageType) {
63         this.imageType = imageType;
64     }*/
65 
66     /**
67      * The ImageHeightmap that will parse the image type that you
68      * specify with setImageType().
69      * @param customImageHeightmap must extend AbstractHeightmap
70      */
71     /*public void setCustomImageHeightmap(ImageHeightmap customImageHeightmap) {
72         if (!(customImageHeightmap instanceof AbstractHeightMap)) {
73             throw new IllegalArgumentException("customImageHeightmap must be an AbstractHeightMap!");
74         }
75         this.customImageHeightmap = customImageHeightmap;
76     }*/
77 
getHeightMapAt(Vector3f location)78     private HeightMap getHeightMapAt(Vector3f location) {
79         // HEIGHTMAP image (for the terrain heightmap)
80         int x = (int) location.x;
81         int z = (int) location.z;
82 
83         AbstractHeightMap heightmap = null;
84         //BufferedImage im = null;
85 
86         String name = null;
87         try {
88             name = namer.getName(x, z);
89             logger.log(Level.INFO, "Loading heightmap from file: {0}", name);
90             final Texture texture = assetManager.loadTexture(new TextureKey(name));
91             heightmap = new ImageBasedHeightMap(texture.getImage());
92             /*if (assetInfo != null){
93                 InputStream in = assetInfo.openStream();
94                 im = ImageIO.read(in);
95             } else {
96                 im = new BufferedImage(patchSize, patchSize, imageType);
97                 logger.log(Level.WARNING, "File: {0} not found, loading zero heightmap instead", name);
98             }*/
99             // CREATE HEIGHTMAP
100             /*if (imageType == BufferedImage.TYPE_USHORT_GRAY) {
101                 heightmap = new Grayscale16BitHeightMap(im);
102             } else if (imageType == BufferedImage.TYPE_3BYTE_BGR) {
103                 heightmap = new ImageBasedHeightMap(im);
104             } else if (customImageHeightmap != null && customImageHeightmap instanceof AbstractHeightMap) {
105                 // If it gets here, it means you have specified a different image type, and you must
106                 // then also supply a custom image heightmap class that can parse that image into
107                 // a heightmap.
108                 customImageHeightmap.setImage(im);
109                 heightmap = (AbstractHeightMap) customImageHeightmap;
110             } else {
111                 // error, no supported image format and no custom image heightmap specified
112                 if (customImageHeightmap == null)
113                     logger.log(Level.SEVERE, "Custom image type specified [{0}] but no customImageHeightmap declared! Use setCustomImageHeightmap()",imageType);
114                 if (!(customImageHeightmap instanceof AbstractHeightMap))
115                     logger.severe("customImageHeightmap must be an AbstractHeightMap!");
116                 return null;
117             }*/
118             heightmap.setHeightScale(1);
119             heightmap.load();
120         //} catch (IOException e) {
121         //    e.printStackTrace();
122         } catch (AssetNotFoundException e) {
123             logger.log(Level.WARNING, "Asset {0} not found, loading zero heightmap instead", name);
124         }
125         return heightmap;
126     }
127 
setSize(int size)128     public void setSize(int size) {
129         this.patchSize = size - 1;
130     }
131 
getTerrainQuadAt(Vector3f location)132     public TerrainQuad getTerrainQuadAt(Vector3f location) {
133         HeightMap heightMapAt = getHeightMapAt(location);
134         TerrainQuad q = new TerrainQuad("Quad" + location, patchSize, quadSize, heightMapAt == null ? null : heightMapAt.getHeightMap());
135         return q;
136     }
137 
setPatchSize(int patchSize)138     public void setPatchSize(int patchSize) {
139         this.patchSize = patchSize;
140     }
141 
setQuadSize(int quadSize)142     public void setQuadSize(int quadSize) {
143         this.quadSize = quadSize;
144     }
145 
write(JmeExporter ex)146     public void write(JmeExporter ex) throws IOException {
147         //TODO: serialization
148     }
149 
read(JmeImporter im)150     public void read(JmeImporter im) throws IOException {
151         //TODO: serialization
152     }
153 }
154