/Users/lyon/j4p/src/j3d/cr325/LevitownDoomKeyboard.java
|
1 package j3d.cr325;
2
3 /**
4 * Created by IntelliJ IDEA.
5 * User: jgervasio
6 * Date: Feb 23, 2004
7 * Time: 12:11:58 PM
8 * To change this template use Options | File Templates.
9 */
10
11
12 import com.sun.j3d.utils.applet.MainFrame;
13 import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
14 import com.sun.j3d.utils.geometry.Cylinder;
15 import com.sun.j3d.utils.geometry.Primitive;
16 import com.sun.j3d.utils.geometry.Sphere;
17 import com.sun.j3d.utils.universe.PlatformGeometry;
18 import j3d.GunController;
19 import j3d.Utils;
20
21 import javax.media.j3d.*;
22 import javax.vecmath.*;
23 import java.applet.Applet;
24 import java.awt.*;
25 import java.util.Enumeration;
26 import java.util.Vector;
27
28
29 // BillboardApp renders a simple landscape with billboard animated trees
30 // j3d.cr325.LevitownDoomKeyboard
31 public class LevitownDoomKeyboard extends Applet {
32
33 Alpha joeAlpha = new Alpha(-1, 0, 0, 3000, 0, 0);
34 protected BoundingSphere bounds
35 = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 200.0);
36 Alpha ballAlpha = new Alpha(1, 0, 0, 500, 0, 0);
37 PositionInterpolator bulletPositionInterpolator = null;
38 TransformGroup gunTransformGroup = Utils.getTransformGroup();
39 TransformGroup bulletTransformGroup = Utils.getTransformGroup();
40 TransformGroup targetTransformGroup = Utils.getTransformGroup();
41 public Switch joeSwitch = getJoeSwitch();
42
43
44 public BranchGroup getSceneBranchGroup(TransformGroup viewTransformGroup) {
45
46 BranchGroup rootBranchGroup = new BranchGroup();
47
48 Vector3f translate = new Vector3f();
49 Transform3D T3D = new Transform3D();
50
51 BoundingSphere bs
52 = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 20000);
53 rootBranchGroup.addChild(Shapes.getLand());
54
55 addTrees(translate, T3D, bs, rootBranchGroup);
56 addHouses(translate, T3D, bs, rootBranchGroup);
57
58 raiseViewAboveGround(translate, T3D, viewTransformGroup);
59 addKeyboardNavigation(viewTransformGroup, rootBranchGroup);
60
61 //BranchGroup target = new BranchGroup();
62 //target = Target.getTargetGroup();
63 //textureSphere.addChild(target);
64
65 BranchGroup textureSphere = new BranchGroup();
66 textureSphere.addChild(getJoeGroup());
67 rootBranchGroup.addChild(textureSphere);
68
69
70 Matrix3d viewTilt = new Matrix3d();
71 viewTilt.rotX(Math.PI / -6);
72 T3D.set(viewTilt, new Vector3d(0.0, 10.0, 10.0), 1.0);
73
74 TransformGroup viewXfmGroup = new TransformGroup(T3D);
75 //ViewPlatform myViewPlatform = new ViewPlatform();
76 PhysicalBody myBody = new PhysicalBody();
77 PhysicalEnvironment myEnvironment = new PhysicalEnvironment();
78 //viewXfmGroup.addChild(myViewPlatform);
79 rootBranchGroup.addChild(viewXfmGroup);
80
81 view = new View();
82 //myView.addCanvas3D(Utils.getCanvas3D(this));
83 //myView.attachViewPlatform(myViewPlatform);
84 view.setPhysicalBody(myBody);
85 view.setPhysicalEnvironment(myEnvironment);
86 gc.setView(view);
87 rootBranchGroup.compile();
88 return rootBranchGroup;
89 } // end of CreateSceneGraph method of BillboardApp
90 KeyBehavior keyNavBeh = null;
91
92
93 public void addKeyboardNavigation(TransformGroup transformGroup, BranchGroup bg) {
94 keyNavBeh = new KeyBehavior(transformGroup);
95 keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 10000.0));
96
97 BranchGroup gunBranchGroup = new BranchGroup();
98 gunBranchGroup = buildGun(transformGroup);
99 gc.setGun(gunBranchGroup);
100 bg.addChild(keyNavBeh);
101
102 bg.addChild(gunBranchGroup);
103 }
104
105 public void addKeyboardNavigation2(TransformGroup tg, BranchGroup bg) {
106 KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(tg);
107 keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 10000.0));
108
109 BranchGroup textureGun = new BranchGroup();
110 textureGun = buildGun(tg);
111 gc.setGun(textureGun);
112 bg.addChild(textureGun);
113 bg.addChild(keyNavBeh);
114 }
115
116 private Vector processCriteria(Enumeration enumeration) {
117 Vector v = new Vector();
118 while (enumeration.hasMoreElements()) {
119 WakeupCriterion wc = (WakeupCriterion) enumeration.nextElement();
120 v.add(wc);
121 if (wc instanceof WakeupOnElapsedFrames) continue;
122 processKeyboardEvent(wc);
123 }
124 return v;
125 }
126
127 private void processKeyboardEvent(WakeupCriterion wc) {
128 WakeupOnAWTEvent w = (WakeupOnAWTEvent) wc;
129 AWTEvent[] awtEvent = w.getAWTEvent();
130 for (int i = 0; i < awtEvent.length; i++)
131 System.out.println("event=" + awtEvent[i]);
132 }
133
134 private void raiseViewAboveGround(Vector3f translate, Transform3D t3D, TransformGroup tg) {
135 translate.set(0.0f, 0.3f, 0.0f);
136 t3D.setTranslation(translate);
137 tg.setTransform(t3D);
138 }
139
140 private void addHouses(Vector3f translate,
141 Transform3D t3D,
142 BoundingSphere bSphere,
143 BranchGroup bg) {
144 SharedGroup share = new SharedGroup();
145
146 share.addChild(Shapes.getHouses());
147
148 float position[][] = {{0.0f, 0f, -2.0f},
149 {3.0f, 0f, 0.0f},
150 {3.0f, 0f, 4.0f},
151 {4.0f, 0f, -8.0f},
152 {6.0f, 0f, -25.0f},
153 {-6.0f, 0f, 25.0f},
154 {-6.0f, 0f, 15.0f},
155 {6.0f, 0f, 1.0f}};
156
157 placeStuff(position, translate, t3D, bSphere, bg, share);
158 }
159
160 private void addTrees(Vector3f translate,
161 Transform3D t3D,
162 BoundingSphere bSphere,
163 BranchGroup bg) {
164 SharedGroup share = new SharedGroup();
165 share.addChild(Shapes.getTrees());
166
167 float position[][] = {{0.0f, 0f, -3.0f},
168 {6.0f, 0f, 0.0f},
169 {6.0f, 0f, 6.0f},
170 {3.0f, 0f, -10.0f},
171 {13.0f, 0f, -30.0f},
172 {-13.0f, 0f, 30.0f},
173 {-13.0f, 0f, 23.0f},
174 {13.0f, 0f, 3.0f}};
175
176 placeStuff(position, translate, t3D, bSphere, bg, share);
177 }
178
179 private void placeStuff(float[][] position,
180 Vector3f translateV3f,
181 Transform3D transform3D,
182 BoundingSphere bSphere,
183 BranchGroup branchGroup,
184 SharedGroup sharedGroup) {
185 TransformGroup transformGroupT;
186 TransformGroup transformGroupR;
187 Billboard billboard;
188 for (int i = 0; i < position.length; i++) {
189 translateV3f.set(position[i]);
190 transform3D.setTranslation(translateV3f);
191 transformGroupT = new TransformGroup(transform3D);
192 transformGroupR = new TransformGroup();
193 transformGroupR.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
194 billboard = new Billboard(transformGroupR);
195 billboard.setSchedulingBounds(bSphere);
196 billboard.setAlignmentMode(Billboard.ROTATE_ABOUT_POINT);
197 branchGroup.addChild(transformGroupT);
198 branchGroup.addChild(billboard);
199 transformGroupT.addChild(transformGroupR);
200 transformGroupR.addChild(new Link(sharedGroup));
201 }
202 }
203
204 GunController gc = null;
205
206 public LevitownDoomKeyboard() {
207
208 Canvas3D c3d = Utils.getCanvas3D(this);
209
210 setLayout(new BorderLayout());
211 add("Center", c3d);
212
213 // SimpleUniverse is a Convenience Utility class
214 //SimpleUniverse su = new SimpleUniverse(c3d);
215 BasicUniverse su = setUpBasicUniverse(c3d);
216
217 //ViewingPlatform viewingPlatform = su.getViewingPlatform();
218 //viewTransformGroup
219 // = viewingPlatform.getViewPlatformTransform();
220 viewTransformGroup = su.getViewTransformGroup();
221 gc = new GunController();
222 gc.setCanvas3D(c3d);
223 c3d.addKeyListener(gc);
224
225
226 su.addBranchGraph(getSceneBranchGroup(viewTransformGroup));
227 }
228
229 private BasicUniverse setUpBasicUniverse(Canvas3D c3d) {
230 BasicUniverse su = new BasicUniverse(c3d, 10);
231 su.addBranchGraph(createBackground(this));
232 su.addBranchGraph(this.getBulletGroup());
233 return su;
234 }
235
236 private Transform3D getViewTransform() {
237 Transform3D viewTransform3d = new Transform3D();
238 viewTransformGroup.getTransform(viewTransform3d);
239 return viewTransform3d;
240 }
241
242 TransformGroup viewTransformGroup = null;
243 private View view = null;
244
245
246 BoundingSphere getBackgroundBoundingSphere() {
247 //return new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 200.0);
248 return new BoundingSphere();
249 }
250
251 public static BranchGroup createBackground(LevitownDoomKeyboard levitown) {
252 // create a parent BranchGroup for the Background
253 BranchGroup backgroundGroup = new BranchGroup();
254
255 // create a new Background node
256 Background back = new Background();
257
258 // set the range of influence of the background
259 back.setApplicationBounds(levitown.getBackgroundBoundingSphere());
260
261 // create a BranchGroup that will hold
262 // our Sphere geometry
263 BranchGroup bgGeometry = new BranchGroup();
264
265 // create the Sphere geometry with radius 1.0
266 // we tell the Sphere to generate texture coordinates
267 // to enable the texture image to be rendered
268 // and because we are *inside* the Sphere we have to generate
269 // Normal coordinates inwards or the Sphere will not be visible.
270 Appearance appearance = new Appearance();
271 appearance.setTexture(Utils.getMandleTexture(640, 480));
272 Sphere sphere = new Sphere(0.50f,
273 Primitive.GENERATE_TEXTURE_COORDS |
274 Primitive.GENERATE_NORMALS_INWARD,
275 appearance);
276
277 // start wiring everything together
278 // add the Sphere to its parent BranchGroup
279 bgGeometry.addChild(sphere);
280
281 // assign the BranchGroup to the Background as geometry.
282 back.setGeometry(bgGeometry);
283
284 // add the Background node to its parent BranchGroup
285 backgroundGroup.addChild(back);
286
287 return backgroundGroup;
288 }
289
290 protected BranchGroup getJoeGroup() {
291 BranchGroup theJoeSphere = new BranchGroup();
292
293 BranchGroup s1 = getJoeSphere("gifs\\animation3.gif");
294 BranchGroup s2 = getDeadJoeSphere("shuttle\\aw626.jpg");
295
296 TransformGroup joeRotXfmGrp = new TransformGroup();
297 Transform3D joeRotXfm = new Transform3D();
298 Matrix3d joeRotMat = new Matrix3d();
299 joeRotMat.rotY(Math.PI / 2);
300 joeRotXfm.set(joeRotMat, new Vector3d(0.0, 0.0, -30.0), 1.0);
301 joeRotXfmGrp.setTransform(joeRotXfm);
302 joeRotXfmGrp.addChild(joeSwitch);
303
304 joeSwitch.addChild(s1);
305 joeSwitch.addChild(s2);
306
307
308 targetTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
309 targetTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
310 targetTransformGroup.addChild(joeRotXfmGrp);
311
312 Transform3D axis = new Transform3D();
313 PositionInterpolator moveJoe =
314 new PositionInterpolator(joeAlpha,
315 targetTransformGroup,
316 axis,
317 -50.0f, 50.0f);
318 BoundingSphere bs = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 200);
319 moveJoe.setSchedulingBounds(bs);
320 theJoeSphere.addChild(moveJoe);
321 theJoeSphere.addChild(targetTransformGroup);
322 return theJoeSphere;
323 }
324
325 public static Switch getJoeSwitch() {
326 Switch s = new Switch(0);
327 s.setCapability(Switch.ALLOW_SWITCH_WRITE);
328 return s;
329 }
330
331 protected BranchGroup getJoeSphere(String img) {
332 BranchGroup theJoeSphere = new BranchGroup();
333 String img1 = img;
334 theJoeSphere = (Utils.getTextureSphere(1.5, 0, 1.125, img1));
335 return theJoeSphere;
336 }
337
338 private BranchGroup getDeadJoeSphere(String img) {
339 BranchGroup theJoeSphere = new BranchGroup();
340 String img1 = img;
341 joeSwitch.addChild(Utils.getTextureSphere(1.5, 0, 1.125, img1));
342 return theJoeSphere;
343 }
344
345
346 public static PlatformGeometry buildGunPlatform() {
347 BranchGroup theGun = new BranchGroup();
348 Appearance gunApp = new Appearance();
349 Color3f ambientColour = new Color3f(0.5f, 0.5f, 0.5f);
350 Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
351 Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
352 Color3f diffuseColour = new Color3f(0.5f, 0.5f, 0.5f);
353 float shininess = 20.0f;
354 gunApp.setMaterial(new Material(ambientColour, emissiveColour,
355 diffuseColour, specularColour, shininess));
356
357 TransformGroup initTransformGroup = new TransformGroup();
358 TransformGroup barrelTransformGroup = new TransformGroup();
359
360 Transform3D gunXfm = new Transform3D();
361 Transform3D barrelXfm = new Transform3D();
362 barrelXfm.set(new Vector3d(0, -2.0, 1));
363 barrelTransformGroup.setTransform(barrelXfm);
364 Matrix3d gunXfmMat = new Matrix3d();
365 gunXfmMat.rotX(Math.PI / 2);
366 gunXfm.set(gunXfmMat);
367 // lets try this..
368 initTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
369 initTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
370 barrelTransformGroup.addChild(new Cylinder(0.3f, 7.0f, gunApp));
371
372 theGun.addChild(initTransformGroup);
373
374 PlatformGeometry pg = new PlatformGeometry();
375 pg.addChild(theGun);
376 return pg;
377 }
378
379 protected BranchGroup buildGun(TransformGroup transformGroup) {
380 BranchGroup theGun = new BranchGroup();
381 Appearance gunApp = new Appearance();
382 Color3f ambientColour = new Color3f(0.5f, 0.5f, 0.5f);
383 Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
384 Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
385 Color3f diffuseColour = new Color3f(0.5f, 0.5f, 0.5f);
386 float shininess = 20.0f;
387 gunApp.setMaterial(new Material(ambientColour, emissiveColour,
388 diffuseColour, specularColour, shininess));
389
390 TransformGroup initTransformGroup = new TransformGroup();
391 TransformGroup barrelTransformGroup = new TransformGroup();
392
393 Transform3D gunXfm = new Transform3D();
394 Transform3D barrelXfm = new Transform3D();
395 barrelXfm.set(new Vector3d(0, -2.0, 1));
396 barrelTransformGroup.setTransform(barrelXfm);
397 Matrix3d gunXfmMat = new Matrix3d();
398 gunXfmMat.rotX(Math.PI / 2);
399 gunXfm.set(gunXfmMat);
400 gunTransformGroup.setTransform(gunXfm);
401 // lets try this..
402 initTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
403 initTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
404 keyNavBeh.setGunTransformGroup(initTransformGroup);
405 keyNavBeh.setBulletTransformGroup(bulletTransformGroup);
406 keyNavBeh.setTargetTransformGroup(targetTransformGroup);
407 gc.setTransformGroup(gunTransformGroup);
408
409 gunTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
410 //gunXfmGrp.addChild(new Box(1.0f, 1.0f, 0.5f, gunApp));
411 barrelTransformGroup.addChild(new Cylinder(0.3f, 7.0f, gunApp));
412 gunTransformGroup.addChild(barrelTransformGroup);
413 theGun.addChild(initTransformGroup);
414 initTransformGroup.addChild(gunTransformGroup);
415 return theGun;
416 }
417
418 protected BranchGroup getBulletGroup() {
419 BranchGroup bulletBranchGroup = new BranchGroup();
420
421 Appearance bulletAppearance = new Appearance();
422 Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f);
423 Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f);
424 Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f);
425 Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f);
426 float shininess = 20.0f;
427 bulletAppearance.setMaterial(new Material(ambientColour, emissiveColour,
428 diffuseColour, specularColour, shininess));
429
430 Sphere bulletSphere = new Sphere(0.2f, bulletAppearance);
431
432
433 bulletTransformGroup.addChild(bulletSphere);
434 bulletBranchGroup.addChild(bulletTransformGroup);
435
436 bulletPositionInterpolator = new PositionInterpolator(ballAlpha,
437 bulletTransformGroup,
438 new Transform3D(), 0.0f, 50.0f);
439 bulletPositionInterpolator.setSchedulingBounds(bounds);
440 bulletBranchGroup.addChild(bulletPositionInterpolator);
441
442 return bulletBranchGroup;
443
444 }
445
446
447 public static void main(String[] args) {
448 new MainFrame(new LevitownDoomKeyboard(), 400, 400);
449 } // end of main (method of BillboardApp)
450 }
451