public void setInput(Input input) throws IOException {
   super.setInput(input);
   Object stuff;
   XStream xstr = XStreamFactory.forVersion(0.2);
   try {
     stuff = xstr.fromXML(input.getReader());
   } catch (Exception e) {
     xstr = XStreamFactory.forVersion(0.1);
     try {
       System.out.println("trying to read JRS v0.1");
       stuff = xstr.fromXML(input.copy().getReader());
       System.out.println("JRS v0.1 success.");
     } catch (Exception e2) {
       throw new IOException("illegal JRS file");
     }
   }
   if (stuff instanceof JrScene) {
     read = (JrScene) stuff;
     root = read.getSceneRoot();
   } else {
     if (stuff instanceof SceneGraphComponent) root = (SceneGraphComponent) stuff;
     else {
       root = new SceneGraphComponent();
       root.setName("jrs");
       SceneGraphUtility.addChildNode(root, (SceneGraphNode) stuff);
     }
   }
 }
 @Override
 public void setContent(SceneGraphNode node) {
   boolean fire = getContentNode() != node;
   if (getContentNode() != null) {
     SceneGraphUtility.removeChildNode(scalingComponent, getContentNode());
   }
   setContentNode(node);
   if (getContentNode() != null) {
     SceneGraphUtility.addChildNode(scalingComponent, getContentNode());
     alignContent();
   }
   if (fire) {
     ContentChangedEvent cce = new ContentChangedEvent(ChangeEventType.ContentChanged);
     cce.node = node;
     fireContentChanged(cce);
   }
 }
 public static void main(String[] args) {
   // a little Scene (two boxes and a bangle, transfomation, appearance)
   IndexedFaceSet box = Primitives.box(2, .5, .5, false);
   IndexedFaceSet box2 = Primitives.box(2, .6, 0.4, true);
   IndexedFaceSet zyl = Primitives.cylinder(20, 1, 0, .5, 5);
   SceneGraphComponent original = new SceneGraphComponent();
   SceneGraphComponent childNode1 = new SceneGraphComponent();
   MatrixBuilder.euclidean().translate(0, 0, 1).assignTo(childNode1);
   SceneGraphComponent childNode2 = new SceneGraphComponent();
   Appearance app = new Appearance();
   app.setAttribute(
       CommonAttributes.POLYGON_SHADER + "." + CommonAttributes.DIFFUSE_COLOR,
       new Color(255, 255, 0));
   childNode2.setAppearance(app);
   original.addChild(childNode1);
   original.addChild(childNode2);
   original.setGeometry(box2);
   childNode1.setGeometry(box);
   childNode2.setGeometry(zyl);
   // the Factory:
   GeometryMergeFactory mergeFact = new GeometryMergeFactory();
   // play with the following 3 optional settings (by default they are true)
   mergeFact.setRespectFaces(true);
   mergeFact.setRespectEdges(true);
   mergeFact.setGenerateVertexNormals(true);
   // you can set some defaults:
   List defaultAtts = new LinkedList();
   List defaultAttValue = new LinkedList();
   List value = new LinkedList();
   defaultAtts.add(Attribute.COLORS);
   defaultAttValue.add(value);
   value.add(new double[] {0, 1, 0, 1}); // remember: only 4d colors
   mergeFact.setDefaultFaceAttributes(defaultAtts, defaultAttValue);
   // merge a list of geometrys:
   // IndexedFaceSet[] list= new IndexedFaceSet[]{box2,zyl};
   // IndexedFaceSet result=mergeFact.mergeIndexedFaceSets(list);
   // or  a complete tree:
   IndexedFaceSet result = mergeFact.mergeGeometrySets(original);
   SceneGraphComponent world = SceneGraphUtility.createFullSceneGraphComponent("world");
   SceneGraphComponent merged = SceneGraphUtility.createFullSceneGraphComponent("merged");
   merged.setGeometry(result);
   MatrixBuilder.euclidean().translate(3, 0, 0).assignTo(merged);
   world.addChildren(merged, original);
   JRViewer.display(world);
 }
Esempio n. 4
0
  @Override
  public void addMesh(double[][][] vertices, double[][][] normals, AppearanceState appearance) {

    if (vertices.length == 0) return;
    int l = vertices[0].length;
    for (int i = 0; i < vertices.length; ++i)
      if (vertices[i].length == 0 || vertices[i].length != l) return;

    SceneGraphComponent sgc = SceneGraphUtility.createFullSceneGraphComponent();
    QuadMeshFactory qmf = new QuadMeshFactory();

    qmf.setVLineCount(vertices.length);
    qmf.setULineCount(l);

    qmf.setClosedInUDirection(false);
    qmf.setClosedInVDirection(false);
    qmf.setVertexCoordinates(vertices);
    if (normals == null) {
      qmf.setGenerateFaceNormals(true);
      // qmf.setGenerateVertexNormals(true);
    } else {
      qmf.setVertexNormals(normals);
    }
    qmf.setGenerateTextureCoordinates(false);
    qmf.setGenerateEdgesFromFaces(true);
    qmf.setEdgeFromQuadMesh(true);

    Color[] colors = new Color[vertices.length * l];
    for (int i = 0; i < vertices.length * l; ++i) {
      colors[i] = appearance.getColor();
    }

    qmf.setVertexColors(colors);

    qmf.update();
    sgc.setGeometry(qmf.getGeometry());

    DefaultGeometryShader dgs =
        ShaderUtility.createDefaultGeometryShader(sgc.getAppearance(), true);

    dgs.setShowLines(true);
    dgs.setShowPoints(true);
    dgs.setShowFaces(true);

    dgs.createPointShader("my");

    MyLineShader ls = (MyLineShader) dgs.createLineShader("my");
    ls.setDiffuseColor(appearance.getColor());
    ls.setLineType(0);

    DefaultPolygonShader dps = (DefaultPolygonShader) dgs.getPolygonShader();

    dps.setTransparency(1 - appearance.getOpacity());

    sceneMeshes.addChild(sgc);
  }
Esempio n. 5
0
  /**
   * ******************************************************************************* loadTemplateSGC
   *
   * <p>This method reads a scene graph component from the input file object. After loading the
   * object, some of the properties of its geometry, like vertex normals, are calculated. This
   * allows properties that are not necessarily stored in the mesh file (like smooth shading) to be
   * applied to the model.
   *
   * <p>Note: Since reading from disk is expensive, this method is only supposed to be used to
   * initialize the "templateSGCs" map. When a marker model is needed, we make a copy of one of the
   * templates in this map.
   * *******************************************************************************
   */
  private static SceneGraphComponent loadTemplateSGC(File ff) {
    SceneGraphComponent sgc = null;

    try {
      sgc = Readers.read(ff);
    } catch (Exception ee) {
      System.err.println("Error: Unable to read model file " + ff);
      ee.printStackTrace();
      System.exit(1);
    }

    GeometryMergeFactory gmf = new GeometryMergeFactory();
    IndexedFaceSet ifs = gmf.mergeIndexedFaceSets(sgc);

    SceneGraphUtility.removeChildren(sgc);
    sgc.setGeometry(ifs);

    return sgc;
  }
 private SceneGraphComponent wrap(SceneGraphNode node) {
   if (node instanceof SceneGraphComponent) return (SceneGraphComponent) node;
   SceneGraphComponent wrap = new SceneGraphComponent("wrapper");
   SceneGraphUtility.addChildNode(wrap, node);
   return wrap;
 }
Esempio n. 7
0
  public JRealityViewer() {
    psf = new PointSetFactory();
    pointCoordinates = new ArrayList<double[]>();
    pointColors = new ArrayList<Color>();
    pointSizes = new ArrayList<Double>();

    psf2 = new PointSetFactory();
    circleCenters = new ArrayList<double[]>();
    circleNormals = new ArrayList<double[]>();
    circleRadii = new ArrayList<Double>();
    circleColors = new ArrayList<Color>();

    ilsf = new IndexedLineSetFactory();
    lineCoordinates = new ArrayList<double[]>();
    lineIndices = new ArrayList<Integer>();
    lineSizes = new ArrayList<Double>();
    lineColors = new ArrayList<Color>();
    lineTypes = new ArrayList<Integer>();

    ifsf = new IndexedFaceSetFactory();
    polygonVertices = new ArrayList<double[][]>();
    polygonColors = new ArrayList<Color>();
    polygonTotalVertexCount = 0;

    sceneRoot = new SceneGraphComponent("root");

    // TODO: Set custom appearances for these components
    scenePoints = SceneGraphUtility.createFullSceneGraphComponent("points");
    scenePoints.setGeometry(psf.getGeometry());
    DefaultGeometryShader dgs =
        ShaderUtility.createDefaultGeometryShader(scenePoints.getAppearance(), true);
    dgs.createPointShader("my");

    sceneCircles = SceneGraphUtility.createFullSceneGraphComponent("circles");
    sceneCircles.setGeometry(psf2.getGeometry());
    dgs = ShaderUtility.createDefaultGeometryShader(sceneCircles.getAppearance(), true);
    dgs.createPointShader("circle");

    sceneLines = SceneGraphUtility.createFullSceneGraphComponent("lines");
    sceneLines.setGeometry(ilsf.getGeometry());
    dgs = ShaderUtility.createDefaultGeometryShader(sceneLines.getAppearance(), true);
    dgs.createPointShader("my");
    dgs.createLineShader("my");

    scenePolygons = new SceneGraphComponent("polygons");
    scenePolygons.setGeometry(ifsf.getGeometry());

    sceneMeshes = SceneGraphUtility.createFullSceneGraphComponent("meshes");

    sceneRoot.addChild(scenePoints);
    sceneRoot.addChild(sceneCircles);
    sceneRoot.addChild(sceneLines);
    sceneRoot.addChild(scenePolygons);
    sceneRoot.addChild(sceneMeshes);

    viewer = new JRViewer();
    viewer.setContent(sceneRoot);
    // viewer.registerPlugin(new DirectContent());
    viewer.registerPlugin(new ContentTools());
    // viewer.registerPlugin(new ContentLoader());
    // viewer.addBasicUI();

    // All plugins from as in BasicUI except shell
    viewer.registerPlugin(new Inspector());
    // viewer.registerPlugin(new Shell());

    viewer.registerPlugin(new BackgroundColor());
    viewer.registerPlugin(new DisplayOptions());
    viewer.registerPlugin(new ViewMenuBar());
    viewer.registerPlugin(new ViewToolBar());

    viewer.registerPlugin(new ExportMenu());
    viewer.registerPlugin(new CameraMenu());
    viewer.registerPlugin(new PropertiesMenu());
    //

    viewer.setShowPanelSlots(false, true, false, false);

    frame = new JFrame("Cindy3D");
    frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(viewer.startupLocal(), BorderLayout.CENTER);
    frame.pack();

    RenderingHintsShader rhs =
        ShaderUtility.createDefaultRenderingHintsShader(
            viewer.getViewer().getSceneRoot().getAppearance(), true);
    rhs.setTransparencyEnabled(true);

    // Set camera near and far plane
    camera = CameraUtility.getCamera(viewer.getViewer());
    camera.setNear(0.1);
    camera.setFar(1000.0);
  }