Examining a Model's Scene GraphAfter loading the model, WrapLoaderInfo3D's next main task is to traverse the model's scene graph and report on its structure. Walking over the graph is easy due to the parent-child relationship between the nodes and that all the nodes are subclasses of a single superclass, SceneGraphObject. A simplified inheritance hierarchy is shown in Figure 16-8. As mentioned in Chapter 14, Leaf nodes are subclassed in various ways to obtain Shape3D and environment nodes for lighting, backgrounds, sound, and so on. The subclasses of Group include BranchGroup and TransformGroup, which may have their own children (Group and/or Leaf nodes). NodeComponent objects are used to store information in nodes, such as Geometry and Appearance attributes, and may be shared between nodes. Figure 16-8. Some subclasses of SceneGraphObjectA simple algorithm for traversing a scene graph is shown here: examineNode(node) { if the node is a Group { print Group info; for each child of the node examineNode(child); // recursive call } else if the node is a Leaf if the node is a Shape3D { examine its appearance; examine its geometry; } else print Leaf info } else print general node info; } This pseudocode is the heart of the examineNode( ) and examineShape3D( ) methods in WrapLoaderInfo3D. The algorithm is simplified by concentrating on a few node types, principally Shape3D, and by considering the graph as a tree. Shape3D details are often the most important since they store the model's geometry, and there's little point looking for environmental data since they are frequently not converted to Java 3D by the loader. examineShape3D( ) calls printAppearance( ) to examine the model's appearance, which is confined to reporting Java 3D ColouringAttributes and/or Material details.
examineShape3D( ) calls examineGeometry( ) to examine the model's geometry, which checks out the possible subclasses of the Geometry object. Loaded models almost always use a subclass of GeometryArray (e.g., triangleArray, QuadArray), and examineGeometry( ) reports the number of vertices in the array. examineShape3D( ) is made more complicated by dealing with the possibility that several geometries may be assigned to a single shape. Two useful methods for the traversal code are Object.getClass( ), which returns the class name of the object, and the infix operation instanceof that tests for membership in a class (or superclass). Graph Traversal OutputexamineNode( ) is called from StoreGraphInfo( ), which first sets up a FileWriter object linked to the text file examObj.txt. The output of the traversal is redirected into the file, as shown below (when a model containing three dolphins is examined): Group: class javax.media.j3d.BranchGroup 3 children Leaf: class javax.media.j3d.Shape3D Material Object:AmbientColor=(0.7, 0.7, 0.7) EmissiveColor=(0.0, 0.0, 0.0) DiffuseColor=(0.3, 0.3, 0.3) SpecularColor=(1.0, 1.0, 1.0) Shininess=0.6 LightingEnable=true ColorTarget=2 Geometry: class javax.media.j3d.TriangleArray Vertex count: 1692 Leaf: class javax.media.j3d.Shape3D Material Object:AmbientColor=(0.7, 0.7, 0.7) EmissiveColor=(0.0, 0.0, 0.0) DiffuseColor=(0.3, 0.3, 0.3) SpecularColor=(1.0, 1.0, 1.0) Shininess=0.6 LightingEnable=true ColorTarget=2 Geometry: class javax.media.j3d.TriangleArray Vertex count: 1692 Leaf: class javax.media.j3d.Shape3D Material Object:AmbientColor=(0.7, 0.7, 0.7) EmissiveColor=(0.0, 0.0, 0.0) DiffuseColor=(0.3, 0.3, 0.3) SpecularColor=(1.0, 1.0, 1.0) Shininess=0.6 LightingEnable=true ColorTarget=2 Geometry: class javax.media.j3d.TriangleArray Vertex count: 1692 The three dolphins are represented by a BranchGroup with three Shape3D children. These store triangleArrays for each dolphin's geometry and have the same Material colors. The on-screen rendering of the dolphins is shown in Figure 16-9. |