Home | History | Annotate | Download | only in export
      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 
     33 package jme3test.export;
     34 
     35 import com.jme3.app.SimpleApplication;
     36 import com.jme3.asset.AssetKey;
     37 import com.jme3.asset.ModelKey;
     38 import com.jme3.export.binary.BinaryExporter;
     39 import com.jme3.export.binary.BinaryImporter;
     40 import com.jme3.light.DirectionalLight;
     41 import com.jme3.light.PointLight;
     42 import com.jme3.material.Material;
     43 import com.jme3.math.ColorRGBA;
     44 import com.jme3.math.FastMath;
     45 import com.jme3.math.Vector3f;
     46 import com.jme3.scene.AssetLinkNode;
     47 import com.jme3.scene.Geometry;
     48 import com.jme3.scene.Node;
     49 import com.jme3.scene.Spatial;
     50 import com.jme3.scene.shape.Sphere;
     51 import java.io.ByteArrayInputStream;
     52 import java.io.ByteArrayOutputStream;
     53 import java.io.IOException;
     54 import java.util.logging.Level;
     55 import java.util.logging.Logger;
     56 
     57 public class TestAssetLinkNode extends SimpleApplication {
     58 
     59     float angle;
     60     PointLight pl;
     61     Spatial lightMdl;
     62 
     63     public static void main(String[] args){
     64         TestAssetLinkNode app = new TestAssetLinkNode();
     65         app.start();
     66     }
     67 
     68     @Override
     69     public void simpleInitApp() {
     70         AssetLinkNode loaderNode=new AssetLinkNode();
     71         loaderNode.addLinkedChild(new ModelKey("Models/MonkeyHead/MonkeyHead.mesh.xml"));
     72         //load/attach the children (happens automatically on load)
     73 //        loaderNode.attachLinkedChildren(assetManager);
     74 //        rootNode.attachChild(loaderNode);
     75 
     76         //save and load the loaderNode
     77         try {
     78             //export to byte array
     79             ByteArrayOutputStream bout=new ByteArrayOutputStream();
     80             BinaryExporter.getInstance().save(loaderNode, bout);
     81             //import from byte array, automatically loads the monkeyhead from file
     82             ByteArrayInputStream bin=new ByteArrayInputStream(bout.toByteArray());
     83             BinaryImporter imp=BinaryImporter.getInstance();
     84             imp.setAssetManager(assetManager);
     85             Node newLoaderNode=(Node)imp.load(bin);
     86             //attach to rootNode
     87             rootNode.attachChild(newLoaderNode);
     88         } catch (IOException ex) {
     89             Logger.getLogger(TestAssetLinkNode.class.getName()).log(Level.SEVERE, null, ex);
     90         }
     91 
     92 
     93         rootNode.attachChild(loaderNode);
     94 
     95         lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
     96         lightMdl.setMaterial( (Material) assetManager.loadAsset(new AssetKey("Common/Materials/RedColor.j3m")));
     97         rootNode.attachChild(lightMdl);
     98 
     99         // flourescent main light
    100         pl = new PointLight();
    101         pl.setColor(new ColorRGBA(0.88f, 0.92f, 0.95f, 1.0f));
    102         rootNode.addLight(pl);
    103 
    104         // sunset light
    105         DirectionalLight dl = new DirectionalLight();
    106         dl.setDirection(new Vector3f(-0.1f,-0.7f,1).normalizeLocal());
    107         dl.setColor(new ColorRGBA(0.44f, 0.30f, 0.20f, 1.0f));
    108         rootNode.addLight(dl);
    109 
    110         // skylight
    111         dl = new DirectionalLight();
    112         dl.setDirection(new Vector3f(-0.6f,-1,-0.6f).normalizeLocal());
    113         dl.setColor(new ColorRGBA(0.10f, 0.22f, 0.44f, 1.0f));
    114         rootNode.addLight(dl);
    115 
    116         // white ambient light
    117         dl = new DirectionalLight();
    118         dl.setDirection(new Vector3f(1, -0.5f,-0.1f).normalizeLocal());
    119         dl.setColor(new ColorRGBA(0.50f, 0.40f, 0.50f, 1.0f));
    120         rootNode.addLight(dl);
    121     }
    122 
    123     @Override
    124     public void simpleUpdate(float tpf){
    125         angle += tpf * 0.25f;
    126         angle %= FastMath.TWO_PI;
    127 
    128         pl.setPosition(new Vector3f(FastMath.cos(angle) * 6f, 3f, FastMath.sin(angle) * 6f));
    129         lightMdl.setLocalTranslation(pl.getPosition());
    130     }
    131 
    132 }
    133