/* * @(#)MountainView.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; import ncsa.j3d.ui.record.RecordableCanvas3D; import kovey.book99.util.IC; import kovey.book99.geometry.basicElements.Vertex; import kovey.book99.geometry.basicElements.VertexList; import kovey.book99.geometry.curves.Curve; import kovey.book99.geometry.curves.CubicBSplineCurve; public class MountainView extends Applet implements ActionListener, Runnable { 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: MountainView [pathname|URL]"); System.exit(0); } else { locString = args[0]; } } new MainFrame(new MountainView(locString), 320, 200); } String location; RecordableCanvas3D 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; MountainView(String initLocation) { setLayout(new BorderLayout()); canvas = new RecordableCanvas3D( null ); canvas.captureInPostRender( false ); canvas.setFilePrefix( "f" ); add("Center",canvas); 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(); } void setupInitialTransformation() { // Set initial view transform initTrans = new Transform3D(); // translate to initial point Point3d initPt = new Point3d( 0.9, 0.03, -0.40 ); Vector3d toStartPt = new Vector3d( initPt ); initTrans.set( toStartPt ); // rotate to face the bottom of the mountains Transform3D xf = new Transform3D(); xf.setIdentity(); xf.rotY( (120.0/180.0) * Math.PI ); initTrans.mul( xf ); // view using the initial transformation vpTransGroup.setTransform( initTrans ); } public void actionPerformed(ActionEvent ae) { } 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(); // set up a viewpoint to include the bounds // setViewpoint(); } canvas.setCursor(handCursor); } class AnimatorFactory extends Behavior { Runnable parent; public AnimatorFactory( Runnable theParent ) { parent = theParent; } public void initialize() { wakeupOn( new WakeupOnElapsedFrames(0) ); } public void processStimulus( java.util.Enumeration criteria ) { if ( null == animator ) { // First scene is being shown. Get ready to animate. animator = new Thread( parent ); animator.start(); } } } 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); AnimatorFactory animationStarter = new AnimatorFactory( this ); animationStarter.setSchedulingBounds(behaviorBounds); sceneRoot.addChild( animationStarter ); 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); } public void run() { // Face the Mountain turnWithEase( -70.0 ); moveToValley(); } void turnWithEase( double turnAngle ) { try { // Turn the camera a step at a time using ease. Transform3D viewTrans = new Transform3D(); Transform3D rotateTrans = new Transform3D(); final double maxRotate = (turnAngle/180.0) * Math.PI; final double fraction = 500; double angleInc = (Math.PI/fraction)/maxRotate; if ( angleInc < 0.0 ) // turnAngle could be negative angleInc *= -1; // final double a = (10.0/180.0) * Math.PI; // accelaration // final double b = maxRotate - ((10.0/180.0) * Math.PI); // decelaration canvas.startRecording(); for ( double angle=0.0; angle <= 1.0; angle+=angleInc ) { double easedAngle = IC.ease( angle ); rotateTrans.setIdentity(); rotateTrans.rotY( easedAngle*maxRotate ); viewTrans.set( initTrans ); viewTrans.mul( rotateTrans ); vpTransGroup.setTransform( viewTrans ); // turn canvas.rendezvous(); // wait until frame is written to jpg file } initTrans.set( viewTrans ); } catch ( Exception e ) { } } VertexList getCurvePoints() { VertexList points = null; try { VertexList cp = new VertexList(); // List of control points cp.addTailReal( 0, 0, 0); cp.addTailReal( 0.05, -0.02, -0.20 ); cp.addTailReal( -0.08, 0.04, -0.25 ); cp.addTailReal( 0.03, -0.02, -0.51 ); CubicBSplineCurve curve = new CubicBSplineCurve( cp ); curve.setCurveType( Curve.PHANTOM ); curve.setIncrementU( 0.01 ); points = curve.getPoints(); } catch ( Throwable e ) { e.toString(); } return points; } void moveToValley() { try { // Move to the valley using spline. VertexList cp = getCurvePoints(); Transform3D viewTrans = new Transform3D(); Transform3D translationTrans = new Transform3D(); Vector3d translate = new Vector3d(); for ( int index=0; index < cp.getCount(); index++ ) { Vertex v = cp.getAt( index ); translate.set( v.getRealX(), v.getRealY(), v.getRealZ() ); translationTrans.setIdentity(); translationTrans.setTranslation( translate ); viewTrans.set( initTrans ); viewTrans.mul( translationTrans ); vpTransGroup.setTransform( viewTrans ); canvas.rendezvous(); // wait until frame is written to jpg file } initTrans.set( viewTrans ); } catch ( Exception e ) { } } }