• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
33 package jme3test.water;
34 
35 import com.jme3.app.SimpleApplication;
36 import com.jme3.input.KeyInput;
37 import com.jme3.input.controls.ActionListener;
38 import com.jme3.input.controls.KeyTrigger;
39 import com.jme3.material.Material;
40 import com.jme3.math.Vector3f;
41 import com.jme3.scene.Geometry;
42 import com.jme3.scene.Node;
43 import com.jme3.scene.Spatial;
44 import com.jme3.scene.shape.Box;
45 import com.jme3.scene.shape.Sphere;
46 import com.jme3.util.SkyFactory;
47 import com.jme3.water.SimpleWaterProcessor;
48 
49 /**
50  *
51  * @author normenhansen
52  */
53 public class TestSimpleWater extends SimpleApplication implements ActionListener {
54 
55     Material mat;
56     Spatial waterPlane;
57     Geometry lightSphere;
58     SimpleWaterProcessor waterProcessor;
59     Node sceneNode;
60     boolean useWater = true;
61     private Vector3f lightPos =  new Vector3f(33,12,-29);
62 
63 
main(String[] args)64     public static void main(String[] args) {
65         TestSimpleWater app = new TestSimpleWater();
66         app.start();
67     }
68 
69     @Override
simpleInitApp()70     public void simpleInitApp() {
71         initInput();
72         initScene();
73 
74         //create processor
75         waterProcessor = new SimpleWaterProcessor(assetManager);
76         waterProcessor.setReflectionScene(sceneNode);
77         waterProcessor.setDebug(true);
78         viewPort.addProcessor(waterProcessor);
79 
80         waterProcessor.setLightPosition(lightPos);
81 
82         //create water quad
83         //waterPlane = waterProcessor.createWaterGeometry(100, 100);
84         waterPlane=(Spatial)  assetManager.loadAsset("Models/WaterTest/WaterTest.mesh.xml");
85         waterPlane.setMaterial(waterProcessor.getMaterial());
86         waterPlane.setLocalScale(40);
87         waterPlane.setLocalTranslation(-5, 0, 5);
88 
89         rootNode.attachChild(waterPlane);
90     }
91 
initScene()92     private void initScene() {
93         //init cam location
94         cam.setLocation(new Vector3f(0, 10, 10));
95         cam.lookAt(Vector3f.ZERO, Vector3f.UNIT_Y);
96         //init scene
97         sceneNode = new Node("Scene");
98         mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
99         mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
100         Box b = new Box(1, 1, 1);
101         Geometry geom = new Geometry("Box", b);
102         geom.setMaterial(mat);
103         sceneNode.attachChild(geom);
104 
105         // load sky
106         sceneNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
107         rootNode.attachChild(sceneNode);
108 
109         //add lightPos Geometry
110         Sphere lite=new Sphere(8, 8, 3.0f);
111         lightSphere=new Geometry("lightsphere", lite);
112         lightSphere.setMaterial(mat);
113         lightSphere.setLocalTranslation(lightPos);
114         rootNode.attachChild(lightSphere);
115     }
116 
initInput()117     protected void initInput() {
118         flyCam.setMoveSpeed(3);
119         //init input
120         inputManager.addMapping("use_water", new KeyTrigger(KeyInput.KEY_O));
121         inputManager.addListener(this, "use_water");
122         inputManager.addMapping("lightup", new KeyTrigger(KeyInput.KEY_T));
123         inputManager.addListener(this, "lightup");
124         inputManager.addMapping("lightdown", new KeyTrigger(KeyInput.KEY_G));
125         inputManager.addListener(this, "lightdown");
126         inputManager.addMapping("lightleft", new KeyTrigger(KeyInput.KEY_H));
127         inputManager.addListener(this, "lightleft");
128         inputManager.addMapping("lightright", new KeyTrigger(KeyInput.KEY_K));
129         inputManager.addListener(this, "lightright");
130         inputManager.addMapping("lightforward", new KeyTrigger(KeyInput.KEY_U));
131         inputManager.addListener(this, "lightforward");
132         inputManager.addMapping("lightback", new KeyTrigger(KeyInput.KEY_J));
133         inputManager.addListener(this, "lightback");
134     }
135 
136     @Override
simpleUpdate(float tpf)137     public void simpleUpdate(float tpf) {
138         fpsText.setText("Light Position: "+lightPos.toString()+" Change Light position with [U], [H], [J], [K] and [T], [G] Turn off water with [O]");
139         lightSphere.setLocalTranslation(lightPos);
140         waterProcessor.setLightPosition(lightPos);
141     }
142 
onAction(String name, boolean value, float tpf)143     public void onAction(String name, boolean value, float tpf) {
144         if (name.equals("use_water") && value) {
145             if (!useWater) {
146                 useWater = true;
147                 waterPlane.setMaterial(waterProcessor.getMaterial());
148             } else {
149                 useWater = false;
150                 waterPlane.setMaterial(mat);
151             }
152         } else if (name.equals("lightup") && value) {
153             lightPos.y++;
154         } else if (name.equals("lightdown") && value) {
155             lightPos.y--;
156         } else if (name.equals("lightleft") && value) {
157             lightPos.x--;
158         } else if (name.equals("lightright") && value) {
159             lightPos.x++;
160         } else if (name.equals("lightforward") && value) {
161             lightPos.z--;
162         } else if (name.equals("lightback") && value) {
163             lightPos.z++;
164         }
165     }
166 }
167