Home | History | Annotate | Download | only in effect
      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.effect;
     34 
     35 import com.jme3.app.SimpleApplication;
     36 import com.jme3.light.DirectionalLight;
     37 import com.jme3.material.Material;
     38 import com.jme3.math.*;
     39 import com.jme3.post.HDRRenderer;
     40 import com.jme3.renderer.Caps;
     41 import com.jme3.renderer.queue.RenderQueue.ShadowMode;
     42 import com.jme3.scene.Geometry;
     43 import com.jme3.scene.Node;
     44 import com.jme3.scene.Spatial;
     45 import com.jme3.scene.Spatial.CullHint;
     46 import com.jme3.scene.shape.Box;
     47 import com.jme3.shadow.BasicShadowRenderer;
     48 import com.jme3.texture.Texture;
     49 import com.jme3.texture.Texture.WrapMode;
     50 import com.jme3.util.SkyFactory;
     51 import com.jme3.util.TangentBinormalGenerator;
     52 
     53 public class TestEverything extends SimpleApplication {
     54 
     55     private BasicShadowRenderer bsr;
     56     private HDRRenderer hdrRender;
     57     private Vector3f lightDir = new Vector3f(-1, -1, .5f).normalizeLocal();
     58 
     59     public static void main(String[] args){
     60         TestEverything app = new TestEverything();
     61         app.start();
     62     }
     63 
     64     public void setupHdr(){
     65         if (renderer.getCaps().contains(Caps.GLSL100)){
     66             hdrRender = new HDRRenderer(assetManager, renderer);
     67             hdrRender.setMaxIterations(40);
     68             hdrRender.setSamples(settings.getSamples());
     69 
     70             hdrRender.setWhiteLevel(3);
     71             hdrRender.setExposure(0.72f);
     72             hdrRender.setThrottle(1);
     73 
     74     //        setPauseOnLostFocus(false);
     75     //        new HDRConfig(hdrRender).setVisible(true);
     76 
     77             viewPort.addProcessor(hdrRender);
     78         }
     79     }
     80 
     81     public void setupBasicShadow(){
     82         if (renderer.getCaps().contains(Caps.GLSL100)){
     83             bsr = new BasicShadowRenderer(assetManager, 1024);
     84             bsr.setDirection(lightDir);
     85             viewPort.addProcessor(bsr);
     86         }
     87     }
     88 
     89     public void setupSkyBox(){
     90         Texture envMap;
     91         if (renderer.getCaps().contains(Caps.FloatTexture)){
     92             envMap = assetManager.loadTexture("Textures/Sky/St Peters/StPeters.hdr");
     93         }else{
     94             envMap = assetManager.loadTexture("Textures/Sky/St Peters/StPeters.jpg");
     95         }
     96         rootNode.attachChild(SkyFactory.createSky(assetManager, envMap, new Vector3f(-1,-1,-1), true));
     97     }
     98 
     99     public void setupLighting(){
    100         boolean hdr = false;
    101         if (hdrRender != null){
    102             hdr = hdrRender.isEnabled();
    103         }
    104 
    105         DirectionalLight dl = new DirectionalLight();
    106         dl.setDirection(lightDir);
    107         if (hdr){
    108             dl.setColor(new ColorRGBA(3, 3, 3, 1));
    109         }else{
    110             dl.setColor(new ColorRGBA(.9f, .9f, .9f, 1));
    111         }
    112         rootNode.addLight(dl);
    113 
    114         dl = new DirectionalLight();
    115         dl.setDirection(new Vector3f(1, 0, -1).normalizeLocal());
    116         if (hdr){
    117             dl.setColor(new ColorRGBA(1, 1, 1, 1));
    118         }else{
    119             dl.setColor(new ColorRGBA(.4f, .4f, .4f, 1));
    120         }
    121         rootNode.addLight(dl);
    122     }
    123 
    124     public void setupFloor(){
    125         Material mat = assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m");
    126         mat.getTextureParam("DiffuseMap").getTextureValue().setWrap(WrapMode.Repeat);
    127         mat.getTextureParam("NormalMap").getTextureValue().setWrap(WrapMode.Repeat);
    128         mat.getTextureParam("ParallaxMap").getTextureValue().setWrap(WrapMode.Repeat);
    129 
    130         Box floor = new Box(Vector3f.ZERO, 50, 1f, 50);
    131         TangentBinormalGenerator.generate(floor);
    132         floor.scaleTextureCoordinates(new Vector2f(5, 5));
    133         Geometry floorGeom = new Geometry("Floor", floor);
    134         floorGeom.setMaterial(mat);
    135         floorGeom.setShadowMode(ShadowMode.Receive);
    136         rootNode.attachChild(floorGeom);
    137     }
    138 
    139 //    public void setupTerrain(){
    140 //        Material mat = manager.loadMaterial("Textures/Terrain/Rock/Rock.j3m");
    141 //        mat.getTextureParam("DiffuseMap").getValue().setWrap(WrapMode.Repeat);
    142 //        mat.getTextureParam("NormalMap").getValue().setWrap(WrapMode.Repeat);
    143 //        try{
    144 //            Geomap map = GeomapLoader.fromImage(TestEverything.class.getResource("/textures/heightmap.png"));
    145 //            Mesh m = map.createMesh(new Vector3f(0.35f, 0.0005f, 0.35f), new Vector2f(10, 10), true);
    146 //            Logger.getLogger(TangentBinormalGenerator.class.getName()).setLevel(Level.SEVERE);
    147 //            TangentBinormalGenerator.generate(m);
    148 //            Geometry t = new Geometry("Terrain", m);
    149 //            t.setLocalTranslation(85, -15, 0);
    150 //            t.setMaterial(mat);
    151 //            t.updateModelBound();
    152 //            t.setShadowMode(ShadowMode.Receive);
    153 //            rootNode.attachChild(t);
    154 //        }catch (IOException ex){
    155 //            ex.printStackTrace();
    156 //        }
    157 //
    158 //    }
    159 
    160     public void setupRobotGuy(){
    161         Node model = (Node) assetManager.loadModel("Models/Oto/Oto.mesh.xml");
    162         Material mat = assetManager.loadMaterial("Models/Oto/Oto.j3m");
    163         model.getChild(0).setMaterial(mat);
    164 //        model.setAnimation("Walk");
    165         model.setLocalTranslation(30, 10.5f, 30);
    166         model.setLocalScale(2);
    167         model.setShadowMode(ShadowMode.CastAndReceive);
    168         rootNode.attachChild(model);
    169     }
    170 
    171     public void setupSignpost(){
    172         Spatial signpost = assetManager.loadModel("Models/Sign Post/Sign Post.mesh.xml");
    173         Material mat = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
    174         signpost.setMaterial(mat);
    175         signpost.rotate(0, FastMath.HALF_PI, 0);
    176         signpost.setLocalTranslation(12, 3.5f, 30);
    177         signpost.setLocalScale(4);
    178         signpost.setShadowMode(ShadowMode.CastAndReceive);
    179         rootNode.attachChild(signpost);
    180     }
    181 
    182     @Override
    183     public void simpleInitApp() {
    184         cam.setLocation(new Vector3f(-32.295086f, 54.80136f, 79.59805f));
    185         cam.setRotation(new Quaternion(0.074364014f, 0.92519957f, -0.24794696f, 0.27748522f));
    186         cam.update();
    187 
    188         cam.setFrustumFar(300);
    189         flyCam.setMoveSpeed(30);
    190 
    191         rootNode.setCullHint(CullHint.Never);
    192 
    193         setupBasicShadow();
    194         setupHdr();
    195 
    196         setupLighting();
    197         setupSkyBox();
    198 
    199 //        setupTerrain();
    200         setupFloor();
    201 //        setupRobotGuy();
    202         setupSignpost();
    203 
    204 
    205     }
    206 
    207 }
    208