001: /*
002: * $RCSfile: LwoTexture.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.awt.Component;
048: import java.awt.Image;
049: import java.awt.image.BufferedImage;
050: import java.awt.image.DataBufferInt;
051: import java.io.FileReader;
052: import java.io.File;
053: import java.io.IOException;
054: import java.util.Vector;
055: import java.util.Enumeration;
056: import java.util.Hashtable;
057: import javax.vecmath.Color3f;
058: import javax.vecmath.Vector3f;
059: import com.sun.j3d.utils.image.TextureLoader;
060: import javax.media.j3d.Texture;
061: import javax.media.j3d.Texture2D;
062: import javax.media.j3d.ImageComponent;
063: import javax.media.j3d.ImageComponent2D;
064: import com.sun.j3d.loaders.lw3d.LWOBFileReader;
065: import java.io.FileNotFoundException;
066: import com.sun.j3d.loaders.ParsingErrorException;
067:
068: /**
069: * This class is responsible for parsing the binary data in an Object file
070: * that describes a texture for a particular surface and turning that data
071: * into Java3D texture data. If the texture is coming from a file (which
072: * is the only type of texture handled by the loader currently; other
073: * types of texture definitions are ignored), then this class instantiates
074: * a TargaReader object to read the data in that file. Once all of the
075: * data has been read, the class creates a Java3D Texture object by first
076: * scaling the image using the ImageScaler class (since all textures must
077: * have width/height = power of 2; Note: this functionality is now built
078: * into the TextureLoader class, so it could be removed from this loader)
079: * and then creating a Texture with that image.
080: */
081:
082: class LwoTexture extends ParserObject {
083:
084: LWOBFileReader theReader;
085: int red = 255, green = 255, blue = 255;
086: Color3f color, diffuseColor, specularColor, emissiveColor;
087: Image theImage = null;
088: String imageFile = null;
089: Vector3f textureSize = new Vector3f(1f, 1f, 1f);;
090: Vector3f textureCenter = new Vector3f(0f, 0f, 0f);
091: int textureAxis;
092: int flags = 0;
093: String type;
094: String mappingType;
095: String nextToken = null;
096: static Hashtable imageTable = new Hashtable();
097: static Hashtable textureTable = new Hashtable();
098:
099: /**
100: * Constructor: calls readTexture() to parse the file and retrieve
101: * texture parameters
102: */
103: LwoTexture(LWOBFileReader reader, int length, String typename,
104: int debugVals) throws FileNotFoundException {
105: super (debugVals);
106: debugOutputLn(TRACE, "Constructor");
107: theReader = reader;
108: type = typename;
109: readTexture(length);
110: }
111:
112: String getNextToken() {
113: return nextToken;
114: }
115:
116: /**
117: * The loader currently only handles CTEX and DTEX texture types
118: * (These either represent the surface color like a decal (CTEX)
119: * or modify the diffuse color (DTEX)
120: */
121: boolean isHandled() {
122: if ((type.equals("CTEX") || type.equals("DTEX"))
123: && theImage != null)
124: return true;
125: debugOutputLn(LINE_TRACE,
126: "failed isHandled(), type, theImage = " + type + ", "
127: + theImage);
128: return false;
129: }
130:
131: /**
132: * Return the actual Texture object associated with the current image.
133: * If we've already created a texture for this image, return that;
134: * otherwise create a new Texture
135: */
136: Texture getTexture() {
137: debugOutputLn(TRACE, "getTexture()");
138: if (theImage == null)
139: return null;
140: Texture2D t2d = (Texture2D) textureTable.get(theImage);
141: if (t2d == null) {
142: ImageScaler scaler = new ImageScaler(
143: (BufferedImage) theImage);
144: BufferedImage scaledImage = (BufferedImage) scaler
145: .getScaledImage();
146: TextureLoader tl = new TextureLoader(scaledImage);
147: t2d = (Texture2D) tl.getTexture();
148: textureTable.put(theImage, t2d);
149: }
150:
151: return t2d;
152: }
153:
154: String getType() {
155: return type;
156: }
157:
158: Color3f getColor() {
159: return color;
160: }
161:
162: Image getImage() {
163: return theImage;
164: }
165:
166: Vector3f getTextureSize() {
167: return textureSize;
168: }
169:
170: int getTextureAxis() {
171: return textureAxis;
172: }
173:
174: Vector3f getTextureCenter() {
175: return textureCenter;
176: }
177:
178: String getMappingType() {
179: return mappingType;
180: }
181:
182: /**
183: * Parse the binary file to retrieve all texture parameters for this
184: * surface. If/when we encounter a TIMG parameter, which contains the
185: * filename of an image, then create a new TargaReader object to
186: * read that image file
187: */
188: void readTexture(int length) throws FileNotFoundException,
189: ParsingErrorException {
190:
191: debugOutputLn(TRACE, "readTexture()");
192:
193: int surfStopMarker = theReader.getMarker() + length;
194: mappingType = theReader.getString();
195: debugOutputLn(VALUES, "mappingType = " + mappingType);
196: String tokenString = theReader.getToken();
197: while (!(tokenString == null)
198: && theReader.getMarker() < surfStopMarker) {
199:
200: debugOutputLn(VALUES, " tokenString = " + tokenString);
201: debugOutputLn(VALUES, " marker, stop = "
202: + theReader.getMarker() + ", " + surfStopMarker);
203:
204: if (tokenString.endsWith("TEX")
205: || (!tokenString.startsWith("T") || tokenString
206: .equals("TRAN"))) {
207: nextToken = tokenString;
208: return;
209: }
210:
211: int fieldLength = theReader.getShortInt();
212: debugOutputLn(VALUES, " fl = " + fieldLength);
213:
214: if (tokenString.equals("TFLG")) {
215: debugOutputLn(WARNING, "Not yet handling: "
216: + tokenString);
217: flags = theReader.getShortInt();
218: textureAxis = flags & 0x07;
219: debugOutputLn(WARNING, "val = " + flags);
220: } else if (tokenString.equals("TCLR")) {
221: debugOutputLn(WARNING, "Not yet handling: "
222: + tokenString);
223: try {
224: red = theReader.read();
225: green = theReader.read();
226: blue = theReader.read();
227: theReader.read();
228: } catch (IOException e) {
229: throw new ParsingErrorException(e.getMessage());
230: }
231: debugOutputLn(WARNING, "val = " + red + ", " + green
232: + ", " + blue);
233: } else if (tokenString.equals("TIMG")) {
234: debugOutputLn(WARNING, "Not yet handling: "
235: + tokenString);
236: imageFile = theReader.getString();
237: debugOutputLn(VALUES, "imageFile = " + imageFile);
238: if (imageFile.indexOf("none") == -1) {
239: if ((theImage = (Image) imageTable.get(imageFile)) == null) {
240: try {
241: TargaReader tr = new TargaReader(imageFile,
242: debugPrinter.getValidOutput());
243: theImage = tr.getImage();
244: imageTable.put(imageFile, theImage);
245: } catch (FileNotFoundException e) {
246: // Ignore texture if can't find it
247: debugOutputLn(WARNING,
248: "Image File skipped: " + imageFile);
249: }
250: }
251: }
252: debugOutputLn(WARNING, "val = __" + imageFile + "__");
253: } else if (tokenString.equals("TWRP")) {
254: debugOutputLn(WARNING, "Not yet handling: "
255: + tokenString);
256: int widthWrap = theReader.getShortInt();
257: int heightWrap = theReader.getShortInt();
258: debugOutputLn(WARNING, "val = " + widthWrap + ", "
259: + heightWrap);
260: } else if (tokenString.equals("TCTR")) {
261: debugOutputLn(WARNING, "Not yet handling: "
262: + tokenString);
263: textureCenter.x = theReader.getFloat();
264: textureCenter.y = theReader.getFloat();
265: textureCenter.z = theReader.getFloat();
266: debugOutputLn(WARNING, "val = " + textureCenter);
267: } else if (tokenString.equals("TSIZ")) {
268: debugOutputLn(WARNING, "Not yet handling: "
269: + tokenString);
270: textureSize.x = theReader.getFloat();
271: textureSize.y = theReader.getFloat();
272: textureSize.z = theReader.getFloat();
273: debugOutputLn(WARNING, "val = " + textureSize);
274: } else {
275: debugOutputLn(WARNING, "unrecognized token: "
276: + tokenString);
277: theReader.skipLength(fieldLength);
278: }
279: if (theReader.getMarker() < surfStopMarker) {
280: tokenString = theReader.getToken();
281: }
282: }
283: }
284: }
|