1
2
3
4
5
6
7
8
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 2003 <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/gpl.html">General Public
32 * License (GPL) </a>, no warranty <br>
33 *
34 * @version 1.0 10.05.2004 <br>
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
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
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
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
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
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
97 this.addLights(this.lightBranch);
98
99
100 this.addChild(rootTransformGroup);
101 rootTransformGroup.addChild(this.dynamicObjectBranch);
102 rootTransformGroup.addChild(this.lightBranch);
103 rootTransformGroup.addChild(this.staticObjectBranch);
104
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
115 BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0),
116 1000.0);
117
118
119 Color3f ambientColor = new Color3f(0.4f, 0.4f, 0.4f);
120 AmbientLight ambientLight = new AmbientLight(ambientColor);
121 ambientLight.setInfluencingBounds(bounds);
122
123
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
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 }