001: /*
002: * $RCSfile: LinearFogRetained.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.7 $
028: * $Date: 2008/02/28 20:17:25 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.util.Vector;
035: import javax.vecmath.*;
036: import java.util.ArrayList;
037:
038: /**
039: * The LinearFog leaf node defines distance parameters for
040: * linear fog.
041: */
042: class LinearFogRetained extends FogRetained {
043: /**
044: * Fog front and back distance
045: */
046: private double frontDistance = 0.1;
047: private double backDistance = 1.0;
048: private double frontDistanceInEc;
049: private double backDistanceInEc;
050:
051: // dirty bits for LinearFog
052: static final int FRONT_DISTANCE_CHANGED = FogRetained.LAST_DEFINED_BIT << 1;
053: static final int BACK_DISTANCE_CHANGED = FogRetained.LAST_DEFINED_BIT << 2;
054:
055: LinearFogRetained() {
056: this .nodeType = NodeRetained.LINEARFOG;
057: }
058:
059: /**
060: * Initializes front distance for fog before the object is live
061: */
062: void initFrontDistance(double frontDistance) {
063: this .frontDistance = frontDistance;
064: }
065:
066: /**
067: * Sets front distance for fog and sends a message
068: */
069: void setFrontDistance(double frontDistance) {
070: this .frontDistance = frontDistance;
071: J3dMessage createMessage = new J3dMessage();
072: createMessage.threads = targetThreads;
073: createMessage.type = J3dMessage.FOG_CHANGED;
074: createMessage.universe = universe;
075: createMessage.args[0] = this ;
076: createMessage.args[1] = new Integer(FRONT_DISTANCE_CHANGED);
077: createMessage.args[2] = new Double(frontDistance);
078: VirtualUniverse.mc.processMessage(createMessage);
079:
080: }
081:
082: /**
083: * Gets front distance for fog
084: */
085: double getFrontDistance() {
086: return this .frontDistance;
087: }
088:
089: /**
090: * Initializes back distance for fog
091: */
092: void initBackDistance(double backDistance) {
093: this .backDistance = backDistance;
094: }
095:
096: /**
097: * Sets back distance for fog
098: */
099: void setBackDistance(double backDistance) {
100: this .backDistance = backDistance;
101: J3dMessage createMessage = new J3dMessage();
102: createMessage.threads = targetThreads;
103: createMessage.type = J3dMessage.FOG_CHANGED;
104: createMessage.universe = universe;
105: createMessage.args[0] = this ;
106: createMessage.args[1] = new Integer(BACK_DISTANCE_CHANGED);
107: createMessage.args[2] = new Double(backDistance);
108: VirtualUniverse.mc.processMessage(createMessage);
109: }
110:
111: /**
112: * Gets back distance for fog
113: */
114: double getBackDistance() {
115: return this .backDistance;
116: }
117:
118: /**
119: * This method and its native counterpart update the native context
120: * fog values.
121: */
122: void update(Context ctx, double scale) {
123: validateDistancesInEc(scale);
124: Pipeline.getPipeline().updateLinearFog(ctx, color.x, color.y,
125: color.z, frontDistanceInEc, backDistanceInEc);
126: }
127:
128: void setLive(SetLiveState s) {
129: GroupRetained group;
130:
131: super .setLive(s);
132:
133: // Initialize the mirror object, this needs to be done, when
134: // renderBin is not accessing any of the fields
135: J3dMessage createMessage = new J3dMessage();
136: createMessage.threads = J3dThread.UPDATE_RENDERING_ENVIRONMENT;
137: createMessage.universe = universe;
138: createMessage.type = J3dMessage.FOG_CHANGED;
139: createMessage.args[0] = this ;
140: // a snapshot of all attributes that needs to be initialized
141: // in the mirror object
142: createMessage.args[1] = new Integer(INIT_MIRROR);
143: ArrayList addScopeList = new ArrayList();
144: for (int i = 0; i < scopes.size(); i++) {
145: group = (GroupRetained) scopes.get(i);
146: tempKey.reset();
147: group.addAllNodesForScopedFog(mirrorFog, addScopeList,
148: tempKey);
149: }
150: Object[] scopeInfo = new Object[2];
151: scopeInfo[0] = ((scopes.size() > 0) ? Boolean.TRUE
152: : Boolean.FALSE);
153: scopeInfo[1] = addScopeList;
154: createMessage.args[2] = scopeInfo;
155: Color3f clr = new Color3f(color);
156: createMessage.args[3] = clr;
157:
158: Object[] obj = new Object[6];
159: obj[0] = boundingLeaf;
160: obj[1] = (regionOfInfluence != null ? regionOfInfluence.clone()
161: : null);
162: obj[2] = (inBackgroundGroup ? Boolean.TRUE : Boolean.FALSE);
163: obj[3] = geometryBackground;
164: obj[4] = new Double(frontDistance);
165: obj[5] = new Double(backDistance);
166:
167: createMessage.args[4] = obj;
168: VirtualUniverse.mc.processMessage(createMessage);
169:
170: }
171:
172: // The update Object function.
173: // Note : if you add any more fields here , you need to update
174: // updateFog() in RenderingEnvironmentStructure
175: synchronized void updateMirrorObject(Object[] objs) {
176:
177: int component = ((Integer) objs[1]).intValue();
178: Transform3D trans;
179:
180: if ((component & FRONT_DISTANCE_CHANGED) != 0)
181: ((LinearFogRetained) mirrorFog).frontDistance = ((Double) objs[2])
182: .doubleValue();
183: if ((component & BACK_DISTANCE_CHANGED) != 0)
184: ((LinearFogRetained) mirrorFog).backDistance = ((Double) objs[2])
185: .doubleValue();
186: if ((component & INIT_MIRROR) != 0) {
187: ((LinearFogRetained) mirrorFog).frontDistance = ((Double) ((Object[]) objs[4])[4])
188: .doubleValue();
189: ((LinearFogRetained) mirrorFog).backDistance = ((Double) ((Object[]) objs[4])[5])
190: .doubleValue();
191:
192: }
193: ((LinearFogRetained) mirrorFog)
194: .setLocalToVworldScale(getLastLocalToVworld()
195: .getDistanceScale());
196:
197: super .updateMirrorObject(objs);
198: }
199:
200: /**
201: * Scale distances from local to eye coordinate
202: */
203: protected void validateDistancesInEc(double vworldToCoexistenceScale) {
204: // vworldToCoexistenceScale can be used here since
205: // CoexistenceToEc has a unit scale
206: double localToEcScale = getLocalToVworldScale()
207: * vworldToCoexistenceScale;
208:
209: frontDistanceInEc = frontDistance * localToEcScale;
210: backDistanceInEc = backDistance * localToEcScale;
211: }
212:
213: }
|