001: /*
002: * $RCSfile: ExponentialFogRetained.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:21 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import javax.vecmath.Color3f;
035: import java.util.ArrayList;
036:
037: /**
038: * The ExponentialFog leaf node defines distance parameters for
039: * exponential fog.
040: */
041: class ExponentialFogRetained extends FogRetained {
042: // Fog density
043: private float density = 1.0f;
044:
045: // Issue 144: density in Eye Coordinates (EC)
046: private float densityInEc;
047:
048: // dirty bits for ExponentialFog
049: static final int DENSITY_CHANGED = FogRetained.LAST_DEFINED_BIT << 1;
050:
051: ExponentialFogRetained() {
052: this .nodeType = NodeRetained.EXPONENTIALFOG;
053: }
054:
055: /**
056: * initializes fog density
057: */
058: void initDensity(float density) {
059: this .density = density;
060: }
061:
062: /**
063: * Sets fog density and send a message
064: */
065: void setDensity(float density) {
066: this .density = density;
067: J3dMessage createMessage = new J3dMessage();
068: createMessage.threads = targetThreads;
069: createMessage.type = J3dMessage.FOG_CHANGED;
070: createMessage.universe = universe;
071: createMessage.args[0] = this ;
072: createMessage.args[1] = new Integer(DENSITY_CHANGED);
073: createMessage.args[2] = new Float(density);
074: VirtualUniverse.mc.processMessage(createMessage);
075: }
076:
077: /**
078: * Gets fog density
079: */
080: float getDensity() {
081: return this .density;
082: }
083:
084: void setLive(SetLiveState s) {
085: super .setLive(s);
086: GroupRetained group;
087:
088: // Initialize the mirror object, this needs to be done, when
089: // renderBin is not accessing any of the fields
090: J3dMessage createMessage = new J3dMessage();
091: createMessage.threads = J3dThread.UPDATE_RENDERING_ENVIRONMENT;
092: createMessage.universe = universe;
093: createMessage.type = J3dMessage.FOG_CHANGED;
094: createMessage.args[0] = this ;
095: // a snapshot of all attributes that needs to be initialized
096: // in the mirror object
097: createMessage.args[1] = new Integer(INIT_MIRROR);
098: ArrayList addScopeList = new ArrayList();
099: for (int i = 0; i < scopes.size(); i++) {
100: group = (GroupRetained) scopes.get(i);
101: tempKey.reset();
102: group.addAllNodesForScopedFog(mirrorFog, addScopeList,
103: tempKey);
104: }
105: Object[] scopeInfo = new Object[2];
106: scopeInfo[0] = ((scopes.size() > 0) ? Boolean.TRUE
107: : Boolean.FALSE);
108: scopeInfo[1] = addScopeList;
109: createMessage.args[2] = scopeInfo;
110: Color3f clr = new Color3f(color);
111: createMessage.args[3] = clr;
112:
113: Object[] obj = new Object[5];
114: obj[0] = boundingLeaf;
115: obj[1] = (regionOfInfluence != null ? regionOfInfluence.clone()
116: : null);
117: obj[2] = (inBackgroundGroup ? Boolean.TRUE : Boolean.FALSE);
118: obj[3] = geometryBackground;
119: obj[4] = new Float(density);
120:
121: createMessage.args[4] = obj;
122: VirtualUniverse.mc.processMessage(createMessage);
123:
124: }
125:
126: /**
127: * This method and its native counterpart update the native context
128: * fog values.
129: */
130: void update(Context ctx, double scale) {
131: // Issue 144: recompute the density in EC, and send it to native code
132: validateDistancesInEc(scale);
133: Pipeline.getPipeline().updateExponentialFog(ctx, color.x,
134: color.y, color.z, densityInEc);
135: }
136:
137: // The update Object function.
138: // Note : if you add any more fields here , you need to update
139: // updateFog() in RenderingEnvironmentStructure
140: void updateMirrorObject(Object[] objs) {
141:
142: int component = ((Integer) objs[1]).intValue();
143:
144: if ((component & DENSITY_CHANGED) != 0)
145: ((ExponentialFogRetained) mirrorFog).density = ((Float) objs[2])
146: .floatValue();
147:
148: if ((component & INIT_MIRROR) != 0) {
149: ((ExponentialFogRetained) mirrorFog).density = ((Float) ((Object[]) objs[4])[4])
150: .floatValue();
151:
152: }
153: // Issue 144: store the local to vworld scale used to transform the density
154: ((ExponentialFogRetained) mirrorFog)
155: .setLocalToVworldScale(getLastLocalToVworld()
156: .getDistanceScale());
157:
158: super .updateMirrorObject(objs);
159: }
160:
161: // Clone the retained side only, internal use only
162: protected Object clone() {
163: ExponentialFogRetained efr = (ExponentialFogRetained) super
164: .clone();
165:
166: efr.initDensity(getDensity());
167:
168: return efr;
169: }
170:
171: // Issue 144: method to recompute the density in EC by multiplying the specified
172: // density by the inverse of the local to EC scale
173: /**
174: * Scale distances from local to eye coordinate.
175: */
176: protected void validateDistancesInEc(double vworldToCoexistenceScale) {
177: // vworldToCoexistenceScale can be used here since
178: // CoexistenceToEc has a unit scale
179: double localToEcScale = getLocalToVworldScale()
180: * vworldToCoexistenceScale;
181:
182: densityInEc = (float) (density / localToEcScale);
183: }
184:
185: }
|