001: /*
002: * $RCSfile: ChangePolygonAttributes.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.2 $
041: * $Date: 2007/02/09 17:17:02 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.utils.scenegraph.traverser;
046:
047: import javax.media.j3d.PolygonAttributes;
048: import javax.media.j3d.Appearance;
049:
050: /**
051: *
052: * @author paulby
053: * @version
054: */
055: public class ChangePolygonAttributes extends Object {
056:
057: /**
058: * Traverse the graph setting the polygon mode in all Polygon Attributes
059: * object that exist
060: */
061: public static void setPolygonMode(javax.media.j3d.Node treeRoot,
062: final int polygonMode) {
063: setPolygonMode(treeRoot, polygonMode, false);
064: }
065:
066: /**
067: * Traverse the graph setting the polygon mode in all PolygonAttributes.
068: * If createAttribute is false on PolygonAttribute objects that already
069: * exist are modified.
070: * If craeteAttribute is true, PolygonAttributes and Appearance objects will
071: * be created as necessary
072: */
073: public static void setPolygonMode(javax.media.j3d.Node treeRoot,
074: final int polygonMode, final boolean createAttribute) {
075:
076: AppearanceChangeProcessor processor = new AppearanceChangeProcessor() {
077: public void changeAppearance(javax.media.j3d.Shape3D shape,
078: javax.media.j3d.Appearance app) {
079: if (app == null)
080: if (createAttribute) {
081: app = new Appearance();
082: shape.setAppearance(app);
083: } else
084: return;
085: PolygonAttributes poly = app.getPolygonAttributes();
086: if (poly != null)
087: poly.setPolygonMode(polygonMode);
088: else if (createAttribute) {
089: poly = new PolygonAttributes();
090: poly.setPolygonMode(polygonMode);
091: app.setPolygonAttributes(poly);
092: }
093: }
094: };
095:
096: scanTree(treeRoot, processor);
097: }
098:
099: /**
100: * Traverse the graph setting the cullFace in all Polygon Attributes
101: * object that exist
102: */
103: public static void setCullFace(javax.media.j3d.Node treeRoot,
104: final int cullFace) {
105:
106: setCullFace(treeRoot, cullFace, false);
107: }
108:
109: /**
110: * Traverse the graph setting the cullFace in all PolygonAttributes.
111: * If createAttribute is false on PolygonAttribute objects that already
112: * exist are modified.
113: * If craeteAttribute is true, PolygonAttributes and Appearance objects will
114: * be created as necessary
115: */
116: public static void setCullFace(javax.media.j3d.Node treeRoot,
117: final int cullFace, final boolean createAttribute) {
118:
119: AppearanceChangeProcessor processor = new AppearanceChangeProcessor() {
120: public void changeAppearance(javax.media.j3d.Shape3D shape,
121: javax.media.j3d.Appearance app) {
122: if (app == null)
123: if (createAttribute) {
124: app = new Appearance();
125: shape.setAppearance(app);
126: } else
127: return;
128: PolygonAttributes poly = app.getPolygonAttributes();
129: if (poly != null)
130: poly.setCullFace(cullFace);
131: else if (createAttribute) {
132: poly = new PolygonAttributes();
133: poly.setCullFace(cullFace);
134: app.setPolygonAttributes(poly);
135: }
136: }
137: };
138:
139: scanTree(treeRoot, processor);
140: }
141:
142: private static void scanTree(javax.media.j3d.Node treeRoot,
143: AppearanceChangeProcessor processor) {
144: try {
145: Class shapeClass = Class.forName("javax.media.j3d.Shape3D");
146:
147: TreeScan.findNode(treeRoot, shapeClass, processor, false,
148: true);
149: } catch (Exception e) {
150: e.printStackTrace();
151: System.out
152: .println("ERROR ChangePolygonAttributes, SceneGraph contains"
153: + " Live or compiled nodes, without correct capabilities");
154: }
155: }
156:
157: }
|