/Users/lyon/j4p/src/j3d/cr325/Levitown.java

1    /** 
2     * Created by IntelliJ IDEA. 
3     * User: jgervasio 
4     * Date: Feb 23, 2004 
5     * Time: 12:11:58 PM 
6     * To change this template use Options | File Templates. 
7     */ 
8    package j3d.cr325; 
9     
10    
11   import com.sun.j3d.utils.applet.MainFrame; 
12   import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior; 
13   import com.sun.j3d.utils.geometry.Box; 
14   import com.sun.j3d.utils.geometry.Cylinder; 
15   import com.sun.j3d.utils.geometry.Sphere; 
16   import j3d.Utils; 
17    
18   import javax.media.j3d.*; 
19   import javax.vecmath.*; 
20   import java.applet.Applet; 
21   import java.awt.*; 
22    
23    
24   //   BillboardApp renders a simple landscape with billboard animated trees 
25    
26   public class Levitown extends Applet { 
27       private BoundingSphere bsBounds 
28               = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); 
29       PositionInterpolator bulletInterpolator = null; 
30       Alpha bulletAlpha = new Alpha(1, 0, 0, 500, 0, 0); 
31    
32       public Levitown() { 
33           Canvas3D c3d 
34                   = Utils.getCanvas3D(this); 
35           Locale l 
36                   = new Locale(new VirtualUniverse()); 
37           l.addBranchGraph(buildViewBranch(c3d)); 
38           TransformGroup tg = Shapes.getReadWriteTransformGroup(); 
39           l.addBranchGraph(getSceneBranchGroup(tg)); 
40           setLayout(new BorderLayout()); 
41           add("Center", c3d); 
42    
43       } 
44    
45    
46       protected static BranchGroup buildViewBranch(Canvas3D c) { 
47           BranchGroup viewBranch = new BranchGroup(); 
48    
49    
50           ViewPlatform myViewPlatform = new ViewPlatform(); 
51           TransformGroup viewXfmGroup = getViewTransformGroup(getViewTilt()); 
52           viewXfmGroup.addChild(myViewPlatform); 
53           viewBranch.addChild(viewXfmGroup); 
54           addKeyNavigation(viewXfmGroup, viewBranch); 
55           View myView = new View(); 
56           myView.addCanvas3D(c); 
57           myView.attachViewPlatform(myViewPlatform); 
58           myView.setPhysicalBody(new PhysicalBody()); 
59           myView.setPhysicalEnvironment(new PhysicalEnvironment()); 
60           return viewBranch; 
61       } 
62    
63       private static void addKeyNavigation(TransformGroup viewXfmGroup, BranchGroup viewBranch) { 
64           KeyNavigatorBehavior knb = new KeyNavigatorBehavior(viewXfmGroup); 
65           knb.setSchedulingBounds(new BoundingSphere()); 
66           viewBranch.addChild(knb); 
67       } 
68    
69       private static Matrix3d getViewTilt() { 
70           Matrix3d viewTilt = new Matrix3d(); 
71           viewTilt.rotX(Math.PI / -6); 
72           return viewTilt; 
73       } 
74    
75       private static TransformGroup getViewTransformGroup(Matrix3d viewTilt) { 
76           Transform3D t3d = new Transform3D(); 
77           t3d.set(viewTilt, new Vector3d(0.0, 10.0, 10.0), 1.0); 
78           TransformGroup viewXfmGroup 
79                   = new TransformGroup(t3d); 
80           return viewXfmGroup; 
81       } 
82    
83       public BranchGroup getSceneBranchGroup(TransformGroup tg) { 
84           BranchGroup bg = Shapes.getReadWriteBranchGroup(); 
85    
86           Vector3f translate = new Vector3f(); 
87           Transform3D t3d = new Transform3D(); 
88    
89           BoundingSphere bs = new BoundingSphere(); 
90           bs.setRadius(10); 
91    
92           bg.addChild(Shapes.getLand()); 
93           bg.addChild(Shapes.getTreeBranchGroup()); 
94           bg.addChild(Shapes.getHousesBranchGroup()); 
95           bg.addChild(Shapes.getBackgroundBranchGroup()); 
96    
97           raiseViewAboveGround(translate, t3d, tg); 
98           //KeyBehavior keyNavBeh = new KeyBehavior( tg); 
99           KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(tg); 
100          keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 10000.0)); 
101          bg.addChild(keyNavBeh); 
102   
103          bg.addChild(Target.getTargetGroup()); 
104          BranchGroup gunBg = buildGun(); 
105          Node theBulletNode = getBulletGroup(); 
106          bg.addChild(theBulletNode); 
107          bg.addChild(gunBg); 
108          return bg; 
109      } // end of CreateSceneGraph method of BillboardApp 
110   
111      protected BranchGroup getBulletGroup() { 
112          BranchGroup bullet = new BranchGroup(); 
113   
114          Appearance bulletAppearance = new Appearance(); 
115          Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f); 
116          Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); 
117          Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); 
118          Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f); 
119          float shininess = 20.0f; 
120          bulletAppearance.setMaterial(new Material(ambientColour, emissiveColour, 
121                  diffuseColour, specularColour, shininess)); 
122   
123          Sphere ball = new Sphere(0.2f, bulletAppearance); 
124   
125          TransformGroup ballMovXfmGrp = new TransformGroup(); 
126          ballMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); 
127          ballMovXfmGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); 
128          ballMovXfmGrp.addChild(ball); 
129          bullet.addChild(ballMovXfmGrp); 
130   
131   
132          bulletInterpolator = new PositionInterpolator(bulletAlpha, 
133                  ballMovXfmGrp, 
134                  new Transform3D(), 0.0f, 50.0f); 
135          bulletInterpolator.setSchedulingBounds(getBsBounds()); 
136          bullet.addChild(bulletInterpolator); 
137   
138          return bullet; 
139   
140      } 
141   
142      /** 
143       * This builds the gun geometry. 
144       * It uses box and cylinder primitives and sets up a 
145       * transform group so that we can rotate the gun. 
146       */ 
147      protected BranchGroup buildGun() { 
148   
149          Appearance gunApp = new Appearance(); 
150          Color3f ambientColour = new Color3f(0.5f, 0.5f, 0.5f); 
151          Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); 
152          Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); 
153          Color3f diffuseColour = new Color3f(0.5f, 0.5f, 0.5f); 
154          float shininess = 20.0f; 
155          gunApp.setMaterial(new Material(ambientColour, emissiveColour, 
156                  diffuseColour, specularColour, shininess)); 
157          TransformGroup init = Shapes.getReadWriteTransformGroup(); 
158          TransformGroup barrel = Shapes.getReadWriteTransformGroup(); 
159          Transform3D gunXfm = new Transform3D(); 
160          Transform3D barrelXfm = new Transform3D(); 
161          barrelXfm.set(new Vector3d(0.0, -2.0, 0.0)); 
162          barrel.setTransform(barrelXfm); 
163          Matrix3d gunXfmMat = new Matrix3d(); 
164          gunXfmMat.rotX(Math.PI / 2); 
165          Vector3d gunTranslation = new Vector3d(0.0, -.250, -5); 
166          gunXfm.set(gunXfmMat, gunTranslation, 1.0); 
167          init.setTransform(gunXfm); 
168   
169          gunXfmGrp.addChild(new Box(1.0f, 1.0f, 0.5f, gunApp)); 
170          barrel.addChild(new Cylinder(0.3f, 4.0f, gunApp)); 
171          gunXfmGrp.addChild(barrel); 
172   
173          BranchGroup gunBranchGroup = Shapes.getReadWriteBranchGroup(); 
174          gunBranchGroup.addChild(init); 
175          init.addChild(addKeyNavigatorBehavior(init)); 
176   
177          return gunBranchGroup; 
178      } 
179   
180      private KeyNavigatorBehavior addKeyNavigatorBehavior(TransformGroup init) { 
181          KeyNavigatorBehavior knb = new KeyNavigatorBehavior(gunXfmGrp); 
182          knb.setSchedulingBounds(new BoundingSphere()); 
183          init.addChild(gunXfmGrp); 
184          return knb; 
185      } 
186   
187      TransformGroup gunXfmGrp = Shapes.getReadWriteTransformGroup(); 
188   
189      private void raiseViewAboveGround(Vector3f translate, Transform3D t3D, TransformGroup tg) { 
190          translate.set(0.0f, 0.3f, 0.0f); 
191          t3D.setTranslation(translate); 
192          tg.setTransform(t3D); 
193      } 
194   
195   
196      public BoundingSphere getBsBounds() { 
197          return bsBounds; 
198      } 
199   
200      public static void main(String[] args) { 
201          new MainFrame(new Levitown(), 256, 256); 
202      } // end of main (method of BillboardApp) 
203  } 
204