001: /*
002: * $RCSfile: PointLight.java,v $
003: *
004: * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
006: *
007: * This code is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License version 2 only, as
009: * published by the Free Software Foundation. Sun designates this
010: * particular file as subject to the "Classpath" exception as provided
011: * by Sun in the LICENSE file that accompanied this code.
012: *
013: * This code is distributed in the hope that it will be useful, but WITHOUT
014: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: * version 2 for more details (a copy is included in the LICENSE file that
017: * accompanied this code).
018: *
019: * You should have received a copy of the GNU General Public License version
020: * 2 along with this work; if not, write to the Free Software Foundation,
021: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
022: *
023: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
024: * CA 95054 USA or visit www.sun.com if you need additional information or
025: * have any questions.
026: *
027: * $Revision: 1.6 $
028: * $Date: 2008/02/28 20:17:28 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.Point3f;
035: import javax.vecmath.Color3f;
036:
037: /**
038: * The PointLight object specifies an attenuated light source at a
039: * fixed point in space that radiates light equally in all directions
040: * away from the light source. PointLight has the same attributes as
041: * a Light node, with the addition of location and attenuation
042: * parameters.
043: * <p>
044: * A point light contributes to diffuse and specular reflections,
045: * which in turn depend on the orientation and position of a
046: * surface. A point light does not contribute to ambient reflections.
047: * <p>
048: * A PointLight is attenuated by multiplying the contribution of the
049: * light by an attenuation factor. The attenuation factor causes the
050: * the PointLight's brightness to decrease as distance from the light
051: * source increases.
052: * A PointLight's attenuation factor contains three values:
053: * <P><UL>
054: * <LI>Constant attenuation</LI>
055: * <LI>Linear attenuation</LI>
056: * <LI>Quadratic attenuation</LI></UL>
057: * <p>
058: * A PointLight is attenuated by the reciprocal of the sum of:
059: * <p>
060: * <ul>
061: * The constant attenuation factor<br>
062: * The Linear attenuation factor times the distance between the light
063: * and the vertex being illuminated<br>
064: * The quadratic attenuation factor times the square of the distance
065: * between the light and the vertex
066: * </ul>
067: * <p>
068: * By default, the constant attenuation value is 1 and the other
069: * two values are 0, resulting in no attenuation.
070: */
071:
072: public class PointLight extends Light {
073: /**
074: * Specifies that this PointLight node allows reading its position
075: * information.
076: */
077: public static final int ALLOW_POSITION_READ = CapabilityBits.POINT_LIGHT_ALLOW_POSITION_READ;
078:
079: /**
080: * Specifies that this PointLight node allows writing its position
081: * information.
082: */
083: public static final int ALLOW_POSITION_WRITE = CapabilityBits.POINT_LIGHT_ALLOW_POSITION_WRITE;
084:
085: /**
086: * Specifies that this PointLight node allows reading its attenuation
087: * information.
088: */
089: public static final int ALLOW_ATTENUATION_READ = CapabilityBits.POINT_LIGHT_ALLOW_ATTENUATION_READ;
090:
091: /**
092: * Specifies that this PointLight node allows writing its attenuation
093: * information.
094: */
095: public static final int ALLOW_ATTENUATION_WRITE = CapabilityBits.POINT_LIGHT_ALLOW_ATTENUATION_WRITE;
096:
097: // Array for setting default read capabilities
098: private static final int[] readCapabilities = {
099: ALLOW_POSITION_READ, ALLOW_ATTENUATION_READ };
100:
101: /**
102: * Constructs a PointLight node with default parameters.
103: * The default values are as follows:
104: * <ul>
105: * position : (0,0,0)<br>
106: * attenuation : (1,0,0)<br>
107: * </ul>
108: */
109: public PointLight() {
110: // set default read capabilities
111: setDefaultReadCapabilities(readCapabilities);
112: }
113:
114: /**
115: * Constructs and initializes a point light.
116: * @param color the color of the light source
117: * @param position the position of the light in three-space
118: * @param attenuation the attenutation (constant, linear, quadratic) of the light
119: */
120: public PointLight(Color3f color, Point3f position,
121: Point3f attenuation) {
122: super (color);
123:
124: // set default read capabilities
125: setDefaultReadCapabilities(readCapabilities);
126:
127: ((PointLightRetained) this .retained).initPosition(position);
128: ((PointLightRetained) this .retained)
129: .initAttenuation(attenuation);
130: }
131:
132: /**
133: * Constructs and initializes a point light.
134: * @param lightOn flag indicating whether this light is on or off
135: * @param color the color of the light source
136: * @param position the position of the light in three-space
137: * @param attenuation the attenuation (constant, linear, quadratic) of the light
138: */
139: public PointLight(boolean lightOn, Color3f color, Point3f position,
140: Point3f attenuation) {
141: super (lightOn, color);
142:
143: // set default read capabilities
144: setDefaultReadCapabilities(readCapabilities);
145:
146: ((PointLightRetained) this .retained).initPosition(position);
147: ((PointLightRetained) this .retained)
148: .initAttenuation(attenuation);
149: }
150:
151: /**
152: * Creates the retained mode PointLightRetained object that this
153: * PointLight component object will point to.
154: */
155: void createRetained() {
156: this .retained = new PointLightRetained();
157: this .retained.setSource(this );
158: }
159:
160: /**
161: * Set light position.
162: * @param position the new position
163: * @exception CapabilityNotSetException if appropriate capability is
164: * not set and this object is part of live or compiled scene graph
165: */
166: public void setPosition(Point3f position) {
167: if (isLiveOrCompiled())
168: if (!this .getCapability(ALLOW_POSITION_WRITE))
169: throw new CapabilityNotSetException(J3dI18N
170: .getString("PointLight0"));
171:
172: if (isLive())
173: ((PointLightRetained) this .retained).setPosition(position);
174: else
175: ((PointLightRetained) this .retained).initPosition(position);
176: }
177:
178: /**
179: * Set light position.
180: * @param x the new X position
181: * @param y the new Y position
182: * @param z the new Z position
183: * @exception CapabilityNotSetException if appropriate capability is
184: * not set and this object is part of live or compiled scene graph
185: */
186: public void setPosition(float x, float y, float z) {
187: if (isLiveOrCompiled())
188: if (!this .getCapability(ALLOW_POSITION_WRITE))
189: throw new CapabilityNotSetException(J3dI18N
190: .getString("PointLight1"));
191:
192: if (isLive())
193: ((PointLightRetained) this .retained).setPosition(x, y, z);
194: else
195: ((PointLightRetained) this .retained).initPosition(x, y, z);
196: }
197:
198: /**
199: * Gets this Light's current position and places it in the parameter specified.
200: * @param position the vector that will receive this node's position
201: * @exception CapabilityNotSetException if appropriate capability is
202: * not set and this object is part of live or compiled scene graph
203: */
204: public void getPosition(Point3f position) {
205: if (isLiveOrCompiled())
206: if (!this .getCapability(ALLOW_POSITION_READ))
207: throw new CapabilityNotSetException(J3dI18N
208: .getString("PointLight2"));
209:
210: ((PointLightRetained) this .retained).getPosition(position);
211: }
212:
213: /**
214: * Sets this Light's current attenuation values and places it in the parameter specified.
215: * @param attenuation the vector that will receive the attenuation values
216: * @exception CapabilityNotSetException if appropriate capability is
217: * not set and this object is part of live or compiled scene graph
218: */
219: public void setAttenuation(Point3f attenuation) {
220: if (isLiveOrCompiled())
221: if (!this .getCapability(ALLOW_ATTENUATION_WRITE))
222: throw new CapabilityNotSetException(J3dI18N
223: .getString("PointLight3"));
224:
225: if (isLive())
226: ((PointLightRetained) this .retained)
227: .setAttenuation(attenuation);
228: else
229: ((PointLightRetained) this .retained)
230: .initAttenuation(attenuation);
231: }
232:
233: /**
234: * Sets this Light's current attenuation values and places it in the parameter specified.
235: * @param constant the light's constant attenuation
236: * @param linear the light's linear attenuation
237: * @param quadratic the light's quadratic attenuation
238: * @exception CapabilityNotSetException if appropriate capability is
239: * not set and this object is part of live or compiled scene graph
240: */
241: public void setAttenuation(float constant, float linear,
242: float quadratic) {
243: if (isLiveOrCompiled())
244: if (!this .getCapability(ALLOW_ATTENUATION_WRITE))
245: throw new CapabilityNotSetException(J3dI18N
246: .getString("PointLight3"));
247:
248: if (isLive())
249: ((PointLightRetained) this .retained).setAttenuation(
250: constant, linear, quadratic);
251: else
252: ((PointLightRetained) this .retained).initAttenuation(
253: constant, linear, quadratic);
254: }
255:
256: /**
257: * Gets this Light's current attenuation values and places it in the parameter specified.
258: * @param attenuation the vector that will receive the attenuation values
259: * @exception CapabilityNotSetException if appropriate capability is
260: * not set and this object is part of live or compiled scene graph
261: */
262: public void getAttenuation(Point3f attenuation) {
263: if (isLiveOrCompiled())
264: if (!this .getCapability(ALLOW_ATTENUATION_READ))
265: throw new CapabilityNotSetException(J3dI18N
266: .getString("PointLight5"));
267:
268: ((PointLightRetained) this .retained)
269: .getAttenuation(attenuation);
270: }
271:
272: /**
273: * Used to create a new instance of the node. This routine is called
274: * by <code>cloneTree</code> to duplicate the current node.
275: * @param forceDuplicate when set to <code>true</code>, causes the
276: * <code>duplicateOnCloneTree</code> flag to be ignored. When
277: * <code>false</code>, the value of each node's
278: * <code>duplicateOnCloneTree</code> variable determines whether
279: * NodeComponent data is duplicated or copied.
280: *
281: * @see Node#cloneTree
282: * @see Node#cloneNode
283: * @see Node#duplicateNode
284: * @see NodeComponent#setDuplicateOnCloneTree
285: */
286: public Node cloneNode(boolean forceDuplicate) {
287: PointLight p = new PointLight();
288: p.duplicateNode(this , forceDuplicate);
289: return p;
290: }
291:
292: /**
293: * Copies all PointLight information from
294: * <code>originalNode</code> into
295: * the current node. This method is called from the
296: * <code>cloneNode</code> method which is, in turn, called by the
297: * <code>cloneTree</code> method.<P>
298: *
299: * @param originalNode the original node to duplicate.
300: * @param forceDuplicate when set to <code>true</code>, causes the
301: * <code>duplicateOnCloneTree</code> flag to be ignored. When
302: * <code>false</code>, the value of each node's
303: * <code>duplicateOnCloneTree</code> variable determines whether
304: * NodeComponent data is duplicated or copied.
305: *
306: * @exception RestrictedAccessException if this object is part of a live
307: * or compiled scenegraph.
308: *
309: * @see Node#duplicateNode
310: * @see Node#cloneTree
311: * @see NodeComponent#setDuplicateOnCloneTree
312: */
313: void duplicateAttributes(Node originalNode, boolean forceDuplicate) {
314: super .duplicateAttributes(originalNode, forceDuplicate);
315:
316: PointLightRetained attr = (PointLightRetained) originalNode.retained;
317: PointLightRetained rt = (PointLightRetained) retained;
318:
319: Point3f p = new Point3f();
320: attr.getPosition(p);
321: rt.initPosition(p);
322:
323: attr.getAttenuation(p);
324: rt.initAttenuation(p);
325: }
326: }
|