001: /*
002: * $RCSfile: LwsEnvelope.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:08 $
042: * $State: Exp $
043: */
044:
045: package com.sun.j3d.loaders.lw3d;
046:
047: import java.io.*;
048: import javax.media.j3d.*;
049: import javax.vecmath.*;
050: import com.sun.j3d.internal.J3dUtilsI18N;
051: import com.sun.j3d.loaders.ParsingErrorException;
052: import com.sun.j3d.loaders.IncorrectFormatException;
053:
054: /**
055: * This class is a superclass for any implementation of envelopes; the
056: * only subclass currently is LwsEnvelopeLightIntensity. LwsEnvelope
057: * parses the data in a Scene file and extracts the envelope data.
058: */
059:
060: class LwsEnvelope extends TextfileParser {
061:
062: // data from the file
063: String name;
064: LwsEnvelopeFrame frames[];
065: int numFrames;
066: int numChannels;
067: boolean loop;
068: float totalTime;
069: int totalFrames;
070: Behavior behaviors;
071:
072: /**
073: * Constructor: calls getEnvelope() to parse the stream for the
074: * envelope data
075: */
076: LwsEnvelope(StreamTokenizer st, int frames, float time) {
077: numFrames = 0;
078: totalTime = time;
079: totalFrames = frames;
080: name = getName(st);
081: getEnvelope(st);
082: }
083:
084: /**
085: * Parses the stream to retrieve all envelope data. Creates
086: * LwsEnvelopeFrame objects for each keyframe of the envelope
087: * (these frames differ slightly from LwsFrame objects because
088: * envelopes contain slightly different data)
089: */
090: void getEnvelope(StreamTokenizer st)
091: throws IncorrectFormatException, ParsingErrorException {
092: debugOutputLn(TRACE, "getEnvelope()");
093: numChannels = (int) getNumber(st);
094: if (numChannels != 1) {
095: throw new IncorrectFormatException(J3dUtilsI18N
096: .getString("LwsEnvelope0"));
097: }
098: debugOutputLn(LINE_TRACE, "got channels");
099:
100: numFrames = (int) getNumber(st);
101: frames = new LwsEnvelopeFrame[numFrames];
102: debugOutputLn(VALUES, "got frames" + numFrames);
103:
104: for (int i = 0; i < numFrames; ++i) {
105: frames[i] = new LwsEnvelopeFrame(st);
106: }
107: debugOutput(LINE_TRACE, "got all frames");
108:
109: try {
110: st.nextToken();
111: while (!isCurrentToken(st, "EndBehavior")) {
112: // There is an undocumented "FrameOffset" in some files
113: st.nextToken();
114: }
115: } catch (IOException e) {
116: throw new ParsingErrorException(e.getMessage());
117: }
118: int repeatVal = (int) getNumber(st);
119: if (repeatVal == 1)
120: loop = false;
121: else
122: loop = true;
123: }
124:
125: /**
126: * This superclass does nothing here - if the loader understands
127: * how to deal with a particular type of envelope, it will use
128: * a subclass of LwsEnvelope that will override this method
129: */
130: void createJava3dBehaviors(TransformGroup target) {
131: behaviors = null;
132: }
133:
134: Behavior getBehaviors() {
135: return behaviors;
136: }
137:
138: LwsEnvelopeFrame getFirstFrame() {
139: if (numFrames > 0)
140: return frames[0];
141: else
142: return null;
143: }
144:
145: void printVals() {
146: debugOutputLn(VALUES, " name = " + name);
147: debugOutputLn(VALUES, " numChannels = " + numChannels);
148: debugOutputLn(VALUES, " numFrames = " + numFrames);
149: debugOutputLn(VALUES, " loop = " + loop);
150: for (int i = 0; i < numFrames; ++i) {
151: debugOutputLn(VALUES, " FRAME " + i);
152: frames[i].printVals();
153: }
154: }
155:
156: }
|