19159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira/* 2194d427ebcfc2133bda410e0e4c399250d9a6066Scott Kennedy * Copyright (c) 2009-2010 jMonkeyEngine 39159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * All rights reserved. 49159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * 59159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * Redistribution and use in source and binary forms, with or without 69159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * modification, are permitted provided that the following conditions are 79159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * met: 89159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * 99159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * * Redistributions of source code must retain the above copyright 109159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * notice, this list of conditions and the following disclaimer. 119159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * 129159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * * Redistributions in binary form must reproduce the above copyright 139159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * notice, this list of conditions and the following disclaimer in the 149159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * documentation and/or other materials provided with the distribution. 159159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * 169159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 179159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * may be used to endorse or promote products derived from this software 189159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * without specific prior written permission. 199159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * 20ffc0111fa742c72f2457cecc1116e4ea3c6c8c6eJin Cao * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 211d1ec857e5d050163cb87936e233b5f9cf321344Mindy Pereira * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 221d1ec857e5d050163cb87936e233b5f9cf321344Mindy Pereira * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2320c9d620e79ae28994856541761a951074551518Mindy Pereira * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 241d1ec857e5d050163cb87936e233b5f9cf321344Mindy Pereira * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 259159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 261d1ec857e5d050163cb87936e233b5f9cf321344Mindy Pereira * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 271d1ec857e5d050163cb87936e233b5f9cf321344Mindy Pereira * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 2852c441e2c03e0f48572348953b985a4bf989c057Mindy Pereira * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 2922faaa6f1eacb6ae69e4fd5d97aeeefc77973cd1Mindy Pereira * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 312bfe48e4dda1e172f4c857a0ee1a99d924fb226fMindy Pereira */ 32b232e5c33e1e2879c7bc8e96a1b9c41c4fa41244Scott Kennedypackage com.jme3.terrain.geomipmap; 33cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira 34b232e5c33e1e2879c7bc8e96a1b9c41c4fa41244Scott Kennedyimport com.jme3.export.InputCapsule; 352bfe48e4dda1e172f4c857a0ee1a99d924fb226fMindy Pereiraimport com.jme3.export.JmeExporter; 36b10d1c652d0416c284d9792fc9a0a92b3acd51caKevin Linimport com.jme3.export.JmeImporter; 37b232e5c33e1e2879c7bc8e96a1b9c41c4fa41244Scott Kennedyimport com.jme3.export.OutputCapsule; 38e51c7226dfb6420b0d4c1a7bfdf3217ee4e560a6Minh Phamimport com.jme3.renderer.RenderManager; 39cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereiraimport com.jme3.renderer.ViewPort; 402bfe48e4dda1e172f4c857a0ee1a99d924fb226fMindy Pereiraimport com.jme3.scene.Spatial; 41b232e5c33e1e2879c7bc8e96a1b9c41c4fa41244Scott Kennedyimport com.jme3.scene.control.AbstractControl; 42cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereiraimport com.jme3.scene.control.Control; 439159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereiraimport java.io.IOException; 444db8cccf3332ad7c6fb1915f9f0f169953c3953aJin Cao 451852931de1e24e77cb708f4ba010eaa269426657Mindy Pereira 461650ef6bb75ba88340cc59d22b2578c0f460b4f1Scott Kennedy/** 47007a76baab414c9d432d31c661668b1bd07e3f80Mindy Pereira * Handles the normal vector updates when the terrain changes heights. 4878f38a09c9753c0ac1838ce0bfd3a6bc1a307ff7Scott Kennedy * @author bowens 49007a76baab414c9d432d31c661668b1bd07e3f80Mindy Pereira */ 50dc04cd76d8992ad3df98dce9be5c5b6540ab3619Mindy Pereirapublic class NormalRecalcControl extends AbstractControl { 51ffc0111fa742c72f2457cecc1116e4ea3c6c8c6eJin Cao 529159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira private TerrainQuad terrain; 5361b48ccefc655549802556947eb8cf3959c6ddadGilles Debunne 54cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira public NormalRecalcControl(){} 55cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira 56cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira public NormalRecalcControl(TerrainQuad terrain) { 570e128968f242866568bba0f833bb17ffda127f07Scott Kennedy this.terrain = terrain; 58cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira } 59cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira 60cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira @Override 61b88ee450829eb4ac24fb47c377b9ec3aab0782daMindy Pereira protected void controlUpdate(float tpf) { 62cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira terrain.updateNormals(); 633bb52162b7e842243d4a14e73c15b20dbd1804d7Mindy Pereira } 643bb52162b7e842243d4a14e73c15b20dbd1804d7Mindy Pereira 659159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira @Override 66cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira protected void controlRender(RenderManager rm, ViewPort vp) { 67fd68ea64d80f3b299b20ab72b6ed71d3c4ff6c9cMindy Pereira 68aa2afffe7aba707c2406f2e4503fa6037c4cd196Andy Stadler } 69e51c7226dfb6420b0d4c1a7bfdf3217ee4e560a6Minh Pham 701d1ec857e5d050163cb87936e233b5f9cf321344Mindy Pereira public Control cloneForSpatial(Spatial spatial) { 71cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira NormalRecalcControl control = new NormalRecalcControl(terrain); 72750e6e52d9e560d5fbf687f15bf388a947e98eb2Mindy Pereira control.setSpatial(spatial); 73fd68ea64d80f3b299b20ab72b6ed71d3c4ff6c9cMindy Pereira control.setEnabled(true); 74fd68ea64d80f3b299b20ab72b6ed71d3c4ff6c9cMindy Pereira return control; 75cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira } 76cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira 7761b48ccefc655549802556947eb8cf3959c6ddadGilles Debunne @Override 78c1fed8b74327fccbab18458aef2d1bb0a6996e9cMindy Pereira public void setSpatial(Spatial spatial) { 7904f06ea723b0445428d3f563334d653299a73a74Jin Cao super.setSpatial(spatial); 8004f06ea723b0445428d3f563334d653299a73a74Jin Cao if (spatial instanceof TerrainQuad) 8193364a61b0dcd90e19b96ea9b5900a77e2c4902eMindy Pereira this.terrain = (TerrainQuad)spatial; 8293364a61b0dcd90e19b96ea9b5900a77e2c4902eMindy Pereira } 83cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira 84cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira public TerrainQuad getTerrain() { 8580f4abfb682426384e88fb1dddc682be1c8a6c7fTom Taylor return terrain; 86093ecdd3cde3e54df3fb30f5ca47067fdcf46f3cmindyp } 8795a692517516f6cdcd77dd13582e679dd59fcef7Mindy Pereira 88cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira public void setTerrain(TerrainQuad terrain) { 89007a76baab414c9d432d31c661668b1bd07e3f80Mindy Pereira this.terrain = terrain; 909159cd236a0bd16c3ee632eb085c4e432909493cMindy Pereira } 91939f5f1fb5b2d851a5b36fc9908c2c6420b193aeJin Cao 92c1fed8b74327fccbab18458aef2d1bb0a6996e9cMindy Pereira @Override 93750e6e52d9e560d5fbf687f15bf388a947e98eb2Mindy Pereira public void write(JmeExporter ex) throws IOException { 947537f840506bcb642bed9dc1c2bdcf6d31c6b2a7Daisuke Miyakawa super.write(ex); 9516923ee63a79fce4be3f62b08bcd1f80617c1205mindyp OutputCapsule oc = ex.getCapsule(this); 96194d427ebcfc2133bda410e0e4c399250d9a6066Scott Kennedy oc.write(terrain, "terrain", null); 97194d427ebcfc2133bda410e0e4c399250d9a6066Scott Kennedy } 986c2a016fb00482339dc66b93822042f9f9e2424fJin Cao 99194d427ebcfc2133bda410e0e4c399250d9a6066Scott Kennedy @Override 100cd61195b9be5614aefc4cda76c1732cc4840b18eMindy Pereira public void read(JmeImporter im) throws IOException { 10176f1f2d4eb66956814254b0e4f86da7aa8261bcbAlon Albert super.read(im); 10276f1f2d4eb66956814254b0e4f86da7aa8261bcbAlon Albert InputCapsule ic = im.getCapsule(this); 10376f1f2d4eb66956814254b0e4f86da7aa8261bcbAlon Albert terrain = (TerrainQuad) ic.readSavable("terrain", null); 10476f1f2d4eb66956814254b0e4f86da7aa8261bcbAlon Albert } 10576f1f2d4eb66956814254b0e4f86da7aa8261bcbAlon Albert 10676f1f2d4eb66956814254b0e4f86da7aa8261bcbAlon Albert} 10776f1f2d4eb66956814254b0e4f86da7aa8261bcbAlon Albert