001: /*
002: * Copyright (c) 2000 Silvere Martin-Michiellot All Rights Reserved.
003: *
004: * Silvere Martin-Michiellot grants you ("Licensee") a non-exclusive,
005: * royalty free, license to use, modify and redistribute this
006: * software in source and binary code form,
007: * provided that i) this copyright notice and license appear on all copies of
008: * the software; and ii) Licensee does not utilize the software in a manner
009: * which is disparaging to Silvere Martin-Michiellot.
010: *
011: * This software is provided "AS IS," without a warranty of any kind. ALL
012: * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
013: * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
014: * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Silvere Martin-Michiellot
015: * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
016: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
017: * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL
018: * Silvere Martin-Michiellot OR ITS LICENSORS BE LIABLE
019: * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
020: * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
021: * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
022: * OR INABILITY TO USE SOFTWARE, EVEN IF Silvere Martin-Michiellot HAS BEEN
023: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
024: *
025: * This software is not designed or intended for use in on-line control of
026: * aircraft, air traffic, aircraft navigation or aircraft communications; or in
027: * the design, construction, operation or maintenance of any nuclear
028: * facility. Licensee represents and warrants that it will not use or
029: * redistribute the Software for such purposes.
030: *
031: */
032:
033: package com.db.utils;
034:
035: import java.awt.*;
036: import java.util.*;
037: import javax.media.j3d.*;
038: import javax.vecmath.*;
039: import com.sun.j3d.utils.image.*;
040:
041: public class MIPMapTextureLoader extends Object {
042:
043: public static final int SMALLEST = 1;
044:
045: public static final int POINT = 1;
046: public static final int LINEAR = 2;
047:
048: protected Component observer;
049: protected String filename = null;
050:
051: protected int mode = MIPMapTextureLoader.POINT;
052:
053: public MIPMapTextureLoader() {
054:
055: super ();
056:
057: }
058:
059: public MIPMapTextureLoader(String filename) {
060:
061: super ();
062: this .filename = filename;
063:
064: }
065:
066: public MIPMapTextureLoader(int mode) {
067:
068: super ();
069: this .mode = mode;
070:
071: }
072:
073: public MIPMapTextureLoader(Component observer) {
074:
075: super ();
076: this .observer = observer;
077:
078: }
079:
080: public MIPMapTextureLoader(String filename, int mode) {
081:
082: super ();
083: this .filename = filename;
084: this .mode = mode;
085:
086: }
087:
088: public MIPMapTextureLoader(String filename, Component observer) {
089:
090: super ();
091: this .filename = filename;
092: this .observer = observer;
093:
094: }
095:
096: public MIPMapTextureLoader(Component observer, int mode) {
097:
098: super ();
099: this .mode = mode;
100: this .observer = observer;
101:
102: }
103:
104: public MIPMapTextureLoader(String filename, int mode,
105: Component observer) {
106:
107: super ();
108: this .filename = filename;
109: this .mode = mode;
110: this .observer = observer;
111:
112: }
113:
114: public void setMode(int mode) {
115:
116: this .mode = mode;
117:
118: }
119:
120: public int getMode() {
121:
122: return this .mode;
123:
124: }
125:
126: public void setFileName(String filename) {
127:
128: this .filename = filename;
129:
130: }
131:
132: public String getFileName() {
133:
134: return this .filename;
135:
136: }
137:
138: public void setImageObserver(Component observer) {
139: this .observer = observer;
140: }
141:
142: public Component getImageObserver() {
143: return this .observer;
144: }
145:
146: public Texture2D generateMIPMapTextures() {
147:
148: return generateMIPMapTextures(this .filename, this .mode,
149: this .observer);
150:
151: }
152:
153: public Texture2D loadMIPMapTextures() {
154:
155: return loadMIPMapTextures(this .filename, this .mode,
156: this .observer);
157:
158: }
159:
160: public Texture2D generateMIPMapTextures(String filename, int mode,
161: Component observer) {
162:
163: TextureLoader textureLoader;
164: ImageComponent2D imageComponent2D;
165: int imageWidth, imageHeight, imageLevel;
166:
167: if (filename != null) {
168:
169: textureLoader = new TextureLoader(filename,
170: TextureLoader.GENERATE_MIPMAP, observer);
171: imageComponent2D = textureLoader.getImage();
172:
173: if (imageComponent2D != null) {
174:
175: imageWidth = imageComponent2D.getWidth();
176: imageHeight = imageComponent2D.getHeight();
177:
178: Texture2D texture2D = new Texture2D(
179: Texture.MULTI_LEVEL_MIPMAP, Texture.RGBA,
180: imageWidth, imageHeight);
181: imageLevel = 0;
182:
183: texture2D.setImage(imageLevel, imageComponent2D);
184:
185: while (imageWidth > SMALLEST || imageWidth > SMALLEST) {
186: imageLevel++;
187: if (imageWidth > SMALLEST)
188: imageWidth /= 2;
189: if (imageHeight > SMALLEST)
190: imageHeight /= 2;
191: imageComponent2D = textureLoader.getScaledImage(
192: imageWidth, imageHeight);
193: texture2D.setImage(imageLevel, imageComponent2D);
194: }
195:
196: if (mode == MIPMapTextureLoader.POINT) {
197: texture2D.setMagFilter(Texture.BASE_LEVEL_POINT);
198: texture2D.setMinFilter(Texture.MULTI_LEVEL_POINT);
199: } else {
200: texture2D.setMagFilter(Texture.BASE_LEVEL_LINEAR);
201: texture2D.setMinFilter(Texture.MULTI_LEVEL_LINEAR);
202: }
203:
204: return texture2D;
205: } else
206: return null;
207: } else
208: return null;
209:
210: }
211:
212: public Texture2D loadMIPMapTextures(String filename, int mode,
213: Component observer) {
214:
215: TextureLoader textureLoader;
216: ImageComponent2D imageComponent2D;
217: int imageWidth, imageHeight, imageLevel;
218: StringTokenizer stringTokenizer;
219: String beginning;
220: String ending;
221: String othersfilename;
222:
223: if (filename != null) {
224:
225: textureLoader = new TextureLoader(filename, observer);
226: imageComponent2D = textureLoader.getImage();
227:
228: if (imageComponent2D != null) {
229:
230: imageWidth = imageComponent2D.getWidth();
231: imageHeight = imageComponent2D.getHeight();
232:
233: Texture2D texture2D = new Texture2D(
234: Texture.MULTI_LEVEL_MIPMAP, Texture.RGBA,
235: imageWidth, imageHeight);
236: imageLevel = 0;
237: stringTokenizer = new StringTokenizer(filename,
238: new String("."));
239: beginning = stringTokenizer.nextToken();
240: ending = filename.substring(beginning.length(),
241: filename.length());
242: if (beginning.endsWith("0")) {
243: beginning = beginning.substring(0, beginning
244: .length() - 1);
245: }
246:
247: while ((imageWidth > SMALLEST || imageWidth > SMALLEST)
248: && (imageComponent2D != null)) {
249: texture2D.setImage(imageLevel, imageComponent2D);
250: imageLevel++;
251: if (imageWidth > SMALLEST)
252: imageWidth /= 2;
253: if (imageHeight > SMALLEST)
254: imageHeight /= 2;
255:
256: othersfilename = new String(beginning.concat(
257: new Integer(imageLevel).toString()).concat(
258: ending));
259:
260: textureLoader = new TextureLoader(filename,
261: observer);
262: imageComponent2D = textureLoader.getImage();
263:
264: }
265:
266: if (imageComponent2D != null) {
267: texture2D.setImage(imageLevel, imageComponent2D);
268:
269: if (mode == MIPMapTextureLoader.POINT) {
270: texture2D
271: .setMagFilter(Texture.BASE_LEVEL_POINT);
272: texture2D
273: .setMinFilter(Texture.MULTI_LEVEL_POINT);
274: } else {
275: texture2D
276: .setMagFilter(Texture.BASE_LEVEL_LINEAR);
277: texture2D
278: .setMinFilter(Texture.MULTI_LEVEL_LINEAR);
279: }
280:
281: return texture2D;
282: } else
283: return null;
284: } else
285: return null;
286: } else
287: return null;
288: }
289:
290: }
|