1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package com.jme3.terrain.geomipmap.grid;
6
7
8import com.jme3.asset.AssetManager;
9import com.jme3.export.JmeExporter;
10import com.jme3.export.JmeImporter;
11import com.jme3.math.Vector3f;
12import com.jme3.terrain.geomipmap.TerrainGridTileLoader;
13import com.jme3.terrain.geomipmap.TerrainQuad;
14import com.jme3.terrain.heightmap.AbstractHeightMap;
15import com.jme3.terrain.heightmap.HeightMap;
16import java.io.IOException;
17import java.nio.FloatBuffer;
18import com.jme3.terrain.noise.Basis;
19
20/**
21 *
22 * @author Anthyon, normenhansen
23 */
24public class FractalTileLoader implements TerrainGridTileLoader{
25
26    public class FloatBufferHeightMap extends AbstractHeightMap {
27
28        private final FloatBuffer buffer;
29
30        public FloatBufferHeightMap(FloatBuffer buffer) {
31            this.buffer = buffer;
32        }
33
34        @Override
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
47    public FractalTileLoader(Basis base, float heightScale) {
48        this.base = base;
49        this.heightScale = heightScale;
50    }
51
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
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
72    public void setPatchSize(int patchSize) {
73        this.patchSize = patchSize;
74    }
75
76    public void setQuadSize(int quadSize) {
77        this.quadSize = quadSize;
78    }
79
80    public void write(JmeExporter ex) throws IOException {
81        //TODO: serialization
82    }
83
84    public void read(JmeImporter im) throws IOException {
85        //TODO: serialization
86    }
87}
88