001: /*
002: * $RCSfile: TextureImage.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 java.applet.Applet;
048: import java.awt.*;
049: import com.sun.j3d.utils.applet.MainFrame;
050: import com.sun.j3d.utils.universe.*;
051: import com.sun.j3d.utils.image.TextureLoader;
052: import com.sun.j3d.utils.geometry.Box;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import org.jdesktop.j3d.examples.Resources;
056:
057: public class TextureImage extends Applet {
058:
059: private static final String defaultFileName = "resources/images/stone.jpg";
060: private java.net.URL texImage = null;
061:
062: private SimpleUniverse u = null;
063:
064: public BranchGroup createSceneGraph() {
065: // Create the root of the branch graph
066: BranchGroup objRoot = new BranchGroup();
067:
068: // Create the transform group node and initialize it to the
069: // identity. Enable the TRANSFORM_WRITE capability so that
070: // our behavior code can modify it at runtime. Add it to the
071: // root of the subgraph.
072: TransformGroup objTrans = new TransformGroup();
073: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
074: objRoot.addChild(objTrans);
075:
076: // Create appearance object for textured cube
077: Appearance app = new Appearance();
078: Texture tex = new TextureLoader(texImage,
079: TextureLoader.BY_REFERENCE | TextureLoader.Y_UP, this )
080: .getTexture();
081: app.setTexture(tex);
082: TextureAttributes texAttr = new TextureAttributes();
083: texAttr.setTextureMode(TextureAttributes.MODULATE);
084: app.setTextureAttributes(texAttr);
085:
086: // Create textured cube and add it to the scene graph.
087: Box textureCube = new Box(0.4f, 0.4f, 0.4f,
088: Box.GENERATE_TEXTURE_COORDS
089: | Box.GENERATE_TEXTURE_COORDS_Y_UP, app);
090: objTrans.addChild(textureCube);
091:
092: // Create a new Behavior object that will perform the desired
093: // operation on the specified transform object and add it into
094: // the scene graph.
095: Transform3D yAxis = new Transform3D();
096: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
097: 0, 4000, 0, 0, 0, 0, 0);
098:
099: RotationInterpolator rotator = new RotationInterpolator(
100: rotationAlpha, objTrans, yAxis, 0.0f,
101: (float) Math.PI * 2.0f);
102: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
103: 0.0, 0.0), 100.0);
104: rotator.setSchedulingBounds(bounds);
105: objTrans.addChild(rotator);
106:
107: // Have Java 3D perform optimizations on this scene graph.
108: objRoot.compile();
109:
110: return objRoot;
111: }
112:
113: public TextureImage() {
114: }
115:
116: public TextureImage(java.net.URL url) {
117: texImage = url;
118: }
119:
120: public void init() {
121: if (texImage == null) {
122: // the path to the image for an applet
123: texImage = Resources.getResource(defaultFileName);
124: if (texImage == null) {
125: System.err.println(defaultFileName + " not found");
126: System.exit(1);
127: }
128: }
129: setLayout(new BorderLayout());
130: GraphicsConfiguration config = SimpleUniverse
131: .getPreferredConfiguration();
132:
133: Canvas3D c = new Canvas3D(config);
134: add("Center", c);
135:
136: // Create a simple scene and attach it to the virtual universe
137: BranchGroup scene = createSceneGraph();
138: u = new SimpleUniverse(c);
139:
140: // This will move the ViewPlatform back a bit so the
141: // objects in the scene can be viewed.
142: u.getViewingPlatform().setNominalViewingTransform();
143:
144: u.addBranchGraph(scene);
145: }
146:
147: public void destroy() {
148: u.cleanup();
149: }
150:
151: //
152: // The following allows TextureImage to be run as an application
153: // as well as an applet
154: //
155: public static void main(String[] args) {
156: java.net.URL url = null;
157: if (args.length > 0) {
158: try {
159: final String name = args[0];
160: if (name.startsWith("http:")
161: || name.startsWith("https:")
162: || name.startsWith("ftp:")
163: || name.startsWith("file:")) {
164: url = new java.net.URL(name);
165: } else {
166: url = new java.net.URL("file:" + name);
167: }
168: } catch (java.net.MalformedURLException ex) {
169: System.out.println(ex.getMessage());
170: System.exit(1);
171: }
172: } else {
173: // the path to the image for an application
174: url = Resources.getResource(defaultFileName);
175: if (url == null) {
176: System.err.println(defaultFileName + " not found");
177: System.exit(1);
178: }
179: }
180: new MainFrame(new TextureImage(url), 256, 256);
181: }
182:
183: }
|