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 package jme3test.post;
33 
34 import com.jme3.app.SimpleApplication;
35 import com.jme3.input.KeyInput;
36 import com.jme3.input.controls.ActionListener;
37 import com.jme3.input.controls.KeyTrigger;
38 import com.jme3.light.DirectionalLight;
39 import com.jme3.material.Material;
40 import com.jme3.math.*;
41 import com.jme3.post.FilterPostProcessor;
42 import com.jme3.post.filters.ColorOverlayFilter;
43 import com.jme3.post.filters.FadeFilter;
44 import com.jme3.post.filters.RadialBlurFilter;
45 import com.jme3.renderer.Caps;
46 import com.jme3.renderer.queue.RenderQueue.ShadowMode;
47 import com.jme3.scene.Geometry;
48 import com.jme3.scene.Spatial;
49 import com.jme3.scene.Spatial.CullHint;
50 import com.jme3.scene.shape.Box;
51 import com.jme3.texture.Texture;
52 import com.jme3.texture.Texture.WrapMode;
53 import com.jme3.util.SkyFactory;
54 import com.jme3.util.TangentBinormalGenerator;
55 
56 public class TestPostFilters extends SimpleApplication implements ActionListener {
57 
58     private FilterPostProcessor fpp;
59     private Vector3f lightDir = new Vector3f(-1, -1, .5f).normalizeLocal();
60     FadeFilter fade;
61 
main(String[] args)62     public static void main(String[] args) {
63         TestPostFilters app = new TestPostFilters();
64         app.start();
65     }
66 
setupFilters()67     public void setupFilters() {
68         if (renderer.getCaps().contains(Caps.GLSL100)) {
69             fpp = new FilterPostProcessor(assetManager);
70             // fpp.setNumSamples(4);
71             fpp.addFilter(new ColorOverlayFilter(ColorRGBA.LightGray));
72             fpp.addFilter(new RadialBlurFilter());
73             //fade=new FadeFilter(1.0f);
74             //fpp.addFilter(fade);
75 
76 
77             viewPort.addProcessor(fpp);
78         }
79     }
80 
setupSkyBox()81     public void setupSkyBox() {
82         Texture envMap;
83         if (renderer.getCaps().contains(Caps.FloatTexture)) {
84             envMap = assetManager.loadTexture("Textures/Sky/St Peters/StPeters.hdr");
85         } else {
86             envMap = assetManager.loadTexture("Textures/Sky/St Peters/StPeters.jpg");
87         }
88         rootNode.attachChild(SkyFactory.createSky(assetManager, envMap, new Vector3f(-1, -1, -1), true));
89     }
90 
setupLighting()91     public void setupLighting() {
92 
93         DirectionalLight dl = new DirectionalLight();
94         dl.setDirection(lightDir);
95 
96         dl.setColor(new ColorRGBA(.9f, .9f, .9f, 1));
97 
98         rootNode.addLight(dl);
99 
100         dl = new DirectionalLight();
101         dl.setDirection(new Vector3f(1, 0, -1).normalizeLocal());
102 
103         dl.setColor(new ColorRGBA(.4f, .4f, .4f, 1));
104 
105         rootNode.addLight(dl);
106     }
107 
setupFloor()108     public void setupFloor() {
109         Material mat = assetManager.loadMaterial("Textures/Terrain/BrickWall/BrickWall.j3m");
110         mat.getTextureParam("DiffuseMap").getTextureValue().setWrap(WrapMode.Repeat);
111         mat.getTextureParam("NormalMap").getTextureValue().setWrap(WrapMode.Repeat);
112         mat.getTextureParam("ParallaxMap").getTextureValue().setWrap(WrapMode.Repeat);
113         Box floor = new Box(Vector3f.ZERO, 50, 1f, 50);
114         TangentBinormalGenerator.generate(floor);
115         floor.scaleTextureCoordinates(new Vector2f(5, 5));
116         Geometry floorGeom = new Geometry("Floor", floor);
117         floorGeom.setMaterial(mat);
118         floorGeom.setShadowMode(ShadowMode.Receive);
119         rootNode.attachChild(floorGeom);
120     }
121 
setupSignpost()122     public void setupSignpost() {
123         Spatial signpost = assetManager.loadModel("Models/Sign Post/Sign Post.mesh.xml");
124         Material mat = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
125         signpost.setMaterial(mat);
126         signpost.rotate(0, FastMath.HALF_PI, 0);
127         signpost.setLocalTranslation(12, 3.5f, 30);
128         signpost.setLocalScale(4);
129         signpost.setShadowMode(ShadowMode.CastAndReceive);
130         rootNode.attachChild(signpost);
131     }
132 
133     @Override
simpleInitApp()134     public void simpleInitApp() {
135         cam.setLocation(new Vector3f(-32.295086f, 54.80136f, 79.59805f));
136         cam.setRotation(new Quaternion(0.074364014f, 0.92519957f, -0.24794696f, 0.27748522f));
137         cam.update();
138 
139         cam.setFrustumFar(300);
140         flyCam.setMoveSpeed(30);
141 
142         rootNode.setCullHint(CullHint.Never);
143 
144         setupLighting();
145         setupSkyBox();
146 
147 
148         setupFloor();
149 
150         setupSignpost();
151 
152         setupFilters();
153 
154         initInput();
155 
156     }
157 
initInput()158     protected void initInput() {
159         flyCam.setMoveSpeed(3);
160         //init input
161         inputManager.addMapping("fadein", new KeyTrigger(KeyInput.KEY_I));
162         inputManager.addListener(this, "fadein");
163         inputManager.addMapping("fadeout", new KeyTrigger(KeyInput.KEY_O));
164         inputManager.addListener(this, "fadeout");
165 
166     }
167 
onAction(String name, boolean value, float tpf)168     public void onAction(String name, boolean value, float tpf) {
169         if (name.equals("fadein") && value) {
170             fade.fadeIn();
171             System.out.println("fade in");
172 
173         }
174         if (name.equals("fadeout") && value) {
175             fade.fadeOut();
176             System.out.println("fade out");
177         }
178     }
179 }
180