summaryrefslogtreecommitdiffstats
path: root/src/classes/share/com/sun/j3d/loaders/SceneBase.java
blob: 642fa26f60e4e053ca93f7f9659ea3d3d514b6e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * $RCSfile$
 *
 * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistribution of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that this software is not designed, licensed or
 * intended for use in the design, construction, operation or
 * maintenance of any nuclear facility.
 *
 * $Revision$
 * $Date$
 * $State$
 */

package com.sun.j3d.loaders;

import java.lang.Float;

import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;

import javax.media.j3d.Behavior;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.Light;
import javax.media.j3d.Background;
import javax.media.j3d.Fog;
import javax.media.j3d.Sound;


/**
 * This class implements the Scene interface and extends it to incorporate
 * utilities that could be used by loaders.  There should be little need
 * for future loaders to subclass this, or to implement Scene directly,
 * as the functionality of a SceneBase is fairly straightforward.  This
 * class is responsible for both the storage and retrieval of data from
 * the Scene.  The storage methods (used only by Loader writers) are all
 * of the add* routines.  The retrieval methods (used primarily by Loader
 * users) are all of the get* routines.
 */
public class SceneBase implements Scene {

    BranchGroup		sceneGroup = null;
    BranchGroup		behaviorGroup = null;
    Hashtable		namedObjects = new Hashtable();
    String		description = null;

    Vector viewVector = new Vector();
    Vector hfovVector = new Vector();
    Vector behaviorVector = new Vector();
    Vector lightVector = new Vector();
    Vector fogVector = new Vector();
    Vector backgroundVector = new Vector();
    Vector soundVector = new Vector();
    
    // Add methods

    /**
     * Sets the sceneGroup to be the group that is passed in.
     */
    public void setSceneGroup(BranchGroup scene) {
	sceneGroup = scene;
    }

    /**
     * Adds the given group to the list of view groups.
     */
    public void addViewGroup(TransformGroup tg) {
	viewVector.addElement(tg);
    }

    /**
     * Adds the given field of view value to the list of field of view values.
     */
    public void addHorizontalFOV(float hfov) {
	hfovVector.addElement(new Float(hfov));
    }

    /**
     * Adds the given behavior to a list of behaviors 
     */
    public void addBehaviorNode(Behavior b) {
	behaviorVector.addElement(b);
    }

    /**
     * Adds the given Light node to the list of lights.
     */
    public void addLightNode(Light light) {
	lightVector.addElement(light);
    }

    /**
     * Adds the given Background node to the list of backgrounds.
     */
    public void addBackgroundNode(Background background) {
	backgroundVector.addElement(background);
    }

    /**
     * Adds the given Sound node to the list of sounds.
     */
    public void addSoundNode(Sound sound) {
	soundVector.addElement(sound);
    }

    /**
     * Adds the given Fog node to the list of fog nodes.
     */
    public void addFogNode(Fog fog) {
	fogVector.addElement(fog);
    }

    /**
     * Sets the text description of the scene to the passed in String.
     */
    public void addDescription(String descriptionString) {
	description = descriptionString;
    }

    /**
     * Adds the given String/Object pair to the table of named objects.
     */
    public void addNamedObject(String name, Object object) {
	if (namedObjects.get(name) == null)
	    namedObjects.put(name, object);
	else {
	    // key already exists - append a unique integer to end of name
	    int nameIndex = 1;
	    boolean done = false;
	    while (!done) {
		// Iterate starting from "[1]" until we find a unique key
		String tempName = name + "[" + nameIndex + "]";
		if (namedObjects.get(tempName) == null) {
		    namedObjects.put(tempName, object);
		    done = true;
		}
		nameIndex++;
	    }
	}
    }
    
    /**
     * This method returns the BranchGroup containing the overall
     * scene loaded by the loader.
     */
    public BranchGroup getSceneGroup() {
	return sceneGroup;
    }


    /**
     * This method returns an array of all View Groups defined in the file.
     * A View Group is defined as a TransformGroup which contains a
     * ViewPlatform.  The TransformGroup holds the position/orientation
     * information for the given ViewPlatform and the ViewPlatform
     * holds an view-specific information, such as Field of View.
     */
    public TransformGroup[] getViewGroups() {
	if (viewVector.isEmpty())
	    return null;
	TransformGroup[] viewGroups = new TransformGroup[viewVector.size()];
	viewVector.copyInto(viewGroups);
	return viewGroups;
    }

    /**
     * This method returns an array of floats that contains the horizontal
     * field of view values for each corresponding entry in the array of
     * view groups returned by the method getViewGroups. 
     */
    public float[] getHorizontalFOVs() {
	if (hfovVector.isEmpty())
	    return null;

        int arraySize = hfovVector.size();
        float[] hfovs = new float[arraySize];
	Float[] tmpFovs = new Float[hfovVector.size()];
	hfovVector.copyInto(tmpFovs);

        // copy to array of floats and delete Floats
        for (int i=0; i<arraySize; i++) {
          hfovs[i] = tmpFovs[i].floatValue();
          tmpFovs[i] = null;
        }
	return hfovs;
    } 

    
    /**
     * This method returns an array of all Lights defined in the file.
     */
    public Light[] getLightNodes() {
	if (lightVector.isEmpty())
	    return null;
	Light[] lightNodes = new Light[lightVector.size()]; 
	lightVector.copyInto(lightNodes);
	return lightNodes;
    }
    

    /**
     * This method returns a Hashtable which contains a list of all named
     * objects in the file and their associated scene graph objects.  The
     * naming scheme for file objects is file-type dependent, but may include
     * such names as the DEF names of Vrml or filenames of subjects (as
     * in Lightwave 3D).
     */
    public Hashtable getNamedObjects() {
	return namedObjects;
    }
    

    /**
     * This method returns an array of all Background nodes defined in the
     * file.
     */
    public Background[] getBackgroundNodes() {
	if (backgroundVector.isEmpty())
	    return null;
	Background[] backgroundNodes = new Background[backgroundVector.size()];
	backgroundVector.copyInto(backgroundNodes);
	return backgroundNodes;
    }
    

    /**
     * This method returns an array of all Fog nodes defined in the
     * file.
     */
    public Fog[] getFogNodes() {
	if (fogVector.isEmpty())
	    return null;
	Fog[] fogNodes = new Fog[fogVector.size()];
	fogVector.copyInto(fogNodes);
	return fogNodes;
    }
    

    /**
     * This method returns a group containing all of the Behavior nodes
     * in the scene.
     */
    public Behavior[] getBehaviorNodes() {
	if (behaviorVector.isEmpty())
	    return null;
	Behavior[] behaviorNodes = new Behavior[behaviorVector.size()]; 
	behaviorVector.copyInto(behaviorNodes);

	return behaviorNodes;
    }
    

    /**
     * This method returns an array of all of the Sound nodes defined
     * in the file.
     */
    public Sound[] getSoundNodes() {
	if (soundVector.isEmpty())
	    return null;
	Sound[] soundNodes = new Sound[soundVector.size()];
	soundVector.copyInto(soundNodes);
	return soundNodes;
    }


    /**
     * This method returns the text description of the file.  If no
     * such description exists, this method should return null.
     */
    public String getDescription() {
	return description;
    }

}