View Javadoc

    1   /*
    2    * @(#) ContentBranch.java May 10, 2004
    3    *
    4    * Copyright (c) 2002-2005 Delft University of Technology Jaffalaan 5, 2628 BX
    5    * Delft, the Netherlands. All rights reserved.
    6    *
    7    * This software is proprietary information of Delft University of Technology
    8    * The code is published under the Lesser General Public License
    9    */
    10  package nl.tudelft.simulation.dsol.gui.animation3D;
    11
    12  import javax.media.j3d.AmbientLight;
    13  import javax.media.j3d.BoundingSphere;
    14  import javax.media.j3d.BranchGroup;
    15  import javax.media.j3d.DirectionalLight;
    16  import javax.media.j3d.Group;
    17  import javax.media.j3d.Node;
    18  import javax.media.j3d.Transform3D;
    19  import javax.media.j3d.TransformGroup;
    20  import javax.vecmath.AxisAngle4d;
    21  import javax.vecmath.Color3f;
    22  import javax.vecmath.Point3d;
    23  import javax.vecmath.Vector3f;
    24
    25  /***
    26   * ContentBranch, the content branch of the scene graph <br>
    27   * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
    28   * University of Technology </a>, the Netherlands. <br>
    29   * See for project information <a
    30   * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
    31   * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
    32   * General Public License (LGPL) </a>, no warranty.
    33   *
    34   * @version $Revision$ $Date$
    35   * @author <a href="http://www.tbm.tudelft.nl/webstaf/royc/index.htm">Roy Chin
    36   *         </a>
    37   */
    38  public class ContentBranch extends BranchGroup
    39  {
    40  	/*** The light branch */
    41  	protected BranchGroup lightBranch = new BranchGroup();
    42
    43  	/*** The dynamic object branch */
    44  	protected BranchGroup dynamicObjectBranch = new BranchGroup();
    45
    46  	/*** The static object branch */
    47  	protected BranchGroup staticObjectBranch = new BranchGroup();
    48
    49  	/***
    50  	 * Construct the scene graph
    51  	 */
    52  	public ContentBranch()
    53  	{
    54  		super();
    55  		createBranch();
    56  	}
    57
    58  	/***
    59  	 * Create the content branch
    60  	 */
    61  	protected void createBranch()
    62  	{
    63  		// set capabilities of content branch
    64  		this.setCapability(Group.ALLOW_CHILDREN_READ);
    65  		this.setCapability(Group.ALLOW_CHILDREN_WRITE);
    66  		this.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    67  		this.setCapability(BranchGroup.ALLOW_DETACH);
    68  		this.setCapability(Node.ALLOW_PICKABLE_READ);
    69  		this.setCapability(Node.ALLOW_PICKABLE_WRITE);
    70
    71  		// set capabilities of the dynamic branch
    72  		this.dynamicObjectBranch.setCapability(Group.ALLOW_CHILDREN_READ);
    73  		this.dynamicObjectBranch.setCapability(Group.ALLOW_CHILDREN_WRITE);
    74  		this.dynamicObjectBranch.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    75  		this.dynamicObjectBranch.setCapability(BranchGroup.ALLOW_DETACH);
    76  		this.dynamicObjectBranch.setCapability(Node.ALLOW_PICKABLE_READ);
    77  		this.dynamicObjectBranch.setCapability(Node.ALLOW_PICKABLE_READ);
    78
    79  		// set capabilities of the static branch
    80  		this.staticObjectBranch.setCapability(Group.ALLOW_CHILDREN_READ);
    81  		this.staticObjectBranch.setCapability(Group.ALLOW_CHILDREN_WRITE);
    82  		this.staticObjectBranch.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    83  		this.staticObjectBranch.setCapability(BranchGroup.ALLOW_DETACH);
    84
    85  		// Transform so that Z axis points upward
    86  		Transform3D transform = new Transform3D();
    87  		transform.set(new AxisAngle4d(1.0, 0.0, 0.0, Math.PI * -.5));
    88  		TransformGroup rootTransformGroup = new TransformGroup(transform);
    89  		// set capabilities of the rootTransformGroup
    90  		rootTransformGroup.setCapability(Group.ALLOW_CHILDREN_READ);
    91  		rootTransformGroup.setCapability(Group.ALLOW_CHILDREN_WRITE);
    92  		rootTransformGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    93  		rootTransformGroup.setCapability(Node.ALLOW_PICKABLE_READ);
    94  		rootTransformGroup.setCapability(Node.ALLOW_PICKABLE_READ);
    95
    96  		// Add some default light to the darkness
    97  		this.addLights(this.lightBranch);
    98
    99  		// Build the scene
    100 		this.addChild(rootTransformGroup);
    101 		rootTransformGroup.addChild(this.dynamicObjectBranch);
    102 		rootTransformGroup.addChild(this.lightBranch);
    103 		rootTransformGroup.addChild(this.staticObjectBranch);
    104 		// contentBranch.compile();
    105 	}
    106
    107 	/***
    108 	 * Add lights
    109 	 *
    110 	 * @param branchGroup The branch group to add the light to
    111 	 */
    112 	protected void addLights(final BranchGroup branchGroup)
    113 	{
    114 		// Create bounding sphere for the light
    115 		BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
    116 				1000.0);
    117
    118 		// Setup ambient light
    119 		Color3f ambientColor = new Color3f(0.4f, 0.4f, 0.4f);
    120 		AmbientLight ambientLight = new AmbientLight(ambientColor);
    121 		ambientLight.setInfluencingBounds(bounds);
    122
    123 		// Setup directional light
    124 		Color3f lightColor = new Color3f(0.2f, 0.2f, 0.2f);
    125 		Vector3f lightDirection = new Vector3f(-1.0f, -0.7f, -0.5f);
    126 		DirectionalLight light = new DirectionalLight(lightColor,
    127 				lightDirection);
    128 		light.setInfluencingBounds(bounds);
    129
    130 		// Add lights to branch group
    131 		branchGroup.addChild(ambientLight);
    132 		branchGroup.addChild(light);
    133 	}
    134
    135 	/***
    136 	 * @return LightBranch
    137 	 */
    138 	public BranchGroup getLightBranch()
    139 	{
    140 		return this.lightBranch;
    141 	}
    142
    143 	/***
    144 	 * @return DynamicObjectBranch: dynamic objects
    145 	 */
    146 	public BranchGroup getDynamicObjectBranch()
    147 	{
    148 		return this.dynamicObjectBranch;
    149 	}
    150
    151 	/***
    152 	 * @return StaticObjectBranch: static objects
    153 	 */
    154 	public BranchGroup getStaticObjectBranch()
    155 	{
    156 		return this.staticObjectBranch;
    157 	}
    158
    159 }