Home | History | Annotate | Download | only in tools
      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.tools;
     34 
     35 import com.jme3.app.SimpleApplication;
     36 import com.jme3.bounding.BoundingBox;
     37 import com.jme3.light.DirectionalLight;
     38 import com.jme3.material.Material;
     39 import com.jme3.material.MaterialList;
     40 import com.jme3.math.ColorRGBA;
     41 import com.jme3.math.Vector3f;
     42 import com.jme3.post.SceneProcessor;
     43 import com.jme3.renderer.RenderManager;
     44 import com.jme3.renderer.ViewPort;
     45 import com.jme3.renderer.queue.RenderQueue;
     46 import com.jme3.scene.Geometry;
     47 import com.jme3.scene.Spatial;
     48 import com.jme3.scene.debug.WireBox;
     49 import com.jme3.scene.plugins.ogre.MeshLoader;
     50 import com.jme3.scene.plugins.ogre.OgreMeshKey;
     51 import com.jme3.texture.FrameBuffer;
     52 import java.util.ArrayList;
     53 import java.util.HashSet;
     54 import java.util.Set;
     55 import jme3tools.optimize.FastOctnode;
     56 import jme3tools.optimize.Octree;
     57 
     58 
     59 public class TestOctree extends SimpleApplication implements SceneProcessor {
     60 
     61     private Octree tree;
     62     private FastOctnode fastRoot;
     63     private Geometry[] globalGeoms;
     64     private BoundingBox octBox;
     65 
     66     private Set<Geometry> renderSet = new HashSet<Geometry>(300);
     67     private Material mat, mat2;
     68     private WireBox box = new WireBox(1,1,1);
     69 
     70     public static void main(String[] args){
     71         TestOctree app = new TestOctree();
     72         app.start();
     73     }
     74 
     75     public void simpleInitApp() {
     76 //        this.flyCam.setMoveSpeed(2000);
     77 //        this.cam.setFrustumFar(10000);
     78         MeshLoader.AUTO_INTERLEAVE = false;
     79 
     80 //        mat = new Material(assetManager, "Common/MatDefs/Misc/WireColor.j3md");
     81 //        mat.setColor("Color", ColorRGBA.White);
     82 
     83 //        mat2 = new Material(assetManager, "Common/MatDefs/Misc/ShowNormals.j3md");
     84 
     85         assetManager.registerLocator("quake3level.zip", "com.jme3.asset.plugins.ZipLocator");
     86         MaterialList matList = (MaterialList) assetManager.loadAsset("Scene.material");
     87         OgreMeshKey key = new OgreMeshKey("main.meshxml", matList);
     88         Spatial scene = assetManager.loadModel(key);
     89 
     90 //        Spatial scene = assetManager.loadModel("Models/Teapot/teapot.obj");
     91 //        scene.scale(3);
     92 
     93         DirectionalLight dl = new DirectionalLight();
     94         dl.setColor(ColorRGBA.White);
     95         dl.setDirection(new Vector3f(-1, -1, -1).normalize());
     96         rootNode.addLight(dl);
     97 
     98         DirectionalLight dl2 = new DirectionalLight();
     99         dl2.setColor(ColorRGBA.White);
    100         dl2.setDirection(new Vector3f(1, -1, 1).normalize());
    101         rootNode.addLight(dl2);
    102 
    103         // generate octree
    104 //        tree = new Octree(scene, 20000);
    105         tree = new Octree(scene, 50);
    106         tree.construct();
    107 
    108         ArrayList<Geometry> globalGeomList = new ArrayList<Geometry>();
    109         tree.createFastOctnodes(globalGeomList);
    110         tree.generateFastOctnodeLinks();
    111 
    112         for (Geometry geom : globalGeomList){
    113             geom.addLight(dl);
    114             geom.addLight(dl2);
    115             geom.updateGeometricState();
    116         }
    117 
    118         globalGeoms = globalGeomList.toArray(new Geometry[0]);
    119         fastRoot = tree.getFastRoot();
    120         octBox = tree.getBound();
    121 
    122         viewPort.addProcessor(this);
    123     }
    124 
    125     public void initialize(RenderManager rm, ViewPort vp) {
    126     }
    127 
    128     public void reshape(ViewPort vp, int w, int h) {
    129     }
    130 
    131     public boolean isInitialized() {
    132         return true;
    133     }
    134 
    135     public void preFrame(float tpf) {
    136     }
    137 
    138     public void postQueue(RenderQueue rq) {
    139         renderSet.clear();
    140         //tree.generateRenderSet(renderSet, cam);
    141         fastRoot.generateRenderSet(globalGeoms, renderSet, cam, octBox, true);
    142 //        System.out.println("Geoms: "+renderSet.size());
    143         int tris = 0;
    144 
    145         for (Geometry geom : renderSet){
    146             tris += geom.getTriangleCount();
    147 //            geom.setMaterial(mat2);
    148             rq.addToQueue(geom, geom.getQueueBucket());
    149         }
    150 
    151 //        Matrix4f transform = new Matrix4f();
    152 //        transform.setScale(0.2f, 0.2f, 0.2f);
    153 //        System.out.println("Tris: "+tris);
    154 
    155 //        tree.renderBounds(rq, transform, box, mat);
    156 
    157 //        renderManager.flushQueue(viewPort);
    158     }
    159 
    160     public void postFrame(FrameBuffer out) {
    161     }
    162 
    163     public void cleanup() {
    164     }
    165 }
    166