// 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); }
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); }
// In this method, the objects for the scene are generated and added to // the SimpleUniverse. public void createSceneGraph(SimpleUniverse su) { // Create the root of the branch group for the scene. BranchGroup theScene = new BranchGroup(); // Generate an Appearance for the sphere. Color3f ambientColourSphere = new Color3f(0.2f, 0.2f, 0.2f); Color3f emissiveColourSphere = new Color3f(0.0f, 0.0f, 0.0f); Color3f diffuseColourSphere = new Color3f(0.6f, 0.6f, 0.6f); Color3f specularColourSphere = new Color3f(0.5f, 0.5f, 0.5f); float shininessSphere = 20.0f; Appearance sphereApp = new Appearance(); sphereApp.setMaterial( new Material( ambientColourSphere, emissiveColourSphere, diffuseColourSphere, specularColourSphere, shininessSphere)); // n spheres with radius r will be shown. int n = 5; float r = 0.15f; float shift = 2 * r + 0.05f; // The distance between the centres of the spheres. // Arrays for the sphere, their transformations and their transformation groups // transformation groups (for positioning). Sphere[] spheres = new Sphere[n]; TransformGroup[] tg = new TransformGroup[n]; Transform3D[] tf = new Transform3D[n]; // Generate the sphere, their transformations and their // transformation groups. Add everyting to the scene. for (int i = 0; i < n; i++) { spheres[i] = new Sphere(r, Sphere.GENERATE_NORMALS, 4 + i * i * i, sphereApp); tf[i] = new Transform3D(); tf[i].setTranslation(new Vector3f(-0.95f + r + shift * i, 0.0f, 0.0f)); tg[i] = new TransformGroup(tf[i]); tg[i].addChild(spheres[i]); theScene.addChild(tg[i]); } // 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), 1000.0); bg.setApplicationBounds(bounds); theScene.addChild(bg); theScene.compile(); // Add the scene to the universe. su.addBranchGraph(theScene); }
private void setSceneAppearance() { AmbientLight lightA = new AmbientLight(); lightA.setColor(new Color3f(255, 0, 100)); _contents.addChild(lightA); Background background = new Background(); background.setColor(EnvSettings.BACKGROUND_COLOR); _bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 0.1); background.setApplicationBounds(_bounds); _contents.addChild(background); }
private BranchGroup createSceneGraph() { BranchGroup root = new BranchGroup(); root.setCapability(BranchGroup.ALLOW_DETACH); createSpinTG(); root.addChild(spin); SimpleBehavior myRotationBehavior = new SimpleBehavior(spin); myRotationBehavior.setSchedulingBounds(new BoundingSphere()); root.addChild(myRotationBehavior); KeyNavigatorBehavior behavior = new KeyNavigatorBehavior(spin); BoundingSphere bounds = new BoundingSphere(); behavior.setSchedulingBounds(bounds); root.addChild(behavior); // mouse-directed rotation MouseRotate rotator = new MouseRotate(spin); rotator.setSchedulingBounds(bounds); root.addChild(rotator); // mouse-based translation MouseTranslate translator = new MouseTranslate(spin); translator.setSchedulingBounds(bounds); root.addChild(translator); // mouse-directed zoom MouseZoom zoom = new MouseZoom(); zoom.setSchedulingBounds(bounds); root.addChild(zoom); // make background blue Background background = new Background(Colors.blue); background.setApplicationBounds(bounds); root.addChild(background); // light AmbientLight light = new AmbientLight(true, new Color3f(Color.red)); light.setInfluencingBounds(bounds); root.addChild(light); PointLight ptlight = new PointLight(new Color3f(Color.red), new Point3f(0f, 0f, -5f), new Point3f(1f, 0f, 0f)); ptlight.setInfluencingBounds(bounds); root.addChild(ptlight); PointLight ptlight2 = new PointLight( new Color3f(Color.orange), new Point3f(-2f, 2f, 2f), new Point3f(1f, 0f, 0f)); ptlight2.setInfluencingBounds(bounds); root.addChild(ptlight2); // let Java3d perform optimization on this scene graph root.compile(); return root; }
private void initBackground() { Background background = new Background(); background.setColor(getSettings().getRenderSettings3D().getBackground()); background.setCapability(Background.ALLOW_COLOR_WRITE); background.setApplicationBounds(getSettings().getRenderSettings3D().getBounds()); getScene().addChild(background); this.getLogger() .info( "Background initialized to '" + getSettings().getRenderSettings3D().getBackground().toString() + "'."); }
public void createSceneGraph(DrawModel drawing) { // Branch group which can be deattached for changing scene; view = new BranchGroup(); view.setCapability(BranchGroup.ALLOW_DETACH); // BranchGroup for rotation rotation = new TransformGroup(); rotation.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); rotation.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); view.addChild(rotation); bounds = new BoundingSphere(); DirectionalLight lightD = new DirectionalLight(); lightD.setDirection(new Vector3f(0.0f, 0.0f, -0.7f)); lightD.setInfluencingBounds(bounds); view.addChild(lightD); AmbientLight lightA = new AmbientLight(); lightA.setInfluencingBounds(bounds); view.addChild(lightA); Background background = new Background(); background.setColor(1.0f, 1.0f, 1.0f); background.setApplicationBounds(bounds); view.addChild(background); // MouseRotate myMouseRotate = new MouseRotate(); // myMouseRotate.setTransformGroup(rotation); // myMouseRotate.setSchedulingBounds(bounds); // root.addChild(myMouseRotate); // // MouseTranslate myMouseTranslate = new MouseTranslate(); // myMouseTranslate.setTransformGroup(rotation); // myMouseTranslate.setSchedulingBounds(new BoundingSphere()); // root.addChild(myMouseTranslate); // MouseZoom myMouseZoom = new MouseZoom(); // myMouseZoom.setTransformGroup(rotation); // myMouseZoom.setSchedulingBounds(new BoundingSphere()); // root.addChild(myMouseZoom); // PickMouse pick = new PickMouse(this, view, bounds); view.addChild(pick); addKeyListener(new KeyListener()); angleV = (float) Math.PI / 4; angleH = (float) Math.PI / 5; draw(drawing); zoomAll(); }
// // Build scene // public Group buildScene() { // Get the current color Color3f color = (Color3f) colors[currentColor].value; float front = ((Float) fronts[currentFront].value).floatValue(); float back = ((Float) backs[currentBack].value).floatValue(); // Turn off the example headlight setHeadlightEnable(false); // Default to walk navigation setNavigationType(Walk); // Create the scene group Group scene = new Group(); // BEGIN EXAMPLE TOPIC // Create influencing bounds BoundingSphere worldBounds = new BoundingSphere( new Point3d(0.0, 0.0, 0.0), // Center 1000.0); // Extent // Set the fog color, front & back distances, and // its influencing bounds fog = new LinearFog(); fog.setColor(color); fog.setFrontDistance(front); fog.setBackDistance(back); fog.setCapability(Fog.ALLOW_COLOR_WRITE); fog.setCapability(LinearFog.ALLOW_DISTANCE_WRITE); fog.setInfluencingBounds(worldBounds); scene.addChild(fog); // END EXAMPLE TOPIC // 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 scene.addChild(new ColumnScene(this)); return scene; }
// // Handle checkboxes and menu choices // public void checkboxChanged(CheckboxMenu menu, int check) { if (menu == backgroundColorMenu) { // Change the background color currentBackgroundColor = check; Color3f color = (Color3f) colors[check].value; background.setColor(color); return; } if (menu == colorMenu) { // Change the fog color currentColor = check; Color3f color = (Color3f) colors[check].value; fog.setColor(color); // If background is coupled, set the background color if (coupledBackgroundOnOff) { currentBackgroundColor = currentColor; backgroundColorMenu.setCurrent(check); background.setColor(color); } return; } if (menu == frontMenu) { // Change the fog front distance currentFront = check; float front = ((Float) fronts[currentFront].value).floatValue(); fog.setFrontDistance(front); return; } if (menu == backMenu) { // Change the fog back distance currentBack = check; float back = ((Float) backs[currentBack].value).floatValue(); fog.setBackDistance(back); return; } // Handle all other checkboxes super.checkboxChanged(menu, check); }
public BufferedImage getRenderedImage(Dimension d, boolean withBackground) { if (offscreenCanvas == null) { offscreenCanvas = new Canvas3D(SimpleUniverse.getPreferredConfiguration(), true); } offscreenCanvas.setSize(getSize()); offscreenCanvas.getScreen3D().setSize(getScreen3D().getSize()); offscreenCanvas.getScreen3D().setPhysicalScreenWidth(getScreen3D().getPhysicalScreenWidth()); offscreenCanvas.getScreen3D().setPhysicalScreenHeight(getScreen3D().getPhysicalScreenHeight()); BufferedImage bufImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); ImageComponent2D imgComp = new ImageComponent2D(ImageComponent2D.FORMAT_RGBA, bufImage); BufferedImage transparentImage = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB); ImageComponent2D transparentComp = new ImageComponent2D(ImageComponent2D.FORMAT_RGBA, transparentImage); Color3f tempColor = new Color3f(); background.getColor(tempColor); if (!withBackground) { background.setImage(transparentComp); } view.addCanvas3D(offscreenCanvas); offscreenCanvas.setOffScreenBuffer(imgComp); offscreenCanvas.renderOffScreenBuffer(); offscreenCanvas.waitForOffScreenRendering(); if (!withBackground) { background.setImage(null); } bufImage = offscreenCanvas.getOffScreenBuffer().getImage(); // To release the reference of buffer inside Java 3D. offscreenCanvas.setOffScreenBuffer(null); view.removeCanvas3D(offscreenCanvas); return bufImage; }
public void init() { // initialize our SimpleUniverse -> u u = new SimpleUniverse(this); // set viewing platform (the camera) vp = u.getViewingPlatform(); view = u.getViewer().getView(); view.setScreenScalePolicy(View.SCALE_EXPLICIT); view.setBackClipPolicy(View.VIRTUAL_EYE); view.setFrontClipPolicy(View.VIRTUAL_EYE); view.setBackClipDistance(FARTHEST); view.setFrontClipDistance(CLOSEST); fieldOfView = view.getFieldOfView(); view.setDepthBufferFreezeTransparent(false); cameraTG = vp.getViewPlatformTransform(); cameraT3D = new Transform3D(); cameraTG.getTransform(cameraT3D); // initialize root of our scene -> BranchGroup kainBG and TransformGroup kainTG kainBG = new BranchGroup(); kainBG.setCapability(BranchGroup.ALLOW_CHILDREN_READ); kainBG.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE); kainBG.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND); background = new Background(new Color3f(0, 0, 0)); background.setCapability(Background.ALLOW_COLOR_READ); background.setCapability(Background.ALLOW_COLOR_WRITE); background.setCapability(Background.ALLOW_IMAGE_READ); background.setCapability(Background.ALLOW_IMAGE_WRITE); background.setApplicationBounds(new BoundingSphere(new Point3d(0, 0, 0), FARTHEST)); background.setImageScaleMode(Background.SCALE_FIT_ALL); kainBG.addChild(background); kainTG = new TransformGroup(); kainTG.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); kainTG.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); kainBG.addChild(kainTG); // branch for axis lines axisBG = new BranchGroup(); axisBG.setCapability(BranchGroup.ALLOW_DETACH); axisLines = createAxisLines(); axisBG.addChild(axisLines); // branch for ground groundBG = new BranchGroup(); groundBG.setCapability(BranchGroup.ALLOW_DETACH); ground = createGround(); groundBG.addChild(ground); kainBG.compile(); u.addBranchGraph(kainBG); currentAppearance = Appearance3DChangerCookie.Appearance.WIREFRAME; }
public void itemStateChanged(ItemEvent event) { Object src = event.getSource(); // Check if it is the coupled background choice if (src == coupledBackgroundOnOffMenu) { coupledBackgroundOnOff = coupledBackgroundOnOffMenu.getState(); if (coupledBackgroundOnOff) { currentBackgroundColor = currentColor; backgroundColorMenu.setCurrent(currentColor); Color3f color = (Color3f) colors[currentColor].value; background.setColor(color); backgroundColorMenu.setEnabled(false); } else { backgroundColorMenu.setEnabled(true); } } // Handle all other checkboxes super.itemStateChanged(event); }
/** * principal method used to configure the java3D model of the sky. * * @return a BranchGroup with the model contents. */ protected BranchGroup createSceneGraph() { // ##### Transform Groups ################ // Create the transform group node and initialize it to the // identity. Enable the TRANSFORM_WRITE capability so tha // our behavior code can modify it at runtime. objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_READ); objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); // #### Grid ######################################## if (this.showGrid) { gridGroup = new BranchGroup(); gridGroup.setCapability(BranchGroup.ALLOW_DETACH); Group3DSphere sphereRaster = new Group3DSphere(12, 24); Appearance sphereRasterApp = new Appearance(); ColoringAttributes sphereRasterColor = new ColoringAttributes(); sphereRasterColor.setColor(0.0f, 0.41176f, 0.80f); // royal blue sphereRasterApp.setColoringAttributes(sphereRasterColor); sphereRaster.setAppearance(sphereRasterApp); gridGroup = new BranchGroup(); gridGroup.addChild(sphereRaster); objTrans.addChild(gridGroup); } // ##### Equator ######################## equatorGroup = new BranchGroup(); equatorGroup.setCapability(BranchGroup.ALLOW_DETACH); Group3DCircle equator = new Group3DCircle(EQUATOR_RADIUS, EQUATOR_RES); Appearance equatorApp = equator.getAppearance(); ColoringAttributes equatorColor = new ColoringAttributes(); equatorColor.setColor(new Color3f(java.awt.Color.YELLOW)); equatorApp.setColoringAttributes(equatorColor); LineAttributes eqLine = new LineAttributes(); eqLine.setLineWidth(3.0f); equatorApp.setLineAttributes(eqLine); equator.setAppearance(equatorApp); equatorGroup.addChild(equator); objTrans.addChild(equatorGroup); // ###################################### // ##### Axis ########################### axisGroup = new Group3DAxis(); axisGroup.setCapability(BranchGroup.ALLOW_DETACH); objTrans.addChild(axisGroup); // ###################################### objScale = new TransformGroup(); objScale.addChild(objTrans); // ##### root of the branch graph ####### BranchGroup objRoot = new BranchGroup(); objRoot.setCapability(BranchGroup.ALLOW_DETACH); objRoot.addChild(objScale); // ###################################### BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), BOUNDINGSPHERE_RADIUS); // ##### Background ##### Background bgNode = new Background(new Color3f(java.awt.Color.BLACK)); bgNode.setApplicationBounds(bounds); objRoot.addChild(bgNode); // ###################################### // ##### Lighting ####################### // Set up the ambient ligh AmbientLight ambientLightNode = new AmbientLight(); ambientLightNode.setInfluencingBounds(bounds); objRoot.addChild(ambientLightNode); // ###################################### // ##### Behaviour ###################### // Create the rotate behavior node // MouseRotateY behavior = new MouseRotateY(); MouseRotate behavior = new MouseRotate(); behavior.setTransformGroup(objTrans); objTrans.addChild(behavior); behavior.setSchedulingBounds(bounds); // Create the zoom behavior node zoom = new MouseZoom(); zoom.setTransformGroup(objTrans); objTrans.addChild(zoom); zoom.setSchedulingBounds(bounds); // Create the translate behavior node MouseTranslate behavior3 = new MouseTranslate(); behavior3.setTransformGroup(objTrans); objTrans.addChild(behavior3); behavior3.setSchedulingBounds(bounds); // Auto rotator Transform3D axis = new Transform3D(); axis.set(new AxisAngle4f(0, 0, 1, 2)); // theta,x,y,z. Alpha mover = new Alpha(0, ALPHA_INC_DURATION); // loop count, period rotator = new RotationInterpolator(mover, objTrans); rotator.setTransformAxis(axis); rotator.setSchedulingBounds(bounds); objTrans.addChild(rotator); return objRoot; }
public void createBackground(Bounds bounds) { Background background = new Background(); background.setApplicationBounds(bounds); background.setColor(GameColors.BACKGROUND_COLOR); branchGroup.addChild(background); }
public Color getBackgroundColor() { Color3f bgcolor3f = new Color3f(); background.getColor(bgcolor3f); return bgcolor3f.get(); }
public void changeBackgroundColor(Color newColor) { Color3f bgcolor3f = new Color3f(); bgcolor3f.set(newColor); background.setColor(bgcolor3f); }