home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-03-24 | 5.0 KB | 134 lines |
- // Generate animations in VRML from simple input file
-
- // Written by Bernie Roehl, March 1997
-
- import java.io.*;
- import java.util.*;
-
- public class animgen {
- public static void main(String[] args) {
- String animname = null;
- Frame currframe = null;
- Vector joints = new Vector();
- Vector frames = new Vector();
- boolean loop = false;
- try {
- InputStream d;
- if (args.length == 1)
- d = new FileInputStream(args[0]);
- else
- d = System.in;
- DataInputStream input = new DataInputStream(d);
- String line;
- while ((line = input.readLine()) != null) {
- if (line.length() == 0)
- continue;
- else if (line.startsWith("#"))
- continue; // ignore comment lines
- else if (line.startsWith("anim"))
- animname = line.substring(5);
- else if (line.startsWith("loop"))
- loop = true;
- else if (line.startsWith("joint"))
- joints.addElement(new Joint(line.substring(6)));
- else if (line.startsWith("frame"))
- frames.addElement(currframe = new Frame(line.substring(6), joints.size()));
- else {
- StringTokenizer tokens = new StringTokenizer(line);
- int jointNumber = Integer.parseInt(tokens.nextToken());
- currframe.setValue(jointNumber, new RotationValue(tokens));
- }
- }
- }
- catch (IOException e) { System.err.println(e); }
- vrmldump(animname, loop, joints, frames);
- }
-
- static void vrmldump(String animname, boolean loop, Vector joints, Vector frames) {
- System.out.println("#VRML V2.0 utf8\n");
- System.out.print("DEF " + animname + "TS TimeSensor {");
- if (loop) System.out.print(" loop TRUE");
- System.out.println(" }\n");
- Enumeration en = joints.elements();
- Enumeration fen;
- for (int jointnum = 0; en.hasMoreElements(); ++jointnum) {
- Joint j = (Joint) en.nextElement();
- System.out.println("DEF rotInter OrientationInterpolator {");
- System.out.print("\tkey [");
- fen = frames.elements();
- while (fen.hasMoreElements()) {
- Frame f = (Frame) fen.nextElement();
- if (f.getValue(jointnum) != null)
- System.out.print(" " + f.getTime());
- }
- System.out.println(" ]");
- System.out.print("\tkeyValue [");
- fen = frames.elements();
- while (fen.hasMoreElements()) {
- Frame f = (Frame) fen.nextElement();
- Value v = f.getValue(jointnum);
- if (v != null)
- System.out.print(" " + v);
- }
- System.out.println("\t]");
- System.out.println("}");
- System.out.println("ROUTE " + animname + "TS.fraction_changed TO rotInter.set_fraction");
- System.out.println("ROUTE rotInter.value_changed TO " + j.getName() + ".set_rotation");
- System.out.println();
- }
- }
-
- static void dump(String animname, Vector joints, Vector frames) {
- System.out.println("Animation '" + animname + "'");
- System.out.println("Joints:");
- Enumeration jen = joints.elements();
- while (jen.hasMoreElements())
- System.out.println("\t" + (Joint) jen.nextElement());
- System.out.println("Frames:");
- Enumeration fen = frames.elements();
- while (fen.hasMoreElements()) {
- Frame f = (Frame) fen.nextElement();
- System.out.println("\ttime = " + f.getTime());
- for (int i = 0; i < joints.size(); ++i) {
- Value v = f.getValue(i);
- if (v != null)
- System.out.println("\t" + i + " = " + v);
- }
- }
- }
- }
-
- class Joint {
- String name;
- public String getName() { return name; }
- public Joint(String nam) { name = nam; }
- public String toString() { return name; }
- }
-
- class Frame {
- float time;
- Value[] values;
- public Frame(String inputLine, int nvalues) {
- time = Float.valueOf(inputLine).floatValue();
- values = new Value[nvalues];
- }
- public float getTime() { return time; }
- public Value getValue(int n) { return values[n]; }
- public void setValue(int n, Value val) { values[n] = val; }
- }
-
- class Value {
- }
-
- class RotationValue extends Value {
- float x, y, z, a;
- public RotationValue(StringTokenizer tokens) {
- x = Float.valueOf(tokens.nextToken()).floatValue();
- y = Float.valueOf(tokens.nextToken()).floatValue();
- z = Float.valueOf(tokens.nextToken()).floatValue();
- a = Float.valueOf(tokens.nextToken()).floatValue();
- }
- public String toString() { return x + " " + y + " " + z + " " + a; }
- }
-
-