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 8 import com.jme3.asset.AssetManager; 9 import com.jme3.export.JmeExporter; 10 import com.jme3.export.JmeImporter; 11 import com.jme3.math.Vector3f; 12 import com.jme3.terrain.geomipmap.TerrainGridTileLoader; 13 import com.jme3.terrain.geomipmap.TerrainQuad; 14 import com.jme3.terrain.heightmap.AbstractHeightMap; 15 import com.jme3.terrain.heightmap.HeightMap; 16 import java.io.IOException; 17 import java.nio.FloatBuffer; 18 import com.jme3.terrain.noise.Basis; 19 20 /** 21 * 22 * @author Anthyon, normenhansen 23 */ 24 public class FractalTileLoader implements TerrainGridTileLoader{ 25 26 public class FloatBufferHeightMap extends AbstractHeightMap { 27 28 private final FloatBuffer buffer; 29 FloatBufferHeightMap(FloatBuffer buffer)30 public FloatBufferHeightMap(FloatBuffer buffer) { 31 this.buffer = buffer; 32 } 33 34 @Override load()35 public boolean load() { 36 this.heightData = this.buffer.array(); 37 return true; 38 } 39 40 } 41 42 private int patchSize; 43 private int quadSize; 44 private final Basis base; 45 private final float heightScale; 46 FractalTileLoader(Basis base, float heightScale)47 public FractalTileLoader(Basis base, float heightScale) { 48 this.base = base; 49 this.heightScale = heightScale; 50 } 51 getHeightMapAt(Vector3f location)52 private HeightMap getHeightMapAt(Vector3f location) { 53 AbstractHeightMap heightmap = null; 54 55 FloatBuffer buffer = this.base.getBuffer(location.x * (this.quadSize - 1), location.z * (this.quadSize - 1), 0, this.quadSize); 56 57 float[] arr = buffer.array(); 58 for (int i = 0; i < arr.length; i++) { 59 arr[i] = arr[i] * this.heightScale; 60 } 61 heightmap = new FloatBufferHeightMap(buffer); 62 heightmap.load(); 63 return heightmap; 64 } 65 getTerrainQuadAt(Vector3f location)66 public TerrainQuad getTerrainQuadAt(Vector3f location) { 67 HeightMap heightMapAt = getHeightMapAt(location); 68 TerrainQuad q = new TerrainQuad("Quad" + location, patchSize, quadSize, heightMapAt == null ? null : heightMapAt.getHeightMap()); 69 return q; 70 } 71 setPatchSize(int patchSize)72 public void setPatchSize(int patchSize) { 73 this.patchSize = patchSize; 74 } 75 setQuadSize(int quadSize)76 public void setQuadSize(int quadSize) { 77 this.quadSize = quadSize; 78 } 79 write(JmeExporter ex)80 public void write(JmeExporter ex) throws IOException { 81 //TODO: serialization 82 } 83 read(JmeImporter im)84 public void read(JmeImporter im) throws IOException { 85 //TODO: serialization 86 } 87 } 88