001: /*
002: * $RCSfile: J3dNodeTable.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.5 $
028: * $Date: 2008/02/28 20:17:25 $
029: * $State: Exp $
030: */
031:
032: package javax.media.j3d;
033:
034: import java.io.DataOutputStream;
035: import java.io.OutputStream;
036: import java.util.Hashtable;
037: import javax.vecmath.Color3f;
038: import javax.vecmath.Tuple3f;
039:
040: /**
041: * The J3dNodeTable object provides utilities for the save/load
042: * methods in the Java3d nodes. Specifically, it holds an enumerated
043: * list of the Java3D node types and their respective Class names.
044: * It keeps these lists in a Hashtable and an array and allows
045: * other classes to get an enumerated value associated with an Object
046: * type or an instance of an Object associated with an enumerated value.
047: *
048: */
049: class J3dNodeTable {
050:
051: // nodeTable holds the enumerated value/Classname pairs. This is
052: // used to look up enumerated values given a Class name.
053: Hashtable nodeTable = new Hashtable();
054:
055: // nodeArray holds the value/Classname pairs also, but allows lookups
056: // in the other direction (given a value, what's the Class name?)
057: String nodeArray[];
058:
059: // Following is a list of the current scene graph objects of java3D.
060: // In order to make later node insertion easier, and to add some logic to
061: // this arbitrary list, the groups of nodes are grouped in terms of
062: // similar functionality of node types.
063:
064: // Maximum number of nodes
065: static final int MAX_NUM_NODES = 200;
066:
067: // Nothing occupies slot 0 - thus we can return 0 from this class'
068: // methods to denote failure.
069: static final int NOTHING = 0;
070:
071: // 1 - 9: Groups
072: static final int GROUP = 1;
073: static final int TRANSFORM_GROUP = 2;
074: static final int SWITCH_GROUP = 3;
075: static final int ORDERED_GROUP = 4;
076: static final int BRANCH_GROUP = 5;
077: static final int ENDGROUP = 9; // denotes done with group
078:
079: // 10 - 19: Shape3D (in a class by itself)
080: static final int SHAPE3D = 10;
081:
082: // 20 - 49: Appearance and all of its attributes
083: static final int APPEARANCE = 20;
084: static final int MATERIAL = 21;
085: static final int TEXTURE = 22;
086: static final int TEX_COORD_GENERATION = 23;
087: static final int TEXTURE_ATTRIBUTES = 24;
088: static final int COLORING_ATTRIBUTES = 25;
089: static final int TRANSPARENCY_ATTRIBUTES = 26;
090: static final int RENDERING_ATTRIBUTES = 27;
091: static final int POLYGON_ATTRIBUTES = 28;
092: static final int LINE_ATTRIBUTES = 29;
093: static final int POINT_ATTRIBUTES = 30;
094: static final int TEXTURE_2D = 31;
095: static final int TEXTURE_3D = 32;
096: static final int IMAGE_COMPONENT = 33;
097: static final int IMAGE_COMPONENT_2D = 34;
098: static final int IMAGE_COMPONENT_3D = 35;
099: static final int ENDAPPEARANCE = 49;
100:
101: // 100 - 149: All Geometry types
102: static final int GEOMETRY = 100;
103: static final int COMPRESSED_GEOMETRY = 101;
104: static final int GEOMETRY_ARRAY = 102;
105: static final int GEOMETRY_STRIP_ARRAY = 103;
106: static final int INDEXED_GEOMETRY_ARRAY = 104;
107: static final int INDEXED_GEOMETRY_STRIP_ARRAY = 105;
108: static final int INDEXED_LINE_ARRAY = 106;
109: static final int INDEXED_LINE_STRIP_ARRAY = 107;
110: static final int INDEXED_POINT_ARRAY = 108;
111: static final int INDEXED_QUAD_ARRAY = 109;
112: static final int INDEXED_TRIANGLE_ARRAY = 110;
113: static final int INDEXED_TRIANGLE_FAN_ARRAY = 111;
114: static final int INDEXED_TRIANGLE_STRIP_ARRAY = 112;
115: static final int LINE_ARRAY = 113;
116: static final int LINE_STRIP_ARRAY = 114;
117: static final int POINT_ARRAY = 115;
118: static final int QUAD_ARRAY = 116;
119: static final int TRIANGLE_ARRAY = 117;
120: static final int TRIANGLE_FAN_ARRAY = 118;
121: static final int TRIANGLE_STRIP_ARRAY = 119;
122: static final int BACKGROUND_SOUND = 120;
123: static final int POINT_SOUND = 121;
124: static final int CONE_SOUND = 122;
125: static final int MEDIA_CONTAINER = 123;
126:
127: // 150 - 169: Behaviors
128: static final int ROTATION_INTERPOLATOR = 150;
129: static final int ROTPOSSCALEPATH_INTERPOLATOR = 151;
130: static final int ROTATIONPATH_INTERPOLATOR = 152;
131: static final int POSITIONPATH_INTERPOLATOR = 153;
132: static final int ROTPOSPATH_INTERPOLATOR = 154;
133: static final int POSITION_INTERPOLATOR = 155;
134: static final int SWITCHVALUE_INTERPOLATOR = 156;
135: static final int COLOR_INTERPOLATOR = 157;
136: static final int SCALE_INTERPOLATOR = 158;
137: // Problem: these next two are non j3d-core items
138: static final int SOUND_PLAYER = 159;
139: static final int SOUND_FADER = 160;
140:
141: // 170 - 189: Various utility nodes
142: static final int BOUNDS = 170;
143: static final int BOUNDING_SPHERE = 171;
144: static final int BOUNDING_BOX = 172;
145: static final int BOUNDING_POLYTOPE = 173;
146: static final int TRANSFORM3D = 180;
147: static final int BACKGROUND = 181;
148:
149: // 190 - 199: Lights
150: static final int LIGHT = 190;
151: static final int POINT_LIGHT = 191;
152: static final int SPOT_LIGHT = 192;
153: static final int DIRECTIONAL_LIGHT = 193;
154: static final int AMBIENT_LIGHT = 194;
155:
156: /**
157: * Constructs this Object, which initializes the array and Hashtable
158: */
159: J3dNodeTable() {
160: nodeArray = new String[MAX_NUM_NODES];
161: //Initialize all table entries to null
162: for (int i = 0; i < MAX_NUM_NODES; ++i)
163: nodeArray[i] = null;
164: // Setup slots of nodeArray where there should be valid values
165: nodeArray[GROUP] = "Group";
166: nodeArray[TRANSFORM_GROUP] = "TransformGroup";
167: nodeArray[SWITCH_GROUP] = "Switch";
168: nodeArray[ORDERED_GROUP] = "OrderedGroup";
169: nodeArray[BRANCH_GROUP] = "BranchGroup";
170:
171: nodeArray[SHAPE3D] = "Shape3D";
172:
173: nodeArray[APPEARANCE] = "Appearance";
174: nodeArray[MATERIAL] = "Material";
175: nodeArray[TEXTURE] = "Texture";
176: nodeArray[TEXTURE_2D] = "Texture2D";
177: nodeArray[TEXTURE_3D] = "Texture3D";
178: nodeArray[IMAGE_COMPONENT] = "ImageComponent";
179: nodeArray[IMAGE_COMPONENT_2D] = "ImageComponent2D";
180: nodeArray[IMAGE_COMPONENT_3D] = "ImageComponent3D";
181: nodeArray[TRANSPARENCY_ATTRIBUTES] = "TransparencyAttributes";
182:
183: nodeArray[GEOMETRY] = "Geometry";
184: nodeArray[COMPRESSED_GEOMETRY] = "CompressedGeometry";
185: nodeArray[GEOMETRY_ARRAY] = "GeometryArray";
186: nodeArray[GEOMETRY_STRIP_ARRAY] = "GeometryStripArray";
187: nodeArray[INDEXED_GEOMETRY_ARRAY] = "IndexedGeometryArray";
188: nodeArray[INDEXED_GEOMETRY_STRIP_ARRAY] = "IndexedGeometryStripArray";
189: nodeArray[INDEXED_LINE_ARRAY] = "IndexedLineArray";
190: nodeArray[INDEXED_LINE_STRIP_ARRAY] = "IndexedLineStripArray";
191: nodeArray[INDEXED_POINT_ARRAY] = "IndexedPointArray";
192: nodeArray[INDEXED_QUAD_ARRAY] = "IndexedQuadArray";
193: nodeArray[INDEXED_TRIANGLE_ARRAY] = "IndexedTriangleArray";
194: nodeArray[INDEXED_TRIANGLE_FAN_ARRAY] = "IndexedTriangleFanArray";
195: nodeArray[INDEXED_TRIANGLE_STRIP_ARRAY] = "indexedTriangleStripArray";
196: nodeArray[LINE_ARRAY] = "LineArray";
197: nodeArray[LINE_STRIP_ARRAY] = "LineStripArray";
198: nodeArray[POINT_ARRAY] = "PointArray";
199: nodeArray[QUAD_ARRAY] = "QuadArray";
200: nodeArray[TRIANGLE_ARRAY] = "TriangleArray";
201: nodeArray[TRIANGLE_FAN_ARRAY] = "TriangleFanArray";
202: nodeArray[TRIANGLE_STRIP_ARRAY] = "TriangleStripArray";
203: nodeArray[BACKGROUND_SOUND] = "BackgroundSound";
204: nodeArray[POINT_SOUND] = "PointSound";
205: nodeArray[CONE_SOUND] = "ConeSound";
206: nodeArray[MEDIA_CONTAINER] = "MediaContainer";
207:
208: nodeArray[ROTATION_INTERPOLATOR] = "RotationInterpolator";
209: nodeArray[ROTPOSSCALEPATH_INTERPOLATOR] = "RotPosScalePathInterpolator";
210: nodeArray[ROTATIONPATH_INTERPOLATOR] = "RotationPathInterpolator";
211: nodeArray[POSITIONPATH_INTERPOLATOR] = "PositionPathInterpolator";
212: nodeArray[ROTPOSPATH_INTERPOLATOR] = "RotPosPathInterpolator";
213: nodeArray[POSITION_INTERPOLATOR] = "PositionInterpolator";
214: nodeArray[SWITCHVALUE_INTERPOLATOR] = "SwitchValueInterpolator";
215: nodeArray[COLOR_INTERPOLATOR] = "ColorInterpolator";
216: nodeArray[SCALE_INTERPOLATOR] = "ScaleInterpolator";
217: nodeArray[SOUND_PLAYER] = "SoundPlayer";
218: nodeArray[SOUND_FADER] = "SoundFader";
219:
220: nodeArray[BOUNDS] = "Bounds";
221: nodeArray[BOUNDING_SPHERE] = "BoundingSphere";
222: nodeArray[BOUNDING_BOX] = "BoundingBox";
223: nodeArray[BOUNDING_POLYTOPE] = "BoundingPolytope";
224: nodeArray[TRANSFORM3D] = "Transform3D";
225: nodeArray[BACKGROUND] = "Background";
226:
227: nodeArray[LIGHT] = "Light";
228: nodeArray[POINT_LIGHT] = "PointLight";
229: nodeArray[SPOT_LIGHT] = "SpotLight";
230: nodeArray[DIRECTIONAL_LIGHT] = "DirectionalLight";
231: nodeArray[AMBIENT_LIGHT] = "AmbientLight";
232:
233: for (int i = 0; i < MAX_NUM_NODES; ++i) {
234: if (nodeArray[i] != null)
235: nodeTable.put(nodeArray[i], new Integer(i));
236: }
237:
238: }
239:
240: /**
241: * Returns the enumerated value associated with an Object. This
242: * method retrieves the base class name (with no package name and
243: * with no "Retained" portion (if it's part of the Object's name);
244: * we're just looking for the base Java3d node type here.
245: */
246: int getNodeValue(Object object) {
247: Integer i;
248: String fullName = object.getClass().getName();
249: int firstIndex;
250: int lastIndex;
251: if ((firstIndex = fullName.lastIndexOf(".")) == -1)
252: firstIndex = 0;
253: else
254: firstIndex++;
255: if ((lastIndex = fullName.lastIndexOf("Retained")) == -1)
256: lastIndex = fullName.length();
257: String nodeName = fullName.substring(firstIndex, lastIndex);
258: if ((i = (Integer) nodeTable.get(nodeName)) != null) {
259: return i.intValue();
260: } else {
261: // This conditional exists because if a group node is
262: // actually a subclass of some standard Java3d Group type
263: // (e.g., VrmlParser), then we'll just save it and reload
264: // it as a Group.
265: if (object instanceof TransformGroup
266: || object instanceof TransformGroupRetained)
267: return TRANSFORM_GROUP;
268: else if (object instanceof BranchGroup
269: || object instanceof BranchGroupRetained)
270: return BRANCH_GROUP;
271: else if (object instanceof Switch
272: || object instanceof SwitchRetained)
273: return SWITCH_GROUP;
274: else if (object instanceof Group
275: || object instanceof GroupRetained)
276: return GROUP;
277: else if (object instanceof Shape3D)
278: return SHAPE3D; // For Text2D object in particular
279: else {
280: System.err
281: .println("Warning: Don't know how to save object of type "
282: + object);
283: return 0;
284: }
285: }
286: }
287:
288: /**
289: * Returns new instance of an object with the Class name
290: * associated with the given enumerated value.
291: */
292: Object getObject(int nodeValue) {
293: try {
294: if (nodeArray[nodeValue] != null) {
295: String nodeName = "javax.media.j3d."
296: + nodeArray[nodeValue];
297: return Class.forName(nodeName).newInstance();
298: }
299: } catch (Exception e) {
300: throw new RuntimeException(
301: "Exception creating object for nodeValue "
302: + nodeValue
303: + "\n nodeName = javax.media.j3d."
304: + nodeArray[nodeValue]);
305: }
306: return null;
307: }
308:
309: }
|