Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed memory leak in terrains, optimized updating normals. #696

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -47,17 +47,12 @@
import com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator;
import com.jme3.terrain.geomipmap.lodcalc.LodCalculator;
import com.jme3.util.clone.Cloner;
import com.jme3.util.clone.JmeCloneable;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -297,27 +292,20 @@ private List<Vector3f> cloneVectorList(List<Vector3f> locations) {
return cloned;
}





@Override
public Object jmeClone() {
if (spatial instanceof Terrain) {
TerrainLodControl cloned = new TerrainLodControl((Terrain) spatial, cameras);
cloned.setLodCalculator(lodCalculator.clone());
cloned.spatial = spatial;
return cloned;
try {
return super.clone();
} catch (final CloneNotSupportedException e) {
throw new RuntimeException(e);
}
return null;
}

@Override
public void cloneFields( Cloner cloner, Object original ) {
super.cloneFields(cloner, original);

this.lodCalculator = cloner.clone(lodCalculator);

try {
// Not deep clone of the cameras themselves
this.cameras = cloner.javaClone(cameras);
Expand Down Expand Up @@ -452,5 +440,4 @@ public void read(JmeImporter im) throws IOException {
terrain = (Terrain) ic.readSavable("terrain", null);
lodCalculator = (LodCalculator) ic.readSavable("lodCalculator", new DistanceLodCalculator());
}

}
Expand Up @@ -146,7 +146,7 @@ public TerrainPatch(String name, int size) {
* the origin offset of the patch.
*/
public TerrainPatch(String name, int size, Vector3f stepScale,
float[] heightMap, Vector3f origin) {
float[] heightMap, Vector3f origin) {
this(name, size, stepScale, heightMap, origin, size, new Vector2f(), 0);
}

Expand Down Expand Up @@ -174,8 +174,8 @@ public TerrainPatch(String name, int size, Vector3f stepScale,
* the total offset amount. Used for texture coordinates.
*/
public TerrainPatch(String name, int size, Vector3f stepScale,
float[] heightMap, Vector3f origin, int totalSize,
Vector2f offset, float offsetAmount) {
float[] heightMap, Vector3f origin, int totalSize,
Vector2f offset, float offsetAmount) {
super(name);
setBatchHint(BatchHint.Never);
this.size = size;
Expand Down Expand Up @@ -332,15 +332,32 @@ protected void setHeight(List<LocationHeight> locationHeights, boolean overrideH
* recalculate all of the normal vectors in this terrain patch
*/
protected void updateNormals() {
FloatBuffer newNormalBuffer = geomap.writeNormalArray(null, getWorldScale());
getMesh().getBuffer(Type.Normal).updateData(newNormalBuffer);
FloatBuffer newTangentBuffer = null;
FloatBuffer newBinormalBuffer = null;
FloatBuffer[] tb = geomap.writeTangentArray(newNormalBuffer, newTangentBuffer, newBinormalBuffer, (FloatBuffer)getMesh().getBuffer(Type.TexCoord).getData(), getWorldScale());
newTangentBuffer = tb[0];
newBinormalBuffer = tb[1];
getMesh().getBuffer(Type.Tangent).updateData(newTangentBuffer);
getMesh().getBuffer(Type.Binormal).updateData(newBinormalBuffer);

final Mesh mesh = getMesh();
final VertexBuffer normalVertexBuffer = mesh.getBuffer(Type.Normal);
final VertexBuffer tangentVertexBuffer = mesh.getBuffer(Type.Tangent);
final VertexBuffer binormalVertexBuffer = mesh.getBuffer(Type.Binormal);
final VertexBuffer textCoordsVertexBuffer = mesh.getBuffer(Type.TexCoord);

final FloatBuffer textCoordsData = (FloatBuffer) textCoordsVertexBuffer.getData();

FloatBuffer normalData = (FloatBuffer) normalVertexBuffer.getData();
normalData.clear();
FloatBuffer tangentData = (FloatBuffer) tangentVertexBuffer.getData();
tangentData.clear();
FloatBuffer binormalData = (FloatBuffer) binormalVertexBuffer.getData();
binormalData.clear();

normalData = geomap.writeNormalArray(normalData, getWorldScale());

FloatBuffer[] buffers = geomap.writeTangentArray(normalData, tangentData, binormalData, textCoordsData, getWorldScale());

tangentData = buffers[0];
binormalData = buffers[1];

normalVertexBuffer.updateData(normalData);
tangentVertexBuffer.updateData(tangentData);
binormalVertexBuffer.updateData(binormalData);
}

private void setInBuffer(Mesh mesh, int index, Vector3f normal, Vector3f tangent, Vector3f binormal) {
Expand Down Expand Up @@ -372,13 +389,13 @@ private void setInBuffer(Mesh mesh, int index, Vector3f normal, Vector3f tangent
* left to the right.
*/
protected void fixNormalEdges(TerrainPatch right,
TerrainPatch bottom,
TerrainPatch top,
TerrainPatch left,
TerrainPatch bottomRight,
TerrainPatch bottomLeft,
TerrainPatch topRight,
TerrainPatch topLeft)
TerrainPatch bottom,
TerrainPatch top,
TerrainPatch left,
TerrainPatch bottomRight,
TerrainPatch bottomLeft,
TerrainPatch topRight,
TerrainPatch topLeft)
{
Vector3f rootPoint = new Vector3f();
Vector3f rightPoint = new Vector3f();
Expand Down Expand Up @@ -1034,6 +1051,4 @@ protected void clearCaches() {
bottomNeighbour = null;
}
}


}