public BranchGroup createSceneGraph() { // Create the root of the branch graph BranchGroup objRoot = new BranchGroup(); // Create the TransformGroup node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so that // our behavior code can modify it at run time. Add it to // the root of the subgraph. TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objRoot.addChild(objTrans); // Create a simple Shape3D node; add it to the scene graph. objTrans.addChild(new ColorCube(0.4)); // Create a new Behavior object that will perform the // desired operation on the specified transform and add // it into the scene graph. Transform3D yAxis = new Transform3D(); Alpha rotationAlpha = new Alpha(-1, 4000); RotationInterpolator rotator = new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 1.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0); rotator.setSchedulingBounds(bounds); objRoot.addChild(rotator); // Have Java 3D perform optimizations on this scene graph. objRoot.compile(); return objRoot; }
// In this method, the objects for the scene are generated and added to // the SimpleUniverse. public void createSceneGraph(SimpleUniverse su) { // *** The root of the graph containing the scene (with a cube and a sphere). *** BranchGroup theScene = new BranchGroup(); // Generate an Appearance. Color3f ambientColourShaded = new Color3f(0.0f, 0.4f, 0.4f); Color3f emissiveColourShaded = new Color3f(0.0f, 0.0f, 0.0f); Color3f diffuseColourShaded = new Color3f(0.0f, 0.7f, 0.7f); Color3f specularColourShaded = new Color3f(0.0f, 0.5f, 0.5f); float shininessShaded = 20.0f; Appearance shadedApp = new Appearance(); shadedApp.setMaterial( new Material( ambientColourShaded, emissiveColourShaded, diffuseColourShaded, specularColourShaded, shininessShaded)); float r = 0.3f; // The radius of the sphere. float boxHL = 0.7f * r; // Half the vertex length of the cube. float shift = 3.0f * r; // Distance between cube and sphere. // *** The sphere and its transformation group *** Sphere s = new Sphere(r, Sphere.GENERATE_NORMALS, 100, shadedApp); Transform3D tfSphere = new Transform3D(); tfSphere.setTranslation(new Vector3f(-0.95f + r, 0.0f, 0.0f)); TransformGroup tgSphere = new TransformGroup(tfSphere); tgSphere.addChild(s); theScene.addChild(tgSphere); // *** The cube and its transformation group *** Box b2 = new Box(boxHL, boxHL, boxHL, shadedApp); Transform3D tfBox2 = new Transform3D(); tfBox2.setTranslation(new Vector3f(-0.95f + r + shift, 0.0f, 0.0f)); Transform3D rotation = new Transform3D(); rotation.rotY(Math.PI / 4); Transform3D rotationX = new Transform3D(); rotationX.rotX(Math.PI / 6); rotation.mul(rotationX); tfBox2.mul(rotation); TransformGroup tgBox2 = new TransformGroup(tfBox2); tgBox2.addChild(b2); theScene.addChild(tgBox2); // Generate a white background. Background bg = new Background(new Color3f(1.0f, 1.0f, 1.0f)); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.MAX_VALUE); bg.setApplicationBounds(bounds); theScene.addChild(bg); theScene.compile(); // Add the scene to the universe. su.addBranchGraph(theScene); }
/** * Set the version of VRML that this node should represent. Different versions have different * capabilities, even within the same node. * * @param major The major version number of this scene * @param minor The minor version number of this scene * @param isStatic true if this node is under a static group and won't change after the setup is * finished */ public void setVersion(int major, int minor, boolean isStatic) { super.setVersion(major, minor, isStatic); if (isStatic) return; implGroup.setCapability(BranchGroup.ALLOW_DETACH); implGroup.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); implGroup.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); implTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); implTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); implTG.setCapability(BranchGroup.ENABLE_PICK_REPORTING); }
private TransformGroup makeText(Vector3d vertex, String text) // Create a Text2D object at the specified vertex { Text2D message = new Text2D(text, white, "SansSerif", 36, Font.BOLD); // 36 point bold Sans Serif TransformGroup tg = new TransformGroup(); Transform3D t3d = new Transform3D(); t3d.setTranslation(vertex); tg.setTransform(t3d); tg.addChild(message); return tg; } // end of getTG()
public void addLinesAroundRegion(TransformGroup tg) { Color3f x = new Color3f(Color.green.brighter().brighter()); Color3f y = new Color3f(Color.yellow.brighter().brighter()); Color3f z = new Color3f(Color.red.brighter().brighter()); LineArray wireframe = new LineArray(12 * 2, GeometryArray.COORDINATES | GeometryArray.COLOR_3); wireframe.setCapability(LineArray.ALLOW_COLOR_WRITE); wireframe.setCapability(LineArray.ALLOW_COORDINATE_READ); wireframe.setCapability(LineArray.ALLOW_COUNT_READ); ArrayList<Point3f> vx = getVertices(); // Add lines parallel to x wireframe.setCoordinate(0, vx.get(0)); wireframe.setCoordinate(1, vx.get(2)); wireframe.setCoordinate(2, vx.get(1)); wireframe.setCoordinate(3, vx.get(3)); wireframe.setCoordinate(4, vx.get(4)); wireframe.setCoordinate(5, vx.get(6)); wireframe.setCoordinate(6, vx.get(5)); wireframe.setCoordinate(7, vx.get(7)); for (int i = 0; i < 8; i++) wireframe.setColor(i, x); // Add lines parallel to y wireframe.setCoordinate(8, vx.get(0)); wireframe.setCoordinate(9, vx.get(4)); wireframe.setCoordinate(10, vx.get(1)); wireframe.setCoordinate(11, vx.get(5)); wireframe.setCoordinate(12, vx.get(2)); wireframe.setCoordinate(13, vx.get(6)); wireframe.setCoordinate(14, vx.get(3)); wireframe.setCoordinate(15, vx.get(7)); for (int i = 8; i < 16; i++) wireframe.setColor(i, y); // Add lines parallel to z wireframe.setCoordinate(16, vx.get(0)); wireframe.setCoordinate(17, vx.get(1)); wireframe.setCoordinate(18, vx.get(2)); wireframe.setCoordinate(19, vx.get(3)); wireframe.setCoordinate(20, vx.get(4)); wireframe.setCoordinate(21, vx.get(5)); wireframe.setCoordinate(22, vx.get(6)); wireframe.setCoordinate(23, vx.get(7)); for (int i = 16; i < 24; i++) wireframe.setColor(i, z); Shape3D shape = new Shape3D(wireframe); tg.addChild(shape); }
// Directional light rotating around the scene and some ambient light. public void addLight(SimpleUniverse su) { BranchGroup bgLight = new BranchGroup(); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.MAX_VALUE); // Directional light (to be rotated). Color3f lightColour = new Color3f(1.0f, 1.0f, 1.0f); Vector3f lightDir = new Vector3f(0.0f, 0.0f, -1.0f); DirectionalLight light = new DirectionalLight(lightColour, lightDir); light.setInfluencingBounds(bounds); // The transformation group for the directional light and its rotation. TransformGroup tfmLight = new TransformGroup(); tfmLight.addChild(light); // The Alpha for the rotation. Alpha alphaLight = new Alpha(-1, 4000); // The rotation RotationInterpolator rot = new RotationInterpolator( alphaLight, tfmLight, new Transform3D(), 0.0f, (float) Math.PI * 2); rot.setSchedulingBounds(bounds); tfmLight.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tfmLight.addChild(rot); bgLight.addChild(tfmLight); // Ambient light. Color3f ambientLightColour = new Color3f(0.5f, 0.5f, 0.5f); AmbientLight ambLight = new AmbientLight(ambientLightColour); ambLight.setInfluencingBounds(bounds); bgLight.addChild(ambLight); su.addBranchGraph(bgLight); }
public ObjectViewer(URL url) { setLayout(new BorderLayout()); Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration()); add("Center", canvas3D); BoundingSphere bounds = new BoundingSphere(new Point3d(), 1000); BranchGroup root = new BranchGroup(); BranchGroup scene = createSceneGraph(url); scene.setBoundsAutoCompute(true); System.out.println(scene.getBounds()); BoundingSphere sceneBounds = new BoundingSphere(scene.getBounds()); SimpleUniverse univ = new SimpleUniverse(canvas3D); ViewingPlatform view = univ.getViewingPlatform(); view.setNominalViewingTransform(); Transform3D t = new Transform3D(); TransformGroup viewTransform = view.getViewPlatformTransform(); t.set(new Vector3d(0, 0, 3 * sceneBounds.getRadius())); viewTransform.setTransform(t); BranchGroup lights = new BranchGroup(); Light light = new AmbientLight(); light.setInfluencingBounds(bounds); lights.addChild(light); light = new DirectionalLight(); light.setInfluencingBounds(bounds); lights.addChild(light); root.addChild(lights); TransformGroup tg = new TransformGroup(); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); tg.addChild(scene); root.addChild(tg); MouseRotate mouse = new MouseRotate(); mouse.setTransformGroup(tg); mouse.setSchedulingBounds(bounds); root.addChild(mouse); MouseZoom mousezoom = new MouseZoom(); mousezoom.setTransformGroup(tg); mousezoom.setSchedulingBounds(bounds); root.addChild(mousezoom); Background background = new Background(1, 1, 1); background.setApplicationBounds(bounds); root.addChild(background); root.compile(); univ.addBranchGraph(root); }
public void processStimulus(Enumeration criteria) { posVector.set( (float) targetHistogram.neutronNumber - ((float) targetMaxNeutron / 2.0f), (float) targetHistogram.abundance[step] - targetHistogram.normalizationFactor, (float) -targetHistogram.protonNumber + ((float) targetMaxProton / 2.0f)); ca = targetHistogram.fillingAppearance.getColoringAttributes(); targetColorRamp.getColor( targetHistogram.abundance[step] - targetHistogram.normalizationFactor, histColor); ca.setColor(histColor); step++; if (step >= targetHistogram.abundance.length) { step = 0; } targetT3D.set(posVector); targetTG.setTransform(targetT3D); this.wakeupOn(new WakeupOnAWTEvent(MouseEvent.MOUSE_CLICKED)); }
/** Calculate transforms needed to handle VRML semantics formula: T x C x R x SR x S x -SR x -C */ private void updateTransform() { // System.out.println(this); tempVec.x = -vfCenter[0]; tempVec.y = -vfCenter[1]; tempVec.z = -vfCenter[2]; matrix.setIdentity(); matrix.setTranslation(tempVec); float scaleVal = 1.0f; if (floatEq(vfScale[0], vfScale[1]) && floatEq(vfScale[0], vfScale[2])) { scaleVal = vfScale[0]; tempMtx1.set(scaleVal); // System.out.println("S" + tempMtx1); } else { // non-uniform scale // System.out.println("Non Uniform Scale"); tempAxis.x = vfScaleOrientation[0]; tempAxis.y = vfScaleOrientation[1]; tempAxis.z = vfScaleOrientation[2]; tempAxis.angle = -vfScaleOrientation[3]; double tempAxisNormalizer = 1 / Math.sqrt( tempAxis.x * tempAxis.x + tempAxis.y * tempAxis.y + tempAxis.z * tempAxis.z); tempAxis.x *= tempAxisNormalizer; tempAxis.y *= tempAxisNormalizer; tempAxis.z *= tempAxisNormalizer; tempMtx1.set(tempAxis); tempMtx2.mul(tempMtx1, matrix); // Set the scale by individually setting each element tempMtx1.setIdentity(); tempMtx1.m00 = vfScale[0]; tempMtx1.m11 = vfScale[1]; tempMtx1.m22 = vfScale[2]; matrix.mul(tempMtx1, tempMtx2); tempAxis.x = vfScaleOrientation[0]; tempAxis.y = vfScaleOrientation[1]; tempAxis.z = vfScaleOrientation[2]; tempAxis.angle = vfScaleOrientation[3]; tempMtx1.set(tempAxis); } tempMtx2.mul(tempMtx1, matrix); // System.out.println("Sx-C" + tempMtx2); float magSq = vfRotation[0] * vfRotation[0] + vfRotation[1] * vfRotation[1] + vfRotation[2] * vfRotation[2]; if (magSq < ZEROEPS) { tempAxis.x = 0; tempAxis.y = 0; tempAxis.z = 1; tempAxis.angle = 0; } else { if ((magSq > 1.01) || (magSq < 0.99)) { float mag = (float) (1 / Math.sqrt(magSq)); tempAxis.x = vfRotation[0] * mag; tempAxis.y = vfRotation[1] * mag; tempAxis.z = vfRotation[2] * mag; } else { tempAxis.x = vfRotation[0]; tempAxis.y = vfRotation[1]; tempAxis.z = vfRotation[2]; } tempAxis.angle = vfRotation[3]; } tempMtx1.set(tempAxis); // System.out.println("R" + tempMtx1); matrix.mul(tempMtx1, tempMtx2); // System.out.println("RxSx-C" + matrix); tempVec.x = vfCenter[0]; tempVec.y = vfCenter[1]; tempVec.z = vfCenter[2]; tempMtx1.setIdentity(); tempMtx1.setTranslation(tempVec); // System.out.println("C" + tempMtx1); tempMtx2.mul(tempMtx1, matrix); // System.out.println("CxRxSx-C" + tempMtx2); tempVec.x = vfTranslation[0]; tempVec.y = vfTranslation[1]; tempVec.z = vfTranslation[2]; tempMtx1.setIdentity(); tempMtx1.setTranslation(tempVec); matrix.mul(tempMtx1, tempMtx2); transform.set(matrix); implTG.setTransform(transform); }
/** Setup the basic scene which consists of a quad and a viewpoint */ private void setupSceneGraph() { // View group Viewpoint vp = new Viewpoint(); Vector3f trans = new Vector3f(0, 0, 1); Matrix4f mat = new Matrix4f(); mat.setIdentity(); mat.setTranslation(trans); TransformGroup tx = new TransformGroup(); tx.addChild(vp); tx.setTransform(mat); Group scene_root = new Group(); scene_root.addChild(tx); // Flat panel that has the viewable object as the demo float[] coord = {0, 0, -1, 0.25f, 0, -1, 0, 0.25f, -1}; float[] normal = {0, 0, 1, 0, 0, 1, 0, 0, 1}; TriangleArray geom = new TriangleArray(); geom.setValidVertexCount(3); geom.setVertices(TriangleArray.COORDINATE_3, coord); geom.setNormals(normal); Material material = new Material(); material.setDiffuseColor(new float[] {0, 0, 1}); material.setEmissiveColor(new float[] {0, 0, 1}); material.setSpecularColor(new float[] {1, 1, 1}); material.setTransparency(0.5f); Appearance app = new Appearance(); app.setMaterial(material); Shape3D shape = new Shape3D(); shape.setGeometry(geom); shape.setAppearance(app); TransformGroup tg = new TransformGroup(); Matrix4f transform = new Matrix4f(); transform.setIdentity(); transform.setTranslation(new Vector3f(0.15f, 0, -1)); tg.setTransform(transform); Shape3D backShape = new Shape3D(); Material material2 = new Material(); material2.setDiffuseColor(new float[] {1, 0, 0}); material2.setEmissiveColor(new float[] {1, 0, 0}); material2.setSpecularColor(new float[] {1, 1, 1}); Appearance app2 = new Appearance(); app2.setMaterial(material2); backShape.setGeometry(geom); backShape.setAppearance(app2); tg.addChild(backShape); scene_root.addChild(tg); scene_root.addChild(shape); SimpleScene scene = new SimpleScene(); scene.setRenderedGeometry(scene_root); scene.setActiveView(vp); // Then the basic layer and viewport at the top: SimpleViewport view = new SimpleViewport(); view.setDimensions(0, 0, 500, 500); view.setScene(scene); SimpleLayer layer = new SimpleLayer(); layer.setViewport(view); Layer[] layers = {layer}; displayManager.setLayers(layers, 1); }