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
33package jme3test.post;
34
35import com.jme3.app.SimpleApplication;
36import com.jme3.asset.plugins.HttpZipLocator;
37import com.jme3.asset.plugins.ZipLocator;
38import com.jme3.input.KeyInput;
39import com.jme3.input.controls.ActionListener;
40import com.jme3.input.controls.AnalogListener;
41import com.jme3.input.controls.KeyTrigger;
42import com.jme3.light.DirectionalLight;
43import com.jme3.math.ColorRGBA;
44import com.jme3.math.Quaternion;
45import com.jme3.math.Vector3f;
46import com.jme3.post.FilterPostProcessor;
47import com.jme3.post.filters.FogFilter;
48import com.jme3.scene.Node;
49import com.jme3.scene.Spatial;
50import com.jme3.util.SkyFactory;
51import java.io.File;
52
53public class TestFog extends SimpleApplication {
54
55    private FilterPostProcessor fpp;
56    private boolean enabled=true;
57    private FogFilter fog;
58
59    // set default for applets
60    private static boolean useHttp = true;
61
62    public static void main(String[] args) {
63        File file = new File("wildhouse.zip");
64        if (file.exists()) {
65            useHttp = false;
66        }
67        TestFog app = new TestFog();
68        app.start();
69    }
70
71    public void simpleInitApp() {
72        this.flyCam.setMoveSpeed(10);
73        Node mainScene=new Node();
74        cam.setLocation(new Vector3f(-27.0f, 1.0f, 75.0f));
75        cam.setRotation(new Quaternion(0.03f, 0.9f, 0f, 0.4f));
76
77        // load sky
78        mainScene.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
79
80        // create the geometry and attach it
81        // load the level from zip or http zip
82        if (useHttp) {
83            assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip", HttpZipLocator.class.getName());
84        } else {
85            assetManager.registerLocator("wildhouse.zip", ZipLocator.class.getName());
86        }
87        Spatial scene = assetManager.loadModel("main.scene");
88
89        DirectionalLight sun = new DirectionalLight();
90        Vector3f lightDir=new Vector3f(-0.37352666f, -0.50444174f, -0.7784704f);
91        sun.setDirection(lightDir);
92        sun.setColor(ColorRGBA.White.clone().multLocal(2));
93        scene.addLight(sun);
94
95
96        mainScene.attachChild(scene);
97        rootNode.attachChild(mainScene);
98
99        fpp=new FilterPostProcessor(assetManager);
100        //fpp.setNumSamples(4);
101        fog=new FogFilter();
102        fog.setFogColor(new ColorRGBA(0.9f, 0.9f, 0.9f, 1.0f));
103        fog.setFogDistance(155);
104        fog.setFogDensity(2.0f);
105        fpp.addFilter(fog);
106        viewPort.addProcessor(fpp);
107        initInputs();
108    }
109
110     private void initInputs() {
111        inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
112        inputManager.addMapping("DensityUp", new KeyTrigger(KeyInput.KEY_Y));
113        inputManager.addMapping("DensityDown", new KeyTrigger(KeyInput.KEY_H));
114        inputManager.addMapping("DistanceUp", new KeyTrigger(KeyInput.KEY_U));
115        inputManager.addMapping("DistanceDown", new KeyTrigger(KeyInput.KEY_J));
116
117
118        ActionListener acl = new ActionListener() {
119
120            public void onAction(String name, boolean keyPressed, float tpf) {
121                if (name.equals("toggle") && keyPressed) {
122                    if(enabled){
123                        enabled=false;
124                        viewPort.removeProcessor(fpp);
125                    }else{
126                        enabled=true;
127                        viewPort.addProcessor(fpp);
128                    }
129                }
130
131            }
132        };
133
134        AnalogListener anl=new AnalogListener() {
135
136            public void onAnalog(String name, float isPressed, float tpf) {
137                if(name.equals("DensityUp")){
138                    fog.setFogDensity(fog.getFogDensity()+0.001f);
139                    System.out.println("Fog density : "+fog.getFogDensity());
140                }
141                if(name.equals("DensityDown")){
142                    fog.setFogDensity(fog.getFogDensity()-0.010f);
143                    System.out.println("Fog density : "+fog.getFogDensity());
144                }
145                if(name.equals("DistanceUp")){
146                    fog.setFogDistance(fog.getFogDistance()+0.5f);
147                    System.out.println("Fog Distance : "+fog.getFogDistance());
148                }
149                if(name.equals("DistanceDown")){
150                    fog.setFogDistance(fog.getFogDistance()-0.5f);
151                    System.out.println("Fog Distance : "+fog.getFogDistance());
152                }
153
154            }
155        };
156
157        inputManager.addListener(acl, "toggle");
158        inputManager.addListener(anl, "DensityUp","DensityDown","DistanceUp","DistanceDown");
159
160    }
161}
162
163