ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Killer Game Programming in Java: A 3D Checkerboard, Part 2
Pages: 1, 2, 3, 4

Viewing the Scene Graph

This chapter has used scene graphs to illustrate the discussed coding techniques, and scene graphs are a useful way of understanding (and checking) code.

I received help with my drawings by using Daniel Selman's Java3dTree package. It creates a JFrame holding a textual tree representation of the scene graph (Figure 15-9).

Figure 15-9
Figure 15-9. Java3dTree representation of the Checkers3D scene graph

The tree (a JTree object) is initially minimized, and branches can be examined by clicking on the subfolder icons. Information about the currently selected node appears in the bottom window. The package is available in j3dtree.jar as part of the source code downloadable from http://www.manning.com/selman/ for Selman's Java 3D Programming text.

Augmenting code to generate the JTree is simple. WrapCheckers3D must import the j3dtree package and declare a global variable for the JFrame tree display:

    import com.sun.j3d.utils.behaviors.vp.*;

    private Java3dTree j3dTree;

The WrapCheckers3D( ) constructor creates the j3dTree object:

    public WrapCheckers3D( )
      // other code
      su = new SimpleUniverse(canvas3D);

      j3dTree = new Java3dTree( );   // create a display tree for the SG

      createSceneGraph( );
      initUserPosition( );
      su.addBranchGraph( sceneBG );

      j3dTree.updateNodes( su );    // build the tree display window

After the scene graph has been completed, (i.e., at the end of the constructor), the tree display is built with a single line:

    j3dTree.updateNodes( su );

However, prior to this, the capabilities of the scene graph nodes must be adjusted with:

    j3dTree.recursiveApplyCapability( sceneBG );

This operation should be carried out after the content branch group (sceneBG) has been completed, but before it is compiled or made live. In my code, this means adding the line to createSceneGraph( ):

    private void createSceneGraph( )
      sceneBG = new BranchGroup( );
      // other code to create the scene

      j3dTree.recursiveApplyCapability( sceneBG );

      sceneBG.compile( );

Unfortunately, you can't just call:

    j3dTree.recursiveApplyCapability( su );

without generating errors because the SimpleUniverse( ) constructor has made the ViewingPlatform live, which prevents further changes to its capabilities.

Since only the capabilities in the content branch have been adjusted, the call to updateNodes( ) will generate some warning messages when the view branch below the Locale node is encountered.

Compilation and execution must include j3dtree.jar in the classpath. My preferred approach is to do this via command line arguments:

    javac -classpath "%CLASSPATH%;j3dtree.jar" *.java

    java -cp "%CLASSPATH%;j3dtree.jar" Checkers3D

TIP: If typing the classpath repeatedly isn't to your taste, command lines like these can be hidden inside batch files or shell scripts.

The Java3dTree object is a textual representation of the scene, which means that I had to draw the scene graph myself. But the plus side is that tree generation has negligible impact on the rest of the program.

Another solution is to use the Java 3D Scene Graph Editor (http://java3d.netbeans.org/j3deditor_intro.html). This displays a graphical version of the scene graph but has the downside that its installation and usage are complicated and the memory requirements may be severe on some machines.

View catalog information for Killer Game Programming in Java

Return to ONJava.com.