Example #1
0
  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);
  }
  /**
   * Creates a ViewingPlatform from attributes gathered by this object.
   *
   * @param transformCount the number of TransformGroups to attach to the ViewingPlatform
   * @return the new ViewingPlatform
   */
  ViewingPlatform createViewingPlatform(int transformCount) {

    // Get the Viewers attached to this ViewingPlatform.
    // All ConfigViews must be processed at this point.
    if (configViews.size() == 0) {
      viewers = new Viewer[0];
    } else {
      viewers = new Viewer[configViews.size()];
      for (int i = 0; i < viewers.length; i++)
        viewers[i] = ((ConfigView) configViews.get(i)).j3dViewer;
    }

    // Create the viewing platform and get its ViewPlatform instance.
    viewingPlatform = new ViewingPlatform(transformCount);
    ViewPlatform vp = viewingPlatform.getViewPlatform();

    // Set defined policies.
    if (allowPolicyRead) vp.setCapability(ViewPlatform.ALLOW_POLICY_READ);

    if (allowLocalToVworldRead) vp.setCapability(Node.ALLOW_LOCAL_TO_VWORLD_READ);

    if (viewAttachPolicy == -1) {
      // Apply a default based on the eyepoint policy.
      boolean nominalHead = true;
      for (int i = 0; i < viewers.length; i++) {
        if (viewers[i].getView().getWindowEyepointPolicy() != View.RELATIVE_TO_FIELD_OF_VIEW) {
          nominalHead = false;
          break;
        }
      }
      if (nominalHead) vp.setViewAttachPolicy(View.NOMINAL_HEAD);
      else vp.setViewAttachPolicy(View.NOMINAL_SCREEN);
    } else {
      vp.setViewAttachPolicy(viewAttachPolicy);
    }

    // Assign the viewing platform to all viewers.
    for (int i = 0; i < viewers.length; i++) {
      viewers[i].setViewingPlatform(viewingPlatform);
    }

    // Apply initial viewing transforms if defined.
    if (nominalViewingTransform) {
      viewingPlatform.setNominalViewingTransform();
    }

    if (initialViewingTransform != null) {
      TransformGroup tg = viewingPlatform.getViewPlatformTransform();
      tg.setTransform(initialViewingTransform);
    }

    return viewingPlatform;
  }