List of usage examples for javax.media.j3d Appearance setMaterial
public void setMaterial(Material material)
From source file:Text3DTest.java
TransformGroup createText3D(TornadoRotation rotator, String szText, int nSize, float scale, float trans, int nPath) { TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(scale);//from w w w.j ava2s. c om t3d.setTranslation(new Vector3d(0.0, trans, -10.0)); tg.setTransform(t3d); // use a customized FontExtrusion object to control the depth of the // text double X1 = 0; double Y1 = 0; double X2 = 3; double Y2 = 0; Shape extrusionShape = new java.awt.geom.Line2D.Double(X1, Y1, X2, Y2); FontExtrusion fontEx = new FontExtrusion(extrusionShape); Font3D f3d = new Font3D(new Font("TimesRoman", Font.PLAIN, nSize), fontEx); TornadoText3D text3D = new TornadoText3D(f3d, szText, new Point3f(0.0f, 0.0f, 0.0f), Text3D.ALIGN_CENTER, nPath); rotator.addTornadoText3D(text3D); // create an appearance Color3f black = new Color3f(0.1f, 0.1f, 0.1f); Color3f objColor = new Color3f(0.2f, 0.2f, 0.2f); Appearance app = new Appearance(); app.setMaterial(new Material(objColor, black, objColor, black, 90.0f)); // render as a wireframe PolygonAttributes polyAttrbutes = new PolygonAttributes(); polyAttrbutes.setPolygonMode(PolygonAttributes.POLYGON_LINE); polyAttrbutes.setCullFace(PolygonAttributes.CULL_NONE); app.setPolygonAttributes(polyAttrbutes); tg.addChild(new Shape3D(text3D, app)); return tg; }
From source file:SimpleCombine.java
/** * This defines the shapes used in the scene. The function uses the utility * geometries sphere, box, cone and cylinder to build a simple scene. This * demonstrates the use of transformations to group and position items. * /*from w ww. j av a 2 s. co m*/ * @return Node that is the root of the shape hierarchy. */ protected Node buildShape() { //Create a root for the shapes in the scene BranchGroup theScene = new BranchGroup(); //Create an appearance for the ground Appearance groundApp = new Appearance(); Color3f groundColour = new Color3f(0.0f, 0.5f, 0.0f); Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); Color3f specularColour = new Color3f(0.5f, 0.5f, 0.5f); float shininess = 10.0f; groundApp.setMaterial(new Material(groundColour, emissiveColour, groundColour, specularColour, shininess)); //Create a box that will be the ground Box ground = new Box(100.0f, 0.1f, 100.0f, groundApp); //Create a transform and a transform group that //will position the ground Transform3D grndXfm = new Transform3D(); grndXfm.set(new Vector3f(0.0f, -1.0f, 0.0f)); TransformGroup grndXfmGrp = new TransformGroup(grndXfm); //Add the ground shape to the group grndXfmGrp.addChild(ground); //Add the ground group to the scene group theScene.addChild(grndXfmGrp); //Create an appearance for the wall of the house Appearance wallApp = new Appearance(); Color3f wallColour = new Color3f(0.5f, 0.5f, 0.5f); wallApp.setMaterial(new Material(wallColour, emissiveColour, wallColour, specularColour, shininess)); //Create a cylinder that is the wall of the house Cylinder walls = new Cylinder(1.0f, 1.0f, Primitive.GENERATE_NORMALS, wallApp); //Create a group that will be the root of the house TransformGroup house = new TransformGroup(); //Add the walls to the house group house.addChild(walls); //Create an appearance for the roof Appearance roofApp = new Appearance(); Color3f roofColour = new Color3f(0.5f, 0.0f, 0.0f); roofApp.setMaterial(new Material(roofColour, emissiveColour, roofColour, specularColour, shininess)); //Create a cone that will be the roof Cone myRoof = new Cone(1.0f, 1.0f, Primitive.GENERATE_NORMALS, roofApp); //Create the transform and transform group that will position the //roof on the house Transform3D roofXfm = new Transform3D(); roofXfm.set(new Vector3f(0.0f, 1.0f, 0.0f)); TransformGroup roofXfmGrp = new TransformGroup(roofXfm); //Add the roof to the roof transform group roofXfmGrp.addChild(myRoof); //Add the roof group to the house house.addChild(roofXfmGrp); //Create an appearance for the tree trunks Appearance trunkApp = new Appearance(); Color3f trunkColour = new Color3f(0.2f, 0.2f, 0.0f); trunkApp.setMaterial(new Material(trunkColour, emissiveColour, trunkColour, specularColour, shininess)); //Create an appearance for the tree leaves Appearance leafApp = new Appearance(); Color3f leafColour = new Color3f(0.0f, 0.2f, 0.0f); leafApp.setMaterial(new Material(leafColour, emissiveColour, leafColour, specularColour, shininess)); //Create a transform and transform group for the tree Transform3D treeXfm = new Transform3D(); treeXfm.set(new Vector3f(-2.0f, 0.0f, 0.5f)); TransformGroup treeXfmGrp = new TransformGroup(treeXfm); //Create a cylinder for the tree trunk Cylinder myTrunk = new Cylinder(0.1f, 1.0f, trunkApp); //Add the trunk to the tree group treeXfmGrp.addChild(myTrunk); //Create a transform and transform group for the tree leaves Transform3D leafXfm = new Transform3D(); leafXfm.set(new Vector3f(0.0f, 1.0f, 0.0f)); TransformGroup leafXfmGrp = new TransformGroup(leafXfm); //Create the leaves Sphere myLeaf = new Sphere(0.5f, leafApp); //Add the leaves to the leaf group leafXfmGrp.addChild(myLeaf); //Add the leaf group to the tree group treeXfmGrp.addChild(leafXfmGrp); //Create another tree Transform3D tree1Xfm = new Transform3D(); tree1Xfm.set(new Vector3f(1.4f, 0.0f, -0.5f)); TransformGroup tree1XfmGrp = new TransformGroup(tree1Xfm); Cylinder myTrunk1 = new Cylinder(0.1f, 1.0f, trunkApp); tree1XfmGrp.addChild(myTrunk1); Transform3D leaf1Xfm = new Transform3D(); leaf1Xfm.set(new Vector3f(0.0f, 1.0f, 0.0f)); TransformGroup leaf1XfmGrp = new TransformGroup(leaf1Xfm); Sphere myLeaf1 = new Sphere(0.5f, leafApp); leaf1XfmGrp.addChild(myLeaf1); tree1XfmGrp.addChild(leaf1XfmGrp); //Create the final tree Transform3D tree2Xfm = new Transform3D(); tree2Xfm.set(new Vector3f(1.2f, 0.0f, 1.0f)); TransformGroup tree2XfmGrp = new TransformGroup(tree2Xfm); Cylinder myTrunk2 = new Cylinder(0.1f, 1.0f, trunkApp); tree2XfmGrp.addChild(myTrunk2); Transform3D leaf2Xfm = new Transform3D(); leaf2Xfm.set(new Vector3f(0.0f, 1.0f, 0.0f)); TransformGroup leaf2XfmGrp = new TransformGroup(leaf2Xfm); Sphere myLeaf2 = new Sphere(0.5f, leafApp); leaf2XfmGrp.addChild(myLeaf2); tree2XfmGrp.addChild(leaf2XfmGrp); //Put the scene together by adding all the groups //to the scene group theScene.addChild(house); theScene.addChild(treeXfmGrp); theScene.addChild(tree1XfmGrp); theScene.addChild(tree2XfmGrp); return theScene; }
From source file:SimpleDirLight.java
/** * This build the content branch of our scene graph. It creates a transform * group so that the shape is slightly tilted to reveal its 3D shape. * //from www. jav a2 s . co m * @param shape * Node that represents the geometry for the content * @return BranchGroup that is the root of the content branch */ protected BranchGroup buildContentBranch() { BranchGroup contentBranch = new BranchGroup(); Transform3D rotateCube = new Transform3D(); rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0)); TransformGroup rotationGroup = new TransformGroup(rotateCube); contentBranch.addChild(rotationGroup); //Create a new appearance Appearance app = new Appearance(); //Create the colours for the material Color3f ambientColour = new Color3f(1.0f, 0.0f, 0.0f); Color3f diffuseColour = new Color3f(1.0f, 0.0f, 0.0f); Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); //Define the shininess float shininess = 20.0f; //Set the material of the appearance app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess)); //Create and add a new sphere using the appearance rotationGroup.addChild(new Sphere(2.0f, Sphere.GENERATE_NORMALS, 120, app)); //Use the addLights function to add the lights to the branch addLights(contentBranch); //Return the root of the content branch return contentBranch; }
From source file:BillboardTest.java
private TransformGroup createBillboard(String szText, Point3f locationPoint, int nMode, Point3f billboardPoint, BoundingSphere bounds) {// w ww. j a v a 2 s. c o m TransformGroup subTg = new TransformGroup(); subTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); Font3D f3d = new Font3D(new Font("SansSerif", Font.PLAIN, 10), new FontExtrusion()); Text3D label3D = new Text3D(f3d, szText, locationPoint); Appearance app = new Appearance(); Color3f black = new Color3f(0.1f, 0.1f, 0.1f); Color3f objColor = new Color3f(0.2f, 0.2f, 0.2f); app.setMaterial(new Material(objColor, black, objColor, black, 90.0f)); Shape3D sh = new Shape3D(label3D, app); subTg.addChild(sh); Billboard billboard = new Billboard(subTg, nMode, billboardPoint); billboard.setSchedulingBounds(bounds); subTg.addChild(billboard); return subTg; }
From source file:SimpleMorph.java
/** * Build the content branch for the scene graph * //w w w . j a v a 2 s . c o m * @return BranchGroup that is the root of the content */ protected BranchGroup buildContentBranch() { //Create the appearance object Appearance app = new Appearance(); Color3f ambientColour = new Color3f(1.0f, 0.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, 0.0f, 0.0f); float shininess = 20.0f; app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess)); //Make the cube key shape IndexedQuadArray indexedCube = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24); Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f), new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f), new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f), new Point3f(1.0f, -1.0f, -1.0f) }; Vector3f[] cubeNormals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) }; int cubeCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; int cubeNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; indexedCube.setCoordinates(0, cubeCoordinates); indexedCube.setNormals(0, cubeNormals); indexedCube.setCoordinateIndices(0, cubeCoordIndices); indexedCube.setNormalIndices(0, cubeNormalIndices); //Make the pyramid key shape. Although this needs //only five vertices to create the desired shape, we //need to use six vertices so that it has the same //number as the cube. IndexedQuadArray indexedPyramid = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24); Point3f[] pyramidCoordinates = { new Point3f(0.0f, 1.0f, 0.0f), new Point3f(0.0f, 1.0f, 0.0f), new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(0.0f, 1.0f, 0.0f), new Point3f(0.0f, 1.0f, 0.0f), new Point3f(-1.0f, -1.0f, -1.0f), new Point3f(1.0f, -1.0f, -1.0f) }; Vector3f[] pyramidNormals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) }; int pyramidCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; int pyramidNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; indexedPyramid.setCoordinates(0, pyramidCoordinates); indexedPyramid.setNormals(0, pyramidNormals); indexedPyramid.setCoordinateIndices(0, pyramidCoordIndices); indexedPyramid.setNormalIndices(0, pyramidNormalIndices); //Set the contents of the array to the two shapes GeometryArray[] theShapes = new GeometryArray[2]; theShapes[0] = indexedCube; theShapes[1] = indexedPyramid; BranchGroup contentBranch = new BranchGroup(); //Create a transform to rotate the shape slightly Transform3D rotateCube = new Transform3D(); rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0)); TransformGroup rotationGroup = new TransformGroup(rotateCube); contentBranch.addChild(rotationGroup); addLights(contentBranch); //Call the function to build the morph rotationGroup.addChild(createMorph(theShapes, app)); return contentBranch; }
From source file:PureImmediateStereo.java
public void run() { // Set up Graphics context gc = canvas.getGraphicsContext3D();//from w w w . j a v a2s . com // We always need to set this for PureImmediate // stereo mode gc.setBufferOverride(true); Color3f lightColor = new Color3f(1, 1, 1); Vector3f lightDir = new Vector3f(0, 0, -1); DirectionalLight light = new DirectionalLight(lightColor, lightDir); gc.addLight(light); Appearance redApp = new Appearance(); Appearance greenApp = new Appearance(); Color3f ambientColor = new Color3f(0, 0, 0); Color3f emissiveColor = new Color3f(0, 0, 0); Color3f diffuseColor = new Color3f(1, 0, 0); Color3f specularColor = new Color3f(1, 1, 1); redApp.setMaterial(new Material(ambientColor, emissiveColor, diffuseColor, specularColor, 5)); diffuseColor = new Color3f(0, 1, 0); greenApp.setMaterial(new Material(ambientColor, emissiveColor, diffuseColor, specularColor, 5)); // Set up geometry Cone leftCone = new Cone(0.4f, 0.6f, Primitive.GENERATE_NORMALS, redApp); Cone rightCone = new Cone(0.4f, 0.6f, Primitive.GENERATE_NORMALS, greenApp); leftConeBody = leftCone.getShape(Cone.BODY); leftConeCap = leftCone.getShape(Cone.CAP); rightConeBody = rightCone.getShape(Cone.BODY); rightConeCap = rightCone.getShape(Cone.CAP); leftTrans = new Vector3f(-0.6f, 0, 0); rightTrans = new Vector3f(0.6f, 0, 0); while (true) { // compute data which is can be used // for both left and right eye computeSharedData(); if (stereoSupport) { if (!sharedStereoZbuffer) { gc.setStereoMode(GraphicsContext3D.STEREO_BOTH); // This clear both left and right buffers, we // must set STEREO_BOTH before it. Otherwise // it only clear LEFT or RIGHT buffer unless // this is invoke twice for each buffer. gc.clear(); } gc.setStereoMode(GraphicsContext3D.STEREO_LEFT); renderLeft(); gc.setStereoMode(GraphicsContext3D.STEREO_RIGHT); renderRight(); } else { gc.clear(); renderLeft(); } // This swap both left and right buffers so // there is no need to set STEREO_BOTH before it canvas.swap(); // Be polite to other threads ! Thread.yield(); } }
From source file:SimpleSpotLights.java
/** * This build the content branch of our scene graph. It creates a transform * group so that the shape is slightly tilted to reveal its 3D shape. * //from w ww. j a va 2s. c o m * @param shape * Node that represents the geometry for the content * @return BranchGroup that is the root of the content branch */ protected BranchGroup buildContentBranch() { BranchGroup contentBranch = new BranchGroup(); Transform3D rotateCube = new Transform3D(); rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0)); TransformGroup rotationGroup = new TransformGroup(rotateCube); contentBranch.addChild(rotationGroup); //Create a new appearance Appearance app = new Appearance(); //Create the colours for the material Color3f ambientColour = new Color3f(1.0f, 1.0f, 1.0f); Color3f diffuseColour = new Color3f(1.0f, 1.0f, 1.0f); Color3f specularColour = new Color3f(1.0f, 1.0f, 1.0f); Color3f emissiveColour = new Color3f(0.0f, 0.0f, 0.0f); //Define the shininess float shininess = 20.0f; //Set the material of the appearance app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess)); //Create and add a new sphere using the appearance rotationGroup.addChild(new Sphere(2.0f, Sphere.GENERATE_NORMALS, 120, app)); //Use the addLights function to add the lights to the branch addLights(contentBranch); //Return the root of the content branch return contentBranch; }
From source file:AlternateAppearanceBoundsTest.java
BranchGroup createSceneGraph() { BranchGroup objRoot = new BranchGroup(); // Create an alternate appearance otherApp = new Appearance(); altMat = new Material(); altMat.setCapability(Material.ALLOW_COMPONENT_WRITE); altMat.setDiffuseColor(new Color3f(0.0f, 1.0f, 0.0f)); otherApp.setMaterial(altMat);/*from www.j a v a 2 s . com*/ altApp = new AlternateAppearance(); altApp.setAppearance(otherApp); altApp.setCapability(AlternateAppearance.ALLOW_BOUNDS_WRITE); altApp.setCapability(AlternateAppearance.ALLOW_INFLUENCING_BOUNDS_WRITE); altApp.setInfluencingBounds(worldBounds); objRoot.addChild(altApp); // Build foreground geometry Appearance app1 = new Appearance(); mat1 = new Material(); mat1.setCapability(Material.ALLOW_COMPONENT_WRITE); mat1.setDiffuseColor(new Color3f(1.0f, 0.0f, 0.0f)); app1.setMaterial(mat1); content1 = new SphereGroup(0.05f, // radius of spheres 0.15f, // x spacing 0.15f, // y spacing 5, // number of spheres in X 5, // number of spheres in Y app1, // appearance true); // alt app override = true objRoot.addChild(content1); shapes1 = ((SphereGroup) content1).getShapes(); // Add lights light1 = new DirectionalLight(); light1.setEnable(true); light1.setColor(new Color3f(0.2f, 0.2f, 0.2f)); light1.setDirection(new Vector3f(1.0f, 0.0f, -1.0f)); light1.setInfluencingBounds(worldBounds); light1.setCapability(DirectionalLight.ALLOW_INFLUENCING_BOUNDS_WRITE); light1.setCapability(DirectionalLight.ALLOW_BOUNDS_WRITE); objRoot.addChild(light1); // Add an ambient light to dimly illuminate the rest of // the shapes in the scene to help illustrate that the // directional lights are being scoped... otherwise it looks // like we're just removing shapes from the scene AmbientLight ambient = new AmbientLight(); ambient.setEnable(true); ambient.setColor(new Color3f(1.0f, 1.0f, 1.0f)); ambient.setInfluencingBounds(worldBounds); objRoot.addChild(ambient); // Define a bounding leaf leafBounds = new BoundingLeaf(allBounds[currentBounds]); leafBounds.setCapability(BoundingLeaf.ALLOW_REGION_WRITE); objRoot.addChild(leafBounds); if (boundingLeafOn) { altApp.setInfluencingBoundingLeaf(leafBounds); } else { altApp.setInfluencingBounds(allBounds[currentBounds]); } return objRoot; }
From source file:OrientedTest.java
public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); TransformGroup objScale = new TransformGroup(); Transform3D textMat = new Transform3D(); // Assuming uniform size chars, set scale to fit string in view textMat.setScale(1.2 / sl);/* w w w. java 2 s .co m*/ objScale.setTransform(textMat); // Create the transform group node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at runtime. Add it to the // root of the subgraph. TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(objTrans); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); Appearance apText = new Appearance(); Material m = new Material(); m.setLightingEnable(true); apText.setMaterial(m); Appearance apEarth = new Appearance(); Material mm = new Material(); mm.setLightingEnable(true); apEarth.setMaterial(mm); Appearance apStone = new Appearance(); apStone.setMaterial(mm); // create 3D text Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2), new FontExtrusion()); Text3D txt = new Text3D(f3d, textString, new Point3f(-sl / 2.0f, 3.0f, 0.0f)); OrientedShape3D textShape = new OrientedShape3D(); textShape.setGeometry(txt); textShape.setAppearance(apText); textShape.setAlignmentAxis(0.0f, 1.0f, 0.0f); objScale.addChild(textShape); // Create a simple shape leaf node, add it to the scene graph. Transform3D cubeMat = new Transform3D(); TransformGroup cubeTrans = new TransformGroup(cubeMat); cubeMat.set(new Vector3d(0.9, 0.0, -1.0)); cubeTrans.setTransform(cubeMat); cubeTrans.addChild(new ColorCube(0.3)); objTrans.addChild(cubeTrans); TextureLoader stoneTex = new TextureLoader(stoneImage, new String("RGB"), this); if (stoneTex != null) apStone.setTexture(stoneTex.getTexture()); TextureAttributes texAttr = new TextureAttributes(); texAttr.setTextureMode(TextureAttributes.MODULATE); apStone.setTextureAttributes(texAttr); Transform3D coneMat = new Transform3D(); TransformGroup coneTrans = new TransformGroup(coneMat); coneMat.set(new Vector3d(0.0, 0.0, 0.0)); coneTrans.setTransform(coneMat); coneTrans.addChild(new Cone(.2f, 0.8f, Cone.GENERATE_NORMALS | Cone.GENERATE_TEXTURE_COORDS, apStone)); objTrans.addChild(coneTrans); TextureLoader earthTex = new TextureLoader(earthImage, new String("RGB"), this); if (earthTex != null) apEarth.setTexture(earthTex.getTexture()); apEarth.setTextureAttributes(texAttr); Transform3D cylinderMat = new Transform3D(); TransformGroup cylinderTrans = new TransformGroup(cylinderMat); cylinderMat.set(new Vector3d(-0.9, 0.5, -1.0)); cylinderTrans.setTransform(cylinderMat); cylinderTrans.addChild( new Cylinder(.35f, 2.0f, Cylinder.GENERATE_NORMALS | Cylinder.GENERATE_TEXTURE_COORDS, apEarth)); objTrans.addChild(cylinderTrans); objTrans.addChild(objScale); // Set up the background Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f); Background bgNode = new Background(bgColor); bgNode.setApplicationBounds(bounds); objRoot.addChild(bgNode); // Set up the ambient light Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f); AmbientLight ambientLightNode = new AmbientLight(ambientColor); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); // Set up the directional lights Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f); Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f); Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f); DirectionalLight light1 = new DirectionalLight(light1Color, light1Direction); light1.setInfluencingBounds(bounds); objRoot.addChild(light1); DirectionalLight light2 = new DirectionalLight(light2Color, light2Direction); light2.setInfluencingBounds(bounds); objRoot.addChild(light2); apText.setMaterial(mm); // Have Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:SimpleMorph2.java
/** * Build the content branch for the scene graph * /*from w ww .j a va 2s . c o m*/ * @return BranchGroup that is the root of the content */ protected BranchGroup buildContentBranch() { Appearance app = new Appearance(); Color3f ambientColour = new Color3f(1.0f, 0.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, 0.0f, 0.0f); float shininess = 20.0f; app.setMaterial(new Material(ambientColour, emissiveColour, diffuseColour, specularColour, shininess)); //Make the cube key shape IndexedQuadArray indexedCube = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24); Point3f[] cubeCoordinates = { new Point3f(1.0f, 1.0f, 1.0f), new Point3f(-1.0f, 1.0f, 1.0f), new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(1.0f, 1.0f, -1.0f), new Point3f(-1.0f, 1.0f, -1.0f), new Point3f(-1.0f, -1.0f, -1.0f), new Point3f(1.0f, -1.0f, -1.0f) }; Vector3f[] cubeNormals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) }; int cubeCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; int cubeNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; indexedCube.setCoordinates(0, cubeCoordinates); indexedCube.setNormals(0, cubeNormals); indexedCube.setCoordinateIndices(0, cubeCoordIndices); indexedCube.setNormalIndices(0, cubeNormalIndices); //Make the pyramid key shape. Although this needs //only five vertices to create the desired shape, we //need to use six vertices so that it has the same //number as the cube. IndexedQuadArray indexedPyramid = new IndexedQuadArray(8, IndexedQuadArray.COORDINATES | IndexedQuadArray.NORMALS, 24); Point3f[] pyramidCoordinates = { new Point3f(0.0f, 1.0f, 0.0f), new Point3f(0.0f, 1.0f, 0.0f), new Point3f(-1.0f, -1.0f, 1.0f), new Point3f(1.0f, -1.0f, 1.0f), new Point3f(0.0f, 1.0f, 0.0f), new Point3f(0.0f, 1.0f, 0.0f), new Point3f(-1.0f, -1.0f, -1.0f), new Point3f(1.0f, -1.0f, -1.0f) }; Vector3f[] pyramidNormals = { new Vector3f(0.0f, 0.0f, 1.0f), new Vector3f(0.0f, 0.0f, -1.0f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector3f(-1.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f), new Vector3f(0.0f, -1.0f, 0.0f) }; int pyramidCoordIndices[] = { 0, 1, 2, 3, 7, 6, 5, 4, 0, 3, 7, 4, 5, 6, 2, 1, 0, 4, 5, 1, 6, 7, 3, 2 }; int pyramidNormalIndices[] = { 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5 }; indexedPyramid.setCoordinates(0, pyramidCoordinates); indexedPyramid.setNormals(0, pyramidNormals); indexedPyramid.setCoordinateIndices(0, pyramidCoordIndices); indexedPyramid.setNormalIndices(0, pyramidNormalIndices); //Set the contents of the array to the two shapes GeometryArray[] theShapes = new GeometryArray[2]; theShapes[0] = indexedCube; theShapes[1] = indexedPyramid; BranchGroup contentBranch = new BranchGroup(); //Create a transform to rotate the shape slightly Transform3D rotateCube = new Transform3D(); rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI / 4.0)); TransformGroup rotationGroup = new TransformGroup(rotateCube); contentBranch.addChild(rotationGroup); addLights(contentBranch); //Call the function to build the morph rotationGroup.addChild(createMorph(theShapes, app)); //Add the behaviour to the scene graph to activate it rotationGroup.addChild(myBehave); return contentBranch; }