001: /*
002: * $RCSfile: DirectionalLight.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:21 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.Color3f;
035: import javax.vecmath.Vector3f;
036:
037: /**
038: * A DirectionalLight node defines an oriented light with an origin at
039: * infinity. It has the same attributes as a Light node, with the
040: * addition of a directional vector to specify the direction in which the
041: * light shines. A directional light has parallel light rays that travel
042: * in one direction along the specified vector. Directional light contributes
043: * to diffuse and specular reflections, which in turn depend on the
044: * orientation of an object's surface but not its position. A directional
045: * light does not contribute to ambient reflections.
046: */
047:
048: public class DirectionalLight extends Light {
049: /**
050: * Specifies that the Node allows access to its object's direction
051: * information.
052: */
053: public static final int ALLOW_DIRECTION_READ = CapabilityBits.DIRECTIONAL_LIGHT_ALLOW_DIRECTION_READ;
054:
055: /**
056: * Specifies that the Node allows writing to its object's direction
057: * information.
058: */
059: public static final int ALLOW_DIRECTION_WRITE = CapabilityBits.DIRECTIONAL_LIGHT_ALLOW_DIRECTION_WRITE;
060:
061: // Array for setting default read capabilities
062: private static final int[] readCapabilities = { ALLOW_DIRECTION_READ };
063:
064: /**
065: * Constructs a DirectionalLight node with default parameters.
066: * The default values are as follows:
067: * <ul>
068: * direction : (0,0,-1)<br>
069: * </ul>
070: */
071: public DirectionalLight() {
072: // set default read capabilities
073: setDefaultReadCapabilities(readCapabilities);
074: }
075:
076: /**
077: * Constructs and initializes a directional light.
078: * @param color the color of the light source
079: * @param direction the direction vector pointing from the light
080: * to the object
081: */
082: public DirectionalLight(Color3f color, Vector3f direction) {
083: super (color);
084:
085: // set default read capabilities
086: setDefaultReadCapabilities(readCapabilities);
087:
088: ((DirectionalLightRetained) this .retained)
089: .initDirection(direction);
090: }
091:
092: /**
093: * Constructs and initializes a directional light.
094: * @param lightOn flag indicating whether this light is on or off
095: * @param color the color of the light source
096: * @param direction the direction vector pointing from the light
097: * to the object
098: */
099: public DirectionalLight(boolean lightOn, Color3f color,
100: Vector3f direction) {
101: super (lightOn, color);
102:
103: // set default read capabilities
104: setDefaultReadCapabilities(readCapabilities);
105:
106: ((DirectionalLightRetained) this .retained)
107: .initDirection(direction);
108: }
109:
110: /**
111: * Creates the retained mode DirectionalLightRetained object that this
112: * DirectionalLight component object will point to.
113: */
114: void createRetained() {
115: this .retained = new DirectionalLightRetained();
116: this .retained.setSource(this );
117: }
118:
119: /**
120: * Set light direction.
121: * @param direction the new direction
122: * @exception CapabilityNotSetException if appropriate capability is
123: * not set and this object is part of live or compiled scene graph
124: */
125: public void setDirection(Vector3f direction) {
126: if (isLiveOrCompiled())
127: if (!this .getCapability(ALLOW_DIRECTION_WRITE))
128: throw new CapabilityNotSetException(J3dI18N
129: .getString("DirectionalLight0"));
130:
131: if (isLive())
132: ((DirectionalLightRetained) this .retained)
133: .setDirection(direction);
134: else
135: ((DirectionalLightRetained) this .retained)
136: .initDirection(direction);
137: }
138:
139: /**
140: * Set light direction.
141: * @param x the new X direction
142: * @param y the new Y direction
143: * @param z the new Z direction
144: * @exception CapabilityNotSetException if appropriate capability is
145: * not set and this object is part of live or compiled scene graph
146: */
147: public void setDirection(float x, float y, float z) {
148: if (isLiveOrCompiled())
149: if (!this .getCapability(ALLOW_DIRECTION_WRITE))
150: throw new CapabilityNotSetException(J3dI18N
151: .getString("DirectionalLight1"));
152:
153: if (isLive())
154: ((DirectionalLightRetained) this .retained).setDirection(x,
155: y, z);
156: else
157: ((DirectionalLightRetained) this .retained).initDirection(x,
158: y, z);
159: }
160:
161: /**
162: * Gets this Light's current direction and places it in the parameter specified.
163: * @param direction the vector that will receive this node's direction
164: * @exception CapabilityNotSetException if appropriate capability is
165: * not set and this object is part of live or compiled scene graph
166: */
167: public void getDirection(Vector3f direction) {
168: if (isLiveOrCompiled())
169: if (!this .getCapability(ALLOW_DIRECTION_READ))
170: throw new CapabilityNotSetException(J3dI18N
171: .getString("DirectionalLight2"));
172:
173: ((DirectionalLightRetained) this .retained)
174: .getDirection(direction);
175: }
176:
177: /**
178: * Used to create a new instance of the node. This routine is called
179: * by <code>cloneTree</code> to duplicate the current node.
180: * @param forceDuplicate when set to <code>true</code>, causes the
181: * <code>duplicateOnCloneTree</code> flag to be ignored. When
182: * <code>false</code>, the value of each node's
183: * <code>duplicateOnCloneTree</code> variable determines whether
184: * NodeComponent data is duplicated or copied.
185: *
186: * @see Node#cloneTree
187: * @see Node#cloneNode
188: * @see Node#duplicateNode
189: * @see NodeComponent#setDuplicateOnCloneTree
190: */
191: public Node cloneNode(boolean forceDuplicate) {
192: DirectionalLight d = new DirectionalLight();
193: d.duplicateNode(this , forceDuplicate);
194: return d;
195: }
196:
197: /**
198: * Copies all DirectionalLight information from
199: * <code>originalNode</code> into
200: * the current node. This method is called from the
201: * <code>cloneNode</code> method which is, in turn, called by the
202: * <code>cloneTree</code> method.<P>
203: *
204: * @param originalNode the original node to duplicate.
205: * @param forceDuplicate when set to <code>true</code>, causes the
206: * <code>duplicateOnCloneTree</code> flag to be ignored. When
207: * <code>false</code>, the value of each node's
208: * <code>duplicateOnCloneTree</code> variable determines whether
209: * NodeComponent data is duplicated or copied.
210: *
211: * @exception RestrictedAccessException if this object is part of a live
212: * or compiled scenegraph.
213: *
214: * @see Node#duplicateNode
215: * @see Node#cloneTree
216: * @see NodeComponent#setDuplicateOnCloneTree
217: */
218: void duplicateAttributes(Node originalNode, boolean forceDuplicate) {
219: super .duplicateAttributes(originalNode, forceDuplicate);
220:
221: Vector3f v = new Vector3f();
222: ((DirectionalLightRetained) originalNode.retained)
223: .getDirection(v);
224: ((DirectionalLightRetained) retained).initDirection(v);
225: }
226: }
|