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

  1. // Convert a SCUL file to VRML
  2.  
  3. // Written by Bernie Roehl, November 1996
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7. import hash.*;
  8.  
  9. public class scul2wrl {
  10.         public static void main(String args[]) {
  11.                 try {
  12.                 Segment seg = new Segment(new DataInputStream(System.in));
  13.  
  14.                 System.out.println("#VRML V2.0 utf8");
  15.                 System.out.println("\n# Generated by SCUL2WRL\n");
  16.                 System.out.println("Transform {");
  17.                 System.out.println("\ttranslation " + seg.getPivotX() + " " + seg.getPivotY() + " " + seg.getPivotZ());
  18.                 System.out.println("\tchildren [");
  19.                 System.out.println("\t\tShape {");
  20.                 System.out.println("\t\t\tappearance Appearance { material Material { emissiveColor 1 1 1 } }");
  21.                 System.out.println("\t\t\tgeometry IndexedLineSet {");
  22.  
  23.                 // gather the vertices
  24.                 Vector coordinates = new Vector();
  25.                 for (int i = 0; i < seg.getNumberOfSplines(); ++i) {
  26.                         Spline spline = seg.getSpline(i);
  27.                         for (int j = 0; j < spline.getNumberOfControlPoints(); ++j) {
  28.                                 ControlPoint cp = spline.getControlPoint(j);
  29.                                 if (cp.isAttached() == false)
  30.                                         coordinates.addElement(cp);
  31.                         }
  32.                 }
  33.  
  34.                 // and emit them
  35.                 System.out.println("\t\t\t\tcoord Coordinate {");
  36.                 System.out.println("\t\t\t\t\tpoint [");
  37.                 for (int k = 0; k < coordinates.size(); ++k) {
  38.                         ControlPoint pt = (ControlPoint) coordinates.elementAt(k);
  39.                         System.out.println("\t\t\t\t\t\t"
  40.                             + pt.getX() + " " + pt.getY() + " " + pt.getZ()
  41.                             + "  # " + pt.getCPID());
  42.                 }
  43.                 System.out.println("\t\t\t\t\t]");
  44.                 System.out.println("\t\t\t\t}");
  45.  
  46.                 System.out.println("\t\t\t\tcoordIndex [");
  47.                 for (int i = 0; i < seg.getNumberOfSplines(); ++i) {
  48.                         Spline spline = seg.getSpline(i);
  49.                         int firstpoint = 0;
  50.                         boolean lastlooped = false;
  51.                         System.out.print("\t\t\t\t\t\t");
  52.                         for (int j = 0; j < spline.getNumberOfControlPoints(); ++j) {
  53.                                 ControlPoint cp = spline.getControlPoint(j);
  54.                                 lastlooped = cp.isLooped();
  55.                                 while (cp.isAttached())
  56.                                         cp = seg.findControlPoint(cp.getAttachedCPID());
  57.                                 int n = coordinates.indexOf(cp);
  58.                                 System.out.print(n + " ");
  59.                                 if (j == 0)
  60.                                         firstpoint = n;
  61.                         }
  62.                         if (lastlooped)
  63.                                 System.out.print(firstpoint + " ");
  64.                         System.out.println("-1");
  65.                 }
  66.                 System.out.println("\t\t\t\t]");
  67.  
  68.                 System.out.println("\t\t\t}");
  69.                 System.out.println("\t\t}");
  70.                 System.out.println("\t]");
  71.                 System.out.println("}");
  72.                 }
  73.                 catch (IOException e) { System.out.println(e); }
  74.                 catch (HashSyntaxException e) { System.out.println(e); }
  75.  
  76.         }
  77. }
  78.  
  79.