001: /*
002: * $RCSfile: SequenceReader.java,v $
003: *
004: * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * - Redistribution of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * - Redistribution in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * Neither the name of Sun Microsystems, Inc. or the names of
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * This software is provided "AS IS," without a warranty of any
023: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
024: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
025: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
026: * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
027: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
028: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
029: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
030: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
031: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
032: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
033: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
034: * POSSIBILITY OF SUCH DAMAGES.
035: *
036: * You acknowledge that this software is not designed, licensed or
037: * intended for use in the design, construction, operation or
038: * maintenance of any nuclear facility.
039: *
040: * $Revision: 1.4 $
041: * $Date: 2007/02/09 17:20:09 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders.lw3d;
046:
047: import java.awt.Component;
048: import java.util.Enumeration;
049: import java.util.Hashtable;
050: import java.util.Vector;
051: import java.io.BufferedReader;
052: import java.io.File;
053: import java.io.FileReader;
054: import java.io.StreamTokenizer;
055: import java.io.IOException;
056: import javax.media.j3d.*;
057: import javax.vecmath.Point3d;
058:
059: import com.sun.j3d.loaders.IncorrectFormatException;
060: import com.sun.j3d.loaders.ParsingErrorException;
061: import java.io.FileNotFoundException;
062:
063: /**
064: * This class was created to read a special file format devised for
065: * JavaOne '98 that allowed Tloop functionality inside of Lightwave. It
066: * would be best to find a more standard solution, including using some
067: * plug-in for lw3d that I've heard of that allows artists to automatically
068: * save out the geometry for a file at every frame.
069: */
070:
071: class SequenceReader {
072:
073: Vector sequenceLines;
074: float totalTime;
075: int totalFrames;
076:
077: TransformGroup objectTransform;
078: Vector behaviorVector;
079:
080: /**
081: * Constructor: parses a sequence file and creates a new SequenceLine
082: * object to read in every line of the file
083: */
084: SequenceReader(String filename, float time, int frames)
085: throws ParsingErrorException {
086: totalTime = time;
087: totalFrames = frames;
088: sequenceLines = new Vector();
089: try {
090: // System.out.println("reading sequence from " + filename);
091: StreamTokenizer st = new StreamTokenizer(
092: new BufferedReader(new FileReader(filename)));
093: st.wordChars('_', '_');
094: st.wordChars('/', '/');
095: int type = st.nextToken();
096: while (st.ttype != StreamTokenizer.TT_EOF) {
097: sequenceLines.addElement(new SequenceLine(st,
098: totalTime, totalFrames));
099: st.nextToken();
100: }
101: } catch (IOException e) {
102: throw new ParsingErrorException(e.getMessage());
103: }
104: }
105:
106: /**
107: * Creates Java3D objects from the data defined in the sequence
108: * file. Calls each sequenceLine object to create its own
109: * j3d objects, then puts all of those objects in a single Switch
110: * node. Finally, it creates a SwitchPathInterpolator object which
111: * handles switching between each object/s defined by each line
112: */
113: void createJava3dObjects(int debugVals, int loadBehaviors)
114: throws FileNotFoundException {
115:
116: objectTransform = new TransformGroup();
117: behaviorVector = new Vector();
118: Enumeration e = sequenceLines.elements();
119: Switch switchNode = new Switch();
120: switchNode.setCapability(Switch.ALLOW_SWITCH_READ);
121: switchNode.setCapability(Switch.ALLOW_SWITCH_WRITE);
122: objectTransform.addChild(switchNode);
123: while (e.hasMoreElements()) {
124: SequenceLine line = (SequenceLine) e.nextElement();
125: line.createJava3dObjects(debugVals, loadBehaviors);
126: if (line.getGeometry() != null)
127: switchNode.addChild(line.getGeometry());
128: //objectTransform.addChild(line.getGeometry());
129: if (line.getBehavior() != null) {
130: behaviorVector.addElement(line.getBehavior());
131: }
132: }
133: float knots[] = new float[sequenceLines.size() + 1];
134: for (int i = 0; i < knots.length - 1; ++i) {
135: SequenceLine sl = (SequenceLine) sequenceLines.elementAt(i);
136: knots[i] = (float) sl.startFrame / (float) totalFrames;
137: }
138: knots[knots.length - 1] = 1.0f;
139: Alpha theAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0, 0,
140: (long) (1000f * totalTime), 0, 0, 0, 0, 0);
141:
142: SwitchPathInterpolator switchPath = new SwitchPathInterpolator(
143: theAlpha, knots, switchNode);
144: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
145: 0.0, 0.0), 1000000.0);
146: switchPath.setSchedulingBounds(bounds);
147: switchNode.addChild(switchPath);
148: behaviorVector.addElement(switchPath);
149: }
150:
151: TransformGroup getObjectNode() {
152: return objectTransform;
153: }
154:
155: Vector getObjectBehaviors() {
156: return behaviorVector;
157: }
158:
159: void printLines() {
160: Enumeration e = sequenceLines.elements();
161: while (e.hasMoreElements()) {
162: SequenceLine line = (SequenceLine) e.nextElement();
163: }
164: }
165:
166: }
|