/* * @(#)Walk.java 1.0 99/02/14 23:00:00 * * Copyright (c) 1999. All Rights Reserved. * * 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. WE 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. * * @Author: Dr. Richard Parent * @Author: Kovalan Muniandy */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; import com.sun.j3d.utils.geometry.ColorCube; import com.sun.j3d.utils.applet.MainFrame; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.behaviors.mouse.MouseRotate; import com.sun.j3d.utils.behaviors.mouse.MouseZoom; import com.sun.j3d.utils.behaviors.mouse.MouseTranslate; import javax.media.j3d.*; import javax.vecmath.*; import java.net.URL; import java.net.MalformedURLException; import com.sun.j3d.loaders.vrml97.VrmlLoader; import com.sun.j3d.loaders.Scene; public class Walk extends Applet implements ActionListener { public static void main(String[] args) { String locString = null; if (args.length == 0) { locString = "./tc2.wrl"; } else { if (args.length != 1) { System.out.println("Usage: Walk [pathname|URL]"); System.exit(0); } else { locString = args[0]; } } new MainFrame(new Walk(locString), 330, 220); } String location; Canvas3D canvas; SimpleUniverse universe; TransformGroup vpTransGroup; VrmlLoader loader; View view; Panel panel; Label label; BranchGroup sceneRoot; TransformGroup examineGroup; BranchGroup sceneGroup; BoundingSphere sceneBounds; DirectionalLight headLight; AmbientLight ambLight; TextField textField; Cursor waitCursor; Cursor handCursor; Thread animator = null; Transform3D initTrans; Walk(String initLocation) { setLayout(new BorderLayout()); canvas = new Canvas3D( null ); add("Center",canvas); Panel panel = new Panel( new FlowLayout( FlowLayout.LEFT ) ); add( "South", panel ); setupWalkPanel( panel ); waitCursor = new Cursor(Cursor.WAIT_CURSOR); handCursor = new Cursor(Cursor.HAND_CURSOR); universe = new SimpleUniverse(canvas); ViewingPlatform viewingPlatform = universe.getViewingPlatform(); vpTransGroup = viewingPlatform.getViewPlatformTransform(); Viewer viewer = universe.getViewer(); view = viewer.getView(); view.setFrontClipDistance( view.getFrontClipDistance() - 0.095 ); setupBehavior(); loader = new VrmlLoader(); gotoLocation(initLocation); setupInitialTransformation(); } Button xleft = new Button( "<< x" ); Button xright = new Button( "x >>" ); Button ydown = new Button( "\\/ y" ); Button yup = new Button( "y /\\" ); Button zin = new Button( "- z" ); Button zout = new Button( "z +" ); Checkbox rotate = new Checkbox( "turn" ); TextField deltaField = new TextField( "0.5" ); void setupWalkPanel( Panel panel ) { panel.add( xleft ); panel.add( xright ); panel.add( ydown ); panel.add( yup ); panel.add( zin ); panel.add( zout ); panel.add( rotate ); panel.add( deltaField ); xleft.addActionListener( this ); xright.addActionListener( this ); yup.addActionListener( this ); ydown.addActionListener( this ); zin.addActionListener( this ); zout.addActionListener( this ); } public void actionPerformed(ActionEvent ae) { try { Button button = (Button)ae.getSource(); if ( button instanceof Button ) { String deltaText = deltaField.getText(); double delta = Double.valueOf( deltaText ).doubleValue(); double x=0.0, y=0.0, z=0.0; if ( xleft == button ) x = -delta; else if ( xright == button ) x = delta; else if ( ydown == button ) y = -delta; else if ( yup == button ) y = delta; else if ( zin == button ) z = -delta; else if ( zout == button ) z = delta; if ( rotate.getState() ) rotate( x, y, z ); else translate( x, y, z ); } } catch ( Throwable tt ) { tt.toString(); } } Transform3D xf = new Transform3D(); void translate( double x, double y, double z ) { xf.setIdentity(); xf.setTranslation( new Vector3d( x, y, z ) ); initTrans.mul( xf ); // view using the initial transformation vpTransGroup.setTransform( initTrans ); } void rotate( double x, double y, double z ) { if ( 0.0 != x ) { xf.setIdentity(); xf.rotX( (x/180.0) * Math.PI ); initTrans.mul( xf ); } if ( 0.0 != y ) { xf.setIdentity(); xf.rotY( (y/180.0) * Math.PI ); initTrans.mul( xf ); } if ( 0.0 != z ) { xf.setIdentity(); xf.rotZ( (z/180.0) * Math.PI ); initTrans.mul( xf ); } // view using the initial transformation vpTransGroup.setTransform( initTrans ); } void setupInitialTransformation() { // Set initial view transform initTrans = new Transform3D(); Transform3D viewTrans = initTrans; Transform3D eyeTrans = new Transform3D(); // point the view at the center of the object Point3d center = new Point3d(); sceneBounds.getCenter(center); double radius = sceneBounds.getRadius(); Vector3d temp = new Vector3d(center); viewTrans.set(temp); // pull the eye back far enough to see the whole object double eyeDist = 2.5*radius / Math.tan(view.getFieldOfView() / 2.0); view.setBackClipDistance( eyeDist ); temp.x = 0.0; temp.y = 0.0; temp.z = eyeDist; eyeTrans.set(temp); viewTrans.mul(eyeTrans); // set the view transform vpTransGroup.setTransform(viewTrans); } void gotoLocation(String location) { canvas.setCursor(waitCursor); if (sceneGroup != null) { sceneGroup.detach(); } Scene scene = null; try { URL loadUrl = new URL(location); try { // load the scene scene = loader.load( loadUrl ); } catch (Exception e) { System.out.println("Exception loading URL:" + e); } } catch (MalformedURLException badUrl) { // location may be a path name try { // load the scene scene = loader.load(location); } catch (Exception e) { System.out.println("Exception loading file from path:" + e); } } if (scene != null) { // get the scene group sceneGroup = scene.getSceneGroup(); sceneGroup.setCapability(BranchGroup.ALLOW_DETACH); sceneGroup.setCapability(BranchGroup.ALLOW_BOUNDS_READ); // add the scene group to the scene examineGroup.addChild(sceneGroup); // now that the scene group is "live" we can inquire the bounds sceneBounds = (BoundingSphere)sceneGroup.getBounds(); } canvas.setCursor(handCursor); } private void setupBehavior() { sceneRoot = new BranchGroup(); examineGroup = new TransformGroup(); examineGroup.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); examineGroup.setCapability(TransformGroup.ALLOW_CHILDREN_READ); examineGroup.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); examineGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); examineGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); sceneRoot.addChild(examineGroup); BoundingSphere behaviorBounds = new BoundingSphere(new Point3d(), Double.MAX_VALUE); MouseRotate mr = new MouseRotate(); mr.setTransformGroup(examineGroup); mr.setSchedulingBounds(behaviorBounds); sceneRoot.addChild(mr); MouseTranslate mt = new MouseTranslate(); mt.setTransformGroup(examineGroup); mt.setSchedulingBounds(behaviorBounds); sceneRoot.addChild(mt); MouseZoom mz = new MouseZoom(); mz.setTransformGroup(examineGroup); mz.setSchedulingBounds(behaviorBounds); sceneRoot.addChild(mz); BoundingSphere lightBounds = new BoundingSphere(new Point3d(), Double.MAX_VALUE); ambLight = new AmbientLight(true, new Color3f(1.0f, 1.0f, 1.0f)); ambLight.setInfluencingBounds(lightBounds); ambLight.setCapability(Light.ALLOW_STATE_WRITE); sceneRoot.addChild(ambLight); headLight = new DirectionalLight(); headLight.setCapability(Light.ALLOW_STATE_WRITE); headLight.setInfluencingBounds(lightBounds); sceneRoot.addChild(headLight); universe.addBranchGraph(sceneRoot); } }