001: /*
002: * $RCSfile: AnimateTexturesBehavior.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.2 $
041: * $Date: 2007/02/09 17:21:54 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.texture_by_ref;
046:
047: import javax.media.j3d.*;
048: import java.awt.image.BufferedImage;
049: import java.awt.*;
050: import com.sun.j3d.utils.image.TextureLoader;
051: import java.util.Enumeration;
052:
053: public class AnimateTexturesBehavior extends Behavior {
054:
055: // what image are we on
056: private int current;
057: private int max;
058:
059: // the images
060: private ImageComponent2D[] images;
061:
062: // the target
063: private Texture2D texture;
064: private Appearance appearance;
065:
066: // the wakeup criterion
067: private WakeupCriterion wakeupC;
068:
069: // are the images flipped?
070: private boolean flip;
071:
072: // need the current type because by copy changes all images
073: // to TYPE_INT_ARGB
074: private int currentType;
075:
076: // for custom types
077: public static final int TYPE_CUSTOM_RGBA = 0x01;
078: public static final int TYPE_CUSTOM_RGB = 0x02;
079:
080: private int customType;
081:
082: // create a new AnimateTextureBehavior
083: // initialize the images
084: public AnimateTexturesBehavior(Texture2D texP,
085: java.net.URL[] fnames, Appearance appP,
086: TextureByReference applet) {
087: int size = fnames.length;
088: images = new ImageComponent2D[size];
089: BufferedImage bImage;
090: TextureLoader loader;
091: for (int i = 0; i < size; i++) {
092: loader = new TextureLoader(fnames[i],
093: TextureLoader.BY_REFERENCE | TextureLoader.Y_UP,
094: applet);
095: images[i] = loader.getImage();
096: bImage = images[i].getImage();
097:
098: // convert the image to TYPE_4BYTE_ABGR
099: currentType = BufferedImage.TYPE_4BYTE_ABGR;
100: bImage = ImageOps.convertImage(bImage, currentType);
101: // flip the image
102: flip = true;
103: ImageOps.flipImage(bImage);
104:
105: // set the image on the ImageComponent to the new one
106: images[i].set(bImage);
107:
108: images[i].setCapability(ImageComponent.ALLOW_IMAGE_READ);
109: images[i].setCapability(ImageComponent.ALLOW_FORMAT_READ);
110: }
111: texture = texP;
112: current = 0;
113: max = size;
114: wakeupC = new WakeupOnElapsedFrames(20);
115: appearance = appP;
116: }
117:
118: // initialize to the first image
119: public void initialize() {
120: texture.setImage(0, images[current]);
121: if (current < max - 1)
122: current++;
123: else
124: current = 0;
125: wakeupOn(wakeupC);
126: }
127:
128: // procesStimulus changes the ImageComponent of the texture
129: public void processStimulus(Enumeration criteria) {
130: // ImageOps.printType(images[current].getImage());
131: texture.setImage(0, images[current]);
132: appearance.setTexture(texture);
133: if (current < max - 1)
134: current++;
135: else
136: current = 0;
137: wakeupOn(wakeupC);
138: }
139:
140: // flip the image -- useful depending on yUp
141: public void setFlipImages(boolean b) {
142: // double check that flipping is necessary
143: if (b != flip) {
144: BufferedImage bImage;
145:
146: // these are the same for all images so get info once
147: int format = images[0].getFormat();
148: boolean byRef = images[0].isByReference();
149: boolean yUp = images[0].isYUp();
150:
151: // flip all the images
152: // have to new ImageComponents because can't set the image at runtime
153: for (int i = 0; i < images.length; i++) {
154: bImage = images[i].getImage();
155: ImageOps.flipImage(bImage);
156: // if we are byRef and the bImage type does not match currentType
157: // we need to convert it. If we are not byRef we will
158: // save converting until it is changed to byRef
159: if (byRef && bImage.getType() != currentType) {
160: if (currentType != BufferedImage.TYPE_CUSTOM) {
161: bImage = ImageOps.convertImage(bImage,
162: currentType);
163: } else if (customType == this .TYPE_CUSTOM_RGBA) {
164: bImage = ImageOps.convertToCustomRGBA(bImage);
165: } else {
166: bImage = ImageOps.convertToCustomRGB(bImage);
167: }
168: }
169: images[i] = new ImageComponent2D(format, bImage, byRef,
170: yUp);
171: images[i]
172: .setCapability(ImageComponent.ALLOW_IMAGE_READ);
173: images[i]
174: .setCapability(ImageComponent.ALLOW_FORMAT_READ);
175: }
176:
177: // set flip to new value
178: flip = b;
179: }
180: }
181:
182: // create new ImageComponents with yUp set to the parameter. yUp on
183: // an ImageComponent cannot be changed at runtim
184: public void setYUp(boolean b) {
185: // double check that changing yUp is necessary
186: if (b != images[0].isYUp()) {
187:
188: // these are the same for all images so get info once
189: int format = images[0].getFormat();
190: boolean byRef = images[0].isByReference();
191:
192: // reset yUp on all the images -- have to new ImageComponents because
193: // cannot change the value at runtime
194: for (int i = 0; i < images.length; i++) {
195: // if we are byRef and the bImage type does not match currentType
196: // we need to convert it. If we are not byRef we will
197: // save converting until it is changed to byRef
198: BufferedImage bImage = images[i].getImage();
199: if (byRef && bImage.getType() != currentType) {
200: // bImage = ImageOps.convertImage(bImage, currentType);
201: if (currentType != BufferedImage.TYPE_CUSTOM) {
202: bImage = ImageOps.convertImage(bImage,
203: currentType);
204: } else if (customType == this .TYPE_CUSTOM_RGBA) {
205: bImage = ImageOps.convertToCustomRGBA(bImage);
206: } else {
207: bImage = ImageOps.convertToCustomRGB(bImage);
208: }
209: }
210: images[i] = new ImageComponent2D(format, bImage, byRef,
211: b);
212: images[i]
213: .setCapability(ImageComponent.ALLOW_IMAGE_READ);
214: images[i]
215: .setCapability(ImageComponent.ALLOW_FORMAT_READ);
216: }
217: }
218: }
219:
220: // create new ImageComponents with ByReference set by parameter.
221: // by reference cannot be changed on an image component at runtime
222: public void setByReference(boolean b) {
223: // double check that changing is necessary
224: if (b != images[0].isByReference()) {
225:
226: // these are the same for all images so get info once
227: int format = images[0].getFormat();
228: boolean yUp = images[0].isYUp();
229:
230: // reset yUp on all the images
231: // have to new ImageComponents because cannot set value
232: for (int i = 0; i < images.length; i++) {
233: // if the bImage type does not match currentType and we are setting
234: // to byRef we need to convert it
235: BufferedImage bImage = images[i].getImage();
236: if (bImage.getType() != currentType && b) {
237: // bImage = ImageOps.convertImage(bImage, currentType);
238: if (currentType != BufferedImage.TYPE_CUSTOM) {
239: bImage = ImageOps.convertImage(bImage,
240: currentType);
241: } else if (customType == this .TYPE_CUSTOM_RGBA) {
242: bImage = ImageOps.convertToCustomRGBA(bImage);
243: } else {
244: bImage = ImageOps.convertToCustomRGB(bImage);
245: }
246: }
247: images[i] = new ImageComponent2D(format, bImage, b, yUp);
248: images[i]
249: .setCapability(ImageComponent.ALLOW_IMAGE_READ);
250: images[i]
251: .setCapability(ImageComponent.ALLOW_FORMAT_READ);
252: }
253: }
254: }
255:
256: // make a new wakeup criterion object based on the new delay time
257: public void setFrameDelay(int delay) {
258: wakeupC = new WakeupOnElapsedFrames(delay);
259: }
260:
261: //change the type of image
262: public void setImageType(int newType) {
263: currentType = newType;
264:
265: // only need to change the images if we are byRef otherwise will change
266: // them when we chnage to byRef
267: if (images[0].isByReference() == true) {
268: // this information is the same for all
269: int format = images[0].getFormat();
270: boolean yUp = images[0].isYUp();
271: boolean byRef = true;
272: for (int i = 0; i < images.length; i++) {
273: BufferedImage bImage = images[i].getImage();
274: bImage = ImageOps.convertImage(bImage, currentType);
275: images[i] = new ImageComponent2D(format, bImage, byRef,
276: yUp);
277: images[i]
278: .setCapability(ImageComponent.ALLOW_IMAGE_READ);
279: images[i]
280: .setCapability(ImageComponent.ALLOW_FORMAT_READ);
281: }
282: }
283: }
284:
285: public void setImageTypeCustomRGBA() {
286: currentType = BufferedImage.TYPE_CUSTOM;
287: customType = this .TYPE_CUSTOM_RGBA;
288:
289: // only need to change images if we are byRef otherwise will change
290: // them when we change to byRef
291: if (images[0].isByReference()) {
292: // this information is the same for all
293: int format = images[0].getFormat();
294: boolean yUp = images[0].isYUp();
295: boolean byRef = true;
296: for (int i = 0; i < images.length; i++) {
297: BufferedImage bImage = images[i].getImage();
298: bImage = ImageOps.convertToCustomRGBA(bImage);
299: images[i] = new ImageComponent2D(format, bImage, byRef,
300: yUp);
301: images[i]
302: .setCapability(ImageComponent.ALLOW_IMAGE_READ);
303: images[i]
304: .setCapability(ImageComponent.ALLOW_FORMAT_READ);
305: }
306: }
307: }
308:
309: public void setImageTypeCustomRGB() {
310: currentType = BufferedImage.TYPE_CUSTOM;
311: customType = this .TYPE_CUSTOM_RGB;
312:
313: // only need to change images if we are byRef otherwise will change
314: // them when we change to byRef
315: if (images[0].isByReference()) {
316: // this information is the same for all
317: int format = images[0].getFormat();
318: boolean yUp = images[0].isYUp();
319: boolean byRef = true;
320: for (int i = 0; i < images.length; i++) {
321: BufferedImage bImage = images[i].getImage();
322: bImage = ImageOps.convertToCustomRGB(bImage);
323: images[i] = new ImageComponent2D(format, bImage, byRef,
324: yUp);
325: images[i]
326: .setCapability(ImageComponent.ALLOW_IMAGE_READ);
327: images[i]
328: .setCapability(ImageComponent.ALLOW_FORMAT_READ);
329: }
330: }
331: }
332: }
|