/* 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.KeyListener;import java.awt.event.KeyEvent;
import com.sun.j3d.utils.image.TextureLoader;import java.awt.event.*;import java.util.*;
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 TransformGroup tg_2 = null; private Transform3D t3d_2 = null;
private int count = 0;
private Switch selector = new Switch(Switch.CHILD_MASK); private BitSet flag_i = new BitSet(64);
private double dancerHeight = 0.0;
private KeyNavigatorBehavior keyNavBeh;
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();
keyNavBeh = new KeyNavigatorBehavior(viewtrans); keyNavBeh.setSchedulingBounds(bounds); PlatformGeometry platformGeom = new PlatformGeometry(); platformGeom.addChild(keyNavBeh); universe.getViewingPlatform().setPlatformGeometry(platformGeom);
Background background = new Background(); background.setColor(0.94f, 0.69f, 0.17f); background.setApplicationBounds(bounds); objRoot.addChild(background);
objRoot.addChild(createDancer());
return objRoot; }
private BranchGroup createDancer() {
BranchGroup objRoot = new BranchGroup(); tg = new TransformGroup(); t3d = new Transform3D();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3d(0.0, -4.0, -10.0)); // t3d.setRotation(new AxisAngle4f(0.0f, 1.0f, 0.0f, -1.2f)); t3d.setScale(1.43);
tg.setTransform(t3d);
ObjectFile loader = new ObjectFile(); Scene s = null;
File file = new java.io.File("model/Morrigan+Aensland.obj");
try { s = loader.load(file.toURI().toURL()); } catch (Exception e) { System.err.println(e); System.exit(1); }
tg.addChild(s.getSceneGroup());
tg_2 = new TransformGroup(); t3d_2 = new Transform3D();
tg_2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d_2.setTranslation(new Vector3d(0.0, 1.2, 0.0)); t3d_2.setScale(1.0); tg_2.setTransform(t3d_2);
tg_2.addChild(createDancerShadow());
tg.addChild(tg_2);
objRoot.addChild(tg); objRoot.addChild(createLight());
objRoot.compile();
return objRoot;
}
private BranchGroup createDancerShadow() {
BranchGroup objRoot = new BranchGroup();
selector.setCapability(Switch.ALLOW_SWITCH_WRITE);
for (int i = 0; i < 64; i++) { selector.addChild(createOrientedShape3D(4.0f, "model/dancer_shadow" + i + ".png")); }
flag_i.clear(); flag_i.set(count % 64); selector.setChildMask(flag_i);
System.out.println("count_initially: " + count % 64);
objRoot.addChild(selector);
objRoot.compile();
return objRoot;
}
public BranchGroup createOrientedShape3D(float size, String filename) {
BranchGroup root = new BranchGroup(); TransformGroup tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
Appearance ap = new Appearance();
TransparencyAttributes attr = new TransparencyAttributes(TransparencyAttributes.BLENDED, 1.0f); ap.setTransparencyAttributes(attr);
TextureLoader loader = new TextureLoader(filename, this); ap.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);
OrientedShape3D oriShape = new OrientedShape3D(plane, ap, 0, new Point3f(0.0f, 0.0f, 0.0f));
Transform3D t3d = new Transform3D(); t3d.setScale(1.0);
tg.setTransform(t3d);
tg.addChild(oriShape);
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') { t3dstep.set(new Vector3d(0.0, 0.0, 0.1)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d); }
if ((key == 's') && (dancerHeight < 0.1)) { System.out.println("dancerHeight_s: " + dancerHeight);
count++; flag_i.clear(); flag_i.set(count % 64); selector.setChildMask(flag_i);
System.out.println("count % 64_'s': " + count % 64);
t3dstep.rotY(Math.PI / 32); tg.getTransform(t3d); t3d.get(matrix); t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.mul(t3dstep); t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg.setTransform(t3d);
t3dstep.rotY(-Math.PI / 32); tg_2.getTransform(t3d_2); t3d_2.get(matrix); t3d_2.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_2.mul(t3dstep); t3d_2.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg_2.setTransform(t3d_2);
}
if ((key == 'f') && (dancerHeight < 0.1)) { System.out.println("dancerHeight_f: " + dancerHeight);
count--; if (count < 0) { count = count + 64; }
flag_i.clear(); flag_i.set(count % 64); selector.setChildMask(flag_i);
System.out.println("count % 64_'f': " + count % 64);
t3dstep.rotY(-Math.PI / 32); tg.getTransform(t3d); t3d.get(matrix); t3d.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d.mul(t3dstep); t3d.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg.setTransform(t3d);
t3dstep.rotY(Math.PI / 32); tg_2.getTransform(t3d_2); t3d_2.get(matrix); t3d_2.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_2.mul(t3dstep); t3d_2.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); tg_2.setTransform(t3d_2);
}
if (key == 'e') { t3dstep.set(new Vector3d(0.0, 0.1, 0.0)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
t3dstep.set(new Vector3d(0.1, -0.1, 0.0)); tg_2.getTransform(t3d_2); t3d_2.mul(t3dstep); tg_2.setTransform(t3d_2);
dancerHeight += 0.1; System.out.println("dancerHeight_e: " + dancerHeight);
}
if ((key == 'c') && (dancerHeight >= 0.1)) { t3dstep.set(new Vector3d(0.0, -0.1, 0.0)); tg.getTransform(t3d); t3d.mul(t3dstep); tg.setTransform(t3d);
t3dstep.set(new Vector3d(-0.1, 0.1, 0.0)); tg_2.getTransform(t3d_2); t3d_2.mul(t3dstep); tg_2.setTransform(t3d_2);
dancerHeight -= 0.1; System.out.println("dancerHeight_c: " + dancerHeight); } }
public void keyReleased(KeyEvent e) { keyNavBeh.setEnable(true); }
public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_RIGHT: keyNavBeh.setEnable(false); break;
case KeyEvent.VK_LEFT: keyNavBeh.setEnable(false); break;
case KeyEvent.VK_KP_RIGHT: keyNavBeh.setEnable(false); break;
case KeyEvent.VK_KP_LEFT: keyNavBeh.setEnable(false); break;
}
}}