HI,
I'm very frustrated at trying to get the initial view of my Java 3D World to be correct. I am loading in a VRML file and would like my view to come up showing the VRML object using as much of the display window as possible. A portion of my code is shown below. Can anyone give me some pointers so that no matter what VRML file I retrieve into my world, the view will automatically be calculated so that the VRML object will be displayed as big as possible. Also, can anyone tell me where the origin of my back clipping plane starts and if I have to reset my clipping plane every time I change my view (for example zooming in or out)? Thanks for you help Dean. Following is my code. public CNC4Machine(int w, int h, String sceneFile) { this.setSize(w, h); this.setLayout(new BorderLayout()); canvas3D = new Canvas3D(null); canvas3D.setSize(this.getSize().width,this.getSize().height); universe = new VirtualUniverse(); locale = new Locale(universe); physicalBody = new PhysicalBody(); physicalEnv = new PhysicalEnvironment(); view = new View(); view.addCanvas3D(canvas3D); view.setPhysicalBody(physicalBody); view.setPhysicalEnvironment(physicalEnv); view.setBackClipDistance(1000.0); view.setFrontClipDistance(0.001); vpRoot = new BranchGroup(); Transform3D t3d = new Transform3D(); t3d.set(new Vector3f(0.0f, 0.0f, 0.0f)); vpTrans = new TransformGroup(t3d); vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); vpTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); vp = new ViewPlatform(); vpTrans.addChild(vp); vpRoot.addChild(vpTrans); view.attachViewPlatform(vp); locale.addBranchGraph(vpRoot); sceneRoot = new BranchGroup(); TransformGroup worldTrans = new TransformGroup(); worldTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); worldTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); worldTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND); worldTrans.setCapability(TransformGroup.ALLOW_CHILDREN_READ); worldTrans.setCapability(TransformGroup.ALLOW_CHILDREN_WRITE); sceneRoot.addChild(worldTrans); BoundingSphere behaviorBounds = new BoundingSphere(new Point3d(), Double.MAX_VALUE); Background bg = new Background(0.1f, 0.4f, 0.7f); bg.setApplicationBounds(behaviorBounds); sceneRoot.addChild(bg Scene scene = null;
VrmlLoader loader = new VrmlLoader(); try { URL loadURL = new URL(sceneFile); try { scene = loader.load(loadURL); } catch (Exception e) { System.out.println("Exception loading URL:" + e); } } catch (MalformedURLException badURL) { try { scene = loader.load(sceneFile); } catch (Exception e) { System.out.println("Exception loading file from path: " + e.getMessage()); } } if (scene != null) { machineRoot = scene.getSceneGroup(); machineRoot.setCapability(BranchGroup.ALLOW_DETACH); machineRoot.setCapability(BranchGroup.ALLOW_BOUNDS_READ); machineRoot.setCapability(BranchGroup.ALLOW_BOUNDS_WRITE); worldTrans.addChild(machineRoot); Transform3D scaleTrans = new Transform3D(); scaleTrans.setIdentity(); scaleTrans.setScale(1.0); Transform3D t1 = new Transform3D(); Matrix3d m1 = new Matrix3d(); m1.rotZ(180.0 * (Math.PI / 180.0)); t1.set(m1); scaleTrans.mul(t1); t1.setZero(); m1.setZero(); m1.rotX(-90.0 * (Math.PI / 180.0)); t1.set(m1); scaleTrans.mul(t1); worldTrans.setTransform(scaleTrans); } locale.addBranchGraph(sceneRoot); BoundingSphere machineBounds = (BoundingSphere)machineRoot.getBounds(); System.out.println("MachineBounds = " + machineBounds.toString()); SetViewPoint(machineBounds); this.add(canvas3D, BorderLayout.CENTER); } private void SetViewPoint(BoundingSphere bounds) { Transform3D viewTrans = new Transform3D(); Transform3D eyeTrans = new Transform3D(); Point3d center = new Point3d(); bounds.getCenter(center); Vector3d temp = new Vector3d(center); temp.z = 0.0; System.out.println("temp = " + temp.toString()); viewTrans.set(temp); double eyeDist = (1.5 * bounds.getRadius()) / (view.getFieldOfView() / 2.0); temp.x = 0.0; temp.y = 0.0; temp.z = 1600.0; eyeTrans.set(temp); viewTrans.mul(eyeTrans); System.out.println("viewTrans = " + viewTrans.toString()); vpTrans.setTransform(viewTrans); view.setBackClipDistance(eyeDist); } |