Home | History | Annotate | Download | only in light
      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.light;
     34 
     35 import com.jme3.app.SimpleApplication;
     36 import com.jme3.light.AmbientLight;
     37 import com.jme3.light.SpotLight;
     38 import com.jme3.material.Material;
     39 import com.jme3.math.*;
     40 import com.jme3.renderer.queue.RenderQueue.ShadowMode;
     41 import com.jme3.scene.Geometry;
     42 import com.jme3.scene.Spatial;
     43 import com.jme3.scene.shape.Box;
     44 import com.jme3.scene.shape.Sphere;
     45 import com.jme3.texture.Texture.WrapMode;
     46 import com.jme3.util.TangentBinormalGenerator;
     47 
     48 public class TestSpotLight extends SimpleApplication {
     49 
     50     private Vector3f lightTarget = new Vector3f(12, 3.5f, 30);
     51 
     52     public static void main(String[] args){
     53         TestSpotLight app = new TestSpotLight();
     54         app.start();
     55     }
     56 
     57  SpotLight spot;
     58     Geometry lightMdl;
     59     public void setupLighting(){
     60       AmbientLight al=new AmbientLight();
     61       al.setColor(ColorRGBA.White.mult(0.8f));
     62       rootNode.addLight(al);
     63 
     64       spot=new SpotLight();
     65 
     66       spot.setSpotRange(1000);
     67       spot.setSpotInnerAngle(5*FastMath.DEG_TO_RAD);
     68       spot.setSpotOuterAngle(10*FastMath.DEG_TO_RAD);
     69       spot.setPosition(new Vector3f(77.70334f, 34.013165f, 27.1017f));
     70       spot.setDirection(lightTarget.subtract(spot.getPosition()));
     71       spot.setColor(ColorRGBA.White.mult(2));
     72       rootNode.addLight(spot);
     73 
     74 
     75 //        PointLight pl=new PointLight();
     76 //      pl.setPosition(new Vector3f(77.70334f, 34.013165f, 27.1017f));
     77 //      pl.setRadius(1000);
     78 //      pl.setColor(ColorRGBA.White.mult(2));
     79 //      rootNode.addLight(pl);
     80        lightMdl = new Geometry("Light", new Sphere(10, 10, 0.1f));
     81       lightMdl.setMaterial(assetManager.loadMaterial("Common/Materials/RedColor.j3m"));
     82       lightMdl.setLocalTranslation(new Vector3f(77.70334f, 34.013165f, 27.1017f));
     83       lightMdl.setLocalScale(5);
     84       rootNode.attachChild(lightMdl);
     85 
     86 //        DirectionalLight dl = new DirectionalLight();
     87 //        dl.setDirection(lightTarget.subtract(new Vector3f(77.70334f, 34.013165f, 27.1017f)));
     88 //        dl.setColor(ColorRGBA.White.mult(2));
     89 //        rootNode.addLight(dl);
     90 
     91 
     92     }
     93 
     94     public void setupFloor(){
     95         Material mat = assetManager.loadMaterial("Textures/Terrain/Pond/Pond.j3m");
     96         mat.getTextureParam("DiffuseMap").getTextureValue().setWrap(WrapMode.Repeat);
     97         mat.getTextureParam("NormalMap").getTextureValue().setWrap(WrapMode.Repeat);
     98        // mat.getTextureParam("ParallaxMap").getTextureValue().setWrap(WrapMode.Repeat);
     99         mat.setFloat("Shininess",3);
    100       //  mat.setBoolean("VertexLighting", true);
    101 
    102 
    103         Box floor = new Box(Vector3f.ZERO, 50, 1f, 50);
    104         TangentBinormalGenerator.generate(floor);
    105         floor.scaleTextureCoordinates(new Vector2f(5, 5));
    106         Geometry floorGeom = new Geometry("Floor", floor);
    107         floorGeom.setMaterial(mat);
    108         floorGeom.setShadowMode(ShadowMode.Receive);
    109         rootNode.attachChild(floorGeom);
    110     }
    111 
    112 
    113 
    114     public void setupSignpost(){
    115         Spatial signpost = assetManager.loadModel("Models/Sign Post/Sign Post.mesh.xml");
    116         Material mat = assetManager.loadMaterial("Models/Sign Post/Sign Post.j3m");
    117       //   mat.setBoolean("VertexLighting", true);
    118         signpost.setMaterial(mat);
    119         signpost.rotate(0, FastMath.HALF_PI, 0);
    120         signpost.setLocalTranslation(12, 3.5f, 30);
    121         signpost.setLocalScale(4);
    122         signpost.setShadowMode(ShadowMode.CastAndReceive);
    123         TangentBinormalGenerator.generate(signpost);
    124         rootNode.attachChild(signpost);
    125     }
    126 
    127     @Override
    128     public void simpleInitApp() {
    129         cam.setLocation(new Vector3f(27.492603f, 29.138166f, -13.232513f));
    130         cam.setRotation(new Quaternion(0.25168246f, -0.10547892f, 0.02760565f, 0.96164864f));
    131         flyCam.setMoveSpeed(30);
    132 
    133         setupLighting();
    134         setupFloor();
    135         setupSignpost();
    136 
    137 
    138     }
    139 
    140     float angle;
    141 
    142     @Override
    143     public void simpleUpdate(float tpf) {
    144         super.simpleUpdate(tpf);
    145         angle += tpf;
    146         angle %= FastMath.TWO_PI;
    147 
    148         spot.setPosition(new Vector3f(FastMath.cos(angle) * 30f, 34.013165f, FastMath.sin(angle) * 30f));
    149         lightMdl.setLocalTranslation(spot.getPosition());
    150         spot.setDirection(lightTarget.subtract(spot.getPosition()));
    151     }
    152 
    153 
    154 
    155 }
    156