/* Mykeynavbeh.java * Originally based on code from BackgroundApp.java *   @(#)BackgroundApp.java 1.1 00/09/22 14:03 * * portions Copyright (c) 1996-2000 Sun Microsystems, Inc. All Rights Reserved. *  * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */
import java.awt.*;
import javax.media.j3d.*;import javax.vecmath.*;
import com.sun.j3d.utils.universe.SimpleUniverse;import com.sun.j3d.utils.universe.PlatformGeometry;import com.sun.j3d.utils.behaviors.keyboard.*;
import com.sun.j3d.loaders.Scene;
import com.sun.j3d.loaders.objectfile.ObjectFile;
import java.io.*;import java.awt.event.*;
public class Mykeynavbeh extends Frame {
private SimpleUniverse universe = null; private Canvas3D canvas = null; private TransformGroup viewtrans = null; private TransformGroup viewtrans_2 = null;
private boolean firstTime = true;
private static final Point3d USERPOSN = new Point3d(-0.105, 2.4, 7.1);
public Mykeynavbeh() { setLayout(new BorderLayout()); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
canvas = new Canvas3D(config); add("Center", canvas); universe = new SimpleUniverse(canvas);
BranchGroup scene = createSceneGraph(); universe.getViewingPlatform().setNominalViewingTransform(); universe.getViewer().getView().setBackClipDistance(100.0); universe.addBranchGraph(scene);
viewtrans_2 = universe.getViewingPlatform().getViewPlatformTransform(); Transform3D t3d_vt = new Transform3D(); viewtrans_2.getTransform(t3d_vt);
// args are: viewer posn, where looking, up direction t3d_vt.lookAt(USERPOSN, new Point3d(-0.1, 0, 0), new Vector3d(0, 1, 0)); t3d_vt.invert(); viewtrans_2.setTransform(t3d_vt);
}
private BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup();
BoundingSphere bounds = new BoundingSphere(new Point3d(), 10000.0);
viewtrans = universe.getViewingPlatform().getViewPlatformTransform(); KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(viewtrans); keyNavBeh.setSchedulingBounds(bounds); PlatformGeometry platformGeom = new PlatformGeometry(); platformGeom.addChild(keyNavBeh); universe.getViewingPlatform().setPlatformGeometry(platformGeom);
objRoot.addChild(createFerrisWheel());
return objRoot; }
private BranchGroup createFerrisWheel() {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(-0.7, -4.6, -15.3)); t3d.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, -0.6f)); t3d.setScale(4.5); tg.setTransform(t3d);
TransformGroup tg_2 = new TransformGroup(); Transform3D t3d_2 = new Transform3D();
t3d_2.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_2.setScale(1.5); tg_2.setTransform(t3d_2);
tg.addChild(createWheel()); tg_2.addChild(createObjLoad("model/support_clr.obj")); tg.addChild(tg_2);
objRoot.addChild(tg); objRoot.addChild(createLight(-0.3f, 0.2f, 0.0f)); objRoot.addChild(createLight(0.3f, -0.2f, 0.0f)); objRoot.addChild(createLight(-0.3f, 0.2f, 0.0f)); objRoot.addChild(createLight(0.3f, -0.2f, 0.0f)); objRoot.addChild(createLight(-0.3f, 0.2f, 0.0f)); objRoot.addChild(createLight(0.3f, -0.2f, 0.0f)); objRoot.compile();
return objRoot;
}
private BranchGroup createWheel() {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); // tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.setScale(2.0); tg.setTransform(t3d);
TransformGroup tg_rot = new TransformGroup(); tg_rot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg_rot.addChild(createGondolas()); tg_rot.addChild(createObjLoad("model/wheel_clr.obj"));
Transform3D axis = new Transform3D(); axis.setRotation(new AxisAngle4f(1.0f, 0.0f, 0.0f, 1.57f));
Alpha alpha = new Alpha(-1, 80000);
RotationInterpolator rotator = new RotationInterpolator(alpha, tg_rot, axis, 0.0f, (float) -Math.PI * 2.0f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); rotator.setSchedulingBounds(bounds);
tg.addChild(rotator); tg.addChild(tg_rot);
objRoot.addChild(tg); objRoot.compile();
return objRoot;
}
private BranchGroup createGondolas() {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); // tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); Transform3D t3d = new Transform3D(); t3d.setScale(0.6); tg.setTransform(t3d);
TransformGroup tg_3 = new TransformGroup(); Transform3D t3d_3 = new Transform3D(); t3d_3.setTranslation(new Vector3d(0.0, -0.05, 0.0)); t3d_3.setScale(0.026); tg_3.setTransform(t3d_3);
SharedGroup shared = new SharedGroup(); TransformGroup tg_link = null; Transform3D t3d_link = new Transform3D(); TransformGroup tg_2 = null;
double r = 1.0; for (int i = 14; i < 32; i++) { t3d_link.setTranslation( new Vector3d(r * Math.cos((Math.PI / 9) * i), r * Math.sin((Math.PI / 9) * i), 0.0f));
tg_link = new TransformGroup(t3d_link); tg_link.addChild(new Link(shared)); tg.addChild(tg_link);
TransformGroup tg_rot = new TransformGroup(); // tg_link.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg_2 = new TransformGroup();
tg_rot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tg_rot.addChild(tg_2); tg_2.addChild(createObjLoad("model/gondola_clr.obj"));
tg_link.addChild(tg_rot);
if (firstTime) { tg_2.addChild(tg_3); tg_3.addChild(createPonta_Pom()); firstTime = false; }
Transform3D axis = new Transform3D(); axis.setRotation(new AxisAngle4f(1.0f, 0.0f, 0.0f, 1.57f));
Alpha alpha = new Alpha(-1, 80000);
RotationInterpolator rotator2 = new RotationInterpolator(alpha, tg_rot, axis, 0.0f, (float) Math.PI * 2.0f);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); rotator2.setSchedulingBounds(bounds); tg_2.addChild(rotator2);
}
objRoot.addChild(tg);
objRoot.addChild(createLight(-0.3f, 0.2f, 0.0f)); objRoot.addChild(createLight(0.3f, 0.2f, 0.0f));
objRoot.compile();
return objRoot;
}
private BranchGroup createPonta_Pom() {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(0.8); tg.setTransform(t3d);
TransformGroup tg_pt = new TransformGroup(); Transform3D t3d_pt = new Transform3D(); // tg_pt.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
TransformGroup tg_p = new TransformGroup(); Transform3D t3d_p = new Transform3D(); // tg_p.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_pt.setTranslation(new Vector3d(-0.6, -2.0, -0.8)); t3d_pt.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, -0.6f)); tg_pt.setTransform(t3d_pt);
t3d_p.setTranslation(new Vector3d(0.6, -2.0, -0.8)); t3d_p.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, 0.6f)); tg_p.setTransform(t3d_p);
tg_pt.addChild(createObjLoad("model/Sitting_Pomeranian.obj")); tg_p.addChild(createObjLoad("model/Sitting_Pomeranian.obj"));
tg.addChild(tg_pt); tg.addChild(tg_p); objRoot.addChild(tg); objRoot.compile();
return objRoot;
}
private BranchGroup createObjLoad(String filename) {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(0.5); tg.setTransform(t3d);
ObjectFile loader = new ObjectFile(); Scene s = null;
File file = new java.io.File(filename);
try { s = loader.load(file.toURI().toURL()); } catch (Exception e) { System.err.println(e); System.exit(1); }
tg.addChild(s.getSceneGroup());
objRoot.addChild(tg);
objRoot.compile();
return objRoot;
}
private Light createLight(float x, float y, float z) { DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(x, y, z));
light.setInfluencingBounds(new BoundingSphere(new Point3d(), 10000.0));
return light; }
public static void main(String args[]) {
Mykeynavbeh window = new Mykeynavbeh();
window.setSize(800, 600);
window.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } });
window.setVisible(true); }
}