159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.light;
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bounding.BoundingVolume;
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.*;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.FastMath;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Spatial;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Represents a spot light.
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A spot light emmit a cone of light from a position and in a direction.
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * It can be used to fake torch lights or car's lights.
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * <p>
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * In addition to a position and a direction, spot lights also have a range which
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * can be used to attenuate the influence of the light depending on the
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * distance between the light and the effected object.
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Also the angle of the cone can be tweaked by changing the spot inner angle and the spot outer angle.
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the spot inner angle determin the cone of light where light has full influence.
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the spot outer angle determin the cone global cone of light of the spot light.
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * the light intensity slowly decrease between the inner cone and the outer cone.
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *  @author Nehon
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class SpotLight extends Light implements Savable {
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected Vector3f position = new Vector3f();
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected Vector3f direction = new Vector3f(0,-1,0);
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected float spotInnerAngle = FastMath.QUARTER_PI / 8;
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected float spotOuterAngle = FastMath.QUARTER_PI / 6;
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected float spotRange = 100;
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected float invSpotRange = 1 / 100;
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected float packedAngleCos=0;
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public SpotLight() {
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super();
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        computePackedCos();
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private void computePackedCos() {
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        float innerCos=FastMath.cos(spotInnerAngle);
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        float outerCos=FastMath.cos(spotOuterAngle);
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        packedAngleCos=(int)(innerCos*1000);
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        packedAngleCos+=outerCos;
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    protected void computeLastDistance(Spatial owner) {
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (owner.getWorldBound() != null) {
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            BoundingVolume bv = owner.getWorldBound();
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            lastDistance = bv.distanceSquaredTo(position);
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            lastDistance = owner.getWorldTranslation().distanceSquared(position);
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Type getType() {
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return Type.Spot;
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Vector3f getDirection() {
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return direction;
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setDirection(Vector3f direction) {
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.direction.set(direction);
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public Vector3f getPosition() {
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return position;
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setPosition(Vector3f position) {
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.position.set(position);
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getSpotRange() {
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return spotRange;
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Set the range of the light influence.
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * <p>
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Setting a non-zero range indicates the light should use attenuation.
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * If a pixel's distance to this light's position
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * is greater than the light's range, then the pixel will not be
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * effected by this light, if the distance is less than the range, then
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * the magnitude of the influence is equal to distance / range.
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param spotRange the range of the light influence.
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     *
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @throws IllegalArgumentException If spotRange is negative
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setSpotRange(float spotRange) {
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (spotRange < 0) {
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            throw new IllegalArgumentException("SpotLight range cannot be negative");
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.spotRange = spotRange;
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (spotRange != 0) {
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.invSpotRange = 1 / spotRange;
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.invSpotRange = 0;
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * for internal use only
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the inverse of the spot range
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getInvSpotRange() {
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return invSpotRange;
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the spot inner angle
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the spot inner angle
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getSpotInnerAngle() {
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return spotInnerAngle;
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Sets the inner angle of the cone of influence.
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * This angle is the angle between the spot direction axis and the inner border of the cone of influence.
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param spotInnerAngle
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setSpotInnerAngle(float spotInnerAngle) {
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.spotInnerAngle = spotInnerAngle;
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        computePackedCos();
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * returns the spot outer angle
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the spot outer angle
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getSpotOuterAngle() {
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return spotOuterAngle;
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * Sets the outer angle of the cone of influence.
17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * This angle is the angle between the spot direction axis and the outer border of the cone of influence.
17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * this should be greater than the inner angle or the result will be unexpected.
17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @param spotOuterAngle
17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setSpotOuterAngle(float spotOuterAngle) {
17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.spotOuterAngle = spotOuterAngle;
17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        computePackedCos();
18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * for internal use only
18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the cosines of the inner and outter angle packed in a float
18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getPackedAngleCos() {
18759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return packedAngleCos;
18859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
18959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
19259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
19359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void write(JmeExporter ex) throws IOException {
19459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super.write(ex);
19559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        OutputCapsule oc = ex.getCapsule(this);
19659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(direction, "direction", new Vector3f());
19759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(position, "position", new Vector3f());
19859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(spotInnerAngle, "spotInnerAngle", FastMath.QUARTER_PI / 8);
19959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(spotOuterAngle, "spotOuterAngle", FastMath.QUARTER_PI / 6);
20059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        oc.write(spotRange, "spotRange", 100);
20159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
20259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
20359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
20459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void read(JmeImporter im) throws IOException {
20559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        super.read(im);
20659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        InputCapsule ic = im.getCapsule(this);
20759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        spotInnerAngle = ic.readFloat("spotInnerAngle", FastMath.QUARTER_PI / 8);
20859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        spotOuterAngle = ic.readFloat("spotOuterAngle", FastMath.QUARTER_PI / 6);
20959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        direction = (Vector3f) ic.readSavable("direction", new Vector3f());
21059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        position = (Vector3f) ic.readSavable("position", new Vector3f());
21159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        spotRange = ic.readFloat("spotRange", 100);
21259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (spotRange != 0) {
21359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.invSpotRange = 1 / spotRange;
21459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
21559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.invSpotRange = 0;
21659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
21759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
21859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
219