home *** CD-ROM | disk | FTP | other *** search
/ Late Night VRML 2.0 with Java CD-ROM / code.zip / Ch20 / animgen.java next >
Encoding:
Java Source  |  1997-03-24  |  5.0 KB  |  134 lines

  1. // Generate animations in VRML from simple input file
  2.  
  3. // Written by Bernie Roehl, March 1997
  4.  
  5. import java.io.*;
  6. import java.util.*;
  7.  
  8. public class animgen {
  9.     public static void main(String[] args) {
  10.         String animname = null;
  11.         Frame currframe = null;
  12.         Vector joints = new Vector();
  13.         Vector frames = new Vector();
  14.         boolean loop = false;
  15.         try {
  16.            InputStream d;
  17.            if (args.length == 1)
  18.                 d = new FileInputStream(args[0]);
  19.            else
  20.                 d = System.in;
  21.            DataInputStream input = new DataInputStream(d);
  22.            String line;
  23.            while ((line = input.readLine()) != null) {
  24.                if (line.length() == 0)
  25.                    continue;
  26.                else if (line.startsWith("#"))
  27.                    continue;  // ignore comment lines
  28.                else if (line.startsWith("anim"))
  29.                    animname = line.substring(5);
  30.                else if (line.startsWith("loop"))
  31.                    loop = true;
  32.                else if (line.startsWith("joint"))
  33.                    joints.addElement(new Joint(line.substring(6)));
  34.                else if (line.startsWith("frame"))
  35.                    frames.addElement(currframe = new Frame(line.substring(6), joints.size()));
  36.                else {
  37.                    StringTokenizer tokens = new StringTokenizer(line);
  38.                    int jointNumber = Integer.parseInt(tokens.nextToken());
  39.                    currframe.setValue(jointNumber, new RotationValue(tokens));
  40.                }
  41.            }
  42.         }
  43.         catch (IOException e) { System.err.println(e); }
  44.         vrmldump(animname, loop, joints, frames);
  45.     }
  46.  
  47.     static void vrmldump(String animname, boolean loop, Vector joints, Vector frames) {
  48.         System.out.println("#VRML V2.0 utf8\n");
  49.         System.out.print("DEF " + animname + "TS TimeSensor {");
  50.         if (loop) System.out.print(" loop TRUE");
  51.         System.out.println(" }\n");
  52.         Enumeration en = joints.elements();
  53.         Enumeration fen;
  54.         for (int jointnum = 0; en.hasMoreElements(); ++jointnum) {
  55.             Joint j = (Joint) en.nextElement();
  56.             System.out.println("DEF rotInter OrientationInterpolator {");
  57.             System.out.print("\tkey [");
  58.             fen = frames.elements();
  59.             while (fen.hasMoreElements()) {
  60.                 Frame f = (Frame) fen.nextElement();
  61.                 if (f.getValue(jointnum) != null)
  62.                     System.out.print(" " + f.getTime());
  63.             }
  64.             System.out.println(" ]");
  65.             System.out.print("\tkeyValue [");
  66.             fen = frames.elements();
  67.             while (fen.hasMoreElements()) {
  68.                 Frame f = (Frame) fen.nextElement();
  69.                 Value v = f.getValue(jointnum);
  70.                 if (v != null)
  71.                     System.out.print(" " + v);
  72.             }
  73.             System.out.println("\t]");
  74.             System.out.println("}");
  75.             System.out.println("ROUTE " + animname + "TS.fraction_changed TO rotInter.set_fraction");
  76.             System.out.println("ROUTE rotInter.value_changed TO " + j.getName() + ".set_rotation");
  77.             System.out.println();
  78.         }
  79.     }
  80.  
  81.     static void dump(String animname, Vector joints, Vector frames) {
  82.         System.out.println("Animation '" + animname + "'");
  83.         System.out.println("Joints:");
  84.         Enumeration jen = joints.elements();
  85.         while (jen.hasMoreElements())
  86.             System.out.println("\t" + (Joint) jen.nextElement());
  87.         System.out.println("Frames:");
  88.         Enumeration fen = frames.elements();
  89.         while (fen.hasMoreElements()) {
  90.             Frame f = (Frame) fen.nextElement();
  91.             System.out.println("\ttime = " + f.getTime());
  92.             for (int i = 0; i < joints.size(); ++i) {
  93.                 Value v = f.getValue(i);
  94.                 if (v != null)
  95.                     System.out.println("\t" + i + " = " + v);
  96.             }
  97.         }
  98.     }
  99. }
  100.  
  101. class Joint {
  102.     String name;
  103.     public String getName() { return name; }
  104.     public Joint(String nam) { name = nam; }
  105.     public String toString() { return name; }
  106. }
  107.  
  108. class Frame {
  109.     float time;
  110.     Value[] values;
  111.     public Frame(String inputLine, int nvalues) {
  112.         time = Float.valueOf(inputLine).floatValue();
  113.         values = new Value[nvalues];
  114.     }
  115.     public float getTime() { return time; }
  116.     public Value getValue(int n) { return values[n]; }
  117.     public void setValue(int n, Value val) { values[n] = val; }
  118. }
  119.  
  120. class Value {
  121. }
  122.  
  123. class RotationValue extends Value {
  124.     float x, y, z, a;
  125.     public RotationValue(StringTokenizer tokens) {
  126.         x = Float.valueOf(tokens.nextToken()).floatValue();
  127.         y = Float.valueOf(tokens.nextToken()).floatValue();
  128.         z = Float.valueOf(tokens.nextToken()).floatValue();
  129.         a = Float.valueOf(tokens.nextToken()).floatValue();
  130.     }
  131.     public String toString() { return x + " " + y + " " + z + " " + a; }
  132. }
  133.  
  134.