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 */
32package jme3test.post;
33
34import com.jme3.app.SimpleApplication;
35import com.jme3.input.KeyInput;
36import com.jme3.input.controls.ActionListener;
37import com.jme3.input.controls.KeyTrigger;
38import com.jme3.light.DirectionalLight;
39import com.jme3.math.ColorRGBA;
40import com.jme3.math.Quaternion;
41import com.jme3.math.Vector3f;
42import com.jme3.post.FilterPostProcessor;
43import com.jme3.post.filters.*;
44import com.jme3.post.ssao.SSAOFilter;
45import com.jme3.renderer.Camera;
46import com.jme3.renderer.ViewPort;
47import com.jme3.scene.Geometry;
48import com.jme3.util.SkyFactory;
49
50public class TestMultiViewsFilters extends SimpleApplication {
51
52    public static void main(String[] args) {
53        TestMultiViewsFilters app = new TestMultiViewsFilters();
54        app.start();
55    }
56    private boolean filterEnabled = true;
57
58    public void simpleInitApp() {
59        // create the geometry and attach it
60        Geometry teaGeom = (Geometry) assetManager.loadModel("Models/Teapot/Teapot.obj");
61        teaGeom.scale(3);
62        teaGeom.getMaterial().setColor("GlowColor", ColorRGBA.Green);
63
64        DirectionalLight dl = new DirectionalLight();
65        dl.setColor(ColorRGBA.White);
66        dl.setDirection(Vector3f.UNIT_XYZ.negate());
67
68        rootNode.addLight(dl);
69        rootNode.attachChild(teaGeom);
70
71        // Setup first view
72        cam.setViewPort(.5f, 1f, 0f, 0.5f);
73        cam.setLocation(new Vector3f(3.3212643f, 4.484704f, 4.2812433f));
74        cam.setRotation(new Quaternion(-0.07680723f, 0.92299235f, -0.2564353f, -0.27645364f));
75
76        // Setup second view
77        Camera cam2 = cam.clone();
78        cam2.setViewPort(0f, 0.5f, 0f, 0.5f);
79        cam2.setLocation(new Vector3f(-0.10947256f, 1.5760219f, 4.81758f));
80        cam2.setRotation(new Quaternion(0.0010108891f, 0.99857414f, -0.04928594f, 0.020481428f));
81
82        final ViewPort view2 = renderManager.createMainView("Bottom Left", cam2);
83        view2.setClearFlags(true, true, true);
84        view2.attachScene(rootNode);
85
86        // Setup third view
87        Camera cam3 = cam.clone();
88        cam3.setName("cam3");
89        cam3.setViewPort(0f, .5f, .5f, 1f);
90        cam3.setLocation(new Vector3f(0.2846221f, 6.4271426f, 0.23380789f));
91        cam3.setRotation(new Quaternion(0.004381671f, 0.72363687f, -0.69015175f, 0.0045953835f));
92
93        final ViewPort view3 = renderManager.createMainView("Top Left", cam3);
94        view3.setClearFlags(true, true, true);
95        view3.attachScene(rootNode);
96
97
98        // Setup fourth view
99        Camera cam4 = cam.clone();
100        cam4.setName("cam4");
101        cam4.setViewPort(.5f, 1f, .5f, 1f);
102
103        cam4.setLocation(new Vector3f(4.775564f, 1.4548365f, 0.11491505f));
104        cam4.setRotation(new Quaternion(0.02356979f, -0.74957186f, 0.026729556f, 0.66096294f));
105
106        final ViewPort view4 = renderManager.createMainView("Top Right", cam4);
107        view4.setClearFlags(true, true, true);
108        view4.attachScene(rootNode);
109
110//        Camera cam5 = new Camera(200, 200);
111//        cam5.setFrustumPerspective(45f, (float)cam.getWidth() / cam.getHeight(), 1f, 1000f);
112//        cam5.setName("cam5");
113//        cam5.setViewPort(5.23f, 6.33f, 0.56f, 1.66f);
114//          this.setViewPortAreas(5.23f, 6.33f, 0.56f, 1.66f);
115//          this.setViewPortCamSize(200, 200);
116//          1046,1266,112,332
117        Camera cam5 = cam.clone();
118        cam5.setName("cam5");
119        cam5.setViewPort(1046f/settings.getWidth(), 1266f/settings.getWidth(), 112f/settings.getHeight(), 332f/settings.getHeight());
120        cam5.setLocation(new Vector3f(0.2846221f, 6.4271426f, 0.23380789f));
121        cam5.setRotation(new Quaternion(0.004381671f, 0.72363687f, -0.69015175f, 0.0045953835f));
122
123        final ViewPort view5 = renderManager.createMainView("center", cam5);
124        view5.setClearFlags(true, true, true);
125        view5.attachScene(rootNode);
126
127
128
129        rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
130
131        final FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
132        final FilterPostProcessor fpp2 = new FilterPostProcessor(assetManager);
133        final FilterPostProcessor fpp3 = new FilterPostProcessor(assetManager);
134        final FilterPostProcessor fpp4 = new FilterPostProcessor(assetManager);
135        final FilterPostProcessor fpp5 = new FilterPostProcessor(assetManager);
136
137
138        //  fpp.addFilter(new WaterFilter(rootNode, Vector3f.UNIT_Y.mult(-1)));
139        fpp3.addFilter(new CartoonEdgeFilter());
140
141        fpp2.addFilter(new CrossHatchFilter());
142        final FogFilter ff = new FogFilter(ColorRGBA.Yellow, 0.7f, 2);
143        fpp.addFilter(ff);
144
145        final RadialBlurFilter rbf = new RadialBlurFilter(1, 10);
146        //    rbf.setEnabled(false);
147        fpp.addFilter(rbf);
148
149
150        SSAOFilter f = new SSAOFilter(1.8899765f, 20.490374f, 0.4699998f, 0.1f);;
151        fpp4.addFilter(f);
152        SSAOUI ui = new SSAOUI(inputManager, f);
153
154        fpp5.addFilter(new BloomFilter(BloomFilter.GlowMode.Objects));
155
156        viewPort.addProcessor(fpp);
157        view2.addProcessor(fpp2);
158        view3.addProcessor(fpp3);
159        view4.addProcessor(fpp4);
160        view5.addProcessor(fpp5);
161
162
163
164        inputManager.addListener(new ActionListener() {
165
166            public void onAction(String name, boolean isPressed, float tpf) {
167                if (name.equals("press") && isPressed) {
168                    if (filterEnabled) {
169                        viewPort.removeProcessor(fpp);
170                        view2.removeProcessor(fpp2);
171                        view3.removeProcessor(fpp3);
172                        view4.removeProcessor(fpp4);
173                        view5.removeProcessor(fpp5);
174                    } else {
175                        viewPort.addProcessor(fpp);
176                        view2.addProcessor(fpp2);
177                        view3.addProcessor(fpp3);
178                        view4.addProcessor(fpp4);
179                        view5.addProcessor(fpp5);
180                    }
181                    filterEnabled = !filterEnabled;
182                }
183                if (name.equals("filter") && isPressed) {
184                    ff.setEnabled(!ff.isEnabled());
185                    rbf.setEnabled(!rbf.isEnabled());
186                }
187            }
188        }, "press", "filter");
189
190        inputManager.addMapping("press", new KeyTrigger(KeyInput.KEY_SPACE));
191        inputManager.addMapping("filter", new KeyTrigger(KeyInput.KEY_F));
192
193    }
194}
195