📄 wraploaderinfo3d.java
字号:
examineNode(0, bg); ofw.close(); } catch( IOException ioe ) { System.err.println("Cannot write to " + EXAMINE_FN); } } // end of storeGraphInfo() private void examineNode(int level, Node node) throws IOException /* A Node can be a Group or a Leaf. Depending on the type of Group or Leaf, report different things. Recursively call examineNode() on the children of a Group, incrementing level so the output can be indented correctly. */ { if(node instanceof Group) { // the Node is a Group Group g = (Group) node; levelPrint(level, "Group: " + g.getClass()); if(g instanceof TransformGroup) { // consider subclass Transform3D t3d = new Transform3D(); ((TransformGroup) g).getTransform(t3d); levelPrint(level, t3d.toString() ); // show Transform3D info for TG } levelPrint(level, g.numChildren() + " children"); Enumeration enumKids = g.getAllChildren(); while(enumKids.hasMoreElements()) // visit Group children examineNode(level+1, (Node) enumKids.nextElement()); } else if (node instanceof Leaf) { // the Node is a Leaf levelPrint(level, "Leaf: " + node.getClass()); if (node instanceof Shape3D) examineShape3D(level, (Shape3D) node); // treat Shape3D specially } else // the Node is something other than a Group or Leaf levelPrint(level, "Node: " + node.getClass()); } // end of examineNode() private void examineShape3D(int level, Shape3D shape) throws IOException /* A Shape3D is a container for Appearance and Geometry components. Show appearance info with printAppearance(). A shape may contain many geometries: examine each one with examineGeometry() */ { Appearance app = shape.getAppearance(); // consider appearance if (app == null) levelPrint(level+1, "No Appearance Component"); else printAppearance(level, app); int numGeoms = shape.numGeometries(); // consider geometries if (numGeoms == 0) levelPrint(level+1, "No Geometry Components"); else if (numGeoms == 1) { Geometry g = shape.getGeometry(); examineGeometry(level+1, 1, g); } else { // more than one geometry in the shape levelPrint(level+1, "No. of Geometries: " + numGeoms); Enumeration enumGeoms = shape.getAllGeometries(); int i = 1; while(enumGeoms.hasMoreElements()) { examineGeometry(level+1, i, (Geometry) enumGeoms.nextElement() ); i++; } } levelPrint(level, ""); // put in a newline in the output } // end of examineShape3D() private void printAppearance(int level, Appearance app) throws IOException /* Lots of Appearance information could be printed here. We only display colour related stuff. */ { ColoringAttributes ca = app.getColoringAttributes(); if (ca != null) levelPrint(level, ca.toString() ); Material mat = app.getMaterial(); if (mat != null) levelPrint(level, mat.toString() ); } // end of printAppearance() private void examineGeometry(int level, int index, Geometry geo) throws IOException // Display geometry info found in a shape { levelPrint(level, "Geometry: " + geo.getClass()); if(geo instanceof GeometryArray) // geometryArray is frequently used in models, so give some extra info levelPrint(level, "Vertex count: " + ((GeometryArray)geo).getVertexCount()); } // end of examineGeometry() private void levelPrint(int level, String s) throws IOException // the s string is indented according to level { for(int i=0; i<level; i++) { // System.out.print(" "); ofw.write(" "); } // System.out.println(s); ofw.write(s + "\n"); } // end of levelPrint() // ----------------------- adjust the model's Shape3Ds ------------------ private void adjustShapes(Node node) { System.out.println("Adjusting shapes..."); if ((adaptNo == 3) || (adaptNo == 4)) // will add a texture to the shapes loadTexture(TEXTURE_FN); visitNode(node); } // end of adjustShapes() private void loadTexture(String fn) // load image from file fn as a texture { TextureLoader texLoader = new TextureLoader(fn, null); texture = (Texture2D) texLoader.getTexture(); if (texture == null) System.out.println("Cannot load texture from " + fn); else { System.out.println("Loaded texture from " + fn); texture.setEnable(true); } } // end of loadTexture() private void visitNode(Node node) /* If the node is a Group then recursively visit its children. Otherwise if the node is a Shape3D then so the changes. */ { if(node instanceof Group) { Group g = (Group) node; Enumeration enumKids = g.getAllChildren(); while(enumKids.hasMoreElements()) { // visit children SceneGraphObject obj = (SceneGraphObject) enumKids.nextElement(); if (obj instanceof Node) visitNode((Node) obj); } } else if (node instanceof Shape3D) adjustShape3D((Shape3D) node); } // end of visitNode() private void adjustShape3D(Shape3D shape) /* Shape adjusting examples: * change the colour of a shape to blue * draw the shape in outline (i.e. as a wireframe) * make the shape transparent * add a texture to the shape (this can be combined with the colour changing method) The choice of which one depends on adaptNo: 0-3, and 4 does makeBlue() and addTexture() */ { switch(adaptNo) { case 0: makeBlue(shape); break; case 1: drawOutline(shape); break; case 2: makeAlmostTransparent(shape); break; case 3: addTexture(shape); break; case 4: makeBlue(shape); addTexture(shape); break; default: break; // say nothing } } // end of adjustShape3D() private void makeBlue(Shape3D shape) // change the shape's colour to blue { Appearance app = shape.getAppearance(); Material blueMat = new Material(black, black, blue, white, 20.0f); // the black ambient means that unlit surfaces are pitch black blueMat.setLightingEnable(true); app.setMaterial( blueMat ); shape.setAppearance(app); } // end of makeBlue() private void drawOutline(Shape3D shape) // draw only the shape's outline (i.e. as a wireframe) { Appearance app = shape.getAppearance(); PolygonAttributes pa = new PolygonAttributes(); pa.setCullFace( PolygonAttributes.CULL_NONE ); pa.setPolygonMode( PolygonAttributes.POLYGON_LINE ); app.setPolygonAttributes( pa ); shape.setAppearance(app); } // end of drawOutline() private void makeAlmostTransparent(Shape3D shape) // make the shape almost transparent { Appearance app = shape.getAppearance(); TransparencyAttributes ta = new TransparencyAttributes(); ta.setTransparencyMode( TransparencyAttributes.BLENDED ); ta.setTransparency(0.8f); // 1.0f is totally transparent app.setTransparencyAttributes( ta ); shape.setAppearance(app); } // end of makeAlmostTransparent() private void addTexture(Shape3D shape) /* Add a texture to the shape, but only if it is represented by a single GeometryArray. */ { if (shape.numGeometries() == 1) { Geometry g = shape.getGeometry(); if (g instanceof GeometryArray) addTextureGA(shape); else System.out.println("Shape geometry is not a GeometryArray"); } else System.out.println("Shape has too many geometries"); } // end of addTexture() private void addTextureGA(Shape3D shape) /* Add a texture to a GeometryArray. Add the texture to all faces of the model, 'stretch' the texture over the model, modulate the texture with the existing colour and lighting components. The texture comes from TEXTURE_FN, and was loaded before the traversal of the model began. */ { Appearance app = shape.getAppearance(); // make shape two-sided, so texture appears on both sides PolygonAttributes pa = new PolygonAttributes(); pa.setCullFace( PolygonAttributes.CULL_NONE ); app.setPolygonAttributes( pa ); // generate texture coords that 'stretch' the texture over the model app.setTexCoordGeneration( stampTexCoords(shape) ); // combine texture with colour and lighting of underlying surface TextureAttributes ta = new TextureAttributes(); ta.setTextureMode( TextureAttributes.MODULATE ); app.setTextureAttributes( ta ); // apply texture to shape if (texture != null) { // loaded at start, from adjustShapes() app.setTexture(texture); shape.setAppearance(app); } } // end of addTextureGA() private TexCoordGeneration stampTexCoords(Shape3D shape) /* Specify how the texture is mapped to the model by adjusting the genration planes so that a single texture is 'stetched' over the entire model rather than repeatedly 'tiled'. This makes a big difference for models that are very large since the default tiling approach tends to lose texture detail and the display 'shimmers' since the texture is mapped to only a few pixels on the model. */ { // get the bounds of the shape BoundingBox boundBox = new BoundingBox( shape.getBounds() ); Point3d lower = new Point3d(); Point3d upper = new Point3d(); boundBox.getLower(lower); boundBox.getUpper(upper); // System.out.println("lower: " + lower + "\nupper: " + upper ); double width = upper.x - lower.x; double height = upper.y - lower.y; // System.out.println("width: " + df.format(width) + // "; height: " + df.format(height) ); // adjust generation planes so shape box is mapped to texture // coordinates [0,0] and [1,1]. Vector4f planeS = new Vector4f( (float)(1.0/width), 0.0f, 0.0f, (float)(-lower.x/width)); Vector4f planeT = new Vector4f( 0.0f, (float)(1.0/height), 0.0f, (float)(-lower.y/height)); // generate new texture coordinates for GeometryArray TexCoordGeneration texGen = new TexCoordGeneration(); texGen.setPlaneS(planeS); texGen.setPlaneT(planeT); return texGen; } // end of stampTexCoords()} // end of WrapLoaderInfo3D class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -