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 com.jme3.light;
34 
35 import com.jme3.export.*;
36 import com.jme3.math.ColorRGBA;
37 import com.jme3.scene.Spatial;
38 import java.io.IOException;
39 
40 /**
41  * Abstract class for representing a light source.
42  * <p>
43  * All light source types have a color.
44  */
45 public abstract class Light implements Savable, Cloneable {
46 
47     /**
48      * Describes the light type.
49      */
50     public enum Type {
51 
52         /**
53          * Directional light
54          *
55          * @see DirectionalLight
56          */
57         Directional(0),
58 
59         /**
60          * Point light
61          *
62          * @see PointLight
63          */
64         Point(1),
65 
66         /**
67          * Spot light.
68          * <p>
69          * Not supported by jMonkeyEngine
70          */
71         Spot(2),
72 
73         /**
74          * Ambient light
75          *
76          * @see AmbientLight
77          */
78         Ambient(3);
79 
80         private int typeId;
81 
Type(int type)82         Type(int type){
83             this.typeId = type;
84         }
85 
86         /**
87          * Returns an index for the light type
88          * @return an index for the light type
89          */
getId()90         public int getId(){
91             return typeId;
92         }
93     }
94 
95     protected ColorRGBA color = new ColorRGBA(1f,1f,1f,1f);
96 
97     /**
98      * Used in LightList for caching the distance
99      * to the owner spatial. Should be reset after the sorting.
100      */
101     protected transient float lastDistance = -1;
102 
103     /**
104      * If light is disabled, it will not have any
105      */
106     protected boolean enabled = true;
107 
108     /**
109      * The light name.
110      */
111     protected String name;
112 
113     /**
114      * Returns the color of the light.
115      *
116      * @return The color of the light.
117      */
getColor()118     public ColorRGBA getColor() {
119         return color;
120     }
121 
122     /**
123      * This method sets the light name.
124      *
125      * @param name the light name
126      */
setName(String name)127     public void setName(String name) {
128         this.name = name;
129     }
130 
131     /**
132      * Return the light name.
133      *
134      * @return the light name
135      */
getName()136     public String getName() {
137         return name;
138     }
139 
140     /*
141     public void setLastDistance(float lastDistance){
142         this.lastDistance = lastDistance;
143     }
144 
145     public float getLastDistance(){
146         return lastDistance;
147     }
148     */
149 
150     /**
151      * Sets the light color.
152      *
153      * @param color the light color.
154      */
setColor(ColorRGBA color)155     public void setColor(ColorRGBA color){
156         this.color.set(color);
157     }
158 
159 
160     /*
161      * Returns true if the light is enabled
162      *
163      * @return true if the light is enabled
164      *
165      * @see Light#setEnabled(boolean)
166      */
167     /*
168     public boolean isEnabled() {
169         return enabled;
170     }
171     */
172 
173     @Override
clone()174     public Light clone(){
175         try {
176             return (Light) super.clone();
177         } catch (CloneNotSupportedException ex) {
178             throw new AssertionError();
179         }
180     }
181 
write(JmeExporter ex)182     public void write(JmeExporter ex) throws IOException {
183         OutputCapsule oc = ex.getCapsule(this);
184         oc.write(color, "color", null);
185         oc.write(enabled, "enabled", true);
186         oc.write(name, "name", null);
187     }
188 
read(JmeImporter im)189     public void read(JmeImporter im) throws IOException {
190         InputCapsule ic = im.getCapsule(this);
191         color = (ColorRGBA) ic.readSavable("color", null);
192         enabled = ic.readBoolean("enabled", true);
193         name = ic.readString("name", null);
194     }
195 
196     /**
197      * Used internally to compute the last distance value.
198      */
computeLastDistance(Spatial owner)199     protected abstract void computeLastDistance(Spatial owner);
200 
201     /**
202      * Returns the light type
203      *
204      * @return the light type
205      *
206      * @see Type
207      */
getType()208     public abstract Type getType();
209 
210 }
211