List of usage examples for javax.media.j3d Switch Switch
public Switch()
From source file:PointTest.java
protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 14000, 0, 0, 0, 0, 0); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f); rotator.setSchedulingBounds(getApplicationBounds()); objTrans.addChild(rotator);//from w ww . j a va 2 s . c o m Switch switchGroup = new Switch(); switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE); switchGroup.addChild(createPoints(1, 5, false)); switchGroup.addChild(createPoints(1, 5, true)); switchGroup.addChild(createPoints(8, 10, false)); switchGroup.addChild(createPoints(8, 10, true)); switchGroup.addChild(createPoints(2, 5, false)); switchGroup.addChild(createPoints(2, 5, true)); switchGroup.addChild(createPoints(2, 20, false)); switchGroup.addChild(createPoints(2, 20, true)); // create a SwitchValueInterpolator to // cycle through the child nodes in the Switch Node Alpha switchAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 15000, 0, 0, 0, 0, 0); SwitchValueInterpolator switchInterpolator = new SwitchValueInterpolator(switchAlpha, switchGroup); switchInterpolator.setSchedulingBounds(getApplicationBounds()); switchInterpolator.setEnable(true); // WARNING: do not add the SwitchValueInterpolator to the Switch Node! objRoot.addChild(switchInterpolator); objTrans.addChild(switchGroup); objRoot.addChild(objTrans); return objRoot; }
From source file:ExTransform.java
public Group buildScene() { // Turn on the headlight setHeadlightEnable(true);/*from www . j a va 2s .co m*/ // Build the scene root switchGroup = new Switch(); switchGroup.setCapability(Switch.ALLOW_SWITCH_WRITE); // Create application bounds BoundingSphere worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center 1000.0); // Extent Transform3D t3d; Appearance app = new Appearance(); Material mat = new Material(); mat.setAmbientColor(0.2f, 0.8f, 0.4f); mat.setDiffuseColor(0.2f, 0.8f, 0.4f); mat.setSpecularColor(0.0f, 0.0f, 0.f); app.setMaterial(mat); // Build the 3D object: Box box = new Box(3.0f, 2.0f, 1.0f, app); // Build the shared object: sharedObject = new SharedGroup(); sharedObject.addChild(box); // Build 4 separate transforms: Transform3D id = new Transform3D(); TransformGroup idGroup = new TransformGroup(id); idGroup.addChild(new Link(sharedObject)); switchGroup.addChild(idGroup); Transform3D rot = new Transform3D(); rot.set(new AxisAngle4d(0., 1., 0., Math.PI / 4.)); TransformGroup rotGroup = new TransformGroup(rot); rotGroup.addChild(new Link(sharedObject)); switchGroup.addChild(rotGroup); Transform3D trans = new Transform3D(); trans.set(new Vector3d(2., 0., 0.)); TransformGroup transGroup = new TransformGroup(trans); transGroup.addChild(new Link(sharedObject)); switchGroup.addChild(transGroup); Transform3D scale = new Transform3D(); scale.set(2.0); TransformGroup scaleGroup = new TransformGroup(scale); scaleGroup.addChild(new Link(sharedObject)); switchGroup.addChild(scaleGroup); switchGroup.setWhichChild(options[currentSwitch].child); return switchGroup; }
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 o m 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:SimpleLOD.java
/** * Build the content branch for the scene graph This creates three * cylinders, each with a different resolution. These are then used with a * LOD node to implement a crude level of detail. * // w w w . j a v a2s . com * @return BranchGroup that is the root of the content */ protected BranchGroup buildContentBranch() { //Create the appearance Appearance app = new Appearance(); Color3f ambientColour = new Color3f(1.0f, 1.0f, 0.0f); Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); Color3f diffuseColour = new Color3f(1.0f, 1.0f, 0.0f); float shininess = 20.0f; app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess)); //Make the switch node that is to used with the LOD //and make it writable Switch LODswitch = new Switch(); LODswitch.setCapability(Switch.ALLOW_SWITCH_WRITE); //Add the three cylinders LODswitch.addChild(new Cylinder(1.0f, 1.0f, Cylinder.GENERATE_NORMALS, 10, 10, app)); LODswitch.addChild(new Cylinder(1.0f, 1.0f, Cylinder.GENERATE_NORMALS, 5, 5, app)); LODswitch.addChild(new Cylinder(1.0f, 1.0f, Cylinder.GENERATE_NORMALS, 3, 3, app)); //Define the distances for the LOD float[] LODdistances = { 5.0f, 10.0f, 15.0f }; DistanceLOD myLOD = new DistanceLOD(LODdistances, new Point3f(0.0f, 0.0f, 0.0f)); myLOD.setSchedulingBounds(bounds); //Add the switch to the LOD myLOD.addSwitch(LODswitch); BranchGroup contentBranch = new BranchGroup(); contentBranch.addChild(myLOD); addLights(contentBranch); contentBranch.addChild(LODswitch); return contentBranch; }
From source file:InterpolatorTest.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); // create the Appearance for the Shape3D Appearance app = new Appearance(); // create a Material, modified by the ColorInterpolator Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Material mat = new Material(objColor, black, objColor, black, 80.0f); mat.setCapability(Material.ALLOW_COMPONENT_WRITE); app.setMaterial(mat);/*from w w w.j a va 2 s .c o m*/ // create a TransparencyAttributes, modified by the // TransparencyInterpolator TransparencyAttributes transparency = new TransparencyAttributes(); transparency.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE); transparency.setTransparencyMode(TransparencyAttributes.NICEST); app.setTransparencyAttributes(transparency); // create a Switch Node and set capabilities Switch switchNode = new Switch(); switchNode.setCapability(Switch.ALLOW_SWITCH_WRITE); // create a Alpha object for the Interpolators Alpha alpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 500, 100, 5000, 2000, 1000, 5000, 2000, 500); // add each BG and Interpolator as a child of the Switch Node TransformGroup tg = createSharedGroup(app); switchNode.addChild(createBranchGroup(tg, new ColorInterpolator(alpha, app.getMaterial()))); tg = createSharedGroup(app); switchNode.addChild(createBranchGroup(tg, new PositionInterpolator(alpha, tg))); tg = createSharedGroup(app); switchNode.addChild(createBranchGroup(tg, new RotationInterpolator(alpha, tg))); tg = createSharedGroup(app); switchNode.addChild(createBranchGroup(tg, new ScaleInterpolator(alpha, tg))); tg = createSharedGroup(app); switchNode.addChild(createBranchGroup(tg, new TransparencyInterpolator(alpha, app.getTransparencyAttributes(), 0, 0.8f))); // define the data for the RotPosScalePathInterpolator float[] knots = { 0.0f, 0.1f, 0.2f, 0.3f, 0.4f, 0.6f, 0.8f, 0.9f, 1.0f }; float[] scales = { 0.2f, 0.5f, 0.8f, 2.3f, 5.4f, 0.6f, 0.4f, 0.2f, 0.1f }; Quat4f[] quats = new Quat4f[9]; Point3f[] positions = new Point3f[9]; quats[0] = new Quat4f(0.3f, 1.0f, 1.0f, 0.0f); quats[1] = new Quat4f(1.0f, 0.0f, 0.0f, 0.3f); quats[2] = new Quat4f(0.2f, 1.0f, 0.0f, 0.0f); quats[3] = new Quat4f(0.0f, 0.2f, 1.0f, 0.0f); quats[4] = new Quat4f(1.0f, 0.0f, 0.4f, 0.0f); quats[5] = new Quat4f(0.0f, 1.0f, 1.0f, 0.2f); quats[6] = new Quat4f(0.3f, 0.3f, 0.0f, 0.0f); quats[7] = new Quat4f(1.0f, 0.0f, 1.0f, 1.0f); quats[8] = quats[0]; positions[0] = new Point3f(0.0f, 0.0f, -1.0f); positions[1] = new Point3f(1.0f, -2.0f, -2.0f); positions[2] = new Point3f(-2.0f, 2.0f, -3.0f); positions[3] = new Point3f(1.0f, 1.0f, -4.0f); positions[4] = new Point3f(-4.0f, -2.0f, -5.0f); positions[5] = new Point3f(2.0f, 0.3f, -6.0f); positions[6] = new Point3f(-4.0f, 0.5f, -7.0f); positions[7] = new Point3f(0.0f, -1.5f, -4.0f); positions[8] = positions[0]; tg = createSharedGroup(app); // create the Interpolator RotPosScalePathInterpolator rotPosScalePathInterplator = new RotPosScalePathInterpolator(alpha, tg, new Transform3D(), knots, quats, positions, scales); // add a BG for the Interpolator switchNode.addChild(createBranchGroup(tg, rotPosScalePathInterplator)); // create a RandomAlpha object to control a SwitchInterpolator // to set the Switches active child node randomly RandomAlpha randomAlpha = new RandomAlpha(); // create the interpolator SwitchValueInterpolator switchInterpolator = new SwitchValueInterpolator(randomAlpha, switchNode); switchInterpolator.setSchedulingBounds(getApplicationBounds()); // connect the scenegraph objTrans.addChild(switchNode); objTrans.addChild(switchInterpolator); // 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(getApplicationBounds()); DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1); lgt1.setInfluencingBounds(getApplicationBounds()); // add the lights objRoot.addChild(aLgt); objRoot.addChild(lgt1); // connect objRoot.addChild(objTrans); return objRoot; }
From source file:ExDepthCue.java
public Group buildScene() { // Get the current color Color3f color = (Color3f) colors[currentColor].value; // Turn off the example headlight setHeadlightEnable(false);/*from ww w . j a v a2 s. co m*/ // Create the scene group Group scene = new Group(); // Create a switch group to hold the fog node. This enables // us to turn the fog node on and off via the GUI. fogSwitch = new Switch(); fogSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); // Create influencing bounds Bounds worldBounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), // Center 1000.0); // Extent // Set the fog color, density, and its influencing bounds fog = new LinearFog(); fog.setColor(color); // front and back distances set below fog.setCapability(Fog.ALLOW_COLOR_WRITE); fog.setCapability(Fog.ALLOW_INFLUENCING_BOUNDS_WRITE); fog.setInfluencingBounds(worldBounds); fogSwitch.addChild(fog); scene.addChild(fogSwitch); if (depthCueOnOff) fogSwitch.setWhichChild(0); // on else fogSwitch.setWhichChild(Switch.CHILD_NONE); // off // Set the background color and its application bounds // Usually, the background color should match the fog color // or the results look odd. background = new Background(); background.setColor(color); background.setApplicationBounds(worldBounds); background.setCapability(Background.ALLOW_COLOR_WRITE); scene.addChild(background); // Build foreground geometry Group content = buildIsoline(); scene.addChild(content); // Automatically compute good front and back distances for // fog to get good depth-cueing. To do this, first get the // dimensions of the bounds around the content. Then, // set the front distance to be at the center of the content // (or closer by a tad) and the back distance at the front // distance PLUS half the depth of the content's bounding box BoundingSphere sampleSphere = new BoundingSphere(); BoundingBox sampleBox = new BoundingBox(); Bounds bounds = content.getBounds(); double deltaDistance = 0.0; double centerZ = 0.0; if (bounds == null) { // No bounds available. Estimate the values knowing // that the above content is what it is. centerZ = 0.5; // 0.5 closer than true center deltaDistance = 2.0; } else if (bounds.getClass() == sampleSphere.getClass()) { BoundingSphere sphereBounds = (BoundingSphere) bounds; deltaDistance = Math.abs(sphereBounds.getRadius()); Point3d center = new Point3d(); sphereBounds.getCenter(center); centerZ = center.z + 0.5; // 0.5 closer than true center } else if (bounds.getClass() == sampleBox.getClass()) { BoundingBox boxBounds = (BoundingBox) bounds; Point3d p1 = new Point3d(); Point3d p2 = new Point3d(); boxBounds.getLower(p1); boxBounds.getUpper(p2); deltaDistance = p2.z - p1.z; if (deltaDistance < 0.0) deltaDistance *= -1.0; if (p1.z > p2.z) centerZ = p1.z - deltaDistance / 2.0; else centerZ = p2.z - deltaDistance / 2.0; centerZ += 0.5; // 0.5 closer than true center } else { System.err.println("Unknown bounds type"); } // Set front distance to the distance from the default // viewing position (0,0,10) to the center of the bounds. fog.setFrontDistance(10.0f - (float) centerZ); // Set back distance to the distance from the default // viewing position (0,0,10) to the back of the bounds. fog.setBackDistance(10.0f - (float) centerZ + (float) deltaDistance); return scene; }
From source file:ExSwitch.java
public Group buildScene() { // Turn on the example headlight setHeadlightEnable(true);/* w w w.j av a 2 s .c o m*/ // 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:LightBug.java
void setupLights() { // set up the BoundingSphere for all the lights BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0); // create the switch and set the default lightSwitch = new Switch(); lightSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); lightSwitch.setWhichChild(LIGHT_DIRECTIONAL); // Set up the directional light Vector3f lightDirection = new Vector3f(0.0f, 0.0f, -1.0f); lightDirectional = new DirectionalLight(white, lightDirection); lightDirectional.setInfluencingBounds(bounds); lightSwitch.addChild(lightDirectional); // Set up the point light Point3f lightPosition = new Point3f(-1.0f, 1.0f, 0.6f); Point3f lightAttenuation = new Point3f(0.0f, 1.0f, 0.0f); lightPoint = new PointLight(white, lightPosition, lightAttenuation); lightPoint.setInfluencingBounds(bounds); lightSwitch.addChild(lightPoint);/* w w w . ja va2 s. co m*/ // Set up the spot light // Point the light back at the origin Vector3f lightSpotDirection = new Vector3f(lightPosition); lightSpotDirection.negate(); // point back lightSpotDirection.normalize(); // make unit length float spreadAngle = 60; // degrees float concentration = 5.0f; lightSpot = new SpotLight(white, lightPosition, lightAttenuation, lightSpotDirection, (float) Math.toRadians(spreadAngle), concentration); lightSpot.setInfluencingBounds(bounds); lightSwitch.addChild(lightSpot); }
From source file:ExSpotLight.java
private Group buildArrows() { // Create a switch group to hold the different arrow fan // spread angle choices. Enable child choice writing. arrowSpreadAngleSwitch = new Switch(); arrowSpreadAngleSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); // Create a set of arrow fans, one per spread angle // shown on the menu. AnnotationArrowFan af = null;//from www .j ava 2 s .com float spread = 0.0f; for (int i = 0; i < spreads.length; i++) { spread = ((Double) spreads[i].value).floatValue(); af = new AnnotationArrowFan(0.0f, 0.0f, 0.0f, // center position 2.5f, // arrow length -spread, // start angle spread, // end angle 5); // number of arrows arrowSpreadAngleSwitch.addChild(af); } // Select the current fan. arrowSpreadAngleSwitch.setWhichChild(currentSpread); // Create an outer transform group used to change the fan // position. Enable writing of its transform. arrowPositionTransformGroup = new TransformGroup(); arrowPositionTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); // Create a set of Transform3Ds for the different arrow positions. arrowPositionTransforms = new Transform3D[positions.length]; Point3f pos; Vector3f v = new Vector3f(); for (int i = 0; i < positions.length; i++) { // Create a Transform3D, setting its translation. arrowPositionTransforms[i] = new Transform3D(); pos = (Point3f) positions[i].value; v.set(pos); arrowPositionTransforms[i].setTranslation(v); } // Set the initial transform to be the current position arrowPositionTransformGroup.setTransform(arrowPositionTransforms[currentPosition]); // Create an inner transform group surrounding the arrows, // used to set the aim direction. Enable writing of its transform. arrowDirectionTransformGroup = new TransformGroup(); arrowDirectionTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); // Add the switch group to the direction-change transform group, // and add the direction-change transform group to the // position-change transform gorup. arrowDirectionTransformGroup.addChild(arrowSpreadAngleSwitch); arrowPositionTransformGroup.addChild(arrowDirectionTransformGroup); // Create a set of Transform3Ds for the different // arrow directions. arrowDirectionTransforms = new Transform3D[directions.length]; Vector3f dir = new Vector3f(); Vector3f positiveX = new Vector3f(1.0f, 0.0f, 0.0f); Vector3f axis = new Vector3f(); float angle; float dot; for (int i = 0; i < directions.length; i++) { // Normalize the direction vector dir.normalize((Vector3f) directions[i].value); // Cross the direction vector with the arrow's // +X aim direction to get a vector orthogonal // to both. This is the rotation axis. axis.cross(positiveX, dir); if (axis.x == 0.0f && axis.y == 0.0f && axis.z == 0.0f) { // New direction is parallel to current // arrow direction. Default to a Y axis. axis.y = 1.0f; } // Compute the angle between the direction and +X // vectors, where: // // cos(angle) = (dir dot positiveX) // ------------------------------- // (positiveX.length * dir.length) // // but since positiveX is normalized (as created // above) and dir has been normalized, both have // a length of 1. So, the angle between the // vectors is: // // angle = arccos(dir dot positiveX) dot = dir.dot(positiveX); angle = (float) Math.acos(dot); // Create a Transform3D, setting its rotation using // an AxisAngle4f, which takes an XYZ rotation vector // and an angle to rotate by around that vector. arrowDirectionTransforms[i] = new Transform3D(); arrowDirectionTransforms[i].setRotation(new AxisAngle4f(axis.x, axis.y, axis.z, angle)); } // Set the initial transform to be the current aim direction. arrowDirectionTransformGroup.setTransform(arrowDirectionTransforms[currentDirection]); return arrowPositionTransformGroup; }
From source file:ExBluePrint.java
private Group buildGadget() { if (debug)/*from w w w . j av a2 s . co m*/ System.err.println(" gadget..."); // // Create two appearances: // wireframeApp: draw as blue wireframe // shadedApp: draw as metalic shaded polygons // // Wireframe: // no Material - defaults to coloring attributes color // polygons as lines, with backfaces // thick lines Appearance wireframeApp = new Appearance(); ColoringAttributes wireframeCatt = new ColoringAttributes(); wireframeCatt.setColor(0.0f, 0.2559f, 0.4213f); wireframeCatt.setShadeModel(ColoringAttributes.SHADE_FLAT); wireframeApp.setColoringAttributes(wireframeCatt); PolygonAttributes wireframePatt = new PolygonAttributes(); wireframePatt.setPolygonMode(PolygonAttributes.POLYGON_LINE); wireframePatt.setCullFace(PolygonAttributes.CULL_NONE); wireframeApp.setPolygonAttributes(wireframePatt); LineAttributes wireframeLatt = new LineAttributes(); wireframeLatt.setLineWidth(2.0f); wireframeApp.setLineAttributes(wireframeLatt); // Shaded: // silver material Appearance shadedApp = new Appearance(); Material shadedMat = new Material(); shadedMat.setAmbientColor(0.30f, 0.30f, 0.30f); shadedMat.setDiffuseColor(0.30f, 0.30f, 0.50f); shadedMat.setSpecularColor(0.60f, 0.60f, 0.80f); shadedMat.setShininess(0.10f); shadedApp.setMaterial(shadedMat); ColoringAttributes shadedCatt = new ColoringAttributes(); shadedCatt.setShadeModel(ColoringAttributes.SHADE_GOURAUD); shadedApp.setColoringAttributes(shadedCatt); // // Create a switch group to hold two versions of the // shape: one wireframe, and one shaded // Transform3D tr = new Transform3D(); tr.set(new Vector3f(-1.0f, 0.2f, 0.0f)); TransformGroup gadget = new TransformGroup(tr); shadingSwitch = new Switch(); shadingSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE); Group wireframe = new Group(); Group shaded = new Group(); shadingSwitch.addChild(wireframe); shadingSwitch.addChild(shaded); shadingSwitch.setWhichChild(1); // shaded gadget.addChild(shadingSwitch); // // Build a gear (wireframe and shaded) // tr = new Transform3D(); tr.rotY(Math.PI / 2.0); TransformGroup tg = new TransformGroup(tr); SpurGear gear = new SpurGearThinBody(24, // tooth count 1.6f, // pitch circle radius 0.3f, // shaft radius 0.08f, // addendum 0.05f, // dedendum 0.3f, // gear thickness 0.28f, // tooth tip thickness wireframeApp);// appearance tg.addChild(gear); wireframe.addChild(tg); tg = new TransformGroup(tr); gear = new SpurGearThinBody(24, // tooth count 1.6f, // pitch circle radius 0.3f, // shaft radius 0.08f, // addendum 0.05f, // dedendum 0.3f, // gear thickness 0.28f, // tooth tip thickness shadedApp); // appearance tg.addChild(gear); shaded.addChild(tg); // // Build another gear (wireframe and shaded) // tr.rotY(Math.PI / 2.0); Vector3f trans = new Vector3f(-0.5f, 0.0f, 0.0f); tr.setTranslation(trans); tg = new TransformGroup(tr); gear = new SpurGearThinBody(30, // tooth count 2.0f, // pitch circle radius 0.3f, // shaft radius 0.08f, // addendum 0.05f, // dedendum 0.3f, // gear thickness 0.28f, // tooth tip thickness wireframeApp);// appearance tg.addChild(gear); wireframe.addChild(tg); tg = new TransformGroup(tr); gear = new SpurGearThinBody(30, // tooth count 2.0f, // pitch circle radius 0.3f, // shaft radius 0.08f, // addendum 0.05f, // dedendum 0.3f, // gear thickness 0.28f, // tooth tip thickness shadedApp); // appearance tg.addChild(gear); shaded.addChild(tg); // // Build a cylindrical shaft (wireframe and shaded) // tr.rotZ(-Math.PI / 2.0); trans = new Vector3f(1.0f, 0.0f, 0.0f); tr.setTranslation(trans); tg = new TransformGroup(tr); Cylinder cyl = new Cylinder(0.3f, // radius 4.0f, // length Primitive.GENERATE_NORMALS, // format 16, // radial resolution 1, // length-wise resolution wireframeApp);// appearance tg.addChild(cyl); wireframe.addChild(tg); tg = new TransformGroup(tr); cyl = new Cylinder(0.3f, // radius 4.0f, // length Primitive.GENERATE_NORMALS, // format 16, // radial resolution 1, // length-wise resolution shadedApp); // appearance tg.addChild(cyl); shaded.addChild(tg); // // Build shaft teeth (wireframe and shaded) // tr.rotY(Math.PI / 2.0); trans = new Vector3f(2.05f, 0.0f, 0.0f); tr.setTranslation(trans); tg = new TransformGroup(tr); gear = new SpurGear(12, // tooth count 0.5f, // pitch circle radius 0.3f, // shaft radius 0.05f, // addendum 0.05f, // dedendum 1.5f, // gear thickness 0.8f, // tooth tip thickness wireframeApp);// appearance tg.addChild(gear); wireframe.addChild(tg); tg = new TransformGroup(tr); gear = new SpurGear(12, // tooth count 0.5f, // pitch circle radius 0.3f, // shaft radius 0.05f, // addendum 0.05f, // dedendum 1.5f, // gear thickness 0.8f, // tooth tip thickness shadedApp); // appearance tg.addChild(gear); shaded.addChild(tg); return gadget; }