Package javax.media.j3d

Examples of javax.media.j3d.Appearance


    /**
     * Returns the material of the appearance at <code>effectId</code>.
     */
    private Material getAppearanceMaterial(String effectId) {
      Appearance appearance = this.effectAppearances.get(effectId);
      Material material = appearance.getMaterial();
      if (material == null) {
        material = new Material();
        appearance.setMaterial(material);
      }
      return material;
    }
View Full Code Here


      }
    } else if (node instanceof Link) {
      writeNode(((Link)node).getSharedGroup(), nodeName, parentTransformations);
    } else if (node instanceof Shape3D) {
      Shape3D shape = (Shape3D)node;
      Appearance appearance = shape.getAppearance();
      RenderingAttributes renderingAttributes = appearance != null
          ? appearance.getRenderingAttributes() : null;
      if (renderingAttributes == null
          || renderingAttributes.getVisible()) {
        // Build a unique human readable object name
        String objectName = "";
        if (accept(nodeName)) {
          objectName = nodeName + "_";
        }
         
        String shapeName = null;
        if (shape.getUserData() instanceof String) {
          shapeName = (String)shape.getUserData();
        }
        if (accept(shapeName)) {
          objectName += shapeName + "_";
        }
       
        objectName += String.valueOf(this.shapeIndex++);
       
        // Start a new object at OBJ format
        this.out.write("g " + objectName + "\n");
       
        TexCoordGeneration texCoordGeneration = null;
        if (this.mtlFileName != null) {
          if (appearance != null) {
            texCoordGeneration = appearance.getTexCoordGeneration();
            ComparableAppearance comparableAppearance = new ComparableAppearance(appearance);
            String appearanceName = this.appearances.get(comparableAppearance);
            if (appearanceName == null) {
              // Store appearance
              appearanceName = objectName;
              this.appearances.put(comparableAppearance, appearanceName);
             
              Texture texture = appearance.getTexture();
              if (texture != null) {
                File textureFile = this.textures.get(texture);
                if (textureFile == null) {
                  // Store texture
                  textureFile = new File(this.mtlFileName.substring(0, this.mtlFileName.length() - 4)
                      + "_" + appearanceName + ".png");
                  this.textures.put(texture, textureFile);
                }
              }
            }
            this.out.write("usemtl " + appearanceName + "\n");
          }
        }
       
        int cullFace = PolygonAttributes.CULL_BACK;
        boolean backFaceNormalFlip = false;
        if (appearance != null) {
          PolygonAttributes polygonAttributes = appearance.getPolygonAttributes();
          if (polygonAttributes != null) {
            cullFace = polygonAttributes.getCullFace();
            backFaceNormalFlip = polygonAttributes.getBackFaceNormalFlip();
          }
        }
View Full Code Here

          Geometry firstGeometry = geometries.get(i);         
          boolean firstGeometryHasTextureCoordinateIndices = firstGeometry.hasTextureCoordinateIndices();
          boolean firstFaceHasNormalIndices = (firstGeometry instanceof Face) && ((Face)firstGeometry).hasNormalIndices();
         
          String firstGeometryMaterial = firstGeometry.getMaterial();
          Appearance appearance = getAppearance(firstGeometryMaterial);
         
          // Search how many geometries share the same characteristics
          int max = i;
          while (++max < geometries.size()) {
            Geometry geometry = geometries.get(max);
            String material = geometry.getMaterial();
            if (geometry.getClass() != firstGeometry.getClass()
                || material == null && firstGeometryMaterial != null
                || material != null && getAppearance(material) != appearance
                || (firstGeometryHasTextureCoordinateIndices ^ geometry.hasTextureCoordinateIndices())
                || (firstFaceHasNormalIndices ^ ((firstGeometry instanceof Face) && ((Face)geometry).hasNormalIndices()))) {
              break;
            }
          }
         
          // Create indices arrays for the faces with an index between i and max
          int faceCount = max - i;
          int indexCount = 0;
          for (int j = 0; j < faceCount; j++) {
            indexCount += geometries.get(i + j).getVertexIndices().length;
          }
          int [] coordinatesIndices = new int [indexCount];
          int [] stripCounts = new int [faceCount];
          for (int j = 0, destIndex = 0; j < faceCount; j++) {
            int [] geometryVertexIndices = geometries.get(i + j).getVertexIndices();
            System.arraycopy(geometryVertexIndices, 0, coordinatesIndices, destIndex, geometryVertexIndices.length);
            stripCounts [j] = geometryVertexIndices.length;
            destIndex += geometryVertexIndices.length;
          }

          int [] textureCoordinateIndices = null;
          if (firstGeometryHasTextureCoordinateIndices) {
            textureCoordinateIndices = new int [indexCount];
            for (int j = 0, destIndex = 0; j < faceCount; j++) {
              int [] geometryTextureCoordinateIndices = geometries.get(i + j).getTextureCoordinateIndices();
              System.arraycopy(geometryTextureCoordinateIndices, 0, textureCoordinateIndices, destIndex, geometryTextureCoordinateIndices.length);
              destIndex += geometryTextureCoordinateIndices.length;
            }
          }

          GeometryArray geometryArray;
          if (firstGeometry instanceof Face) {
            GeometryInfo geometryInfo = new GeometryInfo(GeometryInfo.POLYGON_ARRAY);
            geometryInfo.setCoordinates(vertices);
            geometryInfo.setCoordinateIndices(coordinatesIndices);
            geometryInfo.setStripCounts(stripCounts);
           
            if (firstGeometryHasTextureCoordinateIndices) {
              geometryInfo.setTextureCoordinateParams(1, 2);
              geometryInfo.setTextureCoordinates(0, textureCoodinates);
              geometryInfo.setTextureCoordinateIndices(0, textureCoordinateIndices);
            }
           
            if (firstFaceHasNormalIndices) {
              int [] normalIndices = new int [indexCount];
              for (int j = 0, destIndex = 0; j < faceCount; j++) {
                int [] faceNormalIndices = ((Face)geometries.get(i + j)).getNormalIndices();
                System.arraycopy(faceNormalIndices, 0, normalIndices, destIndex, faceNormalIndices.length);
                destIndex += faceNormalIndices.length;
              }
              geometryInfo.setNormals(normals);
              geometryInfo.setNormalIndices(normalIndices);
            } else {
              NormalGenerator normalGenerator = new NormalGenerator(Math.PI / 2);
              if (!group.isSmooth()) {
                normalGenerator.setCreaseAngle(0);
              }
              normalGenerator.generateNormals(geometryInfo);
            }
            geometryArray = geometryInfo.getGeometryArray(true, true, false);
          } else { // Line
            int format = IndexedGeometryArray.COORDINATES;
            if (firstGeometryHasTextureCoordinateIndices) {
              format |= IndexedGeometryArray.TEXTURE_COORDINATE_2;
            }
           
            // Use non indexed line array to avoid referencing the whole vertices
            geometryArray = new LineStripArray(coordinatesIndices.length, format, stripCounts);           
            for (int j = 0; j < coordinatesIndices.length; j++) {
              geometryArray.setCoordinate(j, vertices [coordinatesIndices [j]]);
            }
            if (firstGeometryHasTextureCoordinateIndices) {
              for (int j = 0; j < coordinatesIndices.length; j++) {
                geometryArray.setTextureCoordinate(0, j, textureCoodinates [textureCoordinateIndices [j]]);
              }
            }
          }
         
          // Clone appearance to avoid sharing it
          if (appearance != null) {
            appearance = (Appearance)appearance.cloneNodeComponent(false);
            // Create texture coordinates if geometry doesn't define its own coordinates
            // and appearance contains a texture
            if (!firstGeometryHasTextureCoordinateIndices
                && appearance.getTexture() != null) {
              appearance.setTexCoordGeneration(new TexCoordGeneration());
            }
          }
          Shape3D shape = new Shape3D(geometryArray, appearance);  
          sceneRoot.addChild(shape);
          scene.addNamedObject(group.getName() + (i == 0 ? "" : String.valueOf(i)), shape);
View Full Code Here

  /**
   * Returns the appearance matching a given <code>material</code>.
   */
  private Appearance getAppearance(String material) {
    Appearance appearance = null;
    if (material != null) {
      appearance = this.appearances.get(material);
    }
    if (appearance == null) {
      appearance = DEFAULT_APPEARANCES.get("default");
View Full Code Here

   * Parses a map of appearances parsed from the given stream.
   */
  private static Map<String, Appearance> parseMaterialStream(Reader reader,
                                                             URL baseUrl) throws IOException {
    Map<String, Appearance> appearances = new HashMap<String, Appearance>();
    Appearance              currentAppearance = null;   
    StreamTokenizer tokenizer = createTokenizer(reader);
    while (tokenizer.nextToken() != StreamTokenizer.TT_EOF) {
      switch (tokenizer.ttype) {
        case StreamTokenizer.TT_WORD :
          currentAppearance = parseMaterialLine(tokenizer,
View Full Code Here

                                              Appearance currentAppearance,
                                              URL baseUrl) throws IOException {
    if ("newmtl".equals(tokenizer.sval)) {
      // Read material name newmtl name
      if (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {
        currentAppearance = new Appearance();
        appearances.put(tokenizer.sval, currentAppearance);
      } else {
        throw new IncorrectFormatException("Expected material name at line " + tokenizer.lineno());
      }
    } else if ("Ka".equals(tokenizer.sval)) {
View Full Code Here

    // Allow wall shape to change its geometry
    wallShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    wallShape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
    wallShape.setCapability(Shape3D.ALLOW_APPEARANCE_READ);

    Appearance wallAppearance = new Appearance();
    wallShape.setAppearance(wallAppearance);
    wallAppearance.setCapability(Appearance.ALLOW_TRANSPARENCY_ATTRIBUTES_READ);
    TransparencyAttributes transparencyAttributes = new TransparencyAttributes();
    transparencyAttributes.setCapability(TransparencyAttributes.ALLOW_VALUE_WRITE);
    transparencyAttributes.setCapability(TransparencyAttributes.ALLOW_MODE_WRITE);
    wallAppearance.setTransparencyAttributes(transparencyAttributes);
    wallAppearance.setCapability(Appearance.ALLOW_RENDERING_ATTRIBUTES_READ);
    RenderingAttributes renderingAttributes = new RenderingAttributes();
    renderingAttributes.setCapability(RenderingAttributes.ALLOW_VISIBLE_WRITE);
    wallAppearance.setRenderingAttributes(renderingAttributes);
   
    if (outline) {
      wallAppearance.setColoringAttributes(Object3DBranch.OUTLINE_COLORING_ATTRIBUTES);
      wallAppearance.setPolygonAttributes(Object3DBranch.OUTLINE_POLYGON_ATTRIBUTES);
      wallAppearance.setLineAttributes(Object3DBranch.OUTLINE_LINE_ATTRIBUTES);
    } else {
      wallAppearance.setCapability(Appearance.ALLOW_MATERIAL_WRITE);
      wallAppearance.setMaterial(DEFAULT_MATERIAL);     
      wallAppearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);
      wallAppearance.setCapability(Appearance.ALLOW_TEXTURE_READ);
      // Mix texture and wall color
      wallAppearance.setTextureAttributes(MODULATE_TEXTURE_ATTRIBUTES);
    }
   
    return wallShape;
  }
View Full Code Here

    // Use coloring attributes for ground to avoid ground lighting
    ColoringAttributes groundColoringAttributes = new ColoringAttributes();
    groundColoringAttributes.setCapability(ColoringAttributes.ALLOW_COLOR_WRITE);
   
    Appearance groundAppearance = new Appearance();
    groundAppearance.setColoringAttributes(groundColoringAttributes);
    groundAppearance.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
    groundAppearance.setCapability(Appearance.ALLOW_TEXTURE_WRITE);

    final Shape3D groundShape = new Shape3D();
    groundShape.setAppearance(groundAppearance);
    groundShape.setCapability(Shape3D.ALLOW_GEOMETRY_WRITE);
    groundShape.setCapability(Shape3D.ALLOW_GEOMETRY_READ);
View Full Code Here

    Home home = (Home)getUserData();
    Shape3D groundShape = (Shape3D)getChild(0);
    int currentGeometriesCount = groundShape.numGeometries();
   
    Color3f groundColor = new Color3f(new Color(home.getEnvironment().getGroundColor()));
    final Appearance groundAppearance = groundShape.getAppearance();
    groundAppearance.getColoringAttributes().setColor(groundColor);
    HomeTexture groundTexture = home.getEnvironment().getGroundTexture();
    if (groundTexture != null) {
      final TextureManager imageManager = TextureManager.getInstance();
      imageManager.loadTexture(groundTexture.getImage(), waitTextureLoadingEnd,
          new TextureManager.TextureObserver() {
              public void textureUpdated(Texture texture) {
                groundAppearance.setTexture(texture);
              }
            });
    } else {
      groundAppearance.setTexture(null);
    }
   
    // Create ground geometry
    List<Point3f> coords = new ArrayList<Point3f>();
    List<Integer> stripCounts = new ArrayList<Integer>();
View Full Code Here

          cloneTexture((Node)enumeration.nextElement(), replacedTextures);
        }
      } else if (node instanceof Link) {
        cloneTexture(((Link)node).getSharedGroup(), replacedTextures);
      } else if (node instanceof Shape3D) {
        Appearance appearance = ((Shape3D)node).getAppearance();
        if (appearance != null) {
          Texture texture = appearance.getTexture();
          if (texture != null) {
            Texture replacedTexture = replacedTextures.get(texture);
            if (replacedTexture == null) {
              replacedTexture = (Texture)texture.cloneNodeComponent(false);
              replacedTextures.put(texture, replacedTexture);
            }
            appearance.setTexture(replacedTexture);
          }
        }
      }
    }
View Full Code Here

TOP

Related Classes of javax.media.j3d.Appearance

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.