/* 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.applet.*;import java.awt.*;
import javax.media.j3d.*;import javax.vecmath.*;
import com.sun.j3d.utils.applet.MainFrame;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 java.io.*;import com.sun.j3d.loaders.objectfile.ObjectFile;
import com.sun.j3d.utils.geometry.Sphere;import com.sun.j3d.utils.geometry.Box;
public class Mykeynavbeh extends Applet implements KeyListener {
private SimpleUniverse universe = null; private Canvas3D canvas = null; private TransformGroup viewtrans = null; private Transform3D t3d_vt = new Transform3D();
private TransformGroup tg = null; // private Transform3D t3d = null; private Transform3D t3dstep = new Transform3D(); private Matrix4d matrix = new Matrix4d();
private MovingView view = null;
private boolean cstate = false;
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(500.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(); universe.getViewer().getView().setDepthBufferFreezeTransparent(false);
KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(viewtrans); keyNavBeh.setSchedulingBounds(bounds); // PlatformGeometry platformGeom = new PlatformGeometry(); // platformGeom.addChild(keyNavBeh); // universe.getViewingPlatform().setPlatformGeometry(platformGeom);
BranchGroup bg = new BranchGroup(); bg.addChild(keyNavBeh); universe.addBranchGraph(bg);
objRoot.addChild(createView()); objRoot.addChild(createBackground()); objRoot.addChild(createSaturn());
/* * Background background = new Background(); background.setColor(1.0f, * 1.0f, 1.0f); background.setApplicationBounds(bounds); * objRoot.addChild(background); */
return objRoot; }
private BranchGroup createView() {
BranchGroup objRoot = new BranchGroup(); tg = new TransformGroup();
view = new MovingView(); tg.addChild(view);
objRoot.addChild(tg); objRoot.addChild(createLight(-0.3f, 0.2f, 0.0f));
objRoot.compile();
return objRoot;
}
private BranchGroup createSaturn() {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, 0.0, -50.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0);
tg.setTransform(t3d);
Texture2D texture2d = (Texture2D) new TextureLoader("model/saturn.jpg", this).getTexture(); Appearance ap = new Appearance(); ap.setTexture(texture2d); tg.addChild(new Sphere(8.1f, Sphere.GENERATE_TEXTURE_COORDS, 62, ap));
tg.addChild(createTitan()); tg.addChild(createRocks(1.0)); tg.addChild(createRocks(-1.0));
objRoot.addChild(tg);
objRoot.compile();
return objRoot;
}
private BranchGroup createTitan() {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D();
t3d.setTranslation(new Vector3d(0.0, 0.0, 170.1)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0);
tg.setTransform(t3d);
Texture2D texture2d = (Texture2D) new TextureLoader("model/Titan.jpg", this).getTexture(); Appearance ap = new Appearance(); ap.setTexture(texture2d); tg.addChild(new Sphere(0.358f, Sphere.GENERATE_TEXTURE_COORDS, 62, ap));
objRoot.addChild(tg);
objRoot.compile();
return objRoot;
}
private BranchGroup createRocks(double sign) {
BranchGroup objRoot = new BranchGroup();
TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); TransformGroup tg_rocks = new TransformGroup();
t3d.setTranslation(new Vector3d(-0.9, 0.0, 0.0)); t3d.setRotation(new AxisAngle4f(0.0f, 0.0f, 0.0f, 0.0f)); t3d.setScale(1.0);
tg.setTransform(t3d);
tg_rocks.addChild(createObjLoad("model/rocks.obj"));
SharedGroup shared = new SharedGroup(); shared.addChild(tg_rocks);
TransformGroup tg_link = null; Transform3D t3d_link = new Transform3D();
double r = 20.0; for (float x = -20.0f; x <= 20.0f; x += 0.1f) {
t3d_link.setScale(0.25); t3d_link.setTranslation(new Vector3d(x * (0.6 + Math.random() * 0.4), (Math.random()) * 1.1, sign * ((Math.sqrt(r * r - x * x)) * (0.6 + Math.random() * 0.4))));
tg_link = new TransformGroup(t3d_link);
tg_link.addChild(new Link(shared)); tg.addChild(tg_link);
}
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 createBackground() {
BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 10000.0); BranchGroup objRoot = new BranchGroup();
Texture2D texture2d = (Texture2D) new TextureLoader("model/star-2633893_1920.jpg", this).getTexture(); Appearance ap = new Appearance(); ap.setTexture(texture2d);
PolygonAttributes pattr = new PolygonAttributes(); pattr.setCullFace(PolygonAttributes.CULL_NONE); pattr.setBackFaceNormalFlip(true); ap.setPolygonAttributes(pattr);
int flag = Box.GENERATE_NORMALS_INWARD | Box.GENERATE_TEXTURE_COORDS; Box box = new Box(0.8f, 0.8f, 0.8f, flag, ap);
BranchGroup bg = new BranchGroup();
bg.addChild(box);
Background background = new Background(bg); background.setApplicationBounds(bounds);
objRoot.addChild(background);
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; }
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() { 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 applet = new Mykeynavbeh(); Frame frame = new MainFrame(applet, 800, 600); }
public void keyTyped(KeyEvent e) { char key = e.getKeyChar();
if (key == 'd') { cstate = true; view.setEnable(cstate); }
if (key == 's') {
t3dstep.rotY(Math.PI / 32); viewtrans.getTransform(t3d_vt); t3d_vt.get(matrix); t3d_vt.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_vt.mul(t3dstep); t3d_vt.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); viewtrans.setTransform(t3d_vt);
}
if (key == 'f') {
t3dstep.rotY(-Math.PI / 32); viewtrans.getTransform(t3d_vt); t3d_vt.get(matrix); t3d_vt.setTranslation(new Vector3d(0.0, 0.0, 0.0)); t3d_vt.mul(t3dstep); t3d_vt.setTranslation(new Vector3d(matrix.m03, matrix.m13, matrix.m23)); viewtrans.setTransform(t3d_vt);
}
if (key == 'x') { cstate = false; view.setEnable(cstate); }
}
public void keyReleased(KeyEvent e) { }
public void keyPressed(KeyEvent e) { }
class MovingView extends Behavior {
private Transform3D t3dstep = new Transform3D(); private WakeupOnElapsedFrames wakeFrame = null;
public MovingView() {
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, -0.1f)); viewtrans.getTransform(t3d_vt); t3d_vt.mul(t3dstep); viewtrans.setTransform(t3d_vt);
wakeupOn(wakeFrame); } }
}