?? wraploaderinfo3d.java
字號(hào):
// WrapLoaderInfo3D.java// Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th/* A checkboard floor, with a red center square, and labelled XZ axes. Load the model stored in filename fn using a NCSA Portfolio loader. The model is rotated and scaled so it is easy to see. Carry out the following operations on the loaded model: * display its named objects * traverse its scene graph, saving info to the EXAMINE_FN file * adjust the model's component shapes in various ways - there are 4 different variations; see adjustShape3D()*/import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.io.*;import java.util.*;import java.text.DecimalFormat;import com.sun.j3d.utils.universe.*;import com.sun.j3d.utils.geometry.*;import com.sun.j3d.utils.image.*;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.utils.behaviors.vp.*;import ncsa.j3d.loaders.*; // Portfolio loadersimport com.sun.j3d.loaders.Scene;// import com.tornadolabs.j3dtree.*; // for displaying the scene graphpublic class WrapLoaderInfo3D extends JPanel// Holds the 3D canvas where the loaded image is displayed{ private static final int PWIDTH = 512; // size of panel private static final int PHEIGHT = 512; private static final int BOUNDSIZE = 100; // larger than world private static final Point3d USERPOSN = new Point3d(0,5,20); // initial user position private static final Color3f white = new Color3f(1.0f, 1.0f, 1.0f); private static final Color3f black = new Color3f(0.0f, 0.0f, 0.0f); private static final Color3f blue = new Color3f(0.6f,0.6f, 1.0f); private static final String EXAMINE_FN = "examObj.txt"; private static final String TEXTURE_FN = "models/stone.jpg"; private SimpleUniverse su; private BranchGroup sceneBG; private BoundingSphere bounds; // for environment nodes private FileWriter ofw; // for writing out model info private DecimalFormat df; // for simpler output // references to the loading model private Scene loadedScene = null; private BranchGroup loadedBG = null; private int adaptNo; // used to choose the shape adaption method in adjustShape3D() private Texture2D texture = null; // used when changing a shape's texture // private Java3dTree j3dTree; // frame to hold tree display public WrapLoaderInfo3D(String fn, int adaptNo) // construct the 3D canvas { this.adaptNo = adaptNo; setLayout( new BorderLayout() ); setOpaque( false ); setPreferredSize( new Dimension(PWIDTH, PHEIGHT)); GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas3D = new Canvas3D(config); add("Center", canvas3D); canvas3D.setFocusable(true); // give focus to the canvas canvas3D.requestFocus(); su = new SimpleUniverse(canvas3D); // j3dTree = new Java3dTree(); // create a display tree for the SG createSceneGraph(fn); initUserPosition(); // set user's viewpoint orbitControls(canvas3D); // controls for moving the viewpoint su.addBranchGraph( sceneBG ); // j3dTree.updateNodes( su ); // build the tree display window } // end of WrapLoaderInfo3D() private void createSceneGraph(String fn) // initilise the scene { sceneBG = new BranchGroup(); bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE); df = new DecimalFormat("0.###"); // 3 dp lightScene(); // add the lights addBackground(); // add the sky sceneBG.addChild( new CheckerFloor().getBG() ); // add the floor loadModel(fn); // load the model stored in fn // loadedScene and loadedBG should now have values // examine the model if (loadedScene != null) { showNamedObject(loadedScene); // display its named objects storeGraphInfo(loadedBG); // traverse model's graph; save info to file // adjust the model's component shapes in various ways adjustShapes(loadedBG); } // j3dTree.recursiveApplyCapability( sceneBG ); // set capabilities for tree display sceneBG.compile(); // fix the scene } // end of createSceneGraph() private void lightScene() /* One ambient light, 2 directional lights */ { // Set up the ambient light AmbientLight ambientLightNode = new AmbientLight(white); ambientLightNode.setInfluencingBounds(bounds); sceneBG.addChild(ambientLightNode); // Set up the directional lights Vector3f light1Direction = new Vector3f(-1.0f, -1.0f, -1.0f); // left, down, backwards Vector3f light2Direction = new Vector3f(1.0f, -1.0f, 1.0f); // right, down, forwards DirectionalLight light1 = new DirectionalLight(white, light1Direction); light1.setInfluencingBounds(bounds); sceneBG.addChild(light1); DirectionalLight light2 = new DirectionalLight(white, light2Direction); light2.setInfluencingBounds(bounds); sceneBG.addChild(light2); } // end of lightScene() private void addBackground() // A blue sky { Background back = new Background(); back.setApplicationBounds( bounds ); back.setColor(0.17f, 0.65f, 0.92f); // sky colour sceneBG.addChild( back ); } // end of addBackground() private void orbitControls(Canvas3D c) /* OrbitBehaviour allows the user to rotate around the scene, and to zoom in and out. */ { OrbitBehavior orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL); orbit.setSchedulingBounds(bounds); ViewingPlatform vp = su.getViewingPlatform(); vp.setViewPlatformBehavior(orbit); } // end of orbitControls() private void initUserPosition() // Set the user's initial viewpoint using lookAt() { ViewingPlatform vp = su.getViewingPlatform(); TransformGroup steerTG = vp.getViewPlatformTransform(); Transform3D t3d = new Transform3D(); steerTG.getTransform(t3d); // args are: viewer posn, where looking, up direction t3d.lookAt( USERPOSN, new Point3d(0,0,0), new Vector3d(0,1,0)); t3d.invert(); steerTG.setTransform(t3d); } // end of initUserPosition()// -------------------------- model loading -------------- private void loadModel(String fn) /* Load the model from fn into the scene graph using a NCSA Portfolio loader. Rotate and scale it to make it easier to see. Store the loaded model's scene in the global loadedScene, and its branch group in loadedBG. */ { FileWriter ofw = null; System.out.println( "Loading: " + fn ); try { ModelLoader loader = new ModelLoader(); // the NCSA portfolio loader // System.out.println("Loader flags: " + loader.getFlags()); loadedScene = loader.load(fn); // the loaded scene // Rotate and scale the model if(loadedScene != null ) { loadedBG = loadedScene.getSceneGroup(); // the model's BG Transform3D t3d = new Transform3D(); t3d.rotX( -Math.PI/2.0 ); // models are often on their face; fix that Vector3d scaleVec = calcScaleFactor(loadedBG, fn); // scale the model t3d.setScale( scaleVec ); TransformGroup tg = new TransformGroup(t3d); tg.addChild(loadedBG); sceneBG.addChild(tg); // add (tg->loadedBG) to scene } else System.out.println("Load error with: " + fn); } catch( IOException ioe ) { System.err.println("Could not find object file: " + fn); } } // end of loadModel() private Vector3d calcScaleFactor(BranchGroup loadedBG, String fn) // Scale the model based on its original bounding box size { BoundingBox boundbox = new BoundingBox( loadedBG.getBounds() ); // System.out.println(boundbox); // obtain the upper and lower coordinates of the box Point3d lower = new Point3d(); boundbox.getLower( lower ); Point3d upper = new Point3d(); boundbox.getUpper( upper ); // store the largest X, Y, or Z dimension and calculate a scale factor double max = 0.0; if( (upper.x - lower.x ) > max ) max = (upper.x - lower.x ); if( (upper.y - lower.y ) > max ) max = (upper.y - lower.y ); if( (upper.z - lower.z ) > max ) max = (upper.z - lower.z ); double scaleFactor = 10.0/max; // 10 is half the width of the floor System.out.println("max dimension: " + df.format(max) + "; scaleFactor: " + df.format(scaleFactor) ); // limit the scaling so that a big model isn't scaled too much if( scaleFactor < 0.0005 ) scaleFactor = 0.0005; return new Vector3d(scaleFactor, scaleFactor, scaleFactor); } // end of calcScaleFactor() // ---------- model examination -------------------------------- private void showNamedObject(Scene loadedScene) /* Display the named objects, if any. The naming scheme is file-type dependent, but includes VRML DEF names and Lightwave 3D object filenames. */ { String name; Hashtable namedObjects = loadedScene.getNamedObjects(); Enumeration e = namedObjects.keys(); if (namedObjects.isEmpty()) System.out.println("No Named Objects"); else { System.out.println("Named Objects"); while(e.hasMoreElements()) { name = (String) e.nextElement(); System.out.println(name); } } } // end of showNamedObject() private void storeGraphInfo(BranchGroup bg) /* Traverse the model's scene graph and store information in EXAMINE_FN via the ofw FileWriter. */ { System.out.println("Writing model details to " + EXAMINE_FN); try { ofw = new FileWriter( EXAMINE_FN );
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -