Exemplo n.º 1
0
  private void createLoaderSceneGraph() {
    loaderBranchGroup = new BranchGroup();
    loaderBranchGroup.setCapability(BranchGroup.ALLOW_DETACH);
    loaderTransformGroup = new TransformGroup();
    loaderTransformGroup.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    addLights(loaderBranchGroup);
    Appearance loaderAppearance = new Appearance();
    Color3f loaderColor = new Color3f(0.5f, 0.5f, 0.5f);
    Material loaderMaterial =
        new Material(loaderColor, new Color3f(), loaderColor, loaderColor, 10);
    loaderMaterial.setLightingEnable(true);
    loaderAppearance.setMaterial(loaderMaterial);
    for (int i = 0; i < 20; i++) {
      Transform3D translate3d = new Transform3D();
      translate3d.setTranslation(new Vector3f(0f, 0f, 0.9f));
      TransformGroup translate = new TransformGroup(translate3d);

      Transform3D rotationY3d = new Transform3D();
      rotationY3d.rotY((Math.PI * 2 * i) / 20);
      TransformGroup rotateY = new TransformGroup(rotationY3d);
      rotateY.addChild(translate);

      Box box = new Box(0.1f, 0.1f, 0.1f, loaderAppearance);
      translate.addChild(box);
      loaderTransformGroup.addChild(rotateY);
    }
    loaderBranchGroup.addChild(loaderTransformGroup);
    sceneBranchGroup.addChild(loaderBranchGroup);
  }
Exemplo n.º 2
0
 private Appearance makeMaterial(float r, float g, float b, float transparency) {
   Appearance appearance = new Appearance();
   Color3f color3f = new Color3f(r, g, b);
   Material material = new Material(color3f, new Color3f(0f, 0f, 0f), color3f, color3f, 10f);
   material.setLightingEnable(true);
   appearance.setMaterial(material);
   if (!Float.isNaN(transparency)) {
     TransparencyAttributes ta = new TransparencyAttributes();
     ta.setTransparencyMode(TransparencyAttributes.NICEST);
     ta.setTransparency(transparency);
     appearance.setTransparencyAttributes(ta);
   }
   return appearance;
 }
Exemplo n.º 3
0
  public MoonSphere() {
    Color3f eColor = new Color3f(1.0f, 1.0f, 1.0f);

    Transform3D t3d = new Transform3D();
    t3d.setTranslation(new Vector3d(0, -100, 0));
    pos.setTransform(t3d);
    addChild(pos);

    // Create a Sphere object, generate one copy of the sphere,
    // and add it into the scene graph.
    Material m = new Material(new Color3f(), eColor, new Color3f(), new Color3f(), 1.0f);
    Appearance a = new Appearance();
    m.setLightingEnable(true);
    a.setMaterial(m);
    Sphere sph = new Sphere(2.0f, Primitive.GENERATE_NORMALS, 80, a);
    pos.addChild(sph);

    pos.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
  }
Exemplo n.º 4
0
  private void setOtherAppearanceValues(int mode, float factor, Color color, boolean visible) {
    // Transparency values
    TransparencyAttributes ta = new TransparencyAttributes(mode, factor);
    ta.setCapability(TransparencyAttributes.ALLOW_VALUE_READ);
    ta.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
    ta.setCapability(TransparencyAttributes.ALLOW_MODE_READ);
    ta.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
    setTransparencyAttributes(ta);

    setCapability(ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
    setCapability(ALLOW_TRANSPARENCY_ATTRIBUTES_WRITE);
    // For highlight
    setCapability(ALLOW_MATERIAL_READ);
    setCapability(ALLOW_MATERIAL_WRITE);
    // For visibility
    setCapability(ALLOW_RENDERING_ATTRIBUTES_READ);
    setCapability(ALLOW_RENDERING_ATTRIBUTES_WRITE);
    // Color
    setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
    setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);

    // Adding Rendering attributes to access visibility flag if layer is available
    if (layer != null) {
      RenderingAttributes ra = new RenderingAttributes();
      ra.setCapability(RenderingAttributes.ALLOW_VISIBLE_READ);
      ra.setCapability(RenderingAttributes.ALLOW_VISIBLE_WRITE);
      ra.setCapability(RenderingAttributes.ALLOW_DEPTH_ENABLE_READ);
      ra.setCapability(RenderingAttributes.ALLOW_DEPTH_ENABLE_WRITE);
      ra.setVisible(visible);
      setRenderingAttributes(ra);
      if (mode != TransparencyAttributes.NONE) ra.setDepthBufferEnable(true);
    }

    // Set up the polygon attributes
    // PolygonAttributes pa = new PolygonAttributes();
    // pa.setCullFace(PolygonAttributes.CULL_NONE);
    // pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
    // setPolygonAttributes(pa);

    // TextureAttributes texAttr = new TextureAttributes();
    // texAttr.setTextureMode(TextureAttributes.MODULATE);
    // texAttr.setTextureColorTable(pattern);
    // setTextureAttributes(texAttr);

    // LineAttributes lineAttr = new LineAttributes();
    // lineAttr.setLineAntialiasingEnable(true);
    // setLineAttributes(lineAttr);

    // Adding to internal material
    //				Material mat = new Material(objColor, black, objColor, white, 70.0f);
    if (color != null) {
      Color3f objColor = new Color3f(color);
      // Emissive is black and specular is plastic!
      // Color3f specular = new Color3f(color.brighter());
      Material mat =
          new Material(objColor, J3DUtils.black, objColor, J3DUtils.plastic /*J3DUtils.white*/, 17);
      mat.setLightingEnable(true);
      mat.setCapability(Material.ALLOW_COMPONENT_READ);
      mat.setCapability(Material.ALLOW_COMPONENT_WRITE);
      mat.setCapability(Material.AMBIENT_AND_DIFFUSE);
      setMaterial(mat);
    }
  }
  /**
   * Build the 3D scene on a Java3D enabled draw.
   *
   * @param objRot the transform object
   */
  @Override
  public void buildJava3DScene(TransformGroup objRot) {
    if (objRot == null) return;

    AtomInfo ai = AtomInfo.getInstance();
    double xMin = gridProperty.getBoundingBox().getUpperLeft().getX(),
        yMin = gridProperty.getBoundingBox().getUpperLeft().getY(),
        zMin = gridProperty.getBoundingBox().getUpperLeft().getZ();
    double f1, f2, f3;
    double[] functionValues = gridProperty.getFunctionValues();
    double incInX = gridProperty.getXIncrement(),
        incInY = gridProperty.getYIncrement(),
        incInZ = gridProperty.getZIncrement();
    int pointsAlongX = gridProperty.getNoOfPointsAlongX(),
        pointsAlongY = gridProperty.getNoOfPointsAlongY(),
        pointsAlongZ = gridProperty.getNoOfPointsAlongZ();

    int i, j, k;

    Atom atom;
    String symbol;
    SphereTriSetGenerator stsg;
    Point3DI point1, point2, point3;

    int noOfAtoms = molecule.getNumberOfAtoms();
    int pointsAlongSlice = pointsAlongY * pointsAlongZ;
    HashMap<String, SphereTriSetGenerator> triSetTableTmp =
        new HashMap<String, SphereTriSetGenerator>(5);

    if (noOfAtoms > largeMoleculeSize) triSetTableTmp = triSetTable;

    Matrix3D tmat = new Matrix3D();
    tmat.unit();

    TransformGroup surfaceScene = new TransformGroup();

    for (int atomIndex = 0; atomIndex < noOfAtoms; atomIndex++) {
      atom = molecule.getAtom(atomIndex);

      // check to see if we are with in the bounding box?
      if (!gridProperty.getBoundingBox().contains(atom.getAtomCenter())) continue;

      symbol = atom.getSymbol();

      if ((stsg = triSetTableTmp.get(symbol)) == null) {
        stsg =
            new SphereTriSetGenerator(
                atom.getAtomCenter(), ai.getVdwRadius(symbol), numberOfSphereDivisions);
        triSetTableTmp.put(symbol, stsg);
      } // end if

      // setup color
      Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
      Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
      Color3f objColor = new Color3f(colorMap.getInterpolatedColor(currentFunctionValue));

      Appearance app = new Appearance();
      Material mm = new Material(objColor, eColor, objColor, sColor, 100.0f);
      mm.setLightingEnable(true);
      app.setMaterial(mm);

      if (fillTransperency > 0) {
        app.setTransparencyAttributes(
            new TransparencyAttributes(
                TransparencyAttributes.NICEST, (float) (fillTransperency / 255.0)));
      } // end if

      int idx = 0;

      stsg.setTransform(tmat);
      stsg.setCenter(atom.getAtomCenter());

      ArrayList<Point3d> validPoints = new ArrayList<Point3d>();
      ArrayList<Color3f> validColors = new ArrayList<Color3f>();

      while (true) {
        Triangle t = stsg.nextVisibleTriSet();

        if (t == null) break;

        // first point
        point1 = t.getPoint1();
        i = (int) Math.round(Math.abs((point1.getX() - xMin) / incInX));
        j = (int) Math.round(Math.abs((point1.getY() - yMin) / incInY));
        k = (int) Math.round(Math.abs((point1.getZ() - zMin) / incInZ));

        if (i > pointsAlongX - 1) i = pointsAlongX - 1;
        if (j > pointsAlongY - 1) j = pointsAlongY - 1;
        if (k > pointsAlongZ - 1) k = pointsAlongZ - 1;

        f1 = functionValues[(i * pointsAlongSlice) + (j * pointsAlongZ) + k];

        // second point
        point2 = t.getPoint2();
        i = (int) Math.round(Math.abs((point2.getX() - xMin) / incInX));
        j = (int) Math.round(Math.abs((point2.getY() - yMin) / incInY));
        k = (int) Math.round(Math.abs((point2.getZ() - zMin) / incInZ));

        if (i > pointsAlongX - 1) i = pointsAlongX - 1;
        if (j > pointsAlongY - 1) j = pointsAlongY - 1;
        if (k > pointsAlongZ - 1) k = pointsAlongZ - 1;

        f2 = functionValues[(i * pointsAlongSlice) + (j * pointsAlongZ) + k];

        // third point
        point3 = t.getPoint3();
        i = (int) Math.round(Math.abs((point3.getX() - xMin) / incInX));
        j = (int) Math.round(Math.abs((point3.getY() - yMin) / incInY));
        k = (int) Math.round(Math.abs((point3.getZ() - zMin) / incInZ));

        if (i > pointsAlongX - 1) i = pointsAlongX - 1;
        if (j > pointsAlongY - 1) j = pointsAlongY - 1;
        if (k > pointsAlongZ - 1) k = pointsAlongZ - 1;

        f3 = functionValues[(i * pointsAlongSlice) + (j * pointsAlongZ) + k];

        validPoints.add(new Point3d(point1.getX(), point1.getY(), point1.getZ()));
        validColors.add(new Color3f(colorMap.getInterpolatedColor(f1)));

        validPoints.add(new Point3d(point2.getX(), point2.getY(), point2.getZ()));
        validColors.add(new Color3f(colorMap.getInterpolatedColor(f2)));

        validPoints.add(new Point3d(point3.getX(), point3.getY(), point3.getZ()));
        validColors.add(new Color3f(colorMap.getInterpolatedColor(f3)));
      } // end while

      TriangleArray ta =
          new TriangleArray(validPoints.size(), GeometryArray.COORDINATES | GeometryArray.COLOR_3);

      for (idx = 0; idx < validPoints.size(); idx++) {
        ta.setCoordinate(idx, validPoints.get(idx));
        ta.setColor(idx, validColors.get(idx));
      }

      validColors = null;
      validPoints = null;

      GeometryInfo gi = new GeometryInfo(ta);
      gi.compact();
      gi.recomputeIndices();

      // generate normals
      NormalGenerator ng = new NormalGenerator();
      ng.generateNormals(gi);

      // stripify
      Stripifier st = new Stripifier();
      st.stripify(gi);

      surfaceScene.addChild(new Shape3D(gi.getGeometryArray(), app));
    } // end for

    if (showBoundingBox) {
      boundingBox.setTransform(transform);
      boundingBox.applyTransforms();
      (new ScreenCuboidJ3D(boundingBox)).buildJava3DScene(surfaceScene);
    } // end if

    objRot.addChild(surfaceScene);
  }