📄 wraptrees3d.java
字号:
// WrapTrees3D.java// Andrew Davison, November 2003, dandrew@ratree.psu.ac.th/* The standard checkboard scene, but several trees now sprout and grow, spread over several seconds.*/import javax.swing.*;import java.awt.*;import com.sun.j3d.utils.universe.*;import com.sun.j3d.utils.geometry.*;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.utils.behaviors.vp.*;import com.sun.j3d.utils.image.*;public class WrapTrees3D 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,7,30); // initial user position (further back than usual) // axis constants private final static int X_AXIS = 0; private final static int Y_AXIS = 1; private final static int Z_AXIS = 2; private SimpleUniverse su; private BranchGroup sceneBG; private BoundingSphere bounds; // for environment nodes public WrapTrees3D() // A panel holding a 3D canvas: the usual way of linking Java 3D to Swing { 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); createSceneGraph(); initUserPosition(); // set user's viewpoint orbitControls(canvas3D); // controls for moving the viewpoint // turn on depth-sorting to deal with all the transparent // GIFs used for the leaves View view = su.getViewer().getView(); // depth-sort transparent objects on a per-geometry basis view.setTransparencySortingPolicy(View.TRANSPARENCY_SORT_GEOMETRY); su.addBranchGraph( sceneBG ); } // end of WrapTrees3D() private void createSceneGraph() // initilise the scene { sceneBG = new BranchGroup(); bounds = new BoundingSphere(new Point3d(0,0,0), BOUNDSIZE); lightScene(); // add the lights addBackground(); // add the sky sceneBG.addChild( new CheckerFloor().getBG() ); // add the floor growTrees(); // the new bit sceneBG.compile(); // fix the scene } // end of createSceneGraph() private void lightScene() /* One ambient light, 2 directional lights */ { Color3f white = new Color3f(1.0f, 1.0f, 1.0f); // 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() // ---------------------- grow trees ----------------- private void growTrees() /* Position three trees and create the behaviour that controls their growth. */ { // starting position for the first tree: (0,0,-5) Transform3D t3d = new Transform3D(); t3d.set( new Vector3f(0,0,-5)); TransformGroup tg0 = new TransformGroup(t3d); sceneBG.addChild(tg0); // create the tree TreeLimb t0 = new TreeLimb(Z_AXIS, 0, 0.05f, 0.5f, tg0, null); // second tree t3d.set( new Vector3f(-5,0,5)); TransformGroup tg1 = new TransformGroup(t3d); sceneBG.addChild(tg1); TreeLimb t1 = new TreeLimb(Y_AXIS, 45, 0.05f, 0.5f, tg1, null); // third tree t3d.set( new Vector3f(5,0,5)); TransformGroup tg2 = new TransformGroup(t3d); sceneBG.addChild(tg2); TreeLimb t2 = new TreeLimb(Y_AXIS, -60, 0.05f, 0.5f, tg2, null); // fourth tree t3d.set( new Vector3f(-9,0,-9)); TransformGroup tg3 = new TransformGroup(t3d); sceneBG.addChild(tg3); TreeLimb t3 = new TreeLimb(Y_AXIS, 30, 0.05f, 0.5f, tg3, null); // 5th tree t3d.set( new Vector3f(9,0,-9)); TransformGroup tg4 = new TransformGroup(t3d); sceneBG.addChild(tg4); TreeLimb t4 = new TreeLimb(Y_AXIS, -30, 0.05f, 0.5f, tg4, null); // load the leaf images used by all the trees ImageComponent2D[] leafIms = loadImages("images/leaf", 6); // the behaviour that manages the growing of the trees GrowthBehavior grower = new GrowthBehavior(leafIms); grower.setSchedulingBounds( bounds ); // add the trees to GrowthBehavior grower.addLimb(t0); grower.addLimb(t1); grower.addLimb(t2); grower.addLimb(t3); grower.addLimb(t4); sceneBG.addChild( grower ); } // end of growTrees() private ImageComponent2D[] loadImages(String fNms, int numIms) /* Load the leaf images: they all start with fNms, and there are numIms of them. */ { String filename; TextureLoader loader; ImageComponent2D[] ims = new ImageComponent2D[numIms]; System.out.println("Loading " + numIms + " textures from " + fNms); for (int i=0; i < numIms; i++) { filename = new String(fNms + i + ".gif"); loader = new TextureLoader(filename, null); ims[i] = loader.getImage(); if(ims[i] == null) System.out.println("Load failed for texture in : " + filename); ims[i].setCapability(ImageComponent2D.ALLOW_SIZE_READ); } return ims; } // end of loadImages()} // end of WrapTrees3D class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -