Home | History | Annotate | Download | only in light
      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.light;
     34 
     35 import com.jme3.app.SimpleApplication;
     36 import com.jme3.input.KeyInput;
     37 import com.jme3.input.controls.ActionListener;
     38 import com.jme3.input.controls.KeyTrigger;
     39 import com.jme3.material.Material;
     40 import com.jme3.math.ColorRGBA;
     41 import com.jme3.math.Quaternion;
     42 import com.jme3.math.Vector3f;
     43 import com.jme3.renderer.queue.RenderQueue.ShadowMode;
     44 import com.jme3.scene.Geometry;
     45 import com.jme3.scene.Spatial;
     46 import com.jme3.scene.shape.Box;
     47 import com.jme3.scene.shape.Sphere;
     48 import com.jme3.shadow.PssmShadowRenderer;
     49 import com.jme3.shadow.PssmShadowRenderer.CompareMode;
     50 import com.jme3.shadow.PssmShadowRenderer.FilterMode;
     51 import java.util.Random;
     52 
     53 public class TestPssmShadow extends SimpleApplication implements ActionListener {
     54 
     55     private Spatial teapot;
     56     private boolean renderShadows = true;
     57     private boolean hardwareShadows = false;
     58     private PssmShadowRenderer pssmRenderer;
     59 
     60     public static void main(String[] args){
     61         TestPssmShadow app = new TestPssmShadow();
     62         app.start();
     63     }
     64 
     65     public void loadScene(){
     66         Material mat = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
     67         Material matSoil = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
     68         matSoil.setColor("Color", ColorRGBA.Cyan);
     69 
     70         teapot = new Geometry("sphere", new Sphere(30, 30, 2));
     71 //        teapot = new Geometry("cube", new Box(1.0f, 1.0f, 1.0f));
     72 //        teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
     73         teapot.setLocalTranslation(0,0,10);
     74 
     75         teapot.setMaterial(mat);
     76         teapot.setShadowMode(ShadowMode.CastAndReceive);
     77         rootNode.attachChild(teapot);
     78 
     79         long seed = 1294719330150L; //System.currentTimeMillis();
     80         Random random = new Random(seed);
     81         System.out.println(seed);
     82 
     83         for (int i = 0; i < 30; i++) {
     84             Spatial t = teapot.clone(false);
     85             rootNode.attachChild(t);
     86             teapot.setLocalTranslation((float) random.nextFloat() * 3, (float) random.nextFloat() * 3, (i + 2));
     87         }
     88 
     89         Geometry soil = new Geometry("soil", new Box(new Vector3f(0, -13, 550), 800, 10, 700));
     90         soil.setMaterial(matSoil);
     91         soil.setShadowMode(ShadowMode.Receive);
     92         rootNode.attachChild(soil);
     93 
     94         for (int i = 0; i < 30; i++) {
     95             Spatial t = teapot.clone(false);
     96             t.setLocalScale(10.0f);
     97             rootNode.attachChild(t);
     98             teapot.setLocalTranslation((float) random.nextFloat() * 300, (float) random.nextFloat() * 30, 30 * (i + 2));
     99         }
    100     }
    101 
    102     @Override
    103     public void simpleInitApp() {
    104         // put the camera in a bad position
    105         cam.setLocation(new Vector3f(41.59757f, 34.38738f, 11.528807f));
    106         cam.setRotation(new Quaternion(0.2905285f, 0.3816416f, -0.12772122f, 0.86811876f));
    107         flyCam.setMoveSpeed(100);
    108 
    109         loadScene();
    110 
    111         pssmRenderer = new PssmShadowRenderer(assetManager, 1024, 3);
    112         pssmRenderer.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
    113         pssmRenderer.setLambda(0.55f);
    114         pssmRenderer.setShadowIntensity(0.6f);
    115         pssmRenderer.setCompareMode(CompareMode.Software);
    116         pssmRenderer.setFilterMode(FilterMode.Bilinear);
    117         pssmRenderer.displayDebug();
    118         viewPort.addProcessor(pssmRenderer);
    119         initInputs();
    120     }
    121 
    122       private void initInputs() {
    123         inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
    124         inputManager.addMapping("ShadowUp", new KeyTrigger(KeyInput.KEY_T));
    125         inputManager.addMapping("ShadowDown", new KeyTrigger(KeyInput.KEY_G));
    126         inputManager.addMapping("ThicknessUp", new KeyTrigger(KeyInput.KEY_Y));
    127         inputManager.addMapping("ThicknessDown", new KeyTrigger(KeyInput.KEY_H));
    128         inputManager.addMapping("lambdaUp", new KeyTrigger(KeyInput.KEY_U));
    129         inputManager.addMapping("lambdaDown", new KeyTrigger(KeyInput.KEY_J));
    130         inputManager.addMapping("toggleHW", new KeyTrigger(KeyInput.KEY_RETURN));
    131         inputManager.addListener(this, "lambdaUp", "lambdaDown", "toggleHW", "toggle", "ShadowUp","ShadowDown","ThicknessUp","ThicknessDown");
    132     }
    133 
    134     public void onAction(String name, boolean keyPressed, float tpf) {
    135         if (name.equals("toggle") && keyPressed) {
    136             if (renderShadows) {
    137                 renderShadows = false;
    138                 viewPort.removeProcessor(pssmRenderer);
    139             } else {
    140                 renderShadows = true;
    141                 viewPort.addProcessor(pssmRenderer);
    142             }
    143         } else if (name.equals("toggleHW") && keyPressed) {
    144             hardwareShadows = !hardwareShadows;
    145             pssmRenderer.setCompareMode(hardwareShadows ? CompareMode.Hardware : CompareMode.Software);
    146             System.out.println("HW Shadows: " + hardwareShadows);
    147         }
    148 
    149         if (name.equals("lambdaUp") && keyPressed) {
    150             pssmRenderer.setLambda(pssmRenderer.getLambda() + 0.01f);
    151             System.out.println("Lambda : " + pssmRenderer.getLambda());
    152         } else if (name.equals("lambdaDown") && keyPressed) {
    153             pssmRenderer.setLambda(pssmRenderer.getLambda() - 0.01f);
    154             System.out.println("Lambda : " + pssmRenderer.getLambda());
    155         }
    156 
    157         if (name.equals("ShadowUp") && keyPressed) {
    158             pssmRenderer.setShadowIntensity(pssmRenderer.getShadowIntensity() + 0.1f);
    159             System.out.println("Shadow intensity : " + pssmRenderer.getShadowIntensity());
    160         }
    161         if (name.equals("ShadowDown") && keyPressed) {
    162             pssmRenderer.setShadowIntensity(pssmRenderer.getShadowIntensity() - 0.1f);
    163             System.out.println("Shadow intensity : " + pssmRenderer.getShadowIntensity());
    164         }
    165         if (name.equals("ThicknessUp") && keyPressed) {
    166             pssmRenderer.setEdgesThickness(pssmRenderer.getEdgesThickness() + 1);
    167             System.out.println("Shadow thickness : " + pssmRenderer.getEdgesThickness());
    168         }
    169         if (name.equals("ThicknessDown") && keyPressed) {
    170             pssmRenderer.setEdgesThickness(pssmRenderer.getEdgesThickness() - 1);
    171             System.out.println("Shadow thickness : " + pssmRenderer.getEdgesThickness());
    172         }
    173     }
    174 
    175 
    176 }
    177