TestPosterization.java revision 59b2e6871c65f58fdad78cd7229c292f6a177578
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.input.KeyInput;
37import com.jme3.input.controls.ActionListener;
38import com.jme3.input.controls.KeyTrigger;
39import com.jme3.light.DirectionalLight;
40import com.jme3.material.Material;
41import com.jme3.math.ColorRGBA;
42import com.jme3.math.Quaternion;
43import com.jme3.math.Vector3f;
44import com.jme3.post.FilterPostProcessor;
45import com.jme3.post.filters.PosterizationFilter;
46import com.jme3.renderer.queue.RenderQueue.ShadowMode;
47import com.jme3.scene.Geometry;
48import com.jme3.scene.Spatial;
49import com.jme3.scene.debug.WireFrustum;
50import com.jme3.scene.shape.Box;
51import com.jme3.util.SkyFactory;
52
53public class TestPosterization extends SimpleApplication {
54
55    float angle;
56    Spatial lightMdl;
57    Spatial teapot;
58    Geometry frustumMdl;
59    WireFrustum frustum;
60    boolean active=true;
61    FilterPostProcessor fpp;
62
63    public static void main(String[] args){
64        TestPosterization app = new TestPosterization();
65        app.start();
66    }
67
68    @Override
69    public void simpleInitApp() {
70        // put the camera in a bad position
71        cam.setLocation(new Vector3f(-2.336393f, 11.91392f, -7.139601f));
72        cam.setRotation(new Quaternion(0.23602544f, 0.11321983f, -0.027698677f, 0.96473104f));
73        //cam.setFrustumFar(1000);
74
75
76        Material mat = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
77        mat.setFloat("Shininess", 15f);
78        mat.setBoolean("UseMaterialColors", true);
79        mat.setColor("Ambient", ColorRGBA.Yellow.mult(0.2f));
80        mat.setColor("Diffuse", ColorRGBA.Yellow.mult(0.2f));
81        mat.setColor("Specular", ColorRGBA.Yellow.mult(0.8f));
82
83
84
85
86        Material matSoil = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
87        matSoil.setFloat("Shininess", 15f);
88        matSoil.setBoolean("UseMaterialColors", true);
89        matSoil.setColor("Ambient", ColorRGBA.Gray);
90        matSoil.setColor("Diffuse", ColorRGBA.Black);
91        matSoil.setColor("Specular", ColorRGBA.Gray);
92
93
94
95        teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
96        teapot.setLocalTranslation(0,0,10);
97
98        teapot.setMaterial(mat);
99        teapot.setShadowMode(ShadowMode.CastAndReceive);
100        teapot.setLocalScale(10.0f);
101        rootNode.attachChild(teapot);
102
103
104
105        Geometry soil=new Geometry("soil", new Box(new Vector3f(0, -13, 550), 800, 10, 700));
106        soil.setMaterial(matSoil);
107        soil.setShadowMode(ShadowMode.CastAndReceive);
108        rootNode.attachChild(soil);
109
110        DirectionalLight light=new DirectionalLight();
111        light.setDirection(new Vector3f(-1, -1, -1).normalizeLocal());
112        light.setColor(ColorRGBA.White.mult(1.5f));
113        rootNode.addLight(light);
114
115        // load sky
116        Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Bright/FullskiesBlueClear03.dds", false);
117        sky.setCullHint(Spatial.CullHint.Never);
118        rootNode.attachChild(sky);
119
120        fpp=new FilterPostProcessor(assetManager);
121        PosterizationFilter pf=new PosterizationFilter();
122
123
124
125        viewPort.addProcessor(fpp);
126        fpp.addFilter(pf);
127        initInputs();
128
129    }
130
131         private void initInputs() {
132        inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
133
134        ActionListener acl = new ActionListener() {
135
136            public void onAction(String name, boolean keyPressed, float tpf) {
137                if (name.equals("toggle") && keyPressed) {
138                    if(active){
139                        active=false;
140                        viewPort.removeProcessor(fpp);
141                    }else{
142                        active=true;
143                        viewPort.addProcessor(fpp);
144                    }
145                }
146            }
147        };
148
149        inputManager.addListener(acl, "toggle");
150
151    }
152
153
154
155}
156