/* 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 java.awt.event.KeyListener;import java.awt.event.KeyEvent;
import com.sun.j3d.utils.image.TextureLoader;
import java.util.*;import javax.media.j3d.WakeupOnCollisionEntry;import javax.media.j3d.WakeupOnCollisionExit;
import com.sun.j3d.loaders.objectfile.ObjectFile;import java.io.*;
import java.awt.event.*;
public class Mykeynavbeh extends Frame implements KeyListener {
private SimpleUniverse universe = null; private Canvas3D canvas = null; private TransformGroup viewtrans = null;
// private TransformGroup tg = null; // private Transform3D t3d = null; private Transform3D t3dstep = new Transform3D(); private Matrix4d matrix = new Matrix4d();
private MovingCar car = null;
private float speed = -0.03f; private boolean cstate = true; private double carHeight = 0.0; private double speed_y = 0.15;
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);
canvas.addKeyListener(this);
universe.addBranchGraph(scene); }
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(createCar()); objRoot.addChild(createTrees0());
Background background = new Background(); background.setColor(1.0f, 1.0f, 0.98f); background.setApplicationBounds(bounds); objRoot.addChild(background);
return objRoot; }
private BranchGroup createCar() {
BranchGroup objRoot = new BranchGroup(); // tg = new TransformGroup();
// tg_car = new TransformGroup(); // t3d_car = new Transform3D();
TransformGroup tg = new TransformGroup();
TransformGroup tg_car = new TransformGroup(); Transform3D t3d_car = new Transform3D();
// tg_car.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_car.setTranslation(new Vector3d(0.0, -1.4, -8.0)); t3d_car.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, 0.35f)); t3d_car.setScale(2.0);
tg_car.setTransform(t3d_car);
car = new MovingCar("model/model_Camaro.obj"); tg_car.addChild(car.tg); // car.tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg_car.addChild(car);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0);
CollisionDetectorGroup cdGroup = new CollisionDetectorGroup(car.tg); cdGroup.setSchedulingBounds(bounds);
tg.addChild(tg_car); tg.addChild(cdGroup);
// objRoot.addChild(tg_car); objRoot.addChild(tg); objRoot.addChild(createLight());
objRoot.compile();
return objRoot;
}
private BranchGroup createTrees0() {
BranchGroup objRoot = new BranchGroup(); TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3d(-0.3f, -0.0, 10.0f)); t3d.setRotation(new AxisAngle4d(0, 1, 0, Math.toRadians(4))); t3d.setScale(2.0);
tg.setTransform(t3d);
TransformGroup tg_2 = new TransformGroup(); Transform3D t3d_2 = new Transform3D();
t3d_2.setTranslation(new Vector3d(0.0f, -0.57f, 0.25f)); tg_2.setTransform(t3d_2);
tg.addChild(createTrees(false, 0.5f, 0.5f, 0.0f, "model/tree22a.png")); tg.addChild(createTrees(true, 0.12f, 0.12f, 0.0f, "model/transparent.png")); tg_2.addChild(createTrees(false, 0.5f, 0.0f, 0.5f, "model/shadow_tree.png"));
tg.addChild(tg_2);
objRoot.addChild(tg);
objRoot.compile();
return objRoot;
}
public BranchGroup createTrees(boolean coll, float x_pos, float y_pos, float z_pos, String filename) {
BranchGroup root = new BranchGroup(); TransformGroup tg = new TransformGroup();
Appearance app = new Appearance();
TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparencyMode(ta.BLENDED); app.setTransparencyAttributes(ta);
PolygonAttributes pattr = new PolygonAttributes(); pattr.setCullFace(PolygonAttributes.CULL_NONE); app.setPolygonAttributes(pattr);
TextureLoader loader = new TextureLoader(filename, this); app.setTexture(loader.getTexture());
QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
Point3f[] p = new Point3f[4];
p[0] = new Point3f(x_pos, y_pos, z_pos); p[1] = new Point3f(-x_pos, y_pos, z_pos); p[2] = new Point3f(-x_pos, -y_pos, -z_pos); p[3] = new Point3f(x_pos, -y_pos, -z_pos); plane.setCoordinates(0, p);
TexCoord2f[] q = new TexCoord2f[4]; q[0] = new TexCoord2f(1.0f, 1.0f); q[1] = new TexCoord2f(0.0f, 1.0f); q[2] = new TexCoord2f(0.0f, 0.0f); q[3] = new TexCoord2f(1.0f, 0.0f); plane.setTextureCoordinates(0, 0, q);
OrientedShape3D oriShape = new OrientedShape3D(plane, app, 0, new Point3f(0.0f, 0.0f, 0.0f));
oriShape.setCollidable(coll);
TransformGroup tg_ori = new TransformGroup(); tg_ori.addChild(oriShape);
SharedGroup shared = new SharedGroup(); shared.addChild(tg_ori);
TransformGroup tg_link = null; Transform3D t3d_link = new Transform3D();
for (float x = -3.0f; x <= 3.0f; x += 6.0f) { for (float z = -360.0f; z <= 30.0f; z += 5.0f) {
t3d_link.setScale(1.2);
t3d_link.setTranslation(new Vector3d(x, -0.15, z)); tg_link = new TransformGroup(t3d_link);
tg_link.addChild(new Link(shared)); tg.addChild(tg_link); } }
root.addChild(tg);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
Color3f light1Color = new Color3f(0.6f, 0.3f, 0.3f); Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); root.addChild(light1);
root.compile();
return root; }
private BranchGroup createObjLoad(String filename) {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.setScale(1.0); 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;
}
public BranchGroup createShape3D(float size) {
BranchGroup root = new BranchGroup(); TransformGroup tg = new TransformGroup();
Appearance app = new Appearance();
TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparencyMode(ta.BLENDED); app.setTransparencyAttributes(ta);
PolygonAttributes pattr = new PolygonAttributes(); pattr.setCullFace(PolygonAttributes.CULL_NONE); app.setPolygonAttributes(pattr);
TextureLoader loader = new TextureLoader("model/shadow_car.png", this); app.setTexture(loader.getTexture());
QuadArray plane = new QuadArray(4, GeometryArray.COORDINATES | GeometryArray.TEXTURE_COORDINATE_2);
Point3f[] p = new Point3f[4]; p[0] = new Point3f(size, 0.0f, size); p[1] = new Point3f(-size, 0.0f, size); p[2] = new Point3f(-size, 0.0f, -size); p[3] = new Point3f(size, 0.0f, -size); plane.setCoordinates(0, p);
TexCoord2f[] q = new TexCoord2f[4]; q[0] = new TexCoord2f(1.0f, 1.0f); q[1] = new TexCoord2f(0.0f, 1.0f); q[2] = new TexCoord2f(0.0f, 0.0f); q[3] = new TexCoord2f(1.0f, 0.0f); plane.setTextureCoordinates(0, 0, q);
Shape3D quad3D = new Shape3D(plane, app);
tg.addChild(quad3D);
root.addChild(tg);
root.compile();
return root; }
private Light createLight() { DirectionalLight light = new DirectionalLight(true, new Color3f(1.0f, 1.0f, 1.0f), new Vector3f(-0.3f, 0.2f, -1.0f));
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); }
public void keyTyped(KeyEvent e) { char key = e.getKeyChar();
if (key == 'd') { speed = -0.03f;
cstate = true; car.setEnable(cstate); }
if (key == 's') {
t3dstep.rotY(Math.PI / 32); car.tg.getTransform(car.t3d); car.t3d.get(matrix); car.t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); car.t3d.mul(t3dstep); car.t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); car.tg.setTransform(car.t3d);
}
if (key == 'f') {
t3dstep.rotY(-Math.PI / 32); car.tg.getTransform(car.t3d); car.t3d.get(matrix); car.t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); car.t3d.mul(t3dstep); car.t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); car.tg.setTransform(car.t3d);
}
if (((key == 'e') && (speed_y >= 0.0)) && (cstate)) { speed_y -= 0.008; t3dstep.set(new Vector3d(0.0, speed_y, 0.0)); car.tg.getTransform(car.t3d); car.t3d.mul(t3dstep); car.tg.setTransform(car.t3d);
t3dstep.set(new Vector3d(0.0, -speed_y, 0.0)); car.tg_2.getTransform(car.t3d_2); car.t3d_2.mul(t3dstep); car.tg_2.setTransform(car.t3d_2);
carHeight += speed_y; }
if ((key == 'c') && (carHeight >= 0.0)) { speed_y += 0.002; t3dstep.set(new Vector3d(0.0, -speed_y, 0.0)); car.tg.getTransform(car.t3d); car.t3d.mul(t3dstep); car.tg.setTransform(car.t3d);
t3dstep.set(new Vector3d(0.0, speed_y, 0.0)); car.tg_2.getTransform(car.t3d_2); car.t3d_2.mul(t3dstep); car.tg_2.setTransform(car.t3d_2);
carHeight -= speed_y;
if (carHeight < 0) { t3dstep.set(new Vector3d(0.0, -carHeight, 0.0)); car.tg.getTransform(car.t3d); car.t3d.mul(t3dstep); car.tg.setTransform(car.t3d);
t3dstep.set(new Vector3d(0.0, carHeight, 0.0)); car.tg_2.getTransform(car.t3d_2); car.t3d_2.mul(t3dstep); car.tg_2.setTransform(car.t3d_2);
carHeight = 0.0; speed_y = 0.15; } }
if (key == 'x') { cstate = false; car.setEnable(cstate); }
}
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
class CollisionDetectorGroup extends Behavior { private boolean inCollision; private Group group;
private WakeupOnCollisionEntry wEnter; private WakeupOnCollisionExit wExit;
private Vector3d trans = new Vector3d();
public CollisionDetectorGroup(Group gp) { // Corrected: gp group = gp; // Corrected: gp inCollision = false;
}
public void initialize() { wEnter = new WakeupOnCollisionEntry(group); wExit = new WakeupOnCollisionExit(group); wakeupOn(wEnter); }
public void processStimulus(Enumeration criteria) {
inCollision = !inCollision; if (inCollision) {
car.tg.getTransform(car.t3d); car.t3d.get(trans);
t3dstep.set(new Vector3d(0.0, 0.0, -speed * 5)); // Revised car.tg.getTransform(car.t3d); car.t3d.mul(t3dstep); // Revised car.tg.setTransform(car.t3d);
if ((trans.y) > 0.0) {
t3dstep.set(new Vector3d(0.0, -(trans.y), 0.0)); car.tg.getTransform(car.t3d); car.t3d.mul(t3dstep); car.tg.setTransform(car.t3d);
t3dstep.set(new Vector3d(0.0, trans.y, 0.0)); car.tg_2.getTransform(car.t3d_2); car.t3d_2.mul(t3dstep); car.tg_2.setTransform(car.t3d_2);
carHeight = 0.0; speed_y = 0.15;
}
cstate = false; car.setEnable(cstate);
wakeupOn(wExit); } else { wakeupOn(wEnter); } } }
class MovingCar extends Behavior {
public TransformGroup tg = null; public Transform3D t3d = null; // private Transform3D t3dstep = new Transform3D(); Revised
private TransformGroup tg_2 = null; private Transform3D t3d_2 = null;
private WakeupOnElapsedFrames wakeFrame = null;
public MovingCar(String filename) {
// Revised: tg = new TransformGroup(); t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.setScale(1.0); tg.setTransform(t3d); //
tg.addChild(createObjLoad(filename));
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
tg_2 = new TransformGroup(); t3d_2 = new Transform3D();
t3d_2.setTranslation(new Vector3d(0.0, 0.0, 0.0)); tg_2.setTransform(t3d_2);
tg_2.addChild(createShape3D(0.4f)); tg.addChild(tg_2); tg_2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 1000.0); this.setSchedulingBounds(bounds); }
public void initialize() { wakeFrame = new WakeupOnElapsedFrames(0); wakeupOn(wakeFrame); }
public void processStimulus(Enumeration criteria) { t3dstep.set(new Vector3d(0.0, 0.0, speed)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
wakeupOn(wakeFrame);
} }
}