001: /*
002: * $RCSfile: TextureImageNPOT.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.7 $
041: * $Date: 2007/04/24 18:55:58 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.texture;
046:
047: import com.sun.j3d.utils.applet.MainFrame;
048: import com.sun.j3d.utils.geometry.Box;
049: import com.sun.j3d.utils.image.TextureLoader;
050: import com.sun.j3d.utils.universe.SimpleUniverse;
051: import java.applet.Applet;
052: import java.awt.BorderLayout;
053: import java.awt.GraphicsConfiguration;
054: import java.util.Map;
055: import javax.media.j3d.*;
056: import javax.swing.JOptionPane;
057: import javax.vecmath.Point3d;
058: import org.jdesktop.j3d.examples.Resources;
059:
060: public class TextureImageNPOT extends Applet {
061:
062: private static final String defaultFileName = "resources/images/Java3d.jpg";
063: private java.net.URL texImage = null;
064:
065: private SimpleUniverse u = null;
066: private boolean allowNonPowerOfTwo = true;
067: private boolean mipmap = true;
068:
069: public BranchGroup createSceneGraph() {
070: // Create the root of the branch graph
071: BranchGroup objRoot = new BranchGroup();
072:
073: // Create the transform group node and initialize it to the
074: // identity. Enable the TRANSFORM_WRITE capability so that
075: // our behavior code can modify it at runtime. Add it to the
076: // root of the subgraph.
077: TransformGroup objTrans = new TransformGroup();
078: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
079: objRoot.addChild(objTrans);
080:
081: // Create the scaling transform group node and initialize it to the
082: // identity. Enable the TRANSFORM_WRITE capability so that
083: // our behavior code can modify it at runtime. Add it to the
084: // objTrans group
085: TransformGroup objScale = new TransformGroup();
086: objScale.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
087: objTrans.addChild(objScale);
088:
089: // Create appearance object for textured cube
090: Appearance app = new Appearance();
091: int flags = TextureLoader.BY_REFERENCE | TextureLoader.Y_UP;
092:
093: if (allowNonPowerOfTwo) {
094: flags |= TextureLoader.ALLOW_NON_POWER_OF_TWO;
095: }
096: if (mipmap) {
097: flags |= TextureLoader.GENERATE_MIPMAP;
098: }
099: Texture tex = new TextureLoader(texImage, flags, this )
100: .getTexture();
101: tex.setMagFilter(Texture.BASE_LEVEL_LINEAR);
102: if (mipmap) {
103: tex.setMinFilter(Texture.MULTI_LEVEL_LINEAR);
104: } else {
105: tex.setMinFilter(Texture.BASE_LEVEL_LINEAR);
106: }
107: app.setTexture(tex);
108: TextureAttributes texAttr = new TextureAttributes();
109: texAttr.setTextureMode(TextureAttributes.MODULATE);
110: app.setTextureAttributes(texAttr);
111:
112: // Create textured cube and add it to the scene graph.
113: Box textureCube = new Box(0.4f, 0.4f, 0.4f,
114: Box.GENERATE_TEXTURE_COORDS
115: | Box.GENERATE_TEXTURE_COORDS_Y_UP, app);
116: objScale.addChild(textureCube);
117:
118: // Create a new Behavior object that will perform the desired
119: // operation on the specified transform object and add it into
120: // the scene graph.
121: Transform3D yAxis = new Transform3D();
122: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
123: 0, 50000, 0, 0, 0, 0, 0);
124:
125: RotationInterpolator rotator = new RotationInterpolator(
126: rotationAlpha, objTrans, yAxis, 0.0f,
127: (float) Math.PI * 2.0f);
128: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
129: 0.0, 0.0), 100.0);
130: rotator.setSchedulingBounds(bounds);
131: objTrans.addChild(rotator);
132:
133: // Create a new Behavior object that will perform the desired
134: // operation on the specified transform object and add it into
135: // the scene graph.
136: Alpha scaleAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
137: | Alpha.DECREASING_ENABLE, 0, 0, 8000, 0, 0, 8000, 0, 0);
138:
139: ScaleInterpolator scaler = new ScaleInterpolator(scaleAlpha,
140: objScale, yAxis, 0.01f, 1.5f);
141: scaler.setSchedulingBounds(bounds);
142: objScale.addChild(scaler);
143:
144: // Have Java 3D perform optimizations on this scene graph.
145: objRoot.compile();
146:
147: return objRoot;
148: }
149:
150: public TextureImageNPOT() {
151: }
152:
153: public TextureImageNPOT(java.net.URL url) {
154: texImage = url;
155: }
156:
157: public void init() {
158: if (texImage == null) {
159: // the path to the image for an applet
160: texImage = Resources.getResource(defaultFileName);
161: if (texImage == null) {
162: System.err.println(defaultFileName + " not found");
163: System.exit(1);
164: }
165: }
166: setLayout(new BorderLayout());
167: GraphicsConfiguration config = SimpleUniverse
168: .getPreferredConfiguration();
169:
170: Canvas3D c = new Canvas3D(config);
171:
172: Map map = c.queryProperties();
173: Boolean value = (Boolean) map
174: .get("textureNonPowerOfTwoAvailable");
175: String errorStr = null;
176: if (value == null) {
177: errorStr = "Canvas3D: textureNonPowerOfTwoAvailable property not found";
178: } else if (!value) {
179: errorStr = "Non-power-of-two textures are not available";
180: }
181:
182: if (errorStr != null) {
183: String errorMessage = errorStr
184: + "\n"
185: + "You should expect to see a white cube as a result";
186: // System.err.println(errorMessage);
187: JOptionPane.showMessageDialog(this , errorMessage,
188: "Insufficient Capabilities",
189: JOptionPane.ERROR_MESSAGE);
190: }
191:
192: add("Center", c);
193:
194: // Create a simple scene and attach it to the virtual universe
195: BranchGroup scene = createSceneGraph();
196: u = new SimpleUniverse(c);
197:
198: // This will move the ViewPlatform back a bit so the
199: // objects in the scene can be viewed.
200: u.getViewingPlatform().setNominalViewingTransform();
201:
202: u.addBranchGraph(scene);
203: }
204:
205: public void destroy() {
206: u.cleanup();
207: }
208:
209: //
210: // The following allows TextureImageNPOT to be run as an application
211: // as well as an applet
212: //
213: public static void main(String[] args) {
214: java.net.URL url = null;
215: if (args.length > 0) {
216: try {
217: url = new java.net.URL("file:" + args[0]);
218: } catch (java.net.MalformedURLException ex) {
219: System.out.println(ex.getMessage());
220: System.exit(1);
221: }
222: } else {
223: // the path to the image for an application
224: url = Resources.getResource(defaultFileName);
225: if (url == null) {
226: System.err.println(defaultFileName + " not found");
227: System.exit(1);
228: }
229: }
230: new MainFrame(new TextureImageNPOT(url), 512, 512);
231: }
232:
233: }
|