List of usage examples for javax.media.j3d SharedGroup addChild
public void addChild(Node child)
From source file:KeyNavigatorApp.java
public BranchGroup createSceneGraph(SimpleUniverse su) { // Create the root of the branch graph TransformGroup vpTrans = null;/* w w w . j a va 2 s . c om*/ BranchGroup objRoot = new BranchGroup(); Vector3f translate = new Vector3f(); Transform3D T3D = new Transform3D(); TransformGroup TG = null; objRoot.addChild(createLand()); SharedGroup share = new SharedGroup(); share.addChild(createPyramid()); float[][] position = { { 0.0f, 0.0f, -3.0f }, { 6.0f, 0.0f, 0.0f }, { 6.0f, 0.0f, 6.0f }, { 3.0f, 0.0f, -10.0f }, { 13.0f, 0.0f, -30.0f }, { -13.0f, 0.0f, 30.0f }, { -13.0f, 0.0f, 23.0f }, { 13.0f, 0.0f, 3.0f } }; for (int i = 0; i < position.length; i++) { translate.set(position[i]); T3D.setTranslation(translate); TG = new TransformGroup(T3D); TG.addChild(new Link(share)); objRoot.addChild(TG); } vpTrans = su.getViewingPlatform().getViewPlatformTransform(); translate.set(0.0f, 0.3f, 0.0f); T3D.setTranslation(translate); vpTrans.setTransform(T3D); KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans); keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(), 1000.0)); objRoot.addChild(keyNavBeh); // Let Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:LightBug.java
void setupSpheres() { // create a Switch for the spheres, allow switch changes spheresSwitch = new Switch(Switch.CHILD_ALL); spheresSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); // Set up an appearance to make the Sphere with red ambient, // black emmissive, red diffuse and white specular coloring Material material = new Material(red, black, red, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material);//from w w w . j av a 2 s . c om // create a sphere and put it into a shared group Sphere sphere = new Sphere(0.5f, appearance); SharedGroup sphereSG = new SharedGroup(); sphereSG.addChild(sphere); // create a grid of spheres in the z=0 plane // each has a TransformGroup to position the sphere which contains // a link to the shared group for the sphere for (int y = -2; y <= 2; y++) { for (int x = -2; x <= 2; x++) { TransformGroup tg = new TransformGroup(); tmpVector.set(x * 1.2f, y * 1.2f, 0.0f); tmpTrans.set(tmpVector); tg.setTransform(tmpTrans); tg.addChild(new Link(sphereSG)); spheresSwitch.addChild(tg); } } }
From source file:NodesTest.java
protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); double labelScale = 20; // create the top level Switch Node // we will use the Switch Node to switch the // other Nodes on and off. // 1: Switch/*from w w w . j a v a2 s . c om*/ Switch switchGroup = new Switch(); switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE); switchGroup.addChild(createLabel("1. Switch Label", labelScale)); // 2: BranchGroup BranchGroup branchGroup = new BranchGroup(); branchGroup.addChild(createLabel("2. BranchGroup", labelScale)); switchGroup.addChild(branchGroup); // 3: OrderedGroup, OrderedGroup orderedGroup = new OrderedGroup(); orderedGroup.addChild(createLabel("3. OrderedGroup", labelScale)); orderedGroup.addChild(createLabel("Child 1", labelScale)); orderedGroup.addChild(createLabel("Child 2", labelScale)); switchGroup.addChild(orderedGroup); // 4: SharedGroup, SharedGroup sharedGroup1 = new SharedGroup(); sharedGroup1.addChild(createLabel("4. Shared Group 1", labelScale)); switchGroup.addChild(new Link(sharedGroup1)); // 5: Primitive, BranchGroup primitiveGroup = new BranchGroup(); primitiveGroup.addChild(createLabel("5. Primitive", labelScale)); primitiveGroup.addChild(new Sphere(2)); switchGroup.addChild(primitiveGroup); // 6: TransformGroup TransformGroup transformGroup = new TransformGroup(); transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); transformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, transformGroup, yAxis, 0.0f, (float) Math.PI * 2.0f); rotator.setSchedulingBounds(createApplicationBounds()); transformGroup.addChild(rotator); transformGroup.addChild(new ColorCube(2)); transformGroup.addChild(createLabel("6. TransformGroup", labelScale)); switchGroup.addChild(transformGroup); // 7: add another copy of the shared group switchGroup.addChild(new Link(sharedGroup1)); // create a SwitchValueInterpolator to // cycle through the child nodes in the Switch Node Alpha switchAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 10000, 0, 0, 0, 0, 0); SwitchValueInterpolator switchInterpolator = new SwitchValueInterpolator(switchAlpha, switchGroup); switchInterpolator.setSchedulingBounds(createApplicationBounds()); switchInterpolator.setEnable(true); // WARNING: do not add the SwitchValueInterpolator to the Switch Node! objRoot.addChild(switchInterpolator); // finally add the Switch Node objRoot.addChild(switchGroup); return objRoot; }
From source file:EnvironmentExplorer.java
void setupSpheres() { // create a Switch for the spheres, allow switch changes spheresSwitch = new Switch(Switch.CHILD_ALL); spheresSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); // Set up an appearance to make the Sphere with objColor ambient, // black emmissive, objColor diffuse and white specular coloring Material material = new Material(objColor, black, objColor, white, 32); Appearance appearance = new Appearance(); appearance.setMaterial(material);//from www.ja va 2s.co m // create a sphere and put it into a shared group Sphere sphere = new Sphere(0.5f, appearance); SharedGroup sphereSG = new SharedGroup(); sphereSG.addChild(sphere); // create a grid of spheres in the z=0 plane // each has a TransformGroup to position the sphere which contains // a link to the shared group for the sphere for (int y = -2; y <= 2; y++) { for (int x = -2; x <= 2; x++) { TransformGroup tg = new TransformGroup(); tmpVector.set(x * 1.2f, y * 1.2f, -0.1f); tmpTrans.set(tmpVector); tg.setTransform(tmpTrans); tg.addChild(new Link(sphereSG)); spheresSwitch.addChild(tg); } } }
From source file:TransformExplorer.java
RotAxis(float axisLength) { super(Switch.CHILD_NONE); setCapability(Switch.ALLOW_SWITCH_READ); setCapability(Switch.ALLOW_SWITCH_WRITE); // set up the proportions for the arrow float axisRadius = axisLength / 120.0f; float arrowRadius = axisLength / 50.0f; float arrowHeight = axisLength / 30.0f; // create the TransformGroup which will be used to orient the axis axisTG = new TransformGroup(); axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); addChild(axisTG);//from ww w . j a v a 2s . co m // Set up an appearance to make the Axis have // blue ambient, black emmissive, blue diffuse and white specular // coloring. Material material = new Material(blue, black, blue, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); // create a cylinder for the central line of the axis Cylinder cylinder = new Cylinder(axisRadius, axisLength, appearance); // cylinder goes from -length/2 to length/2 in y axisTG.addChild(cylinder); // create a SharedGroup for the arrowHead Cone arrowHead = new Cone(arrowRadius, arrowHeight, appearance); SharedGroup arrowHeadSG = new SharedGroup(); arrowHeadSG.addChild(arrowHead); // Create a TransformGroup to move the cone to the top of the // cylinder tmpVector.set(0.0f, axisLength / 2 + arrowHeight / 2, 0.0f); tmpTrans.set(tmpVector); TransformGroup topTG = new TransformGroup(); topTG.setTransform(tmpTrans); topTG.addChild(new Link(arrowHeadSG)); axisTG.addChild(topTG); // create the bottom of the arrow // Create a TransformGroup to move the cone to the bottom of the // axis so that its pushes into the bottom of the cylinder tmpVector.set(0.0f, -(axisLength / 2), 0.0f); tmpTrans.set(tmpVector); TransformGroup bottomTG = new TransformGroup(); bottomTG.setTransform(tmpTrans); bottomTG.addChild(new Link(arrowHeadSG)); axisTG.addChild(bottomTG); updateAxisTransform(); }
From source file:TransformExplorer.java
CoordSys(float axisLength) { super(Switch.CHILD_ALL); float coordSysLength = axisLength; float labelOffset = axisLength / 20.0f; float axisRadius = axisLength / 500.0f; float arrowRadius = axisLength / 125.0f; float arrowHeight = axisLength / 50.0f; float tickRadius = axisLength / 125.0f; float tickHeight = axisLength / 250.0f; // Set the Switch to allow changes setCapability(Switch.ALLOW_SWITCH_READ); setCapability(Switch.ALLOW_SWITCH_WRITE); // Set up an appearance to make the Axis have // grey ambient, black emmissive, grey diffuse and grey specular // coloring.//from w w w . j a va 2 s .co m //Material material = new Material(grey, black, grey, white, 64); Material material = new Material(white, black, white, white, 64); Appearance appearance = new Appearance(); appearance.setMaterial(material); // Create a shared group to hold one axis of the coord sys SharedGroup coordAxisSG = new SharedGroup(); // create a cylinder for the central line of the axis Cylinder cylinder = new Cylinder(axisRadius, coordSysLength, appearance); // cylinder goes from -coordSysLength/2 to coordSysLength in y coordAxisSG.addChild(cylinder); // create the shared arrowhead Cone arrowHead = new Cone(arrowRadius, arrowHeight, appearance); SharedGroup arrowHeadSG = new SharedGroup(); arrowHeadSG.addChild(arrowHead); // Create a TransformGroup to move the arrowhead to the top of the // axis // The arrowhead goes from -arrowHeight/2 to arrowHeight/2 in y. // Put it at the top of the axis, coordSysLength / 2 tmpVector.set(0.0f, coordSysLength / 2 + arrowHeight / 2, 0.0f); tmpTrans.set(tmpVector); TransformGroup topTG = new TransformGroup(); topTG.setTransform(tmpTrans); topTG.addChild(new Link(arrowHeadSG)); coordAxisSG.addChild(topTG); // create the minus arrowhead // Create a TransformGroup to turn the cone upside down: // Rotate 180 degrees around Z axis tmpAxisAngle.set(0.0f, 0.0f, 1.0f, (float) Math.toRadians(180)); tmpTrans.set(tmpAxisAngle); // Put the arrowhead at the bottom of the axis tmpVector.set(0.0f, -coordSysLength / 2 - arrowHeight / 2, 0.0f); tmpTrans.setTranslation(tmpVector); TransformGroup bottomTG = new TransformGroup(); bottomTG.setTransform(tmpTrans); bottomTG.addChild(new Link(arrowHeadSG)); coordAxisSG.addChild(bottomTG); // Now add "ticks" at 1, 2, 3, etc. // create a shared group for the tick Cylinder tick = new Cylinder(tickRadius, tickHeight, appearance); SharedGroup tickSG = new SharedGroup(); tickSG.addChild(tick); // transform each instance and add it to the coord axis group int maxTick = (int) (coordSysLength / 2); int minTick = -maxTick; for (int i = minTick; i <= maxTick; i++) { if (i == 0) continue; // no tick at 0 // use a TransformGroup to offset to the tick location TransformGroup tickTG = new TransformGroup(); tmpVector.set(0.0f, (float) i, 0.0f); tmpTrans.set(tmpVector); tickTG.setTransform(tmpTrans); // then link to an instance of the Tick shared group tickTG.addChild(new Link(tickSG)); // add the TransformGroup to the coord axis coordAxisSG.addChild(tickTG); } // add a Link to the axis SharedGroup to the coordSys addChild(new Link(coordAxisSG)); // Y axis // Create TransformGroups for the X and Z axes TransformGroup xAxisTG = new TransformGroup(); // rotate 90 degrees around Z axis tmpAxisAngle.set(0.0f, 0.0f, 1.0f, (float) Math.toRadians(90)); tmpTrans.set(tmpAxisAngle); xAxisTG.setTransform(tmpTrans); xAxisTG.addChild(new Link(coordAxisSG)); addChild(xAxisTG); // X axis TransformGroup zAxisTG = new TransformGroup(); // rotate 90 degrees around X axis tmpAxisAngle.set(1.0f, 0.0f, 0.0f, (float) Math.toRadians(90)); tmpTrans.set(tmpAxisAngle); zAxisTG.setTransform(tmpTrans); zAxisTG.addChild(new Link(coordAxisSG)); addChild(zAxisTG); // Z axis // Add the labels. First we need a Font3D for the Text3Ds // select the default font, plain style, 0.5 tall. Use null for // the extrusion so we get "flat" text since we will be putting it // into an oriented Shape3D Font3D f3d = new Font3D(new Font("Default", Font.PLAIN, 1), null); // set up the +X label Text3D plusXText = new Text3D(f3d, "+X", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT); // orient around the local origin OrientedShape3D plusXTextShape = new OrientedShape3D(plusXText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, origin); // transform to scale down to 0.15 in height, locate at end of axis TransformGroup plusXTG = new TransformGroup(); tmpVector.set(coordSysLength / 2 + labelOffset, 0.0f, 0.0f); tmpTrans.set(0.15f, tmpVector); plusXTG.setTransform(tmpTrans); plusXTG.addChild(plusXTextShape); addChild(plusXTG); // set up the -X label Text3D minusXText = new Text3D(f3d, "-X", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT); // orient around the local origin OrientedShape3D minusXTextShape = new OrientedShape3D(minusXText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, origin); // transform to scale down to 0.15 in height, locate at end of axis TransformGroup minusXTG = new TransformGroup(); tmpVector.set(-coordSysLength / 2 - labelOffset, 0.0f, 0.0f); tmpTrans.set(0.15f, tmpVector); minusXTG.setTransform(tmpTrans); minusXTG.addChild(minusXTextShape); addChild(minusXTG); // set up the +Y label Text3D plusYText = new Text3D(f3d, "+Y", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT); // orient around the local origin OrientedShape3D plusYTextShape = new OrientedShape3D(plusYText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, origin); // transform to scale down to 0.15 in height, locate at end of axis TransformGroup plusYTG = new TransformGroup(); tmpVector.set(0.0f, coordSysLength / 2 + labelOffset, 0.0f); tmpTrans.set(0.15f, tmpVector); plusYTG.setTransform(tmpTrans); plusYTG.addChild(plusYTextShape); addChild(plusYTG); // set up the -Y label Text3D minusYText = new Text3D(f3d, "-Y", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT); // orient around the local origin OrientedShape3D minusYTextShape = new OrientedShape3D(minusYText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, origin); // transform to scale down to 0.15 in height, locate at end of axis TransformGroup minusYTG = new TransformGroup(); tmpVector.set(0.0f, -coordSysLength / 2 - labelOffset, 0.0f); tmpTrans.set(0.15f, tmpVector); minusYTG.setTransform(tmpTrans); minusYTG.addChild(minusYTextShape); addChild(minusYTG); // set up the +Z label Text3D plusZText = new Text3D(f3d, "+Z", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT); // orient around the local origin OrientedShape3D plusZTextShape = new OrientedShape3D(plusZText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, origin); // transform to scale down to 0.15 in height, locate at end of axis TransformGroup plusZTG = new TransformGroup(); tmpVector.set(0.0f, 0.0f, coordSysLength / 2 + labelOffset); tmpTrans.set(0.15f, tmpVector); plusZTG.setTransform(tmpTrans); plusZTG.addChild(plusZTextShape); addChild(plusZTG); // set up the -Z label Text3D minusZText = new Text3D(f3d, "-Z", origin, Text3D.ALIGN_CENTER, Text3D.PATH_RIGHT); // orient around the local origin OrientedShape3D minusZTextShape = new OrientedShape3D(minusZText, appearance, OrientedShape3D.ROTATE_ABOUT_POINT, origin); // transform to scale down to 0.15 in height, locate at end of axis TransformGroup minusZTG = new TransformGroup(); tmpVector.set(0.0f, 0.0f, -coordSysLength / 2 - labelOffset); tmpTrans.set(0.15f, tmpVector); minusZTG.setTransform(tmpTrans); minusZTG.addChild(minusZTextShape); addChild(minusZTG); }
From source file:ExLinearFog.java
private SharedGroup buildSharedColumn() { Appearance columnApp = new Appearance(); Material columnMat = new Material(); columnMat.setAmbientColor(0.6f, 0.6f, 0.6f); columnMat.setDiffuseColor(1.0f, 1.0f, 1.0f); columnMat.setSpecularColor(0.0f, 0.0f, 0.0f); columnApp.setMaterial(columnMat);/*w ww . ja va2s . c o m*/ TextureAttributes columnTexAtt = new TextureAttributes(); columnTexAtt.setTextureMode(TextureAttributes.MODULATE); columnTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); columnApp.setTextureAttributes(columnTexAtt); if (columnTex != null) columnApp.setTexture(columnTex); GothicColumn columnShape = new GothicColumn(ColumnHeight, // height ColumnRadius, // radius GothicColumn.BUILD_TOP, // flags columnApp); // appearance // BEGIN EXAMPLE TOPIC // Build a shared group to hold the column shape SharedGroup column = new SharedGroup(); column.addChild(columnShape); // END EXAMPLE TOPIC return column; }
From source file:ExHenge.java
public Group buildScene() { // Turn off the example headlight setHeadlightEnable(false);// www .jav a 2s. c o m // Default to walk navigation setNavigationType(Walk); // // Preload the texture images // if (debug) System.err.println(" textures..."); Texture groundTex = null; Texture spurTex = null; Texture domeTex = null; TextureLoader texLoader = null; ImageComponent image = null; texLoader = new TextureLoader("mud01.jpg", this); image = texLoader.getImage(); if (image == null) System.err.println("Cannot load mud01.jpg texture"); else { groundTex = texLoader.getTexture(); groundTex.setBoundaryModeS(Texture.WRAP); groundTex.setBoundaryModeT(Texture.WRAP); groundTex.setMinFilter(Texture.NICEST); groundTex.setMagFilter(Texture.NICEST); groundTex.setMipMapMode(Texture.BASE_LEVEL); groundTex.setEnable(true); } texLoader = new TextureLoader("stonebrk2.jpg", this); image = texLoader.getImage(); if (image == null) System.err.println("Cannot load stonebrk2.jpg texture"); else { spurTex = texLoader.getTexture(); spurTex.setBoundaryModeS(Texture.WRAP); spurTex.setBoundaryModeT(Texture.WRAP); spurTex.setMinFilter(Texture.NICEST); spurTex.setMagFilter(Texture.NICEST); spurTex.setMipMapMode(Texture.BASE_LEVEL); spurTex.setEnable(true); } texLoader = new TextureLoader("fire.jpg", this); image = texLoader.getImage(); if (image == null) System.err.println("Cannot load fire.jpg texture"); else { domeTex = texLoader.getTexture(); domeTex.setBoundaryModeS(Texture.WRAP); domeTex.setBoundaryModeT(Texture.WRAP); domeTex.setMinFilter(Texture.NICEST); domeTex.setMagFilter(Texture.NICEST); domeTex.setMipMapMode(Texture.BASE_LEVEL); domeTex.setEnable(true); } // // Build some shapes we'll need // if (debug) System.err.println(" flying buttresses..."); // Build three types of spurs (flying buttresses) Appearance spurApp = new Appearance(); Material spurMat = new Material(); spurMat.setAmbientColor(0.6f, 0.6f, 0.6f); spurMat.setDiffuseColor(1.0f, 1.0f, 1.0f); spurMat.setSpecularColor(0.0f, 0.0f, 0.0f); spurApp.setMaterial(spurMat); Transform3D tr = new Transform3D(); tr.setIdentity(); tr.setScale(new Vector3d(1.0, 4.0, 1.0)); TextureAttributes spurTexAtt = new TextureAttributes(); spurTexAtt.setTextureMode(TextureAttributes.MODULATE); spurTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); spurTexAtt.setTextureTransform(tr); spurApp.setTextureAttributes(spurTexAtt); if (spurTex != null) spurApp.setTexture(spurTex); Arch spur1 = new Arch(0.0, // start Phi 1.571, // end Phi 9, // nPhi -0.0982, // start Theta 0.0982, // end Theta (11.25 degrees) 2, // nTheta 2.5, // start radius 1.0, // end radius 0.05, // start phi thickness 0.025, // end phi thickness spurApp); // appearance Arch spur2 = new Arch(0.0, // start Phi 1.571, // end Phi 9, // nPhi -0.0982, // start Theta 0.0982, // end Theta (11.25 degrees) 2, // nTheta 1.5, // start radius 2.0, // end radius 0.05, // start phi thickness 0.025, // end phi thickness spurApp); // appearance Arch spur3 = new Arch(0.0, // start Phi 1.571, // end Phi 9, // nPhi -0.0982, // start Theta 0.0982, // end Theta (11.25 degrees) 2, // nTheta 1.5, // start radius 1.0, // end radius 0.05, // start phi thickness 0.025, // end phi thickness spurApp); // appearance Arch spur4 = new Arch(0.0, // start Phi 1.178, // end Phi 9, // nPhi -0.0982, // start Theta 0.0982, // end Theta (11.25 degrees) 2, // nTheta 4.0, // start radius 4.0, // end radius 0.05, // start phi thickness 0.025, // end phi thickness spurApp); // appearance // Put each spur into a shared group so we can instance // the spurs multiple times SharedGroup spur1Group = new SharedGroup(); spur1Group.addChild(spur1); spur1Group.compile(); SharedGroup spur2Group = new SharedGroup(); spur2Group.addChild(spur2); spur2Group.compile(); SharedGroup spur3Group = new SharedGroup(); spur3Group.addChild(spur3); spur3Group.compile(); SharedGroup spur4Group = new SharedGroup(); spur4Group.addChild(spur4); spur4Group.compile(); // Build a central dome if (debug) System.err.println(" central dome..."); Appearance domeApp = new Appearance(); // No material needed - we want the dome to glow, // so use a REPLACE mode texture only TextureAttributes domeTexAtt = new TextureAttributes(); domeTexAtt.setTextureMode(TextureAttributes.REPLACE); domeTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); domeApp.setTextureAttributes(domeTexAtt); if (domeTex != null) domeApp.setTexture(domeTex); Arch dome = new Arch(0.0, // start Phi 1.571, // end Phi 5, // nPhi 0.0, // start Theta 2.0 * Math.PI, // end Theta (360 degrees) 17, // nTheta 1.0, // start radius 1.0, // end radius 0.0, // start phi thickness 0.0, // end phi thickness domeApp); // appearance // Build the ground. Use a trick to get better lighting // effects by using an elevation grid. The idea is this: // for interactive graphics systems, such as those // controlled by Java3D, lighting effects are computed only // at triangle vertexes. Imagine a big rectangular ground // underneath a PointLight (added below). If the // PointLight is above the center of the square, in the real // world we'd expect a bright spot below it, fading to // darkness at the edges of the square. Not so in // interactive graphics. Since lighting is only computed // at vertexes, and the square's vertexes are each // equidistant from a centered PointLight, all four square // coordinates get the same brightness. That brightness // is interpolated across the square, giving a *constant* // brightness for the entire square! There is no bright // spot under the PointLight. So, here's the trick: use // more triangles. Pretty simple. Split the ground under // the PointLight into a grid of smaller squares. Each // smaller square is shaded using light brightness computed // at the square's vertexes. Squares directly under the // PointLight get brighter lighting at their vertexes, and // thus they are bright. This gives the desired bright // spot under the PointLight. The more squares we use // (a denser grid), the more accurate the bright spot and // the smoother the lighting gradation from bright directly // under the PointLight, to dark at the distant edges. Of // course, with more squares, we also get more polygons to // draw and a performance slow-down. So there is a // tradeoff between lighting quality and drawing speed. // For this example, we'll use a coarse mesh of triangles // created using an ElevationGrid shape. if (debug) System.err.println(" ground..."); Appearance groundApp = new Appearance(); Material groundMat = new Material(); groundMat.setAmbientColor(0.3f, 0.3f, 0.3f); groundMat.setDiffuseColor(0.7f, 0.7f, 0.7f); groundMat.setSpecularColor(0.0f, 0.0f, 0.0f); groundApp.setMaterial(groundMat); tr = new Transform3D(); tr.setScale(new Vector3d(8.0, 8.0, 1.0)); TextureAttributes groundTexAtt = new TextureAttributes(); groundTexAtt.setTextureMode(TextureAttributes.MODULATE); groundTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); groundTexAtt.setTextureTransform(tr); groundApp.setTextureAttributes(groundTexAtt); if (groundTex != null) groundApp.setTexture(groundTex); ElevationGrid ground = new ElevationGrid(11, // X dimension 11, // Z dimension 2.0f, // X spacing 2.0f, // Z spacing // Automatically use zero heights groundApp); // Appearance // // Build the scene using the shapes above. Place everything // withing a TransformGroup. // // Build the scene root TransformGroup scene = new TransformGroup(); tr = new Transform3D(); tr.setTranslation(new Vector3f(0.0f, -1.6f, 0.0f)); scene.setTransform(tr); // Create influencing bounds BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center 1000.0); // Extent // General Ambient light ambient = new AmbientLight(); ambient.setEnable(ambientOnOff); ambient.setColor(new Color3f(0.3f, 0.3f, 0.3f)); ambient.setCapability(AmbientLight.ALLOW_STATE_WRITE); ambient.setInfluencingBounds(worldBounds); scene.addChild(ambient); // Bright Ambient light brightAmbient = new AmbientLight(); brightAmbient.setEnable(brightAmbientOnOff); brightAmbient.setColor(new Color3f(1.0f, 1.0f, 1.0f)); brightAmbient.setCapability(AmbientLight.ALLOW_STATE_WRITE); brightAmbient.setInfluencingBounds(worldBounds); scene.addChild(brightAmbient); // Red directional light redDirectional = new DirectionalLight(); redDirectional.setEnable(redDirectionalOnOff); redDirectional.setColor(new Color3f(1.0f, 0.0f, 0.0f)); redDirectional.setDirection(new Vector3f(1.0f, -0.5f, -0.5f)); redDirectional.setCapability(AmbientLight.ALLOW_STATE_WRITE); redDirectional.setInfluencingBounds(worldBounds); scene.addChild(redDirectional); // Yellow directional light yellowDirectional = new DirectionalLight(); yellowDirectional.setEnable(yellowDirectionalOnOff); yellowDirectional.setColor(new Color3f(1.0f, 0.8f, 0.0f)); yellowDirectional.setDirection(new Vector3f(-1.0f, 0.5f, 1.0f)); yellowDirectional.setCapability(AmbientLight.ALLOW_STATE_WRITE); yellowDirectional.setInfluencingBounds(worldBounds); scene.addChild(yellowDirectional); // Orange point light orangePoint = new PointLight(); orangePoint.setEnable(orangePointOnOff); orangePoint.setColor(new Color3f(1.0f, 0.5f, 0.0f)); orangePoint.setPosition(new Point3f(0.0f, 0.5f, 0.0f)); orangePoint.setCapability(AmbientLight.ALLOW_STATE_WRITE); orangePoint.setInfluencingBounds(worldBounds); scene.addChild(orangePoint); // Ground scene.addChild(ground); // Dome scene.addChild(dome); // Spur 1's Group g = buildRing(spur1Group); scene.addChild(g); // Spur 2's TransformGroup tg = new TransformGroup(); tr = new Transform3D(); tr.rotY(0.3927); tg.setTransform(tr); g = buildRing(spur2Group); tg.addChild(g); scene.addChild(tg); // Spur 3's g = buildRing(spur3Group); scene.addChild(g); // Spur 4's tg = new TransformGroup(); tg.setTransform(tr); g = buildRing(spur4Group); tg.addChild(g); scene.addChild(tg); return scene; }
From source file:ExSwitch.java
public Group buildScene() { // Turn on the example headlight setHeadlightEnable(true);/* w w w .j a v a 2s . c om*/ // Default to walk navigation setNavigationType(Walk); // Build the scene group Group scene = new Group(); if (debug) System.err.println(" switch shapes..."); // BEGIN EXAMPLE TOPIC // Build the switch group and allow its switch // value to be changed via menu items swtch = new Switch(); swtch.setCapability(Switch.ALLOW_SWITCH_WRITE); // Create several shapes to place in a switch group // Child 0: a red sphere Appearance app0 = new Appearance(); Material mat0 = new Material(); mat0.setAmbientColor(0.2f, 0.2f, 0.2f); mat0.setDiffuseColor(1.0f, 0.0f, 0.2f); mat0.setSpecularColor(0.7f, 0.7f, 0.7f); app0.setMaterial(mat0); Transform3D t3d = new Transform3D(); t3d.setTranslation(new Vector3f(-2.0f, 1.5f, 0.0f)); TransformGroup tg0 = new TransformGroup(t3d); Sphere sph0 = new Sphere(0.5f, // radius Primitive.GENERATE_NORMALS, // components 16, // facets app0); // appearance tg0.addChild(sph0); swtch.addChild(tg0); // Child 0 // Child 1: a green sphere Appearance app1 = new Appearance(); Material mat1 = new Material(); mat1.setAmbientColor(0.2f, 0.2f, 0.2f); mat1.setDiffuseColor(0.0f, 1.0f, 0.0f); mat1.setSpecularColor(0.7f, 0.7f, 0.7f); app1.setMaterial(mat1); t3d.setTranslation(new Vector3f(0.0f, 1.5f, 0.0f)); TransformGroup tg1 = new TransformGroup(t3d); Sphere sph1 = new Sphere(0.5f, // radius Primitive.GENERATE_NORMALS, // components 16, // facets app1); // appearance tg1.addChild(sph1); swtch.addChild(tg1); // Child 1 // Child 2: a blue sphere Appearance app2 = new Appearance(); Material mat2 = new Material(); mat2.setAmbientColor(0.2f, 0.2f, 0.2f); mat2.setDiffuseColor(0.0f, 0.6f, 1.0f); mat2.setSpecularColor(0.7f, 0.7f, 0.7f); app2.setMaterial(mat2); t3d.setTranslation(new Vector3f(2.0f, 1.5f, 0.0f)); TransformGroup tg2 = new TransformGroup(t3d); Sphere sph2 = new Sphere(0.5f, // radius Primitive.GENERATE_NORMALS, // components 16, // facets app2); // appearance tg2.addChild(sph2); swtch.addChild(tg2); // Set the initial child choice swtch.setWhichChild(options[currentSwitch].child); scene.addChild(swtch); // END EXAMPLE TOPIC // Build foreground geometry including a floor and // columns on which the switchable shapes stand // Load textures TextureLoader texLoader = new TextureLoader("granite07rev.jpg", this); Texture columnTex = texLoader.getTexture(); if (columnTex == null) System.err.println("Cannot load granite07rev.jpg texture"); else { columnTex.setBoundaryModeS(Texture.WRAP); columnTex.setBoundaryModeT(Texture.WRAP); columnTex.setMinFilter(Texture.NICEST); columnTex.setMagFilter(Texture.NICEST); columnTex.setMipMapMode(Texture.BASE_LEVEL); columnTex.setEnable(true); } texLoader = new TextureLoader("flooring.jpg", this); Texture groundTex = texLoader.getTexture(); if (groundTex == null) System.err.println("Cannot load flooring.jpg texture"); else { groundTex.setBoundaryModeS(Texture.WRAP); groundTex.setBoundaryModeT(Texture.WRAP); groundTex.setMinFilter(Texture.NICEST); groundTex.setMagFilter(Texture.NICEST); groundTex.setMipMapMode(Texture.BASE_LEVEL); groundTex.setEnable(true); } // // Build several columns on the floor // if (debug) System.err.println(" columns..."); SharedGroup column = new SharedGroup(); Appearance columnApp = new Appearance(); Material columnMat = new Material(); columnMat.setAmbientColor(0.6f, 0.6f, 0.6f); columnMat.setDiffuseColor(1.0f, 1.0f, 1.0f); columnMat.setSpecularColor(0.0f, 0.0f, 0.0f); columnApp.setMaterial(columnMat); TextureAttributes columnTexAtt = new TextureAttributes(); columnTexAtt.setTextureMode(TextureAttributes.MODULATE); columnTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); columnApp.setTextureAttributes(columnTexAtt); if (columnTex != null) columnApp.setTexture(columnTex); GothicColumn columnShape = new GothicColumn(1.8f, // height 0.25f, // radius GothicColumn.BUILD_TOP, // flags columnApp); // appearance column.addChild(columnShape); Vector3f trans = new Vector3f(); Transform3D tr = new Transform3D(); TransformGroup tg; // Left trans.set(-2.0f, -1.0f, 0.0f); tr.set(trans); tg = new TransformGroup(tr); tg.addChild(new Link(column)); scene.addChild(tg); // Middle trans.set(0.0f, -1.0f, 0.0f); tr.set(trans); tg = new TransformGroup(tr); tg.addChild(new Link(column)); scene.addChild(tg); // Right trans.set(2.0f, -1.0f, 0.0f); tr.set(trans); tg = new TransformGroup(tr); tg.addChild(new Link(column)); scene.addChild(tg); // // Add the ground // if (debug) System.err.println(" ground..."); Appearance groundApp = new Appearance(); Material groundMat = new Material(); groundMat.setAmbientColor(0.6f, 0.6f, 0.6f); groundMat.setDiffuseColor(1.0f, 1.0f, 1.0f); groundMat.setSpecularColor(0.0f, 0.0f, 0.0f); groundApp.setMaterial(groundMat); tr = new Transform3D(); tr.setScale(new Vector3d(4.0, 4.0, 1.0)); TextureAttributes groundTexAtt = new TextureAttributes(); groundTexAtt.setTextureMode(TextureAttributes.MODULATE); groundTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); groundTexAtt.setTextureTransform(tr); groundApp.setTextureAttributes(groundTexAtt); if (groundTex != null) groundApp.setTexture(groundTex); ElevationGrid ground = new ElevationGrid(11, // X dimension 11, // Z dimension 2.0f, // X spacing 2.0f, // Z spacing // Automatically use zero heights groundApp); // Appearance trans.set(0.0f, -1.0f, 0.0f); tr.set(trans); tg = new TransformGroup(tr); tg.addChild(ground); scene.addChild(tg); // Add a light BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center 1000.0); // Extent DirectionalLight light = new DirectionalLight(); light.setEnable(true); light.setColor(new Color3f(1.0f, 1.0f, 1.0f)); light.setDirection(new Vector3f(0.5f, -1.0f, -0.5f)); light.setInfluencingBounds(worldBounds); scene.addChild(light); return scene; }
From source file:ExBackgroundImage.java
public TowerScene(Component observer) { BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center 1000.0); // Extent // Add a few lights AmbientLight ambient = new AmbientLight(); ambient.setEnable(true);//from www .ja v a2 s . co m ambient.setColor(new Color3f(0.2f, 0.2f, 0.2f)); ambient.setInfluencingBounds(worldBounds); addChild(ambient); DirectionalLight dir1 = new DirectionalLight(); dir1.setEnable(true); dir1.setColor(new Color3f(1.0f, 0.15f, 0.15f)); dir1.setDirection(new Vector3f(0.8f, -0.35f, -0.5f)); dir1.setInfluencingBounds(worldBounds); addChild(dir1); DirectionalLight dir2 = new DirectionalLight(); dir2.setEnable(true); dir2.setColor(new Color3f(0.15f, 0.15f, 1.0f)); dir2.setDirection(new Vector3f(-0.7f, -0.35f, 0.5f)); dir2.setInfluencingBounds(worldBounds); addChild(dir2); // Load textures TextureLoader texLoader = new TextureLoader("moon5.jpg", observer); Texture moon = texLoader.getTexture(); if (moon == null) System.err.println("Cannot load moon5.jpg texture"); else { moon.setBoundaryModeS(Texture.WRAP); moon.setBoundaryModeT(Texture.WRAP); moon.setMinFilter(Texture.NICEST); moon.setMagFilter(Texture.NICEST); moon.setMipMapMode(Texture.BASE_LEVEL); moon.setEnable(true); } texLoader = new TextureLoader("stonebrk2.jpg", observer); Texture stone = texLoader.getTexture(); if (stone == null) System.err.println("Cannot load stonebrk2.jpg texture"); else { stone.setBoundaryModeS(Texture.WRAP); stone.setBoundaryModeT(Texture.WRAP); stone.setMinFilter(Texture.NICEST); stone.setMagFilter(Texture.NICEST); stone.setMipMapMode(Texture.BASE_LEVEL); stone.setEnable(true); } // // Build a rough terrain // Appearance moonApp = new Appearance(); Material moonMat = new Material(); moonMat.setAmbientColor(0.5f, 0.5f, 0.5f); moonMat.setDiffuseColor(1.0f, 1.0f, 1.0f); moonMat.setSpecularColor(0.0f, 0.0f, 0.0f); moonApp.setMaterial(moonMat); TextureAttributes moonTexAtt = new TextureAttributes(); moonTexAtt.setTextureMode(TextureAttributes.MODULATE); moonTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); moonApp.setTextureAttributes(moonTexAtt); if (moon != null) moonApp.setTexture(moon); CraterGrid grid = new CraterGrid(50, 50, // grid dimensions 1.0, 1.0, // grid spacing 4.0, // height exageration factor craters, // grid elevations moonApp); // grid appearance addChild(grid); // // Build several towers on the terrain // SharedGroup tower = new SharedGroup(); Appearance towerApp = new Appearance(); Material towerMat = new Material(); towerMat.setAmbientColor(0.6f, 0.6f, 0.6f); towerMat.setDiffuseColor(1.0f, 1.0f, 1.0f); towerMat.setSpecularColor(0.0f, 0.0f, 0.0f); towerApp.setMaterial(towerMat); Transform3D tr = new Transform3D(); tr.setScale(new Vector3d(4.0, 4.0, 1.0)); TextureAttributes towerTexAtt = new TextureAttributes(); towerTexAtt.setTextureMode(TextureAttributes.MODULATE); towerTexAtt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); towerTexAtt.setTextureTransform(tr); towerApp.setTextureAttributes(towerTexAtt); if (stone != null) towerApp.setTexture(stone); Arch towerShape = new Arch(0.0, // start Phi 1.571, // end Phi 2, // nPhi 0.0, // start Theta Math.PI * 2.0, // end Theta 5, // nTheta 3.0, // start radius 8.0, // end radius 0.0, // start phi thickness 0.0, // end phi thickness towerApp); // appearance tower.addChild(towerShape); // Place towers Matrix3f rot = new Matrix3f(); rot.setIdentity(); TransformGroup tg = new TransformGroup(new Transform3D(rot, new Vector3d(2.0, -3.0, -8.0), 1.0)); tg.addChild(new Link(tower)); addChild(tg); tg = new TransformGroup(new Transform3D(rot, new Vector3d(-1.0, -3.0, -6.0), 0.5)); tg.addChild(new Link(tower)); addChild(tg); tg = new TransformGroup(new Transform3D(rot, new Vector3d(5.0, -3.0, -6.0), 0.75)); tg.addChild(new Link(tower)); addChild(tg); tg = new TransformGroup(new Transform3D(rot, new Vector3d(1.0, -3.0, -3.0), 0.35)); tg.addChild(new Link(tower)); addChild(tg); }