Browse Source

fixed add terrain bug, now they all use the same action

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@8022 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
bre..ns 14 years ago
parent
commit
1c6f5fa455

+ 44 - 36
jme3-terrain-editor/src/com/jme3/gde/terraineditor/AddTerrainAction.java

@@ -13,6 +13,7 @@ import com.jme3.gde.core.sceneexplorer.nodes.actions.NewSpatialAction;
 import com.jme3.renderer.Camera;
 import com.jme3.scene.Node;
 import com.jme3.scene.Spatial;
+import com.jme3.terrain.Terrain;
 import com.jme3.terrain.geomipmap.TerrainLodControl;
 import com.jme3.terrain.geomipmap.TerrainQuad;
 import com.jme3.terrain.heightmap.AbstractHeightMap;
@@ -73,11 +74,11 @@ public class AddTerrainAction extends AbstractNewSpatialWizardAction {
         }
         return null;
     }
-
+    
     protected Spatial generateTerrain(Node parent, final WizardDescriptor wizardDescriptor) throws IOException {
         org.openide.nodes.Node selectedNode = (org.openide.nodes.Node) wizardDescriptor.getProperty("main_node");
         final Spatial spatial = selectedNode.getLookup().lookup(Spatial.class);
-        final ProjectAssetManager manager = selectedNode.getLookup().lookup(ProjectAssetManager.class);
+        
 
         String sceneName = selectedNode.getLookup().lookup(DataObject.class).getName();
 
@@ -92,45 +93,51 @@ public class AddTerrainAction extends AbstractNewSpatialWizardAction {
             heightmapData = heightmap.getHeightMap();
         }
 
-        // eg. Scenes/newScene1.j3o
-        TerrainQuad terrain = null;
-
-        terrain = new TerrainQuad("terrain-" + sceneName, patchSize, totalSize, heightmapData); //TODO make this pluggable for different Terrain implementations
+        return doCreateTerrain(parent, totalSize, patchSize, alphaTextureSize, heightmapData, sceneName, selectedNode);
+    }
+    
+    
+    protected Spatial doCreateTerrain(Node parent,
+                                    int totalSize,
+                                    int patchSize,
+                                    int alphaTextureSize,
+                                    float[] heightmapData,
+                                    String sceneName,
+                                    org.openide.nodes.Node selectedNode) throws IOException
+    {
+        final ProjectAssetManager manager = selectedNode.getLookup().lookup(ProjectAssetManager.class);
+        
+        Terrain terrain = new TerrainQuad("terrain-"+sceneName, patchSize, totalSize, heightmapData); //TODO make this pluggable for different Terrain implementations
         com.jme3.material.Material mat = new com.jme3.material.Material(manager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
 
         String assetFolder = "";
-        if (manager != null && manager instanceof ProjectAssetManager) {
-            assetFolder = ((ProjectAssetManager) manager).getAssetFolderName();
-        }
+        if (manager != null && manager instanceof ProjectAssetManager)
+            assetFolder = ((ProjectAssetManager)manager).getAssetFolderName();
 
         // write out 3 alpha blend images
-        for (int i = 0; i < TerrainEditorController.NUM_ALPHA_TEXTURES; i++) {
+        for (int i=0; i<TerrainEditorController.NUM_ALPHA_TEXTURES; i++) {
             BufferedImage alphaBlend = new BufferedImage(alphaTextureSize, alphaTextureSize, BufferedImage.TYPE_INT_ARGB);
             if (i == 0) {
                 // the first alpha level should be opaque so we see the first texture over the whole terrain
-                for (int h = 0; h < alphaTextureSize; h++) {
-                    for (int w = 0; w < alphaTextureSize; w++) {
+                for (int h=0; h<alphaTextureSize; h++)
+                    for (int w=0; w<alphaTextureSize; w++)
                         alphaBlend.setRGB(w, h, 0x00FF0000);//argb
-                    }
-                }
             }
-            File alphaFolder = new File(assetFolder + "/Textures/terrain-alpha/");
-            if (!alphaFolder.exists()) {
+            File alphaFolder = new File(assetFolder+"/Textures/terrain-alpha/");
+            if (!alphaFolder.exists())
                 alphaFolder.mkdir();
-            }
-            String alphaBlendFileName = "/Textures/terrain-alpha/" + sceneName + "-" + terrain.getName() + "-alphablend" + i + ".png";
-            File alphaImageFile = new File(assetFolder + alphaBlendFileName);
+            String alphaBlendFileName = "/Textures/terrain-alpha/"+sceneName+"-"+((Node)terrain).getName()+"-alphablend"+i+".png";
+            File alphaImageFile = new File(assetFolder+alphaBlendFileName);
             ImageIO.write(alphaBlend, "png", alphaImageFile);
             Texture tex = manager.loadAsset(new TextureKey(alphaBlendFileName, false));
-            if (i == 0) {
+            if (i == 0)
                 mat.setTexture("AlphaMap", tex);
-            }
-            /*else if (i == 1) // add these in when they are supported
-            mat.setTexture("AlphaMap_1", tex);
+            else if (i == 1)
+                mat.setTexture("AlphaMap_1", tex);
             else if (i == 2)
-            mat.setTexture("AlphaMap_2", tex);*/
+                mat.setTexture("AlphaMap_2", tex);
         }
-
+        
         // give the first layer default texture
         Texture defaultTexture = manager.loadTexture(TerrainEditorController.DEFAULT_TERRAIN_TEXTURE);
         defaultTexture.setWrap(WrapMode.Repeat);
@@ -138,21 +145,22 @@ public class AddTerrainAction extends AbstractNewSpatialWizardAction {
         mat.setFloat("DiffuseMap_0_scale", TerrainEditorController.DEFAULT_TEXTURE_SCALE);
         mat.setBoolean("WardIso", true);
 
-        terrain.setMaterial(mat);
-        terrain.setModelBound(new BoundingBox());
-        terrain.updateModelBound();
-        terrain.setLocalTranslation(0, 0, 0);
-        terrain.setLocalScale(1f, 1f, 1f);
+        ((Node)terrain).setMaterial(mat);
+        ((Node)terrain).setModelBound(new BoundingBox());
+        ((Node)terrain).updateModelBound();
+        ((Node)terrain).setLocalTranslation(0, 0, 0);
+        ((Node)terrain).setLocalScale(4f, 1f, 4f);
 
         // add the lod control
-        List<Camera> cameras = new ArrayList<Camera>();
-        cameras.add(SceneApplication.getApplication().getCamera());
-        TerrainLodControl control = new TerrainLodControl(terrain, cameras);
-        //terrain.addControl(control); // removing this until we figure out a way to have it get the cameras when saved/loaded
+        TerrainLodControl control = new TerrainLodControl(terrain, SceneApplication.getApplication().getCamera());
+	((Node)terrain).addControl(control);
 
-        return terrain;
-        //createTerrainButton.setEnabled(false); // only let the user add one terrain
+        parent.attachChild((Node)terrain);
 
+        //setNeedsSave(true);
+        //addSpatialUndo(parent, (Node)terrain, jmeNodeParent);
+        
+        return (Spatial)terrain;
     }
 
     /**

+ 7 - 5
jme3-terrain-editor/src/com/jme3/gde/terraineditor/TerrainEditorController.java

@@ -629,7 +629,9 @@ public class TerrainEditorController {
             Terrain terrain =
             SceneApplication.getApplication().enqueue(new Callable<Terrain>() {
                 public Terrain call() throws Exception {
-                    return doCreateTerrain(parent, totalSize, patchSize, alphaTextureSize, heightmapData, sceneName, jmeNodeParent);
+                    //return doCreateTerrain(parent, totalSize, patchSize, alphaTextureSize, heightmapData, sceneName, jmeNodeParent);
+                    AddTerrainAction a = new AddTerrainAction();
+                    return (Terrain) a.doCreateTerrain(parent, totalSize, patchSize, alphaTextureSize, heightmapData, sceneName, jmeRootNode);
                 }
             }).get();
             return terrain;
@@ -638,11 +640,11 @@ public class TerrainEditorController {
         } catch (ExecutionException ex) {
             Exceptions.printStackTrace(ex);
         }
-        //doCreateTerrain(totalSize, patchSize, alphaTextureSize, heightmapData, sceneName, defaultTextureScale);
+        
         return null; // if failed
     }
 
-    private Terrain doCreateTerrain(Node parent,
+    /*private Terrain doCreateTerrain(Node parent,
                                     int totalSize,
                                     int patchSize,
                                     int alphaTextureSize,
@@ -677,7 +679,7 @@ public class TerrainEditorController {
             Texture tex = manager.loadAsset(new TextureKey(alphaBlendFileName, false));
             if (i == 0)
                 mat.setTexture("AlphaMap", tex);
-            else if (i == 1) // add these in when they are supported
+            else if (i == 1)
                 mat.setTexture("AlphaMap_1", tex);
             else if (i == 2)
                 mat.setTexture("AlphaMap_2", tex);
@@ -707,7 +709,7 @@ public class TerrainEditorController {
         addSpatialUndo(parent, (Node)terrain, jmeNodeParent);
         
         return terrain;
-    }
+    }*/
 
     private void addSpatialUndo(final Node undoParent, final Spatial undoSpatial, final AbstractSceneExplorerNode parentNode) {
         //add undo