001: /*
002: * $RCSfile: SceneBase.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.4 $
041: * $Date: 2007/02/09 17:20:06 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders;
046:
047: import java.lang.Float;
048:
049: import java.util.Hashtable;
050: import java.util.Vector;
051: import java.util.Enumeration;
052:
053: import javax.media.j3d.Behavior;
054: import javax.media.j3d.BranchGroup;
055: import javax.media.j3d.TransformGroup;
056: import javax.media.j3d.Light;
057: import javax.media.j3d.Background;
058: import javax.media.j3d.Fog;
059: import javax.media.j3d.Sound;
060:
061: /**
062: * This class implements the Scene interface and extends it to incorporate
063: * utilities that could be used by loaders. There should be little need
064: * for future loaders to subclass this, or to implement Scene directly,
065: * as the functionality of a SceneBase is fairly straightforward. This
066: * class is responsible for both the storage and retrieval of data from
067: * the Scene. The storage methods (used only by Loader writers) are all
068: * of the add* routines. The retrieval methods (used primarily by Loader
069: * users) are all of the get* routines.
070: */
071: public class SceneBase implements Scene {
072:
073: BranchGroup sceneGroup = null;
074: BranchGroup behaviorGroup = null;
075: Hashtable namedObjects = new Hashtable();
076: String description = null;
077:
078: Vector viewVector = new Vector();
079: Vector hfovVector = new Vector();
080: Vector behaviorVector = new Vector();
081: Vector lightVector = new Vector();
082: Vector fogVector = new Vector();
083: Vector backgroundVector = new Vector();
084: Vector soundVector = new Vector();
085:
086: // Add methods
087:
088: /**
089: * Sets the sceneGroup to be the group that is passed in.
090: */
091: public void setSceneGroup(BranchGroup scene) {
092: sceneGroup = scene;
093: }
094:
095: /**
096: * Adds the given group to the list of view groups.
097: */
098: public void addViewGroup(TransformGroup tg) {
099: viewVector.addElement(tg);
100: }
101:
102: /**
103: * Adds the given field of view value to the list of field of view values.
104: */
105: public void addHorizontalFOV(float hfov) {
106: hfovVector.addElement(new Float(hfov));
107: }
108:
109: /**
110: * Adds the given behavior to a list of behaviors
111: */
112: public void addBehaviorNode(Behavior b) {
113: behaviorVector.addElement(b);
114: }
115:
116: /**
117: * Adds the given Light node to the list of lights.
118: */
119: public void addLightNode(Light light) {
120: lightVector.addElement(light);
121: }
122:
123: /**
124: * Adds the given Background node to the list of backgrounds.
125: */
126: public void addBackgroundNode(Background background) {
127: backgroundVector.addElement(background);
128: }
129:
130: /**
131: * Adds the given Sound node to the list of sounds.
132: */
133: public void addSoundNode(Sound sound) {
134: soundVector.addElement(sound);
135: }
136:
137: /**
138: * Adds the given Fog node to the list of fog nodes.
139: */
140: public void addFogNode(Fog fog) {
141: fogVector.addElement(fog);
142: }
143:
144: /**
145: * Sets the text description of the scene to the passed in String.
146: */
147: public void addDescription(String descriptionString) {
148: description = descriptionString;
149: }
150:
151: /**
152: * Adds the given String/Object pair to the table of named objects.
153: */
154: public void addNamedObject(String name, Object object) {
155: if (namedObjects.get(name) == null)
156: namedObjects.put(name, object);
157: else {
158: // key already exists - append a unique integer to end of name
159: int nameIndex = 1;
160: boolean done = false;
161: while (!done) {
162: // Iterate starting from "[1]" until we find a unique key
163: String tempName = name + "[" + nameIndex + "]";
164: if (namedObjects.get(tempName) == null) {
165: namedObjects.put(tempName, object);
166: done = true;
167: }
168: nameIndex++;
169: }
170: }
171: }
172:
173: /**
174: * This method returns the BranchGroup containing the overall
175: * scene loaded by the loader.
176: */
177: public BranchGroup getSceneGroup() {
178: return sceneGroup;
179: }
180:
181: /**
182: * This method returns an array of all View Groups defined in the file.
183: * A View Group is defined as a TransformGroup which contains a
184: * ViewPlatform. The TransformGroup holds the position/orientation
185: * information for the given ViewPlatform and the ViewPlatform
186: * holds an view-specific information, such as Field of View.
187: */
188: public TransformGroup[] getViewGroups() {
189: if (viewVector.isEmpty())
190: return null;
191: TransformGroup[] viewGroups = new TransformGroup[viewVector
192: .size()];
193: viewVector.copyInto(viewGroups);
194: return viewGroups;
195: }
196:
197: /**
198: * This method returns an array of floats that contains the horizontal
199: * field of view values for each corresponding entry in the array of
200: * view groups returned by the method getViewGroups.
201: */
202: public float[] getHorizontalFOVs() {
203: if (hfovVector.isEmpty())
204: return null;
205:
206: int arraySize = hfovVector.size();
207: float[] hfovs = new float[arraySize];
208: Float[] tmpFovs = new Float[hfovVector.size()];
209: hfovVector.copyInto(tmpFovs);
210:
211: // copy to array of floats and delete Floats
212: for (int i = 0; i < arraySize; i++) {
213: hfovs[i] = tmpFovs[i].floatValue();
214: tmpFovs[i] = null;
215: }
216: return hfovs;
217: }
218:
219: /**
220: * This method returns an array of all Lights defined in the file.
221: */
222: public Light[] getLightNodes() {
223: if (lightVector.isEmpty())
224: return null;
225: Light[] lightNodes = new Light[lightVector.size()];
226: lightVector.copyInto(lightNodes);
227: return lightNodes;
228: }
229:
230: /**
231: * This method returns a Hashtable which contains a list of all named
232: * objects in the file and their associated scene graph objects. The
233: * naming scheme for file objects is file-type dependent, but may include
234: * such names as the DEF names of Vrml or filenames of subjects (as
235: * in Lightwave 3D).
236: */
237: public Hashtable getNamedObjects() {
238: return namedObjects;
239: }
240:
241: /**
242: * This method returns an array of all Background nodes defined in the
243: * file.
244: */
245: public Background[] getBackgroundNodes() {
246: if (backgroundVector.isEmpty())
247: return null;
248: Background[] backgroundNodes = new Background[backgroundVector
249: .size()];
250: backgroundVector.copyInto(backgroundNodes);
251: return backgroundNodes;
252: }
253:
254: /**
255: * This method returns an array of all Fog nodes defined in the
256: * file.
257: */
258: public Fog[] getFogNodes() {
259: if (fogVector.isEmpty())
260: return null;
261: Fog[] fogNodes = new Fog[fogVector.size()];
262: fogVector.copyInto(fogNodes);
263: return fogNodes;
264: }
265:
266: /**
267: * This method returns a group containing all of the Behavior nodes
268: * in the scene.
269: */
270: public Behavior[] getBehaviorNodes() {
271: if (behaviorVector.isEmpty())
272: return null;
273: Behavior[] behaviorNodes = new Behavior[behaviorVector.size()];
274: behaviorVector.copyInto(behaviorNodes);
275:
276: return behaviorNodes;
277: }
278:
279: /**
280: * This method returns an array of all of the Sound nodes defined
281: * in the file.
282: */
283: public Sound[] getSoundNodes() {
284: if (soundVector.isEmpty())
285: return null;
286: Sound[] soundNodes = new Sound[soundVector.size()];
287: soundVector.copyInto(soundNodes);
288: return soundNodes;
289: }
290:
291: /**
292: * This method returns the text description of the file. If no
293: * such description exists, this method should return null.
294: */
295: public String getDescription() {
296: return description;
297: }
298:
299: }
|