List of usage examples for javax.media.j3d Transform3D Transform3D
public Transform3D()
From source file:LightTest.java
protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); // create the 4 lights - the actual creation // and UI managment is delegated to an object // that "shadows" (no pun intended) the functionality // of the particular light createLight(new AmbientLightObject(), objRoot); createLight(new PointLightObject(), objRoot); createLight(new DirectionalLightObject(), objRoot); createLight(new SpotLightObject(), objRoot); // rotate some of the spheres in the scene 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, 4000, 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 w w . ja va 2 s . c o m // create a large sphere in the center of the // scene and the floor as staionary objects objRoot.addChild(createSphere(0, 0, 0, 2)); objRoot.addChild(createFloor()); // create a smaller sphere at the corners of a cube final int nCubeSize = 3; objTrans.addChild(createSphere(nCubeSize, nCubeSize, nCubeSize, 1)); objTrans.addChild(createSphere(nCubeSize, nCubeSize, -nCubeSize, 1)); objTrans.addChild(createSphere(nCubeSize, -nCubeSize, nCubeSize, 1)); objTrans.addChild(createSphere(nCubeSize, -nCubeSize, -nCubeSize, 1)); objTrans.addChild(createSphere(-nCubeSize, nCubeSize, nCubeSize, 1)); objTrans.addChild(createSphere(-nCubeSize, nCubeSize, -nCubeSize, 1)); objTrans.addChild(createSphere(-nCubeSize, -nCubeSize, nCubeSize, 1)); objTrans.addChild(createSphere(-nCubeSize, -nCubeSize, -nCubeSize, 1)); // add some small spheres here and there to // make things interesting objRoot.addChild(createSphere(-6, -6, 2, 1)); objRoot.addChild(createSphere(8, -5, 3, 1)); objRoot.addChild(createSphere(6, 7, -1, 1)); objRoot.addChild(createSphere(-5, 6, -3.5f, 0.5f)); objRoot.addChild(objTrans); return objRoot; }
From source file:SimpleTransform.java
/** * This processes the AWT events and performs the appropriate operations. * The exit button causes the program to terminate, the left button causes a * rotation to be applied to the shape's transformation to spin it to the * left and the right has the similar effect but to the right button. * // w w w. j ava2 s .c o m * @param e * ActionEvent that has been performed */ public void actionPerformed(ActionEvent e) { if (e.getSource() == exitButton) { dispose(); System.exit(0); } else if (e.getSource() == leftButton) { //Create a temporary transform Transform3D temp = new Transform3D(); //Read the transform from the shape rotationGroup.getTransform(temp); //Create a rotation that will be applied Transform3D tempDelta = new Transform3D(); tempDelta.rotY(-0.3); //Apply the rotation temp.mul(tempDelta); //Write the value back into the scene graph rotationGroup.setTransform(temp); } else if (e.getSource() == rightButton) { //Do the same for the right rotation Transform3D temp = new Transform3D(); rotationGroup.getTransform(temp); Transform3D tempDelta = new Transform3D(); tempDelta.rotY(0.3); temp.mul(tempDelta); rotationGroup.setTransform(temp); } }
From source file:SimpleSounds.java
/** * Add a sound to the transform group. This takes a point sound object and * loads into it a sounds from a given file. The edge of the sound's extent * is also defined in a parameter./*from www. j av a 2 s . c om*/ * * @param tg * TransformGroup that the sound is to be added to * @param sound * PointSound to be used * @param soundFile * String that is the name of the sound file to be loaded * @param edge * float that represents the sound's maximum extent */ protected void addObjectSound(TransformGroup tg, PointSound sound, String soundFile, float edge) { //First we get the current transform so that we can //position the sound in the same place Transform3D objXfm = new Transform3D(); Vector3f objPosition = new Vector3f(); tg.getTransform(objXfm); objXfm.get(objPosition); //Create the media container to load the sound MediaContainer soundContainer = new MediaContainer(soundFile); //Use the loaded data in the sound sound.setSoundData(soundContainer); sound.setInitialGain(1.0f); //Set the position to that of the given transform sound.setPosition(new Point3f(objPosition)); //Allow use to switch the sound on and off sound.setCapability(PointSound.ALLOW_ENABLE_READ); sound.setCapability(PointSound.ALLOW_ENABLE_WRITE); sound.setSchedulingBounds(bounds); //Set it off to start with sound.setEnable(false); //Set it to loop forever sound.setLoop(BackgroundSound.INFINITE_LOOPS); //Use the edge value to set to extent of the sound Point2f[] attenuation = { new Point2f(0.0f, 1.0f), new Point2f(edge, 0.1f) }; sound.setDistanceGain(attenuation); //Add the sound to the transform group tg.addChild(sound); }
From source file:ExTexture.java
public Group buildScene() { // Get the current menu choices for appearance attributes int textureMode = ((Integer) modes[currentMode].value).intValue(); Color3f color = (Color3f) colors[currentColor].value; Color3f blendColor = (Color3f) colors[currentBlendColor].value; // Turn on the example headlight setHeadlightEnable(true);//from w w w .j a v a2s . c o m // Default to examine navigation setNavigationType(Examine); // Disable scene graph compilation for this example setCompilable(false); // Create the scene group Group scene = new Group(); // BEGIN EXAMPLE TOPIC // Set up a basic material Material mat = new Material(); mat.setAmbientColor(0.2f, 0.2f, 0.2f); mat.setDiffuseColor(1.0f, 1.0f, 1.0f); mat.setSpecularColor(0.0f, 0.0f, 0.0f); mat.setLightingEnable(true); // Set up the texturing attributes with an initial // texture mode, texture transform, and blend color texatt = new TextureAttributes(); texatt.setPerspectiveCorrectionMode(TextureAttributes.NICEST); texatt.setTextureMode(textureMode); texatt.setTextureTransform(new Transform3D()); // Identity texatt.setTextureBlendColor(blendColor.x, blendColor.y, blendColor.z, 0.5f); // Enable changing these while the node component is live texatt.setCapability(TextureAttributes.ALLOW_MODE_WRITE); texatt.setCapability(TextureAttributes.ALLOW_BLEND_COLOR_WRITE); texatt.setCapability(TextureAttributes.ALLOW_TRANSFORM_WRITE); // Create an appearance using these attributes app = new Appearance(); app.setMaterial(mat); app.setTextureAttributes(texatt); app.setTexture(tex); // And enable changing these while the node component is live app.setCapability(Appearance.ALLOW_TEXTURE_WRITE); app.setCapability(Appearance.ALLOW_TEXTURE_ATTRIBUTES_WRITE); // Build a shape and enable changing its appearance shape = new Shape3D(buildGeometry(), app); shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE); // END EXAMPLE TOPIC // Create some dummy appearance and tex attribute node components // In response to menu choices, we quickly switch the shape to // use one of these, then diddle with the main appearance or // tex attribute, then switch the shape back. This effectively // makes the appearance or tex attributes we want to change // become un-live during a change. We have to do this approach // because some texture features have no capability bits to set // to allow changes while live. dummyApp = new Appearance(); dummyAtt = new TextureAttributes(); scene.addChild(shape); return scene; }
From source file:TickTockCollision.java
private Group createBox(double scale, Vector3d pos) { // Create a transform group node to scale and position the object. Transform3D t = new Transform3D(); t.set(scale, pos);//from ww w.j a va2s . c om TransformGroup objTrans = new TransformGroup(t); // Create a simple shape leaf node and add it to the scene graph Shape3D shape = new Box(0.5, 5.0, 1.0); objTrans.addChild(shape); // Create a new ColoringAttributes object for the shape's // appearance and make it writable at runtime. Appearance app = shape.getAppearance(); ColoringAttributes ca = new ColoringAttributes(); ca.setColor(0.6f, 0.3f, 0.0f); app.setCapability(app.ALLOW_COLORING_ATTRIBUTES_WRITE); app.setColoringAttributes(ca); // Create a new Behavior object that will perform the collision // detection on the specified object, and add it into // the scene graph. CollisionDetector cd = new CollisionDetector(shape); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); cd.setSchedulingBounds(bounds); // Add the behavior to the scene graph objTrans.addChild(cd); return objTrans; }
From source file:ExSwitch.java
public Group buildScene() { // Turn on the example headlight setHeadlightEnable(true);/*ww w . ja v a2 s. com*/ // 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:CustomAlphaTest.java
protected BranchGroup createSceneBranchGroup() { BranchGroup objRoot = super.createSceneBranchGroup(); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); ColorCube cube = new ColorCube(2); objTrans.addChild(cube);/* w ww.jav a 2 s.c om*/ FileAlpha fileAlpha = null; try { fileAlpha = new FileAlpha(new URL(getWorkingDirectory(), "values.xls"), this); } catch (Exception e) { e.toString(); } PositionInterpolator posInterpolator = new PositionInterpolator(fileAlpha, objTrans, new Transform3D(), -6, 6); posInterpolator.setSchedulingBounds(getApplicationBounds()); objTrans.addChild(posInterpolator); objRoot.addChild(objTrans); return objRoot; }
From source file:PolygonOffset.java
public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // 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); objRoot.addChild(objTrans);// w ww . j a va 2 s .c o m // Create a Sphere. We will display this as both wireframe and // solid to make a hidden line display // wireframe Appearance wireApp = new Appearance(); ColoringAttributes wireCa = new ColoringAttributes(); wireCa.setColor(black); wireApp.setColoringAttributes(wireCa); wirePa = new PolygonAttributes(PolygonAttributes.POLYGON_LINE, PolygonAttributes.CULL_BACK, 0.0f); wireApp.setPolygonAttributes(wirePa); Sphere outWireSphere = new Sphere(sphereRadius, 0, 15, wireApp); objTrans.addChild(outWireSphere); // solid ColoringAttributes outCa = new ColoringAttributes(red, ColoringAttributes.SHADE_FLAT); Appearance outSolid = new Appearance(); outSolid.setColoringAttributes(outCa); solidPa = new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_BACK, 0.0f); solidPa.setPolygonOffsetFactor(dynamicOffset); solidPa.setPolygonOffset(staticOffset); solidPa.setCapability(PolygonAttributes.ALLOW_OFFSET_WRITE); outSolid.setPolygonAttributes(solidPa); Sphere outSolidSphere = new Sphere(sphereRadius, 0, 15, outSolid); objTrans.addChild(outSolidSphere); innerTG = new TransformGroup(); innerTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); scale = new Transform3D(); updateInnerScale(); objTrans.addChild(innerTG); // Create a smaller sphere to go inside. This sphere has a different // tesselation and color Sphere inWireSphere = new Sphere(sphereRadius, 0, 10, wireApp); innerTG.addChild(inWireSphere); // inside solid ColoringAttributes inCa = new ColoringAttributes(blue, ColoringAttributes.SHADE_FLAT); Appearance inSolid = new Appearance(); inSolid.setColoringAttributes(inCa); inSolid.setPolygonAttributes(solidPa); Sphere inSolidSphere = new Sphere(sphereRadius, 0, 10, inSolid); innerTG.addChild(inSolidSphere); // Create a new Behavior object that will perform the desired // operation on the specified transform object and add it into // the scene graph. AxisAngle4f axisAngle = new AxisAngle4f(0.0f, 0.0f, 1.0f, -(float) Math.PI / 2.0f); Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0, 80000, 0, 0, 0, 0, 0); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 2.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); rotator.setSchedulingBounds(bounds); objTrans.addChild(rotator); // set up a white background Background bgWhite = new Background(new Color3f(1.0f, 1.0f, 1.0f)); bgWhite.setApplicationBounds(bounds); objTrans.addChild(bgWhite); // Have Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
From source file:TreePrinter.java
private void printNode(Object o, int indent, Set sharedGroups) { for (int i = 0; i < indent; i++) printStream.print(">"); printStream.print(nodeString(o) + ": "); if (o instanceof SceneGraphObject) { SceneGraphObject sgo = (SceneGraphObject) o; int capBits = 0; // TODO: how to make sure we always check all the valid bits? for (int i = 0; i < 64; i++) { if (sgo.getCapability(i)) { capBits |= 1 << i; }//from ww w .jav a 2s. c om } printStream.print("capBits:Ox" + Integer.toHexString(capBits)); if (o instanceof javax.media.j3d.Group) { javax.media.j3d.Group g = (javax.media.j3d.Group) o; int numChildren = 0; try { numChildren = g.numChildren(); } catch (CapabilityNotSetException e) { //anyone who is using treePrinter, is debugging, so it is //alright to blindly allow read. you should first detach //browser.curScene, print the tree, then add it back to //browser.locale when finished. g.setCapability(javax.media.j3d.Group.ALLOW_CHILDREN_READ); numChildren = g.numChildren(); //System.out.println("Can't read children on group"); //return; } printStream.print(" children:" + numChildren); if (o instanceof TransformGroup) { Transform3D transform = new Transform3D(); Transform3D identity = new Transform3D(); TransformGroup t = (TransformGroup) o; t.getTransform(transform); // TODO: use getBestType() when implemented if (transform.equals(identity)) { printStream.print(" xform:IDENTITY "); } else { printStream.print(" xform:NON-IDENTITY "); } } } else if (o instanceof Link) { Link l = (Link) o; SharedGroup sg = l.getSharedGroup(); printStream.print(" sg:" + nodeString(sg)); sharedGroups.add(sg); } else { printStream.print(": leaf"); } } printStream.println(); }
From source file:PickTest.java
public BranchGroup createSceneGraph(Canvas3D canvas) { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create a Transformgroup to scale all objects so they // appear in the scene. TransformGroup objScale = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setScale(1.0);/*from w w w . jav a 2 s . c om*/ objScale.setTransform(t3d); objRoot.addChild(objScale); // Create a bunch of objects with a behavior and add them // into the scene graph. int row, col; int numRows = 4, numCols = 4; for (int i = 0; i < numRows; i++) { double ypos = (double) (i - numRows / 2) * 0.45 + 0.25; for (int j = 0; j < numCols; j++) { double xpos = (double) (j - numCols / 2) * 0.45 + 0.25; objScale.addChild(createObject(i * numCols + j, 0.1, xpos, ypos)); } } BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); // Add a light. Color3f lColor = new Color3f(1.0f, 1.0f, 1.0f); Vector3f lDir = new Vector3f(0.0f, 0.0f, -1.0f); DirectionalLight lgt = new DirectionalLight(lColor, lDir); lgt.setInfluencingBounds(bounds); objRoot.addChild(lgt); // Now create the Alpha object that controls the speed of the // morphing operation. Alpha morphAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 0, 0, 4000, 1000, 500, 4000, 1000, 500); // Finally, create the morphing behavior MorphingBehavior mBeh = new MorphingBehavior(morphAlpha, morph); mBeh.setSchedulingBounds(bounds); objRoot.addChild(mBeh); behavior1 = new PickRotateBehavior(objRoot, canvas, bounds); objRoot.addChild(behavior1); behavior2 = new PickZoomBehavior(objRoot, canvas, bounds); objRoot.addChild(behavior2); behavior3 = new PickTranslateBehavior(objRoot, canvas, bounds); objRoot.addChild(behavior3); // Let Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }