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.water;
34
35import com.jme3.app.SimpleApplication;
36import com.jme3.asset.plugins.HttpZipLocator;
37import com.jme3.asset.plugins.ZipLocator;
38import com.jme3.light.DirectionalLight;
39import com.jme3.material.Material;
40import com.jme3.math.*;
41import com.jme3.renderer.queue.RenderQueue.ShadowMode;
42import com.jme3.scene.Geometry;
43import com.jme3.scene.Node;
44import com.jme3.scene.Spatial;
45import com.jme3.scene.shape.Quad;
46import com.jme3.scene.shape.Sphere;
47import com.jme3.util.SkyFactory;
48import com.jme3.water.SimpleWaterProcessor;
49import java.io.File;
50
51public class TestSceneWater extends SimpleApplication {
52
53    // set default for applets
54    private static boolean useHttp = true;
55
56    public static void main(String[] args) {
57
58        TestSceneWater app = new TestSceneWater();
59        app.start();
60    }
61
62    public void simpleInitApp() {
63        this.flyCam.setMoveSpeed(10);
64        Node mainScene=new Node();
65        cam.setLocation(new Vector3f(-27.0f, 1.0f, 75.0f));
66        cam.setRotation(new Quaternion(0.03f, 0.9f, 0f, 0.4f));
67
68        // load sky
69        mainScene.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
70
71
72        File file = new File("wildhouse.zip");
73        if (file.exists()) {
74            useHttp = false;
75        }
76        // create the geometry and attach it
77        // load the level from zip or http zip
78        if (useHttp) {
79            assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip", HttpZipLocator.class.getName());
80        } else {
81            assetManager.registerLocator("wildhouse.zip", ZipLocator.class.getName());
82        }
83        Spatial scene = assetManager.loadModel("main.scene");
84
85        DirectionalLight sun = new DirectionalLight();
86        Vector3f lightDir=new Vector3f(-0.37352666f, -0.50444174f, -0.7784704f);
87        sun.setDirection(lightDir);
88        sun.setColor(ColorRGBA.White.clone().multLocal(2));
89        scene.addLight(sun);
90
91        Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
92        mat.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
93           //add lightPos Geometry
94        Sphere lite=new Sphere(8, 8, 3.0f);
95        Geometry lightSphere=new Geometry("lightsphere", lite);
96        lightSphere.setMaterial(mat);
97        Vector3f lightPos=lightDir.multLocal(-400);
98        lightSphere.setLocalTranslation(lightPos);
99        rootNode.attachChild(lightSphere);
100
101
102        SimpleWaterProcessor waterProcessor = new SimpleWaterProcessor(assetManager);
103        waterProcessor.setReflectionScene(mainScene);
104        waterProcessor.setDebug(false);
105        waterProcessor.setLightPosition(lightPos);
106        waterProcessor.setRefractionClippingOffset(1.0f);
107
108
109        //setting the water plane
110        Vector3f waterLocation=new Vector3f(0,-20,0);
111        waterProcessor.setPlane(new Plane(Vector3f.UNIT_Y, waterLocation.dot(Vector3f.UNIT_Y)));
112        WaterUI waterUi=new WaterUI(inputManager, waterProcessor);
113        waterProcessor.setWaterColor(ColorRGBA.Brown);
114        waterProcessor.setDebug(true);
115        //lower render size for higher performance
116//        waterProcessor.setRenderSize(128,128);
117        //raise depth to see through water
118//        waterProcessor.setWaterDepth(20);
119        //lower the distortion scale if the waves appear too strong
120//        waterProcessor.setDistortionScale(0.1f);
121        //lower the speed of the waves if they are too fast
122//        waterProcessor.setWaveSpeed(0.01f);
123
124        Quad quad = new Quad(400,400);
125
126        //the texture coordinates define the general size of the waves
127        quad.scaleTextureCoordinates(new Vector2f(6f,6f));
128
129        Geometry water=new Geometry("water", quad);
130        water.setShadowMode(ShadowMode.Receive);
131        water.setLocalRotation(new Quaternion().fromAngleAxis(-FastMath.HALF_PI, Vector3f.UNIT_X));
132        water.setMaterial(waterProcessor.getMaterial());
133        water.setLocalTranslation(-200, -20, 250);
134
135        rootNode.attachChild(water);
136
137        viewPort.addProcessor(waterProcessor);
138
139        mainScene.attachChild(scene);
140        rootNode.attachChild(mainScene);
141    }
142}
143