예제 #1
0
  private void loadSheep() {
    VrmlLoader loader = new VrmlLoader();
    Scene myScene = null;
    try {
      myScene = loader.load("models" + File.separator + "Sheep.wrl");
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IncorrectFormatException e) {
      e.printStackTrace();
    } catch (ParsingErrorException e) {
      e.printStackTrace();
    }

    BranchGroup bg = new BranchGroup();
    TransformGroup offset = new TransformGroup();
    Transform3D t3d = new Transform3D();
    t3d.setRotation(new AxisAngle4d(1.0, 0.0, 0.0, Math.PI / 2));
    t3d.setTranslation(new Vector3d(0.0, 0.0, 0.025));
    offset.setTransform(t3d);
    bg.addChild(offset);
    offset.addChild(myScene.getSceneGroup());

    sheepObject = new ModelObject(bg);

    viewer.addObject(sheepObject);
  }
예제 #2
0
  public BranchGroup createSceneGraph(URL url) {

    try {
      Loader loader = new com.sun.j3d.loaders.objectfile.ObjectFile();
      Scene scene = loader.load(url);
      BranchGroup bg = scene.getSceneGroup();
      System.out.println(bg);
      TransformGroup[] views = scene.getViewGroups();
      if (views != null) {
        for (int i = 0; i < views.length; i++) {
          System.out.print(views[i]);
        }
        if (views.length > 0) viewStart = views[0];
      }
      return bg;
    } catch (Exception ex) {
      // in case there was a problem, print the stack out
      ex.printStackTrace();
      // System.out.println(ex);
      add("South", new Label(ex.toString()));
      System.out.println("URL: " + url);
      BranchGroup bg = new BranchGroup();
      bg.addChild(new ColorCube());
      System.out.println(bg);
      return bg;
    }
  }
예제 #3
0
  @SuppressWarnings("unchecked")
  public void openFile(File fx) {
    saveFile = null;
    File file;
    if (fx == null) file = showOpenImageDialog();
    else file = fx;
    if (file != null) {
      if ("3ds".equalsIgnoreCase(FileUtil.getFileExtension(file))) {
        if (lobj != null) render.removeObj3D(lobj);
        Inspector3DS loader = new Inspector3DS(file.getAbsolutePath()); // constructor
        loader.parseIt(); // process the file
        TransformGroup theModel1 = loader.getModel(); // get the
        // resulting 3D
        NeptusLog.waste().info("Point to view window " + Util3D.getModelDim(theModel1));
        lobj = new Obj3D();
        lobj.setModel3D(theModel1);
        // lobj.setRoll(Math.PI/2);
        render.addObj3D(lobj);

        update3dFilesOpened(file);
      } else if ("wrl".equalsIgnoreCase(FileUtil.getFileExtension(file))) {

        /*
         * try {
         *
         * if (lobj != null) render.removeObj3D(lobj);
         *
         * String filename = file.getAbsolutePath(); VrmlLoader loader =
         * new VrmlLoader();
         *
         * BufferedReader in = null; in = new BufferedReader(new
         * InputStreamReader(new FileInputStream(filename), "UTF8"));
         *
         * URL url = FileUtil.pathToURL(file.getParent()+"/");
         * loader.setBaseUrl(url);
         *
         * Scene scene = loader.load(in); BranchGroup branch =
         * scene.getSceneGroup();
         *
         * TransformGroup ret = new TransformGroup(); Enumeration<Group>
         * enume = branch.getAllChildren(); while
         * (enume.hasMoreElements()) { Group next = enume.nextElement();
         * branch.removeChild(next); ret.addChild(next); }
         * lobj.setModel3D(ret); //lobj.setRoll(Math.PI / 2);
         * render.addObj3D(lobj);
         *
         *
         * } catch (Throwable e) { e.printStackTrace();
         * GuiUtils.errorMessage(this, "Load",
         * "Error Loading WRL File");
         *
         * }
         */

        // --------------------------------------------------------
        if (lobj != null) render.removeObj3D(lobj);

        Loader myFileLoader = null; // holds the file loader
        Scene myVRMLScene = null; // holds the loaded scene
        BranchGroup myVRMLModel = null; // BG of the VRML scene
        try {
          // create an instance of the Loader
          myFileLoader = new org.web3d.j3d.loaders.VRML97Loader();

          myFileLoader.setBasePath(file.getParent());
          myFileLoader.setFlags(
              myFileLoader.getFlags() | org.web3d.j3d.loaders.VRML97Loader.LOAD_BEHAVIOR_NODES);
          // Load the scene from your VRML97 file
          myVRMLScene = myFileLoader.load(file.getAbsolutePath());

          // Obtain the root BranchGroup for the Scene
          myVRMLModel = myVRMLScene.getSceneGroup();
          lobj = new Obj3D();
          TransformGroup scene = new TransformGroup();
          Enumeration<Node> enume = myVRMLModel.getAllChildren();
          while (enume.hasMoreElements()) {
            Node next = enume.nextElement();
            myVRMLModel.removeChild(next);
            scene.addChild(next);
          }
          lobj.setModel3D(scene);
          // lobj.setRoll(Math.PI / 2);
          render.addObj3D(lobj);

          /*
           * VrmlLoader f = new VrmlLoader(); Scene s = null;
           * BranchGroup myVRMLModel = null; //BG of the VRML scene
           * try { //f.setFlags(VrmlLoader.LOAD_ALL); s =
           * f.load(file.getAbsolutePath()); // Obtain the root
           * BranchGroup for the Scene myVRMLModel =
           * s.getSceneGroup(); lobj = new Obj3D(); TransformGroup
           * scene = new TransformGroup(); Enumeration<Group> enume =
           * myVRMLModel.getAllChildren(); while
           * (enume.hasMoreElements()) { Group next =
           * enume.nextElement(); myVRMLModel.removeChild(next);
           * scene.addChild(next); } lobj.setModel3D(scene);
           * //lobj.setRoll(Math.PI / 2); render.addObj3D(lobj); }
           * catch (FileNotFoundException e) { e.printStackTrace();
           * GuiUtils.errorMessage(this, "Load",
           * "Error Loading WRL File"); }
           */

          update3dFilesOpened(file);

          System.err.println("fez o load----------");
        } catch (Exception e) {
          // in case there was a problem, print the stack out
          e.printStackTrace();
          // we still need a model, even if we can't load the right
          // one, I use a color cube just in case
          GuiUtils.errorMessage(this, "Load", "Error Loading WRL File");
        }

      } else if ("x3d".equalsIgnoreCase(FileUtil.getFileExtension(file))
          || "x3dv".equalsIgnoreCase(FileUtil.getFileExtension(file))) {

        if (lobj != null) render.removeObj3D(lobj);

        Loader myFileLoader = null; // holds the file loader
        Scene myVRMLScene = null; // holds the loaded scene
        BranchGroup myVRMLModel = null; // BG of the VRML scene
        try {
          // create an instance of the Loader
          myFileLoader = new org.web3d.j3d.loaders.X3DLoader();
          myFileLoader.setBasePath(file.getParent());

          // myFileLoader.setFlags(org.web3d.j3d.loaders.X3DLoader.LOAD_ALL);
          // Load the scene from your VRML97 file
          myVRMLScene = myFileLoader.load(file.getAbsolutePath());

          // Obtain the root BranchGroup for the Scene
          myVRMLModel = myVRMLScene.getSceneGroup();
          lobj = new Obj3D();
          TransformGroup scene = new TransformGroup();
          Enumeration<Group> enume = myVRMLModel.getAllChildren();
          while (enume.hasMoreElements()) {
            Group next = enume.nextElement();
            myVRMLModel.removeChild(next);
            scene.addChild(next);
          }
          lobj.setModel3D(scene);
          // lobj.setRoll(Math.PI / 2);
          render.addObj3D(lobj);

          update3dFilesOpened(file);

          System.err.println("fez o load----------");
        } catch (Exception e) {
          // in case there was a problem, print the stack out
          e.printStackTrace();
          // we still need a model, even if we can't load the right
          // one, I use a color cube just in case
          GuiUtils.errorMessage(this, "Load", "Error Loading x3D File");
        }

      } else if ("j3d".equalsIgnoreCase(FileUtil.getFileExtension(file))) {

        if (lobj != null) render.removeObj3D(lobj);

        BranchGroup bg = null;

        try {
          SceneGraphFileReader filer = new SceneGraphFileReader(file);
          bg = (filer.readAllBranchGraphs())[0];
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        if (bg == null) {
          NeptusLog.pub().error("Error loading vehicle model\n" + this);
        }
        TransformGroup scene = new TransformGroup();

        Enumeration<Group> enume = bg.getAllChildren();
        while (enume.hasMoreElements()) {
          Group next = enume.nextElement();
          bg.removeChild(next);
          scene.addChild(next);
        }
        lobj = new Obj3D();
        lobj.setModel3D(scene);
        // lobj.setRoll(Math.PI / 2);
        render.addObj3D(lobj);

        update3dFilesOpened(file);
      } else GuiUtils.errorMessage(this, "Load", "Invalid file type.");
    }
  }