• 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 package com.jme3.terrain.geomipmap.lodcalc;
33 
34 import com.jme3.export.InputCapsule;
35 import com.jme3.export.JmeExporter;
36 import com.jme3.export.JmeImporter;
37 import com.jme3.export.OutputCapsule;
38 import com.jme3.math.Vector3f;
39 import com.jme3.terrain.geomipmap.TerrainPatch;
40 import com.jme3.terrain.geomipmap.UpdatedTerrainPatch;
41 import java.io.IOException;
42 import java.util.HashMap;
43 import java.util.List;
44 
45 /**
46  * Calculates the LOD of the terrain based on its distance from the
47  * cameras. Taking the minimum distance from all cameras.
48  *
49  * @author bowens
50  */
51 public class DistanceLodCalculator implements LodCalculator {
52 
53     private int size; // size of a terrain patch
54     private float lodMultiplier = 2;
55     private boolean turnOffLod = false;
56 
DistanceLodCalculator()57     public DistanceLodCalculator() {
58     }
59 
DistanceLodCalculator(int patchSize, float multiplier)60     public DistanceLodCalculator(int patchSize, float multiplier) {
61         this.size = patchSize;
62         this.lodMultiplier = multiplier;
63     }
64 
calculateLod(TerrainPatch terrainPatch, List<Vector3f> locations, HashMap<String, UpdatedTerrainPatch> updates)65     public boolean calculateLod(TerrainPatch terrainPatch, List<Vector3f> locations, HashMap<String, UpdatedTerrainPatch> updates) {
66         float distance = getCenterLocation(terrainPatch).distance(locations.get(0));
67 
68 
69         if (turnOffLod) {
70             // set to full detail
71             int prevLOD = terrainPatch.getLod();
72             UpdatedTerrainPatch utp = updates.get(terrainPatch.getName());
73             if (utp == null) {
74                 utp = new UpdatedTerrainPatch(terrainPatch, 0);
75                 updates.put(utp.getName(), utp);
76             }
77             utp.setNewLod(0);
78             utp.setPreviousLod(prevLOD);
79             utp.setReIndexNeeded(true);
80             return true;
81         }
82 
83         // go through each lod level to find the one we are in
84         for (int i = 0; i <= terrainPatch.getMaxLod(); i++) {
85             if (distance < getLodDistanceThreshold() * (i + 1)*terrainPatch.getWorldScale().x || i == terrainPatch.getMaxLod()) {
86                 boolean reIndexNeeded = false;
87                 if (i != terrainPatch.getLod()) {
88                     reIndexNeeded = true;
89                     //System.out.println("lod change: "+lod+" > "+i+"    dist: "+distance);
90                 }
91                 int prevLOD = terrainPatch.getLod();
92                 //previousLod = lod;
93                 //lod = i;
94                 UpdatedTerrainPatch utp = updates.get(terrainPatch.getName());
95                 if (utp == null) {
96                     utp = new UpdatedTerrainPatch(terrainPatch, i);//save in here, do not update actual variables
97                     updates.put(utp.getName(), utp);
98                 }
99                 utp.setPreviousLod(prevLOD);
100                 utp.setReIndexNeeded(reIndexNeeded);
101 
102                 return reIndexNeeded;
103             }
104         }
105 
106         return false;
107     }
108 
getCenterLocation(TerrainPatch terrainPatch)109     protected Vector3f getCenterLocation(TerrainPatch terrainPatch) {
110         Vector3f loc = terrainPatch.getWorldTranslation().clone();
111         loc.x += terrainPatch.getSize()*terrainPatch.getWorldScale().x / 2;
112         loc.z += terrainPatch.getSize()*terrainPatch.getWorldScale().z / 2;
113         return loc;
114     }
115 
write(JmeExporter ex)116     public void write(JmeExporter ex) throws IOException {
117         OutputCapsule oc = ex.getCapsule(this);
118         oc.write(size, "patchSize", 32);
119         oc.write(lodMultiplier, "lodMultiplier", 32);
120     }
121 
read(JmeImporter im)122     public void read(JmeImporter im) throws IOException {
123         InputCapsule ic = im.getCapsule(this);
124         size = ic.readInt("patchSize", 32);
125         lodMultiplier = ic.readFloat("lodMultiplier", 2.7f);
126     }
127 
128     @Override
clone()129     public LodCalculator clone() {
130         DistanceLodCalculator clone = new DistanceLodCalculator(size, lodMultiplier);
131         return clone;
132     }
133 
134     @Override
toString()135     public String toString() {
136         return "DistanceLodCalculator "+size+"*"+lodMultiplier;
137     }
138 
139     /**
140      * Gets the camera distance where the LOD level will change
141      */
getLodDistanceThreshold()142     protected float getLodDistanceThreshold() {
143         return size*lodMultiplier;
144     }
145 
146     /**
147      * Does this calculator require the terrain to have the difference of
148      * LOD levels of neighbours to be more than 1.
149      */
usesVariableLod()150     public boolean usesVariableLod() {
151         return false;
152     }
153 
getLodMultiplier()154     public float getLodMultiplier() {
155         return lodMultiplier;
156     }
157 
setLodMultiplier(float lodMultiplier)158     public void setLodMultiplier(float lodMultiplier) {
159         this.lodMultiplier = lodMultiplier;
160     }
161 
getSize()162     public int getSize() {
163         return size;
164     }
165 
setSize(int size)166     public void setSize(int size) {
167         this.size = size;
168     }
169 
turnOffLod()170     public void turnOffLod() {
171         turnOffLod = true;
172     }
173 
isLodOff()174     public boolean isLodOff() {
175         return turnOffLod;
176     }
177 
turnOnLod()178     public void turnOnLod() {
179         turnOffLod = false;
180     }
181 
182 }
183