Executing the Rules


Creating the Scene

The WrapTrees3D class creates the scene: the checkerboard floor, the background, the lights, and an OrbitBehavior allow the user to move around.

Most of the tree-related activity is started in growTrees( ), which creates five transformGroups and five treeLimb objects. The code fragment shows one of these transformGroups and treeLimbs:

     // starting position for the first tree: (0,0,-5)     Transform3D t3d = new Transform3D( );     t3d.set( new Vector3f(0,0,-5));     TransformGroup tg0 = new TransformGroup(t3d);     sceneBG.addChild(tg0);     // create the tree     TreeLimb t0 = new TreeLimb(Z_AXIS, 0, 0.05f, 0.5f, tg0, null);

The TRansformGroup reference (tg0) is passed to the TReeLimb constructor, where it's used to position the limb.

Loading Leaves

growTrees( ) loads a sequence of leaf images, storing them in an ImageComponent2D array. This increases efficiency because each TReeLimb will utilize this array if it requires leaves rather than loading its own copy of the images:

     // load the leaf images used by all the trees     ImageComponent2D[] leafIms = loadImages("images/leaf", 6);

The loadImages( ) method is shown here:

     private ImageComponent2D[] loadImages(String fNms, int numIms)     /* Load the leaf images: they all start with fNms, and there are        numIms of them.  */     {       String filename;       TextureLoader loader;       ImageComponent2D[] ims = new ImageComponent2D[numIms];       System.out.println("Loading " + numIms +" textures from "+ fNms);       for (int i=0; i < numIms; i++) {         filename = new String(fNms + i + ".gif");         loader = new TextureLoader(filename, null);         ims[i] = loader.getImage( );         if(ims[i] == null)           System.out.println("Load failed for: " + filename);         ims[i].setCapability(ImageComponent2D.ALLOW_SIZE_READ);       }       return ims;     }

The leaf images are in the files images/leaf0-5.gif, as shown in Figure 28-3.

The gray and white squares in Figure 28-3 indicate that the GIFs' backgrounds are transparent.


The idea here is that the ImageCsSeries screens will iterate through the images from leaf0.gif to leaf5.gif. The resulting effect will be growing leaves, turning darker.

The drawing style has some room for artistic improvement. However, the effect is still good, especially when viewed at some distance, with many leaves overlapping. Later images were created by modifying earlier ones, so the transition from one image to the next is smooth.

Figure 28-3. The leaf images in leaf0-5.gif


Depth-Sorting for Transparency

Since the scene will contain numerous semi-transparent textures wrapped over quads (i.e., the leaves on the trees), it's necessary to turn on Java 3D's depth-sorting of transparent objects on a per-geometry basis:

     View view = su.getViewer( ).getView( );     view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY);

This is carried out in the WrapTrees3D( ) constructor. If depth-sorting isn't switched on, the relative ordering of the leaf images will frequently be wrong as the viewer moves around the trees.

Getting Ready for Growth

WrapTrees3D's growTrees( ) initializes the GrowthBehavior object:

     // the behavior that manages the growing of the trees     GrowthBehavior grower = new GrowthBehavior(leafIms);     grower.setSchedulingBounds( bounds );     // add the trees to GrowthBehavior     grower.addLimb(t0);     grower.addLimb(t1);     grower.addLimb(t2);     grower.addLimb(t3);     grower.addLimb(t4);     sceneBG.addChild( grower );



Killer Game Programming in Java
Killer Game Programming in Java
ISBN: 0596007302
EAN: 2147483647
Year: 2006
Pages: 340

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net