home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch13 / nff2vrml.java < prev    next >
Text File  |  1997-01-15  |  13KB  |  307 lines

  1. // Parse a Sense8 NFF file and convert it to VRML 2.0 format
  2.  
  3. // Written by Bernie Roehl, November 1996
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7. import nff.*;
  8.  
  9. public class nff2vrml {
  10.     public static void main(String args[]) {
  11.         try {
  12.             InputStream d;
  13.             if (args.length == 1)
  14.                 d = new FileInputStream(args[0]);
  15.             else
  16.                 d = System.in;
  17.             Scene scene = new Scene(new DataInputStream(d));
  18.             dump_scene(scene);
  19.         }
  20.         catch (IOException e) { System.err.println(e); }
  21.         catch (NffSyntaxException e) { System.err.println(e); }
  22.     }
  23.  
  24.     static void dump_scene(Scene scene) {
  25.         System.out.println("#VRML V2.0 utf8");
  26.         System.out.println("\n# Generated by nff2vrml\n");
  27.         for (int i = 0; i < scene.getNumberOfObjects(); ++i)
  28.             dump_object(scene.getObject(i));    
  29.     }
  30.  
  31.     static private boolean dumped_coordinates;
  32.     static private boolean dumped_vertex_normals;
  33.     static private boolean dumped_vertex_colors;
  34.     static private boolean dumped_face_colors;
  35.     static private boolean dumped_texture_coordinates;
  36.     static private boolean emit_texture_coordinates = false;
  37.  
  38.     static void dump_object(Obj object) {
  39.         System.out.println("DEF " + object.getName() + " Group {");
  40.         System.out.println("\tchildren [");
  41.         Vector textures = new Vector();
  42.         boolean have_untextured_faces = false;
  43.         for (int i = 0; i < object.getNumberOfFaces(); ++i) {
  44.             Texture texture = object.getFace(i).getTexture();
  45.             if (texture != null) {
  46.                 if (!contains_texture(textures, texture))
  47.                     textures.addElement(texture);
  48.             }
  49.             else
  50.                 have_untextured_faces = true;
  51.             }
  52.         dumped_coordinates = dumped_vertex_normals = false;
  53.         dumped_vertex_colors = dumped_face_colors = false;
  54.         dumped_texture_coordinates = false;
  55.         Enumeration e = textures.elements();
  56.         while (e.hasMoreElements())  // dump out an object for each texture
  57.             dump_textured_object(object, (Texture) e.nextElement());
  58.         if (have_untextured_faces)
  59.             dump_textured_object(object, null);    // untextured faces
  60.         System.out.println("\t]");
  61.         System.out.println("}");
  62.     }
  63.  
  64.  
  65.     static void dump_textured_object(Obj object, Texture texture) {
  66.         if (hasAny(object, false, false)) {  // no vertex normals or colors
  67.             System.out.println("\t\tShape {");
  68.             dump_appearance(texture);
  69.             System.out.println("\t\t\tgeometry IndexedFaceSet {");
  70.             System.out.println("\t\t\t\tnormalPerVertex FALSE");
  71.             System.out.println("\t\t\t\tcolorPerVertex FALSE");
  72.             dump_coordinates(object);
  73.             if (texture != null)
  74.                 dump_texture_coordinates(object);
  75.             dump_faces(object, texture, false, false);
  76.             dump_face_colors(object, texture, false);
  77.             System.out.println("\t\t\t}");  // end IndexedFaceSet
  78.             System.out.println("\t\t}");    // end Shape
  79.         }
  80.         if (hasAny(object, false, true)) {  // vertex colors, but no normals 
  81.             System.out.println("\t\tShape {");
  82.             dump_appearance(texture);
  83.             System.out.println("\t\t\tgeometry IndexedFaceSet {");
  84.             System.out.println("\t\t\t\tnormalPerVertex FALSE");
  85.             System.out.println("\t\t\t\tcolorPerVertex TRUE");
  86.             dump_coordinates(object);
  87.             if (texture != null)
  88.                 dump_texture_coordinates(object);
  89.             dump_vertex_colors(object);
  90.             dump_faces(object, texture, false, true);
  91.             System.out.println("\t\t\t}");  // end IndexedFaceSet
  92.             System.out.println("\t\t}");    // end Shape
  93.         }
  94.         if (hasAny(object, true, false)) {  // vertex normals, but no colors
  95.             System.out.println("\t\tShape {");
  96.             dump_appearance(texture);
  97.             System.out.println("\t\t\tgeometry IndexedFaceSet {");
  98.             System.out.println("\t\t\t\tnormalPerVertex TRUE");
  99.             System.out.println("\t\t\t\tcolorPerVertex FALSE");
  100.             dump_coordinates(object);
  101.             if (texture != null)
  102.                 dump_texture_coordinates(object);
  103.             dump_vertex_normals(object);
  104.             dump_face_colors(object, texture, true);
  105.             dump_faces(object, texture, true, false);
  106.             System.out.println("\t\t\t}");  // end IndexedFaceSet
  107.             System.out.println("\t\t}");    // end Shape
  108.         }
  109.         if (hasAny(object, true, true)) {  // both vertex normals and colors
  110.             System.out.println("\t\tShape {");
  111.             dump_appearance(texture);
  112.             System.out.println("\t\t\tgeometry IndexedFaceSet {");
  113.             System.out.println("\t\t\t\tnormalPerVertex TRUE");
  114.             System.out.println("\t\t\t\tcolorPerVertex TRUE");
  115.             dump_coordinates(object);
  116.             if (texture != null)
  117.                 dump_texture_coordinates(object);
  118.             dump_vertex_colors(object);
  119.             dump_vertex_normals(object);
  120.             dump_faces(object, texture, true, true);
  121.             System.out.println("\t\t\t}");  // end IndexedFaceSet
  122.             System.out.println("\t\t}");    // end Shape
  123.         }
  124.     }
  125.  
  126.     static void dump_coordinates(Obj object) {
  127.         if (dumped_coordinates == true)
  128.             System.out.println("\t\t\t\tcoord USE C");
  129.         else {
  130.            System.out.println("\t\t\t\tcoord DEF C Coordinate {");
  131.            System.out.println("\t\t\t\t\tpoint [");
  132.            for (int i = 0; i < object.getNumberOfVertices(); ++i)
  133.                 System.out.println("\t\t\t\t\t\t" +
  134.                      object.getVertex(i).getPosition());
  135.            System.out.println("\t\t\t\t\t]");
  136.            System.out.println("\t\t\t\t}");
  137.            dumped_coordinates = true;
  138.         }
  139.     }
  140.  
  141.     static void dump_vertex_normals(Obj object) {
  142.         if (dumped_vertex_normals == true)
  143.             System.out.println("\t\t\t\tnormal USE N");
  144.         else {
  145.            System.out.println("\t\t\t\tnormal DEF N Normal {");
  146.            System.out.println("\t\t\t\t\tvector [");
  147.            for (int i = 0; i < object.getNumberOfVertices(); ++i) {
  148.                 Vec3f norm = object.getVertex(i).getNormal();
  149.                 System.out.print("\t\t\t\t\t\t");
  150.                 if (norm == null)
  151.                     System.out.println("0 0 0   # no normal");
  152.                 else
  153.                     System.out.println(norm); 
  154.            }
  155.            System.out.println("\t\t\t\t\t]");
  156.            System.out.println("\t\t\t\t}");
  157.            dumped_vertex_normals = true;
  158.         }
  159.     }
  160.  
  161.     static void dump_texture_coordinates(Obj object) {
  162.         if (!emit_texture_coordinates)
  163.             return;
  164.         if (dumped_texture_coordinates)
  165.             System.out.println("\t\t\t\ttexCoord USE T");
  166.         else {
  167.             System.out.println("\t\t\t\ttexCoord DEF T TextureCoordinate {");
  168.             System.out.println("\t\t\t\t\tpoint [");
  169.             for (int i = 0; i < object.getNumberOfVertices(); ++i) {
  170.                 Vertex vertex = object.getVertex(i);
  171.                 float u = vertex.getU();
  172.                 float v = vertex.getV();
  173.                 System.out.println("\t\t\t\t\t\t" + u + " " + v);
  174.             }
  175.             System.out.println("\t\t\t\t\t]");
  176.             System.out.println("\t\t\t\t}");
  177.             dumped_texture_coordinates = true;
  178.         }
  179.     }
  180.  
  181.     static void dump_face_colors(Obj object, Texture texture, boolean use_v_normals) {
  182.         if (dumped_face_colors)
  183.             System.out.println("\t\t\t\tcolor USE FC");
  184.         else {
  185.             System.out.println("\t\t\t\tcolor DEF FC Color {");
  186.             System.out.println("\t\t\t\t\tcolor [");
  187.             for (int i = 0; i < object.getNumberOfFaces(); ++i) {
  188.                  Face f = object.getFace(i);
  189.                  if (f.getColor() != null)
  190.                      System.out.println("\t\t\t\t\t\t" + f.getColor());
  191.                  else
  192.                      System.out.println("\t\t\t\t\t\t0 0 0");
  193.             }
  194.             System.out.println("\t\t\t\t\t]");
  195.             System.out.println("\t\t\t\t}");
  196.             dumped_face_colors = true;
  197.         }
  198.         System.out.println("\t\t\t\tcolorIndex [");
  199.         for (int i = 0; i < object.getNumberOfFaces(); ++i) {
  200.              Face f = object.getFace(i);
  201.              if (f.useVertexNormals != use_v_normals
  202.                  || f.useVertexColors != false)  // mismatch
  203.                  continue;
  204.              if (texture == null) {   // looking for untextured faces
  205.                  if (f.getTexture() != null)  // but this one's textured
  206.                      continue;
  207.              }
  208.              else if (!texture.match(f.getTexture()))  // look for a match
  209.                  continue;
  210.              System.out.println("\t\t\t\t\t" + i);    
  211.              if (f.isDoublesided())
  212.                      System.out.println("\t\t\t\t\t" + i);
  213.         }
  214.         System.out.println("\t\t\t\t]");
  215.     }
  216.  
  217.     static void dump_vertex_colors(Obj object) {
  218.         if (dumped_vertex_colors)
  219.             System.out.println("\t\t\t\tcolor USE VC");
  220.         else {
  221.             System.out.println("\t\t\t\tcolor DEF VC Color {");
  222.             System.out.println("\t\t\t\t\tcolor [");
  223.             for (int i = 0; i < object.getNumberOfVertices(); ++i) {
  224.                      Color c = object.getVertex(i).getColor();
  225.                      System.out.print("\t\t\t\t\t\t");
  226.                      if (c == null)
  227.                          System.out.println("0 0 0");
  228.                      else
  229.                          System.out.println(c);
  230.             }
  231.             System.out.println("\t\t\t\t\t]");
  232.             System.out.println("\t\t\t\t}");
  233.             dumped_vertex_colors = true;
  234.         }
  235.     }
  236.  
  237.     static void dump_faces(Obj object, Texture texture, boolean use_v_normals, boolean use_v_colors) {
  238.         System.out.println("\t\t\t\tcoordIndex [");
  239.         for (int i = 0; i < object.getNumberOfFaces(); ++i) {
  240.             Face face = object.getFace(i);
  241.             if (face.useVertexNormals != use_v_normals
  242.                 || face.useVertexColors != use_v_colors)
  243.                 continue;
  244.             if (texture == null) {   // looking for untextured faces
  245.                 if (face.getTexture() != null)  // but this one's textured
  246.                     continue;
  247.             }
  248.             else if (!texture.match(face.getTexture()))  // look for a match
  249.                 continue;
  250.             System.out.print("\t\t\t\t\t");
  251.             for (int j = 0; j < face.getNumberOfPoints(); ++j)
  252.                 System.out.print(face.getPoint(j) + " ");
  253.             System.out.println("-1");
  254.             if (face.isDoublesided()) {
  255.                 System.out.print("\t\t\t\t\t");
  256.                 for (int j = face.getNumberOfPoints()-1; j >= 0; --j)
  257.                     System.out.print(face.getPoint(j) + " ");
  258.                 System.out.println("-1");
  259.             }
  260.         }
  261.         System.out.println("\t\t\t\t]");
  262.     }
  263.  
  264.     static boolean hasAny(Obj object, boolean vnormals, boolean vcolors) {
  265.         for (int i = 0; i < object.getNumberOfFaces(); ++i) {
  266.             Face f = object.getFace(i);
  267.             if (f.useVertexNormals == vnormals && f.useVertexColors == vcolors)
  268.                 return true;
  269.         }
  270.         return false;
  271.     }
  272.  
  273.     static void dump_appearance(Texture texture) {
  274.         System.out.println("\t\t\tappearance Appearance {");
  275.         if (texture == null) {
  276.             System.out.println("\t\t\t\tmaterial Material { }");
  277.             System.out.println("\t\t\t\t}");
  278.             return;
  279.         }
  280.         if (texture.isShaded())
  281.             System.out.println("\t\t\t\tmaterial Material { }");
  282.         System.out.println("\t\t\t\ttexture ImageTexture { url \"" + texture.getFilename() + ".jpg\" }");
  283.         System.out.println("\t\t\t\ttextureTransform TextureTransform {");
  284.         if (texture.getScale() != 1f)
  285.                 System.out.println("\t\t\t\t\tscale " + texture.getScale() + " " + texture.getScale());
  286.         if (texture.getTransS() != 0 || texture.getTransT() != 0)
  287.                 System.out.println("\t\t\t\t\ttranslation " + texture.getTransS() + " " + texture.getTransT());
  288.         if (texture.getRotation() != 0)
  289.                 System.out.println("\t\t\t\t\trotation " + texture.getRotation());
  290.         if (texture.getMirror())
  291.              System.out.println("\t\t\t\t\t# note: mirror ignored");
  292.         System.out.println("\t\t\t\t}");
  293.         System.out.println("\t\t\t}");
  294.     }
  295.  
  296.     static boolean contains_texture(Vector texture_list, Texture texture) {
  297.         Enumeration e = texture_list.elements();
  298.         while (e.hasMoreElements()) {   // see if we've got this one
  299.             Texture t = (Texture) e.nextElement();
  300.             if (t != null)
  301.                 if (t.match(texture))      // found a match!
  302.                     return true;
  303.         }
  304.         return false;
  305.     }
  306. }
  307.