List of usage examples for javax.media.j3d BranchGroup addChild
public void addChild(Node child)
From source file:SwingTest.java
/** * Create the scene side of the scenegraph *///from www . java 2 s . c o m protected BranchGroup createSceneBranchGroup() { // create the root of the scene side scenegraph BranchGroup objRoot = new BranchGroup(); // create a TransformGroup to rotate the objects in the scene // set the capability bits on the TransformGroup so that it // can be modified at runtime TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); // create a spherical bounding volume BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // create a 4x4 transformation matrix Transform3D yAxis = new Transform3D(); // create an Alpha interpolator to automatically generate // modifications to the rotation component of the transformation matrix Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); // create a RotationInterpolator behavior to effect the TransformGroup rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f); // set the scheduling bounds on the behavior rotator.setSchedulingBounds(bounds); // add the behavior to the scenegraph objTrans.addChild(rotator); // create the BranchGroup which contains the objects // we add/remove to and from the scenegraph sceneBranchGroup = new BranchGroup(); // allow the BranchGroup to have children added at runtime sceneBranchGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND); sceneBranchGroup.setCapability(Group.ALLOW_CHILDREN_READ); sceneBranchGroup.setCapability(Group.ALLOW_CHILDREN_WRITE); // add both the cube and the sphere to the scenegraph sceneBranchGroup.addChild(createCube()); sceneBranchGroup.addChild(createSphere()); // create the colors for the lights Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f); Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); // create the ambient light AmbientLight aLgt = new AmbientLight(alColor); aLgt.setInfluencingBounds(bounds); // create the directional light DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); // add the lights to the scenegraph objRoot.addChild(aLgt); objRoot.addChild(lgt1); // wire the scenegraph together objTrans.addChild(sceneBranchGroup); objRoot.addChild(objTrans); // return the root of the scene side of the scenegraph return objRoot; }
From source file:TexCoordTest.java
BranchGroup createDemLandscape() { final double LAND_WIDTH = 200; final double LAND_LENGTH = 200; final double nTileSize = 10; final double yMaxHeight = LAND_WIDTH / 8; // calculate how many vertices we need to store all the "tiles" that // compose the QuadArray. final int nNumTiles = (int) (((LAND_LENGTH / nTileSize) * 2) * ((LAND_WIDTH / nTileSize) * 2)); final int nVertexCount = 4 * nNumTiles; Point3f[] coordArray = new Point3f[nVertexCount]; Point2f[] texCoordArray = new Point2f[nVertexCount]; // create the Appearance for the landscape and initialize all // the texture coordinate generation parameters createAppearance(yMaxHeight);/*from w w w .ja v a2 s . c om*/ // create the parent BranchGroup BranchGroup bg = new BranchGroup(); // create the geometry and populate a QuadArray // we use a simple sin/cos function to create an undulating surface // in the X/Z dimensions. Y dimension is the distance "above sea-level". int nItem = 0; double yValue0 = 0; double yValue1 = 0; double yValue2 = 0; double yValue3 = 0; final double xFactor = LAND_WIDTH / 5; final double zFactor = LAND_LENGTH / 3; // loop over all the tiles in the environment for (double x = -LAND_WIDTH; x <= LAND_WIDTH; x += nTileSize) { for (double z = -LAND_LENGTH; z <= LAND_LENGTH; z += nTileSize) { // if we are not on the last row or column create a "tile" // and add to the QuadArray. Use CCW winding if (z < LAND_LENGTH && x < LAND_WIDTH) { yValue0 = yMaxHeight * Math.sin(x / xFactor) * Math.cos(z / zFactor); yValue1 = yMaxHeight * Math.sin(x / xFactor) * Math.cos((z + nTileSize) / zFactor); yValue2 = yMaxHeight * Math.sin((x + nTileSize) / xFactor) * Math.cos((z + nTileSize) / zFactor); yValue3 = yMaxHeight * Math.sin((x + nTileSize) / xFactor) * Math.cos(z / zFactor); // note, we do not assign any texture coordinates! coordArray[nItem++] = new Point3f((float) x, (float) yValue0, (float) z); coordArray[nItem++] = new Point3f((float) x, (float) yValue1, (float) (z + nTileSize)); coordArray[nItem++] = new Point3f((float) (x + nTileSize), (float) yValue2, (float) (z + nTileSize)); coordArray[nItem++] = new Point3f((float) (x + nTileSize), (float) yValue3, (float) z); } } } // create a GeometryInfo and assign the coordinates GeometryInfo gi = new GeometryInfo(GeometryInfo.QUAD_ARRAY); gi.setCoordinates(coordArray); // generate Normal vectors for the QuadArray that was populated. NormalGenerator normalGenerator = new NormalGenerator(); normalGenerator.generateNormals(gi); // wrap the GeometryArray in a Shape3D Shape3D shape = new Shape3D(gi.getGeometryArray(), m_Appearance); // add the Shape3D to a BranchGroup and return bg.addChild(shape); return bg; }
From source file:AppearanceExplorer.java
BranchGroup createBeethoven() { java.net.URL beethovenURL = null; try {//from ww w .j a v a 2s. c o m beethovenURL = new java.net.URL(codeBaseString + "beethoven.obj"); } catch (Exception e) { System.err.println("Exception: " + e); System.exit(1); } int flags = ObjectFile.RESIZE; ObjectFile f = new ObjectFile(flags); Scene s = null; try { s = f.load(beethovenURL); } catch (Exception e) { System.err.println(e); System.exit(1); } Group sceneGroup = s.getSceneGroup(); Hashtable namedObjects = s.getNamedObjects(); Enumeration e = namedObjects.keys(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); Shape3D shape = (Shape3D) namedObjects.get(name); shape.setAppearance(appearance); } BranchGroup retVal = new BranchGroup(); retVal.addChild(s.getSceneGroup()); return retVal; }
From source file:AppearanceExplorer.java
BranchGroup createGalleon() { java.net.URL galleonURL = null; try {//ww w . java 2 s . c o m galleonURL = new java.net.URL(codeBaseString + "galleon.obj"); } catch (Exception e) { System.err.println("Exception: " + e); System.exit(1); } int flags = ObjectFile.RESIZE; ObjectFile f = new ObjectFile(flags); Scene s = null; try { s = f.load(galleonURL); } catch (Exception e) { System.err.println(e); System.exit(1); } Group sceneGroup = s.getSceneGroup(); Hashtable namedObjects = s.getNamedObjects(); Enumeration e = namedObjects.keys(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); //System.out.println("name = " + name); Shape3D shape = (Shape3D) namedObjects.get(name); shape.setAppearance(appearance); } BranchGroup retVal = new BranchGroup(); retVal.addChild(s.getSceneGroup()); return retVal; }
From source file:ExText.java
/** * Builds the 3D universe by constructing a virtual universe (via * SimpleUniverse), a view platform (via SimpleUniverse), and a view (via * SimpleUniverse). A headlight is added and a set of behaviors initialized * to handle navigation types./* w w w. j a v a2s.c o m*/ */ protected void buildUniverse() { // // Create a SimpleUniverse object, which builds: // // - a Locale using the given hi-res coordinate origin // // - a ViewingPlatform which in turn builds: // - a MultiTransformGroup with which to move the // the ViewPlatform about // // - a ViewPlatform to hold the view // // - a BranchGroup to hold avatar geometry (if any) // // - a BranchGroup to hold view platform // geometry (if any) // // - a Viewer which in turn builds: // - a PhysicalBody which characterizes the user's // viewing preferences and abilities // // - a PhysicalEnvironment which characterizes the // user's rendering hardware and software // // - a JavaSoundMixer which initializes sound // support within the 3D environment // // - a View which renders the scene into a Canvas3D // // All of these actions could be done explicitly, but // using the SimpleUniverse utilities simplifies the code. // if (debug) System.err.println("Building scene graph..."); SimpleUniverse universe = new SimpleUniverse(null, // Hi-res coordinate // for the origin - // use default 1, // Number of transforms in MultiTransformGroup exampleCanvas, // Canvas3D into which to draw null); // URL for user configuration file - use defaults // // Get the viewer and create an audio device so that // sound will be enabled in this content. // Viewer viewer = universe.getViewer(); viewer.createAudioDevice(); // // Get the viewing platform created by SimpleUniverse. // From that platform, get the inner-most TransformGroup // in the MultiTransformGroup. That inner-most group // contains the ViewPlatform. It is this inner-most // TransformGroup we need in order to: // // - add a "headlight" that always aims forward from // the viewer // // - change the viewing direction in a "walk" style // // The inner-most TransformGroup's transform will be // changed by the walk behavior (when enabled). // ViewingPlatform viewingPlatform = universe.getViewingPlatform(); exampleViewTransform = viewingPlatform.getViewPlatformTransform(); // // Create a "headlight" as a forward-facing directional light. // Set the light's bounds to huge. Since we want the light // on the viewer's "head", we need the light within the // TransformGroup containing the ViewPlatform. The // ViewingPlatform class creates a handy hook to do this // called "platform geometry". The PlatformGeometry class is // subclassed off of BranchGroup, and is intended to contain // a description of the 3D platform itself... PLUS a headlight! // So, to add the headlight, create a new PlatformGeometry group, // add the light to it, then add that platform geometry to the // ViewingPlatform. // BoundingSphere allBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100000.0); PlatformGeometry pg = new PlatformGeometry(); headlight = new DirectionalLight(); headlight.setColor(White); headlight.setDirection(new Vector3f(0.0f, 0.0f, -1.0f)); headlight.setInfluencingBounds(allBounds); headlight.setCapability(Light.ALLOW_STATE_WRITE); pg.addChild(headlight); viewingPlatform.setPlatformGeometry(pg); // // Create the 3D content BranchGroup, containing: // // - a TransformGroup who's transform the examine behavior // will change (when enabled). // // - 3D geometry to view // // Build the scene root BranchGroup sceneRoot = new BranchGroup(); // Build a transform that we can modify exampleSceneTransform = new TransformGroup(); exampleSceneTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); exampleSceneTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); exampleSceneTransform.setCapability(Group.ALLOW_CHILDREN_EXTEND); // // Build the scene, add it to the transform, and add // the transform to the scene root // if (debug) System.err.println(" scene..."); Group scene = this.buildScene(); exampleSceneTransform.addChild(scene); sceneRoot.addChild(exampleSceneTransform); // // Create a pair of behaviors to implement two navigation // types: // // - "examine": a style where mouse drags rotate about // the scene's origin as if it is an object under // examination. This is similar to the "Examine" // navigation type used by VRML browsers. // // - "walk": a style where mouse drags rotate about // the viewer's center as if the viewer is turning // about to look at a scene they are in. This is // similar to the "Walk" navigation type used by // VRML browsers. // // Aim the examine behavior at the scene's TransformGroup // and add the behavior to the scene root. // // Aim the walk behavior at the viewing platform's // TransformGroup and add the behavior to the scene root. // // Enable one (and only one!) of the two behaviors // depending upon the current navigation type. // examineBehavior = new ExamineViewerBehavior(exampleSceneTransform, // Transform // gorup // to // modify exampleFrame); // Parent frame for cusor changes examineBehavior.setSchedulingBounds(allBounds); sceneRoot.addChild(examineBehavior); walkBehavior = new WalkViewerBehavior(exampleViewTransform, // Transform // group to // modify exampleFrame); // Parent frame for cusor changes walkBehavior.setSchedulingBounds(allBounds); sceneRoot.addChild(walkBehavior); if (navigationType == Walk) { examineBehavior.setEnable(false); walkBehavior.setEnable(true); } else { examineBehavior.setEnable(true); walkBehavior.setEnable(false); } // // Compile the scene branch group and add it to the // SimpleUniverse. // if (shouldCompile) sceneRoot.compile(); universe.addBranchGraph(sceneRoot); reset(); }
From source file:Morphing.java
private BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create a Transformgroup to scale all objects so they // appear in the scene. TransformGroup objScale = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(0.4);//from w w w . ja va 2s . c o m objScale.setTransform(t3d); objRoot.addChild(objScale); // Create a bounds for the background and lights BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // Set up the background Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f); Background bg = new Background(bgColor); bg.setApplicationBounds(bounds); objScale.addChild(bg); // Set up the global lights Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f); Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f); Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f); AmbientLight aLgt = new AmbientLight(alColor); aLgt.setInfluencingBounds(bounds); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(bounds); objScale.addChild(aLgt); objScale.addChild(lgt1); // // Create the transform group nodes for the 3 original objects // and the morphed object. Add them to the root of the // branch graph. // TransformGroup objTrans[] = new TransformGroup[4]; for (int i = 0; i < 4; i++) { objTrans[i] = new TransformGroup(); objScale.addChild(objTrans[i]); } Transform3D tr = new Transform3D(); Transform3D rotX90 = new Transform3D(); rotX90.rotX(90.0 * Math.PI / 180.0); objTrans[0].getTransform(tr); tr.setTranslation(new Vector3d(-2.0, 1.5, -2.0)); tr.mul(rotX90); objTrans[0].setTransform(tr); objTrans[1].getTransform(tr); tr.setTranslation(new Vector3d(0.0, 1.5, -2.0)); tr.mul(rotX90); objTrans[1].setTransform(tr); objTrans[2].getTransform(tr); tr.setTranslation(new Vector3d(2.0, 1.5, -2.0)); tr.mul(rotX90); objTrans[2].setTransform(tr); objTrans[3].getTransform(tr); tr.setTranslation(new Vector3d(0.0, -2.0, -2.0)); tr.mul(rotX90); objTrans[3].setTransform(tr); // Now load the object files Scene s[] = new Scene[3]; GeometryArray g[] = new GeometryArray[3]; Shape3D shape[] = new Shape3D[3]; ObjectFile loader = new ObjectFile(ObjectFile.RESIZE); for (int i = 0; i < 3; i++) { s[i] = null; g[i] = null; shape[i] = null; } for (int i = 0; i < 3; i++) { try { s[i] = loader.load(objFiles[i]); } catch (FileNotFoundException e) { System.err.println(e); System.exit(1); } catch (ParsingErrorException e) { System.err.println(e); System.exit(1); } catch (IncorrectFormatException e) { System.err.println(e); System.exit(1); } BranchGroup b = s[i].getSceneGroup(); shape[i] = (Shape3D) b.getChild(0); g[i] = (GeometryArray) shape[i].getGeometry(); shape[i].setGeometry(g[i]); objTrans[i].addChild(b); } // // Create a Morph node, and set the appearance and input geometry // arrays. Set the Morph node's capability bits to allow the weights // to be modified at runtime. // Appearance app = new Appearance(); Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); app.setMaterial(new Material(objColor, black, objColor, black, 80.0f)); Morph morph = new Morph(g, app); morph.setCapability(Morph.ALLOW_WEIGHTS_READ); morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE); objTrans[3].addChild(morph); // Now create the Alpha object that controls the speed of the // morphing operation. Alpha morphAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 0, 0, 2000, 1000, 200, 2000, 1000, 200); // Finally, create the morphing behavior MorphingBehavior mBeh = new MorphingBehavior(morphAlpha, morph); mBeh.setSchedulingBounds(bounds); objScale.addChild(mBeh); return objRoot; }
From source file:AppearanceExplorer.java
BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Add the primitives to the scene setupAppearance();//from w ww . jav a2 s. c o m setupSceneSwitch(); objRoot.addChild(sceneSwitch); objRoot.addChild(bgSwitch); Group lightGroup = setupLights(); objRoot.addChild(lightGroup); return objRoot; }
From source file:AppearanceExplorer.java
BackgroundTool(String codeBaseString) { bgSwitch = new Switch(Switch.CHILD_NONE); bgSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); // set up the dark grey BG color node Background bgDarkGrey = new Background(darkGrey); bgDarkGrey.setApplicationBounds(infiniteBounds); bgSwitch.addChild(bgDarkGrey);//from www .j a v a2 s . c o m // set up the grey BG color node Background bgGrey = new Background(grey); bgGrey.setApplicationBounds(infiniteBounds); bgSwitch.addChild(bgGrey); // set up the light grey BG color node Background bgLightGrey = new Background(lightGrey); bgLightGrey.setApplicationBounds(infiniteBounds); bgSwitch.addChild(bgLightGrey); // set up the white BG color node Background bgWhite = new Background(white); bgWhite.setApplicationBounds(infiniteBounds); bgSwitch.addChild(bgWhite); // set up the blue BG color node Background bgBlue = new Background(skyBlue); bgBlue.setApplicationBounds(infiniteBounds); bgSwitch.addChild(bgBlue); // set up the image java.net.URL bgImageURL = null; try { bgImageURL = new java.net.URL(codeBaseString + "bg.jpg"); } catch (java.net.MalformedURLException ex) { System.out.println(ex.getMessage()); System.exit(1); } if (bgImageURL == null) { // application, try file URL try { bgImageURL = new java.net.URL("file:./bg.jpg"); } catch (java.net.MalformedURLException ex) { System.out.println(ex.getMessage()); System.exit(1); } } TextureLoader bgTexture = new TextureLoader(bgImageURL, null); // Create a background with the static image Background bgImage = new Background(bgTexture.getImage()); bgImage.setApplicationBounds(infiniteBounds); bgSwitch.addChild(bgImage); // create a background with the image mapped onto a sphere which // will enclose the world Background bgGeo = new Background(); bgGeo.setApplicationBounds(infiniteBounds); BranchGroup bgGeoBG = new BranchGroup(); Appearance bgGeoApp = new Appearance(); bgGeoApp.setTexture(bgTexture.getTexture()); Sphere sphereObj = new Sphere(1.0f, Sphere.GENERATE_NORMALS | Sphere.GENERATE_NORMALS_INWARD | Sphere.GENERATE_TEXTURE_COORDS, 45, bgGeoApp); bgGeoBG.addChild(sphereObj); bgGeo.setGeometry(bgGeoBG); bgSwitch.addChild(bgGeo); // Create the chooser GUI String[] bgNames = { "No Background (Black)", "Dark Grey", "Grey", "Light Grey", "White", "Blue", "Sky Image", "Sky Geometry", }; int[] bgValues = { Switch.CHILD_NONE, 0, 1, 2, 3, 4, 5, 6 }; bgChooser = new IntChooser("Background:", bgNames, bgValues, 0); bgChooser.addIntListener(new IntListener() { public void intChanged(IntEvent event) { int value = event.getValue(); bgSwitch.setWhichChild(value); } }); bgChooser.setValue(Switch.CHILD_NONE); }
From source file:SplineInterpolatorTest.java
protected Background createBackground() { // add the sky backdrop Background back = new Background(); back.setApplicationBounds(getApplicationBounds()); BranchGroup bgGeometry = new BranchGroup(); // create an appearance and assign the texture image Appearance app = new Appearance(); Texture tex = new TextureLoader("sky.gif", this).getTexture(); app.setTexture(tex);/* w ww .ja v a 2 s . c om*/ Sphere sphere = new Sphere(1.0f, Primitive.GENERATE_TEXTURE_COORDS | Primitive.GENERATE_NORMALS_INWARD, app); bgGeometry.addChild(sphere); back.setGeometry(bgGeometry); return back; }
From source file:SplineInterpolatorTest.java
protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); // create a root TG in case we need to scale the scene TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Transform3D t3d = new Transform3D(); objTrans.setTransform(t3d);/* ww w . ja v a2 s . c om*/ Group hiResGroup = createLodLand(objTrans); createBuildings(objTrans); createHelicopters(objTrans); // connect objRoot.addChild(objTrans); return objRoot; }