001: /*
002: * $RCSfile: TransparencyAttributesRetained.java,v $
003: *
004: * Copyright 1998-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.8 $
028: * $Date: 2008/02/28 20:17:32 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.util.ArrayList;
035:
036: /**
037: * The TransparencyAttributes object defines all attributes affecting
038: * transparency of the object.
039: */
040: class TransparencyAttributesRetained extends NodeComponentRetained {
041: // A list of pre-defined bits to indicate which component
042: // in this TransparencyAttributes object changed.
043: static final int MODE_CHANGED = 0x01;
044: static final int VALUE_CHANGED = 0x02;
045: static final int SRC_BLEND_FUNCTION_CHANGED = 0x04;
046: static final int DST_BLEND_FUNCTION_CHANGED = 0x08;
047:
048: // Integer flag that contains bitset to indicate
049: // which field changed.
050: int isDirty = 0xffff;
051:
052: // Transparency mode (alpha, screen_door)
053: int transparencyMode = TransparencyAttributes.NONE;
054: float transparency = 0.0f;
055:
056: // Transparency blend functions
057: int srcBlendFunction = TransparencyAttributes.BLEND_SRC_ALPHA;
058: int dstBlendFunction = TransparencyAttributes.BLEND_ONE_MINUS_SRC_ALPHA;
059:
060: /**
061: * Sets the transparency mode for this
062: * appearance component object.
063: * @param transparencyMode the transparency mode to be used, one of
064: * <code>NONE</code>, <code>FASTEST</code>, <code>NICEST</code>,
065: * <code>SCREEN_DOOR</code>, or <code>BLENDED</code>
066: */
067: final void initTransparencyMode(int transparencyMode) {
068: this .transparencyMode = transparencyMode;
069: }
070:
071: /**
072: * Sets the transparency mode for this
073: * appearance component object and sends a message notifying
074: * the interested structures of the change.
075: * @param transparencyMode the transparency mode to be used, one of
076: * <code>FASTEST</code>, <code>NICEST</code>,
077: * <code>SCREEN_DOOR</code>, or <code>BLENDED</code>
078: */
079: final void setTransparencyMode(int transparencyMode) {
080: initTransparencyMode(transparencyMode);
081: sendMessage(MODE_CHANGED, new Integer(transparencyMode));
082: }
083:
084: /**
085: * Gets the transparency mode for this
086: * appearance component object.
087: * @return transparencyMode the transparency mode
088: */
089: final int getTransparencyMode() {
090: return transparencyMode;
091: }
092:
093: /**
094: * Sets this appearance's transparency.
095: * @param transparency the appearance's transparency
096: * in the range [0.0, 1.0] with 0.0 being
097: * fully opaque and 1.0 being fully transparent
098: */
099: final void initTransparency(float transparency) {
100: this .transparency = transparency;
101: }
102:
103: /**
104: * Sets this appearance's transparency and sends a message notifying
105: * the interested structures of the change.
106: * @param transparency the appearance's transparency
107: * in the range [0.0, 1.0] with 0.0 being
108: * fully opaque and 1.0 being fully transparent
109: */
110: final void setTransparency(float transparency) {
111: initTransparency(transparency);
112: sendMessage(VALUE_CHANGED, new Float(transparency));
113: }
114:
115: /**
116: * Retrieves this appearance's transparency.
117: * @return the appearance's transparency
118: */
119: final float getTransparency() {
120: return this .transparency;
121: }
122:
123: /**
124: * Sets the source blend function used in blended transparency
125: * and antialiasing operations. The source function specifies the
126: * factor that is multiplied by the source color; this value is
127: * added to the product of the destination factor and the
128: * destination color. The default source blend function is
129: * <code>BLEND_SRC_ALPHA</code>.
130: *
131: * @param blendFunction the blend function to be used for the source
132: * color, one of <code>BLEND_ZERO</code>, <code>BLEND_ONE</code>,
133: * <code>BLEND_SRC_ALPHA</code>, or <code>BLEND_ONE_MINUS_SRC_ALPHA</code>.
134: */
135: final void initSrcBlendFunction(int blendFunction) {
136: this .srcBlendFunction = blendFunction;
137: }
138:
139: /**
140: * Sets the source blend function used in blended transparency
141: * and antialiasing operations and sends a message notifying the
142: * interested structures of the change. The source function specifies the
143: * factor that is multiplied by the source color; this value is
144: * added to the product of the destination factor and the
145: * destination color. The default source blend function is
146: * <code>BLEND_SRC_ALPHA</code>.
147: *
148: * @param blendFunction the blend function to be used for the source
149: * color, one of <code>BLEND_ZERO</code>, <code>BLEND_ONE</code>,
150: * <code>BLEND_SRC_ALPHA</code>, or <code>BLEND_ONE_MINUS_SRC_ALPHA</code>.
151: */
152: final void setSrcBlendFunction(int blendFunction) {
153: initSrcBlendFunction(blendFunction);
154: sendMessage(SRC_BLEND_FUNCTION_CHANGED, new Integer(
155: blendFunction));
156: }
157:
158: /**
159: * Retrieves this appearance's source blend function.
160: * @return the appearance's source blend function
161: */
162: final int getSrcBlendFunction() {
163: return srcBlendFunction;
164: }
165:
166: /**
167: * Sets the destination blend function used in blended transparency
168: * and antialiasing operations. The destination function specifies the
169: * factor that is multiplied by the destination color; this value is
170: * added to the product of the source factor and the
171: * source color. The default destination blend function is
172: * <code>BLEND_ONE_MINUS_SRC_ALPHA</code>.
173: *
174: * @param blendFunction the blend function to be used for the destination
175: * color, one of <code>BLEND_ZERO</code>, <code>BLEND_ONE</code>,
176: * <code>BLEND_SRC_ALPHA</code>, or <code>BLEND_ONE_MINUS_SRC_ALPHA</code>.
177: *
178: */
179: final void initDstBlendFunction(int blendFunction) {
180: this .dstBlendFunction = blendFunction;
181: }
182:
183: /**
184: * Sets the destination blend function used in blended transparency
185: * and antialiasing operations and sends a message notifying the
186: * interested structures of the change. The destination function
187: * specifies the factor that is multiplied by the destination
188: * color; this value is added to the product of the source factor
189: * and the source color. The default destination blend function is
190: * <code>BLEND_ONE_MINUS_SRC_ALPHA</code>.
191: *
192: * @param blendFunction the blend function to be used for the destination
193: * color, one of <code>BLEND_ZERO</code>, <code>BLEND_ONE</code>,
194: * <code>BLEND_SRC_ALPHA</code>, or <code>BLEND_ONE_MINUS_SRC_ALPHA</code>.
195: */
196: final void setDstBlendFunction(int blendFunction) {
197: initDstBlendFunction(blendFunction);
198: sendMessage(DST_BLEND_FUNCTION_CHANGED, new Integer(
199: blendFunction));
200: }
201:
202: /**
203: * Retrieves this appearance's destination blend function.
204: * @return the appearance's destination blend function
205: */
206: final int getDstBlendFunction() {
207: return dstBlendFunction;
208: }
209:
210: /**
211: * Creates and initializes a mirror object, point the mirror object
212: * to the retained object if the object is not editable
213: */
214: synchronized void createMirrorObject() {
215: if (mirror == null) {
216: // Check the capability bits and let the mirror object
217: // point to itself if is not editable
218: if (isStatic()) {
219: mirror = this ;
220: } else {
221: TransparencyAttributesRetained mirrorTa = new TransparencyAttributesRetained();
222: mirrorTa.source = source;
223: mirrorTa.set(this );
224: mirror = mirrorTa;
225:
226: }
227: } else {
228: ((TransparencyAttributesRetained) mirror).set(this );
229: }
230: }
231:
232: void updateNative(Context ctx, float alpha, int geometryType,
233: int polygonMode, boolean lineAA, boolean pointAA) {
234: Pipeline.getPipeline().updateTransparencyAttributes(ctx, alpha,
235: geometryType, polygonMode, lineAA, pointAA,
236: transparencyMode, srcBlendFunction, dstBlendFunction);
237: }
238:
239: /**
240: * Initializes a mirror object, point the mirror object to the retained
241: * object if the object is not editable
242: */
243: synchronized void initMirrorObject() {
244: ((TransparencyAttributesRetained) mirror).set(this );
245: }
246:
247: /**
248: * Update the "component" field of the mirror object with the
249: * given "value"
250: */
251: synchronized void updateMirrorObject(int component, Object value) {
252:
253: TransparencyAttributesRetained mirrorTa = (TransparencyAttributesRetained) mirror;
254:
255: if ((component & MODE_CHANGED) != 0) {
256: mirrorTa.transparencyMode = ((Integer) value).intValue();
257: } else if ((component & VALUE_CHANGED) != 0) {
258: mirrorTa.transparency = ((Float) value).floatValue();
259: } else if ((component & SRC_BLEND_FUNCTION_CHANGED) != 0) {
260: mirrorTa.srcBlendFunction = ((Integer) value).intValue();
261: } else if ((component & DST_BLEND_FUNCTION_CHANGED) != 0) {
262: mirrorTa.dstBlendFunction = ((Integer) value).intValue();
263: }
264: }
265:
266: boolean equivalent(TransparencyAttributesRetained tr) {
267: return ((tr != null)
268: && (tr.transparencyMode == transparencyMode)
269: && (tr.transparency == transparency)
270: && (tr.srcBlendFunction == srcBlendFunction) && (tr.dstBlendFunction == dstBlendFunction));
271: }
272:
273: protected void set(TransparencyAttributesRetained transp) {
274: super .set(transp);
275: transparencyMode = transp.transparencyMode;
276: transparency = transp.transparency;
277: srcBlendFunction = transp.srcBlendFunction;
278: dstBlendFunction = transp.dstBlendFunction;
279: }
280:
281: final void sendMessage(int attrMask, Object attr) {
282:
283: ArrayList univList = new ArrayList();
284: ArrayList gaList = Shape3DRetained.getGeomAtomsList(
285: mirror.users, univList);
286:
287: // Send to rendering attribute structure, regardless of
288: // whether there are users or not (alternate appearance case ..)
289: J3dMessage createMessage = new J3dMessage();
290: createMessage.threads = J3dThread.UPDATE_RENDERING_ATTRIBUTES;
291: createMessage.type = J3dMessage.TRANSPARENCYATTRIBUTES_CHANGED;
292: createMessage.universe = null;
293: createMessage.args[0] = this ;
294: createMessage.args[1] = new Integer(attrMask);
295: createMessage.args[2] = attr;
296: createMessage.args[3] = new Integer(changedFrequent);
297: VirtualUniverse.mc.processMessage(createMessage);
298:
299: // System.err.println("univList.size is " + univList.size());
300: for (int i = 0; i < univList.size(); i++) {
301: createMessage = new J3dMessage();
302: createMessage.threads = J3dThread.UPDATE_RENDER;
303: createMessage.type = J3dMessage.TRANSPARENCYATTRIBUTES_CHANGED;
304:
305: createMessage.universe = (VirtualUniverse) univList.get(i);
306: createMessage.args[0] = this ;
307: createMessage.args[1] = new Integer(attrMask);
308: createMessage.args[2] = attr;
309:
310: ArrayList gL = (ArrayList) gaList.get(i);
311: GeometryAtom[] gaArr = new GeometryAtom[gL.size()];
312: gL.toArray(gaArr);
313: createMessage.args[3] = gaArr;
314:
315: VirtualUniverse.mc.processMessage(createMessage);
316: }
317:
318: }
319:
320: void handleFrequencyChange(int bit) {
321: if (bit == TransparencyAttributes.ALLOW_MODE_WRITE
322: || bit == TransparencyAttributes.ALLOW_VALUE_WRITE
323: || bit == TransparencyAttributes.ALLOW_BLEND_FUNCTION_WRITE) {
324: setFrequencyChangeMask(bit, 0x1);
325: }
326: }
327:
328: }
|