From 77a1ec09f865e49cf152b5ce84cb7950cfb0b1b2 Mon Sep 17 00:00:00 2001
From: "bre..om" <brentowens@gmail.com>
Date: Sun, 30 Sep 2012 23:41:57 +0000
Subject: [PATCH] moved terrainGrid normal recalculations out of the tile
 loading loop to run after all tiles have loaded

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9788 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
---
 .../jme3/terrain/geomipmap/TerrainGrid.java   | 29 +++++++++++++++++--
 .../jme3/terrain/geomipmap/TerrainQuad.java   | 20 ++++++++-----
 2 files changed, 39 insertions(+), 10 deletions(-)

diff --git a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java
index adb02ac45..ab146f3cc 100644
--- a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java
+++ b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainGrid.java
@@ -196,6 +196,22 @@ public class TerrainGrid extends TerrainQuad {
                 }
             }
 
+            getControl(UpdateControl.class).enqueue(new Callable() {
+                    // back on the OpenGL thread:
+                    public Object call() throws Exception {
+                        for (Spatial s : getChildren()) {
+                            if (s instanceof TerrainQuad) {
+                                TerrainQuad tq = (TerrainQuad)s;
+                                tq.resetCachedNeighbours();
+                            }
+                        }
+                        System.out.println("fixed normals "+location.clone().mult(size));
+                        setNeedToRecalculateNormals();
+                        //fixNormalEdges(new BoundingBox(location.clone().mult(size), size*2, Float.MAX_VALUE, size*2));
+                        // the edges are fixed once, but not with lod change
+                        return null;
+                    }
+            });
         }
     }
 
@@ -229,6 +245,9 @@ public class TerrainGrid extends TerrainQuad {
         terrainQuadGrid.setPatchSize(this.patchSize);
         terrainQuadGrid.setQuadSize(this.quadSize);
         addControl(new UpdateControl());
+        
+        fixNormalEdges(new BoundingBox(new Vector3f(0,0,0), size*2, Float.MAX_VALUE, size*2));
+        addControl(new NormalRecalcControl(this));
     }
 
     public TerrainGrid(String name, int patchSize, int maxVisibleSize, Vector3f scale, TerrainGridTileLoader terrainQuadGrid) {
@@ -252,6 +271,9 @@ public class TerrainGrid extends TerrainQuad {
         this.heightMapGrid = heightMapGrid;
         heightMapGrid.setSize(this.quadSize);
         addControl(new UpdateControl());
+        
+        fixNormalEdges(new BoundingBox(new Vector3f(0,0,0), size*2, Float.MAX_VALUE, size*2));
+        addControl(new NormalRecalcControl(this));
     }
 
     @Deprecated
@@ -343,13 +365,16 @@ public class TerrainGrid extends TerrainQuad {
         }
         updateModelBound();
         
-        for (Spatial s : getChildren()) {
+        /*for (Spatial s : getChildren()) {
             if (s instanceof TerrainQuad) {
                 TerrainQuad tq = (TerrainQuad)s;
                 tq.resetCachedNeighbours();
-                tq.fixNormalEdges(new BoundingBox(tq.getWorldTranslation(), totalSize*2, Float.MAX_VALUE, totalSize*2));
             }
         }
+        
+        System.out.println("fix normals "+loc);
+        fixNormalEdges(new BoundingBox(loc, totalSize*6, Float.MAX_VALUE, totalSize*6));
+        */
     }
 
     
diff --git a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java
index 1df3087a7..0c2f6466c 100644
--- a/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java
+++ b/engine/src/terrain/com/jme3/terrain/geomipmap/TerrainQuad.java
@@ -123,6 +123,10 @@ public class TerrainQuad extends Node implements Terrain {
      */
     public TerrainQuad(String name, int patchSize, int totalSize, float[] heightMap) {
         this(name, patchSize, totalSize, Vector3f.UNIT_XYZ, heightMap);
+                
+        affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), size*2, Float.MAX_VALUE, size*2);
+        fixNormalEdges(affectedAreaBBox);
+        addControl(new NormalRecalcControl(this));
     }
     
     /**
@@ -153,9 +157,9 @@ public class TerrainQuad extends Node implements Terrain {
     @Deprecated
     public TerrainQuad(String name, int patchSize, int size, Vector3f scale, float[] heightMap) {
         this(name, patchSize, size, scale, heightMap, size, new Vector2f(), 0);
-        affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), size*2, Float.MAX_VALUE, size*2);
-        fixNormalEdges(affectedAreaBBox);
-        addControl(new NormalRecalcControl(this));
+        //affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), size*2, Float.MAX_VALUE, size*2);
+        //fixNormalEdges(affectedAreaBBox);
+        //addControl(new NormalRecalcControl(this));
     }
     
     /**
@@ -172,9 +176,9 @@ public class TerrainQuad extends Node implements Terrain {
     @Deprecated
     public TerrainQuad(String name, int patchSize, int totalSize, int quadSize, Vector3f scale, float[] heightMap) {
         this(name, patchSize, quadSize, scale, heightMap, totalSize, new Vector2f(), 0);
-        affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), totalSize*2, Float.MAX_VALUE, totalSize*2);
-        fixNormalEdges(affectedAreaBBox);
-        addControl(new NormalRecalcControl(this));
+        //affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), totalSize*2, Float.MAX_VALUE, totalSize*2);
+        //fixNormalEdges(affectedAreaBBox);
+        //addControl(new NormalRecalcControl(this));
     }
 
     protected TerrainQuad(String name, int patchSize, int quadSize,
@@ -851,7 +855,7 @@ public class TerrainQuad extends Node implements Terrain {
         if (affectedAreaBBox != null)
             return true;
         if (!lastScale.equals(getWorldScale())) {
-            affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), size, Float.MAX_VALUE, size);
+            affectedAreaBBox = new BoundingBox(getWorldTranslation(), Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
             lastScale = getWorldScale();
             return true;
         }
@@ -862,7 +866,7 @@ public class TerrainQuad extends Node implements Terrain {
      * This will cause all normals for this terrain quad to be recalculated
      */
     protected void setNeedToRecalculateNormals() {
-        affectedAreaBBox = new BoundingBox(new Vector3f(0,0,0), size*2, Float.MAX_VALUE, size*2);
+        affectedAreaBBox = new BoundingBox(getWorldTranslation(), Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
     }
 
     public float getHeightmapHeight(Vector2f xz) {