001: /*
002: * $RCSfile: ExponentialFog.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:
036: /**
037: * The ExponentialFog leaf node extends the Fog leaf node by adding a
038: * fog density that is used as the exponent of the fog equation. The
039: * density is defined in the local coordinate system of the node, but
040: * the actual fog equation will ideally take place in eye coordinates.
041: * <P>
042: * The fog blending factor, f, is computed as follows:
043: * <P><UL>
044: * f = e<sup>-(density * z)</sup><P>
045: * where
046: * <ul>z is the distance from the viewpoint.<br>
047: * density is the density of the fog.<P></ul></UL>
048: *
049: * In addition to specifying the fog density, ExponentialFog lets you
050: * specify the fog color, which is represented by R, G, and B
051: * color values, where a color of (0,0,0) represents black.
052: */
053: public class ExponentialFog extends Fog {
054: /**
055: * Specifies that this ExponentialFog node allows read access to its
056: * density information.
057: */
058: public static final int ALLOW_DENSITY_READ = CapabilityBits.EXPONENTIAL_FOG_ALLOW_DENSITY_READ;
059:
060: /**
061: * Specifies that this ExponentialFog node allows write access to its
062: * density information.
063: */
064: public static final int ALLOW_DENSITY_WRITE = CapabilityBits.EXPONENTIAL_FOG_ALLOW_DENSITY_WRITE;
065:
066: // Array for setting default read capabilities
067: private static final int[] readCapabilities = { ALLOW_DENSITY_READ };
068:
069: /**
070: * Constructs an ExponentialFog node with default parameters.
071: * The default values are as follows:
072: * <ul>
073: * density : 1.0<br>
074: * </ul>
075: */
076: public ExponentialFog() {
077: // Just use the defaults
078: // set default read capabilities
079: setDefaultReadCapabilities(readCapabilities);
080: }
081:
082: /**
083: * Constructs an ExponentialFog node with the specified fog color.
084: * @param color the fog color
085: */
086: public ExponentialFog(Color3f color) {
087: super (color);
088:
089: // set default read capabilities
090: setDefaultReadCapabilities(readCapabilities);
091: }
092:
093: /**
094: * Constructs an ExponentialFog node with the specified fog color
095: * and density.
096: * @param color the fog color
097: * @param density the density of the fog
098: */
099: public ExponentialFog(Color3f color, float density) {
100: super (color);
101:
102: // set default read capabilities
103: setDefaultReadCapabilities(readCapabilities);
104:
105: ((ExponentialFogRetained) this .retained).initDensity(density);
106: }
107:
108: /**
109: * Constructs an ExponentialFog node with the specified fog color.
110: * @param r the red component of the fog color
111: * @param g the green component of the fog color
112: * @param b the blue component of the fog color
113: */
114: public ExponentialFog(float r, float g, float b) {
115: super (r, g, b);
116:
117: // set default read capabilities
118: setDefaultReadCapabilities(readCapabilities);
119: }
120:
121: /**
122: * Constructs an ExponentialFog node with the specified fog color
123: * and density.
124: * @param r the red component of the fog color
125: * @param g the green component of the fog color
126: * @param b the blue component of the fog color
127: * @param density the density of the fog
128: */
129: public ExponentialFog(float r, float g, float b, float density) {
130: super (r, g, b);
131:
132: // set default read capabilities
133: setDefaultReadCapabilities(readCapabilities);
134:
135: ((ExponentialFogRetained) this .retained).initDensity(density);
136: }
137:
138: /**
139: * Sets fog density.
140: * @param density the new density of this fog
141: * @exception CapabilityNotSetException if appropriate capability is
142: * not set and this object is part of live or compiled scene graph
143: */
144: public void setDensity(float density) {
145: if (isLiveOrCompiled())
146: if (!this .getCapability(ALLOW_DENSITY_WRITE))
147: throw new CapabilityNotSetException(J3dI18N
148: .getString("ExponentialFog0"));
149:
150: if (isLive())
151: ((ExponentialFogRetained) this .retained)
152: .setDensity(density);
153: else
154: ((ExponentialFogRetained) this .retained)
155: .initDensity(density);
156: }
157:
158: /**
159: * Gets fog density.
160: * @return the density of this fog
161: * @exception CapabilityNotSetException if appropriate capability is
162: * not set and this object is part of live or compiled scene graph
163: */
164: public float getDensity() {
165: if (isLiveOrCompiled())
166: if (!this .getCapability(ALLOW_DENSITY_READ))
167: throw new CapabilityNotSetException(J3dI18N
168: .getString("ExponentialFog1"));
169:
170: return ((ExponentialFogRetained) this .retained).getDensity();
171: }
172:
173: /**
174: * Creates the retained mode ExponentialFogRetained object that this
175: * ExponentialFog node will point to.
176: */
177: void createRetained() {
178: this .retained = new ExponentialFogRetained();
179: this .retained.setSource(this );
180: }
181:
182: /**
183: * Used to create a new instance of the node. This routine is called
184: * by <code>cloneTree</code> to duplicate the current node.
185: * @param forceDuplicate when set to <code>true</code>, causes the
186: * <code>duplicateOnCloneTree</code> flag to be ignored. When
187: * <code>false</code>, the value of each node's
188: * <code>duplicateOnCloneTree</code> variable determines whether
189: * NodeComponent data is duplicated or copied.
190: *
191: * @see Node#cloneTree
192: * @see Node#cloneNode
193: * @see Node#duplicateNode
194: * @see NodeComponent#setDuplicateOnCloneTree
195: */
196: public Node cloneNode(boolean forceDuplicate) {
197: ExponentialFog ef = new ExponentialFog();
198: ef.duplicateNode(this , forceDuplicate);
199: return ef;
200: }
201:
202: /**
203: * Copies all ExponentialFog information from
204: * <code>originalNode</code> into
205: * the current node. This method is called from the
206: * <code>cloneNode</code> method which is, in turn, called by the
207: * <code>cloneTree</code> method.<P>
208: *
209: * @param originalNode the original node to duplicate.
210: * @param forceDuplicate when set to <code>true</code>, causes the
211: * <code>duplicateOnCloneTree</code> flag to be ignored. When
212: * <code>false</code>, the value of each node's
213: * <code>duplicateOnCloneTree</code> variable determines whether
214: * NodeComponent data is duplicated or copied.
215: *
216: * @exception RestrictedAccessException if this object is part of a live
217: * or compiled scenegraph.
218: *
219: * @see Node#duplicateNode
220: * @see Node#cloneTree
221: * @see NodeComponent#setDuplicateOnCloneTree
222: */
223: void duplicateAttributes(Node originalNode, boolean forceDuplicate) {
224: super .duplicateAttributes(originalNode, forceDuplicate);
225:
226: ((ExponentialFogRetained) retained)
227: .initDensity(((ExponentialFogRetained) originalNode.retained)
228: .getDensity());
229: }
230: }
|