159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.light;
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.KeyInput;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.ActionListener;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.input.controls.KeyTrigger;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.material.Material;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.ColorRGBA;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Quaternion;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f;
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.renderer.queue.RenderQueue.ShadowMode;
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Geometry;
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Spatial;
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Box;
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Sphere;
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.shadow.PssmShadowRenderer;
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.shadow.PssmShadowRenderer.CompareMode;
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.shadow.PssmShadowRenderer.FilterMode;
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.Random;
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TestPssmShadow extends SimpleApplication implements ActionListener {
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private Spatial teapot;
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private boolean renderShadows = true;
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private boolean hardwareShadows = false;
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private PssmShadowRenderer pssmRenderer;
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static void main(String[] args){
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        TestPssmShadow app = new TestPssmShadow();
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        app.start();
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void loadScene(){
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Material mat = assetManager.loadMaterial("Common/Materials/RedColor.j3m");
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Material matSoil = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        matSoil.setColor("Color", ColorRGBA.Cyan);
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        teapot = new Geometry("sphere", new Sphere(30, 30, 2));
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        teapot = new Geometry("cube", new Box(1.0f, 1.0f, 1.0f));
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta//        teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        teapot.setLocalTranslation(0,0,10);
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        teapot.setMaterial(mat);
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        teapot.setShadowMode(ShadowMode.CastAndReceive);
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(teapot);
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        long seed = 1294719330150L; //System.currentTimeMillis();
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Random random = new Random(seed);
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        System.out.println(seed);
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < 30; i++) {
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Spatial t = teapot.clone(false);
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            rootNode.attachChild(t);
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            teapot.setLocalTranslation((float) random.nextFloat() * 3, (float) random.nextFloat() * 3, (i + 2));
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Geometry soil = new Geometry("soil", new Box(new Vector3f(0, -13, 550), 800, 10, 700));
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        soil.setMaterial(matSoil);
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        soil.setShadowMode(ShadowMode.Receive);
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        rootNode.attachChild(soil);
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < 30; i++) {
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Spatial t = teapot.clone(false);
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            t.setLocalScale(10.0f);
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            rootNode.attachChild(t);
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            teapot.setLocalTranslation((float) random.nextFloat() * 300, (float) random.nextFloat() * 30, 30 * (i + 2));
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void simpleInitApp() {
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // put the camera in a bad position
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        cam.setLocation(new Vector3f(41.59757f, 34.38738f, 11.528807f));
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        cam.setRotation(new Quaternion(0.2905285f, 0.3816416f, -0.12772122f, 0.86811876f));
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        flyCam.setMoveSpeed(100);
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        loadScene();
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pssmRenderer = new PssmShadowRenderer(assetManager, 1024, 3);
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pssmRenderer.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pssmRenderer.setLambda(0.55f);
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pssmRenderer.setShadowIntensity(0.6f);
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pssmRenderer.setCompareMode(CompareMode.Software);
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pssmRenderer.setFilterMode(FilterMode.Bilinear);
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pssmRenderer.displayDebug();
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        viewPort.addProcessor(pssmRenderer);
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        initInputs();
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta      private void initInputs() {
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("ShadowUp", new KeyTrigger(KeyInput.KEY_T));
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("ShadowDown", new KeyTrigger(KeyInput.KEY_G));
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("ThicknessUp", new KeyTrigger(KeyInput.KEY_Y));
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("ThicknessDown", new KeyTrigger(KeyInput.KEY_H));
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("lambdaUp", new KeyTrigger(KeyInput.KEY_U));
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("lambdaDown", new KeyTrigger(KeyInput.KEY_J));
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addMapping("toggleHW", new KeyTrigger(KeyInput.KEY_RETURN));
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        inputManager.addListener(this, "lambdaUp", "lambdaDown", "toggleHW", "toggle", "ShadowUp","ShadowDown","ThicknessUp","ThicknessDown");
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void onAction(String name, boolean keyPressed, float tpf) {
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (name.equals("toggle") && keyPressed) {
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (renderShadows) {
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                renderShadows = false;
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                viewPort.removeProcessor(pssmRenderer);
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } else {
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                renderShadows = true;
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                viewPort.addProcessor(pssmRenderer);
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else if (name.equals("toggleHW") && keyPressed) {
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            hardwareShadows = !hardwareShadows;
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pssmRenderer.setCompareMode(hardwareShadows ? CompareMode.Hardware : CompareMode.Software);
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            System.out.println("HW Shadows: " + hardwareShadows);
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (name.equals("lambdaUp") && keyPressed) {
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pssmRenderer.setLambda(pssmRenderer.getLambda() + 0.01f);
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            System.out.println("Lambda : " + pssmRenderer.getLambda());
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else if (name.equals("lambdaDown") && keyPressed) {
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pssmRenderer.setLambda(pssmRenderer.getLambda() - 0.01f);
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            System.out.println("Lambda : " + pssmRenderer.getLambda());
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (name.equals("ShadowUp") && keyPressed) {
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pssmRenderer.setShadowIntensity(pssmRenderer.getShadowIntensity() + 0.1f);
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            System.out.println("Shadow intensity : " + pssmRenderer.getShadowIntensity());
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (name.equals("ShadowDown") && keyPressed) {
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pssmRenderer.setShadowIntensity(pssmRenderer.getShadowIntensity() - 0.1f);
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            System.out.println("Shadow intensity : " + pssmRenderer.getShadowIntensity());
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (name.equals("ThicknessUp") && keyPressed) {
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pssmRenderer.setEdgesThickness(pssmRenderer.getEdgesThickness() + 1);
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            System.out.println("Shadow thickness : " + pssmRenderer.getEdgesThickness());
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (name.equals("ThicknessDown") && keyPressed) {
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pssmRenderer.setEdgesThickness(pssmRenderer.getEdgesThickness() - 1);
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            System.out.println("Shadow thickness : " + pssmRenderer.getEdgesThickness());
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
177