Ejemplo n.º 1
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;
    }
  }
Ejemplo n.º 2
0
  public BranchGroup createSceneGraph() {
    // Create the root of the branch graph
    BranchGroup objRoot = new BranchGroup();

    // Create the TransformGroup node and initialize it to the
    // identity. Enable the TRANSFORM_WRITE capability so that
    // our behavior code can modify it at run time. Add it to
    // the root of the subgraph.
    TransformGroup objTrans = new TransformGroup();
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objRoot.addChild(objTrans);

    // Create a simple Shape3D node; add it to the scene graph.
    objTrans.addChild(new ColorCube(0.4));

    // Create a new Behavior object that will perform the
    // desired operation on the specified transform and add
    // it into the scene graph.
    Transform3D yAxis = new Transform3D();
    Alpha rotationAlpha = new Alpha(-1, 4000);

    RotationInterpolator rotator =
        new RotationInterpolator(rotationAlpha, objTrans, yAxis, 0.0f, (float) Math.PI * 1.0f);
    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
    rotator.setSchedulingBounds(bounds);
    objRoot.addChild(rotator);

    // Have Java 3D perform optimizations on this scene graph.
    objRoot.compile();

    return objRoot;
  }
Ejemplo n.º 3
0
  // 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);
  }
Ejemplo n.º 4
0
 public World() {
   universe = new VirtualUniverse();
   locale = new Locale(universe);
   viewGroup = new BranchGroup();
   objGroup = new BranchGroup();
   viewGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
   objGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND);
 }
Ejemplo n.º 5
0
  /**
   * Simple algorithm to add or remove an edge from the selection Ideas: 1. Use one shape per quad
   * => easy, but can become very fast too big to fit in memory. => pb: flicker when adding /
   * removing because the scene is detached 2. Use only one shape, modify geometry TODO: pre-create
   * an empty shape to avoid initial flicker
   *
   * @param on
   * @param p
   * @param u
   * @param v
   * @param groupIdx2
   */
  protected void toggleSelectedEdge(boolean on, SelectionEdge se) {
    if (on) {
      // add the given edge to the list
      if (edgeSelection.contains(se)) return; // already in
      edgeSelection.add(se);
    } else {
      // remove the given edge from the list
      if (!edgeSelection.contains(se)) return; // not present
      edgeSelection.remove(se);
      if (edgeSelection.size() == 0) {
        LineArray la = (LineArray) edgeSelectionShape.getGeometry();
        la.setValidVertexCount(0);
        return;
      }
    }
    // Use in-memory arrays instead of NIO because edgeSelection should not
    // be too big
    // => faster
    LineArray la =
        new LineArray(
            edgeSelection.size() * 2,
            GeometryArray.COORDINATES | GeometryArray.COLOR_3 | GeometryArray.BY_REFERENCE);
    double[] coords = new double[edgeSelection.size() * 2 * 3];
    float[] colors = new float[edgeSelection.size() * 2 * 3];

    for (int i = 0; i < coords.length; i += 6) {
      SelectionEdge edge = edgeSelection.get(i / 6);
      edge.updateCoords(grid, coords, i);
      edge.updateColors(colors, i);
    }
    la.setCoordRefDouble(coords);
    la.setColorRefFloat(colors);
    la.setCapability(GeometryArray.ALLOW_COUNT_WRITE);
    // update edgeSelection Shape with the new edgeSelection list
    if (edgeSelectionShape == null) {
      Appearance a = new Appearance();
      // PolygonAttributes pa = new
      // PolygonAttributes(PolygonAttributes.POLYGON_LINE,
      // PolygonAttributes.CULL_NONE, 0);
      // pa.setPolygonOffset(1); // above edges
      // pa.setPolygonOffsetFactor(1);
      LineAttributes lat = new LineAttributes();
      lat.setLineWidth(2.0f);
      lat.setLineAntialiasingEnable(true);
      a.setLineAttributes(lat);
      // a.setPolygonAttributes(pa);
      edgeSelectionShape = new Shape3D(la, a);
      edgeSelectionShape.setUserData(this);
      edgeSelectionShape.setPickable(false);
      edgeSelectionShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
      edgeSelectionShape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);

      BranchGroup bg = new BranchGroup();
      bg.addChild(edgeSelectionShape);
      addChild(bg);
    } else edgeSelectionShape.setGeometry(la);
  }
  // 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);
  }
Ejemplo n.º 7
0
  private BranchGroup setApperancePackInBranchGroup(Shape3D shape, Node handle) {
    shape.setUserData(this);

    shape.setCapability(Shape3D.ALLOW_APPEARANCE_WRITE);

    BranchGroup bg = new BranchGroup();
    bg.setCapability(BranchGroup.ALLOW_DETACH);
    bg.addChild(handle);
    bg.compile();
    return bg;
  }
Ejemplo n.º 8
0
  /**
   * Simple algorithm to add or remove a quad from the selection Ideas: 1. Use one shape per quad =>
   * easy, but can become very fast too big to fit in memory. => pb: flicker when adding / removing
   * because the scene is detached 2. Use only one shape, modify geometry TODO: pre-create an empty
   * shape to avoid initial flicker
   *
   * @param on
   * @param p
   * @param u
   * @param v
   * @param groupIdx2
   */
  protected void toggleSelectedQuad(boolean on, SelectionQuad sq) {
    LOGGER.finest("on=" + on + " selectionQuad=" + sq);
    if (on) {
      // add the given quad to the list
      if (selection.contains(sq)) return; // already in
      selection.add(sq);
    } else {
      // remove the given quad from the list
      if (!selection.contains(sq)) return; // not present
      selection.remove(sq);
      if (selection.size() == 0) {
        QuadArray qa = (QuadArray) selectionShape.getGeometry();
        qa.setValidVertexCount(0);
        return;
      }
    }
    // Use in-memory arrays instead of NIO because selection should not be
    // too big
    // => faster
    QuadArray qa =
        new QuadArray(
            selection.size() * 4,
            GeometryArray.COORDINATES | GeometryArray.COLOR_3 | GeometryArray.BY_REFERENCE);
    float[] coords = new float[selection.size() * 4 * 3];
    float[] colors = new float[selection.size() * 4 * 3];

    for (int i = 0; i < coords.length; i += 12) {
      SelectionQuad quad = selection.get(i / 12);
      quad.updateCoords(grid, coords, i);
      quad.updateColors(colors, i);
    }
    qa.setCoordRefFloat(coords);
    qa.setColorRefFloat(colors);
    qa.setCapability(GeometryArray.ALLOW_COUNT_WRITE);
    // update selection Shape with the new selection list
    if (selectionShape == null) {
      Appearance a = new Appearance();
      PolygonAttributes pa =
          new PolygonAttributes(PolygonAttributes.POLYGON_FILL, PolygonAttributes.CULL_NONE, 0);
      pa.setPolygonOffset(0.5f); // between faces and edges
      pa.setPolygonOffsetFactor(0.5f);
      a.setPolygonAttributes(pa);
      selectionShape = new Shape3D(qa, a);
      selectionShape.setUserData(this);
      selectionShape.setPickable(false);
      selectionShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
      selectionShape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
      BranchGroup bg = new BranchGroup();
      bg.addChild(selectionShape);
      addChild(bg);
    } else selectionShape.setGeometry(qa);
  }
  private void labelAxes()
        // Place numbers along the X- and Z-axes at the integer positions
      {
    Vector3d pt = new Vector3d();
    for (int i = -FLOOR_LEN / 2; i <= FLOOR_LEN / 2; i++) {
      pt.x = i;
      floorBG.addChild(makeText(pt, "" + i)); // along x-axis
    }

    pt.x = 0;
    for (int i = -FLOOR_LEN / 2; i <= FLOOR_LEN / 2; i++) {
      pt.z = i;
      floorBG.addChild(makeText(pt, "" + i)); // along z-axis
    }
  } // end of labelAxes()
  // Some light is added to the scene here.
  public void addLight(SimpleUniverse su) {

    BranchGroup bgLight = new BranchGroup();

    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.MAX_VALUE);

    // Directional light.
    Color3f lightColour = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir = new Vector3f(1.0f, -1.0f, -1.0f);
    DirectionalLight light = new DirectionalLight(lightColour, lightDir);
    light.setInfluencingBounds(bounds);
    bgLight.addChild(light);

    // Ambient light.
    Color3f ambientLightColour = new Color3f(0.5f, 0.5f, 0.5f);
    AmbientLight ambLight = new AmbientLight(ambientLightColour);
    ambLight.setInfluencingBounds(bounds);
    bgLight.addChild(ambLight);

    su.addBranchGraph(bgLight);
  }
Ejemplo n.º 11
0
  public CheckerFloor()
        // create tiles, add origin marker, then the axes labels
      {
    ArrayList blueCoords = new ArrayList();
    ArrayList greenCoords = new ArrayList();
    floorBG = new BranchGroup();

    boolean isBlue;
    for (int z = -FLOOR_LEN / 2; z <= (FLOOR_LEN / 2) - 1; z++) {
      isBlue = (z % 2 == 0) ? true : false; // set colour for new row
      for (int x = -FLOOR_LEN / 2; x <= (FLOOR_LEN / 2) - 1; x++) {
        if (isBlue) createCoords(x, z, blueCoords);
        else createCoords(x, z, greenCoords);
        isBlue = !isBlue;
      }
    }
    floorBG.addChild(new ColouredTiles(blueCoords, blue));
    floorBG.addChild(new ColouredTiles(greenCoords, green));

    addOriginMarker();
    labelAxes();
  } // end of CheckerFloor()
Ejemplo n.º 12
0
  // Directional light rotating around the scene and some ambient light.
  public void addLight(SimpleUniverse su) {

    BranchGroup bgLight = new BranchGroup();

    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), Double.MAX_VALUE);

    // Directional light (to be rotated).
    Color3f lightColour = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lightDir = new Vector3f(0.0f, 0.0f, -1.0f);
    DirectionalLight light = new DirectionalLight(lightColour, lightDir);
    light.setInfluencingBounds(bounds);

    // The transformation group for the directional light and its rotation.
    TransformGroup tfmLight = new TransformGroup();
    tfmLight.addChild(light);

    // The Alpha for the rotation.
    Alpha alphaLight = new Alpha(-1, 4000);
    // The rotation
    RotationInterpolator rot =
        new RotationInterpolator(
            alphaLight, tfmLight, new Transform3D(), 0.0f, (float) Math.PI * 2);
    rot.setSchedulingBounds(bounds);

    tfmLight.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    tfmLight.addChild(rot);

    bgLight.addChild(tfmLight);

    // Ambient light.
    Color3f ambientLightColour = new Color3f(0.5f, 0.5f, 0.5f);
    AmbientLight ambLight = new AmbientLight(ambientLightColour);
    ambLight.setInfluencingBounds(bounds);
    bgLight.addChild(ambLight);

    su.addBranchGraph(bgLight);
  }
Ejemplo n.º 13
0
  private void addOriginMarker()
        // A red square centered at (0,0,0), of length 0.5
      { // points created counter-clockwise, a bit above the floor
    Point3f p1 = new Point3f(-0.25f, 0.01f, 0.25f);
    Point3f p2 = new Point3f(0.25f, 0.01f, 0.25f);
    Point3f p3 = new Point3f(0.25f, 0.01f, -0.25f);
    Point3f p4 = new Point3f(-0.25f, 0.01f, -0.25f);

    ArrayList oCoords = new ArrayList();
    oCoords.add(p1);
    oCoords.add(p2);
    oCoords.add(p3);
    oCoords.add(p4);

    floorBG.addChild(new ColouredTiles(oCoords, medRed));
  } // end of addOriginMarker();
Ejemplo n.º 14
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);
  }
Ejemplo n.º 15
0
 public void activate() {
   viewGroup.compile();
   objGroup.compile();
   locale.addBranchGraph(viewGroup);
   locale.addBranchGraph(objGroup);
 }
Ejemplo n.º 16
0
 public void addChild(Node obj) {
   objGroup.addChild(obj);
 }
Ejemplo n.º 17
0
 public void addBehavior(Behavior behavior) {
   viewGroup.addChild(behavior);
 }
Ejemplo n.º 18
0
 public void addViewer(Viewer viewer) {
   viewGroup.addChild(viewer.getRoot());
 }