001: /*
002: * $RCSfile: MultiTextureTest.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.6 $
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.image.TextureLoader;
048: import com.sun.j3d.utils.geometry.Box;
049: import com.sun.j3d.utils.behaviors.vp.*;
050: import java.applet.Applet;
051: import java.awt.*;
052: import java.awt.event.*;
053: import com.sun.j3d.utils.applet.MainFrame;
054: import com.sun.j3d.utils.universe.*;
055: import javax.media.j3d.*;
056: import javax.vecmath.*;
057: import java.awt.image.BufferedImage;
058: import org.jdesktop.j3d.examples.Resources;
059:
060: public class MultiTextureTest extends Applet implements ItemListener {
061:
062: Choice choice;
063: TextureUnitState textureUnitState[] = new TextureUnitState[2];
064: Texture stoneTex;
065: Texture skyTex;
066: Texture lightTex;
067:
068: private java.net.URL stoneImage = null;
069: private java.net.URL skyImage = null;
070:
071: private SimpleUniverse u = null;
072:
073: public Texture createLightMap() {
074:
075: int width = 128;
076: int height = 128;
077: BufferedImage bimage = new BufferedImage(width, height,
078: BufferedImage.TYPE_INT_RGB);
079: int[] rgbArray = new int[width * height];
080: int index, index2;
081: int rgbInc = 256 / (width / 2 - 20);
082: int rgbValue = 0;
083: int k = width / 2 - 5;
084: int i, j, rgb;
085:
086: rgb = 0xff;
087: rgbValue = rgb | (rgb << 8) | (rgb << 16) | (rgb << 24);
088: for (i = width / 2 - 1, j = 0; j < 10; j++, i--) {
089: rgbArray[i] = rgbValue;
090: }
091:
092: for (; i > 8; i--, rgb -= rgbInc) {
093: rgbValue = rgb | (rgb << 8) | (rgb << 16) | (rgb << 24);
094: rgbArray[i] = rgbValue;
095: }
096:
097: for (; i >= 0; i--) {
098: rgbArray[i] = rgbValue;
099: }
100:
101: for (i = 0; i < width / 2; i++) {
102: rgbValue = rgbArray[i];
103: index = i;
104: index2 = (width - i - 1);
105: for (j = 0; j < height; j++) {
106: rgbArray[index] = rgbArray[index2] = rgbValue;
107: index += width;
108: index2 += width;
109: }
110: }
111:
112: bimage.setRGB(0, 0, width, height, rgbArray, 0, width);
113:
114: ImageComponent2D grayImage = new ImageComponent2D(
115: ImageComponent.FORMAT_RGB, bimage, true, true);
116:
117: lightTex = new Texture2D(Texture.BASE_LEVEL, Texture.RGB,
118: width, height);
119: lightTex.setImage(0, grayImage);
120:
121: return lightTex;
122: }
123:
124: public BranchGroup createSceneGraph() {
125: // Create the root of the branch graph
126: BranchGroup objRoot = new BranchGroup();
127:
128: // Create a Transformgroup to scale all objects so they
129: // appear in the scene.
130: TransformGroup objScale = new TransformGroup();
131: Transform3D t3d = new Transform3D();
132: t3d.setScale(0.4);
133: objScale.setTransform(t3d);
134: objRoot.addChild(objScale);
135:
136: TransformGroup objTrans = new TransformGroup();
137: //write-enable for behaviors
138: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
139: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
140: objTrans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
141: objScale.addChild(objTrans);
142:
143: Appearance ap = new Appearance();
144:
145: // load textures
146: TextureAttributes texAttr1 = new TextureAttributes();
147: texAttr1.setTextureMode(TextureAttributes.DECAL);
148: TextureAttributes texAttr2 = new TextureAttributes();
149: texAttr2.setTextureMode(TextureAttributes.MODULATE);
150:
151: TextureLoader tex = new TextureLoader(stoneImage, new String(
152: "RGB"),
153: TextureLoader.BY_REFERENCE | TextureLoader.Y_UP, this );
154: if (tex == null)
155: return null;
156: stoneTex = tex.getTexture();
157:
158: tex = new TextureLoader(skyImage, new String("RGB"),
159: TextureLoader.BY_REFERENCE | TextureLoader.Y_UP, this );
160: if (tex == null)
161: return null;
162: skyTex = tex.getTexture();
163:
164: lightTex = createLightMap();
165:
166: textureUnitState[0] = new TextureUnitState(stoneTex, texAttr1,
167: null);
168: textureUnitState[0]
169: .setCapability(TextureUnitState.ALLOW_STATE_WRITE);
170:
171: textureUnitState[1] = new TextureUnitState(lightTex, texAttr2,
172: null);
173: textureUnitState[1]
174: .setCapability(TextureUnitState.ALLOW_STATE_WRITE);
175:
176: ap.setTextureUnitState(textureUnitState);
177:
178: //Create a Box
179: Box BoxObj = new Box(1.5f, 1.5f, 0.8f, Box.GENERATE_NORMALS
180: | Box.GENERATE_TEXTURE_COORDS
181: | Box.GENERATE_TEXTURE_COORDS_Y_UP, ap, 2);
182: // add it to the scene graph.
183: objTrans.addChild(BoxObj);
184:
185: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
186: 0.0, 0.0), 100.0);
187:
188: //Shine it with two lights.
189: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
190: Color3f lColor2 = new Color3f(0.2f, 0.2f, 0.1f);
191: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
192: Vector3f lDir2 = new Vector3f(0.0f, 0.0f, -1.0f);
193: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
194: DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
195: lgt1.setInfluencingBounds(bounds);
196: lgt2.setInfluencingBounds(bounds);
197: objScale.addChild(lgt1);
198: objScale.addChild(lgt2);
199:
200: // Let Java 3D perform optimizations on this scene graph.
201: objRoot.compile();
202:
203: return objRoot;
204: }
205:
206: public MultiTextureTest() {
207: }
208:
209: public MultiTextureTest(java.net.URL stoneURL, java.net.URL skyURL) {
210: stoneImage = stoneURL;
211: skyImage = skyURL;
212: }
213:
214: public void init() {
215: if (stoneImage == null) {
216: // the path to the image for an applet
217: stoneImage = Resources
218: .getResource("resources/images/stone.jpg");
219: if (stoneImage == null) {
220: System.err
221: .println("resources/images/stone.jpg not found");
222: System.exit(1);
223: }
224:
225: if (skyImage == null) {
226: // the path to the image for an applet
227: skyImage = Resources
228: .getResource("resources/images/bg.jpg");
229: if (skyImage == null) {
230: System.err
231: .println("resources/images/bg.jpg not found");
232: System.exit(1);
233: }
234: }
235: }
236:
237: setLayout(new BorderLayout());
238: GraphicsConfiguration config = SimpleUniverse
239: .getPreferredConfiguration();
240:
241: Canvas3D c = new Canvas3D(config);
242: add("Center", c);
243:
244: BranchGroup scene = createSceneGraph();
245: u = new SimpleUniverse(c);
246:
247: ViewingPlatform viewingPlatform = u.getViewingPlatform();
248: // This will move the ViewPlatform back a bit so the
249: // objects in the scene can be viewed.
250: viewingPlatform.setNominalViewingTransform();
251:
252: // add orbit behavior but disable translate
253: OrbitBehavior orbit = new OrbitBehavior(c,
254: OrbitBehavior.REVERSE_ALL
255: | OrbitBehavior.DISABLE_TRANSLATE);
256: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
257: 0.0, 0.0), 100.0);
258: orbit.setSchedulingBounds(bounds);
259: viewingPlatform.setViewPlatformBehavior(orbit);
260:
261: u.addBranchGraph(scene);
262:
263: // create the gui
264: choice = new Choice();
265: choice.addItem("stone + light");
266: choice.addItem("stone");
267: choice.addItem("lightMap");
268: choice.addItem("sky");
269: choice.addItem("stone + sky");
270: choice.addItemListener(this );
271: add("North", choice);
272:
273: }
274:
275: public void destroy() {
276: // Cleanup reference to Java3D
277: textureUnitState = new TextureUnitState[2];
278: u.cleanup();
279: }
280:
281: public void itemStateChanged(ItemEvent e) {
282: int index = choice.getSelectedIndex();
283:
284: switch (index) {
285: case 0: /* stone + light */
286: textureUnitState[0].setTexture(stoneTex);
287: textureUnitState[1].setTexture(lightTex);
288: break;
289: case 1: /* stone */
290: textureUnitState[0].setTexture(stoneTex);
291: textureUnitState[1].setTexture(null);
292: break;
293: case 2: /* light */
294: textureUnitState[0].setTexture(null);
295: textureUnitState[1].setTexture(lightTex);
296: break;
297: case 3: /* sky */
298: textureUnitState[0].setTexture(null);
299: textureUnitState[1].setTexture(skyTex);
300: break;
301: case 4: /* stone + sky */
302: textureUnitState[0].setTexture(stoneTex);
303: textureUnitState[1].setTexture(skyTex);
304: break;
305: default: /* both */
306: break;
307: }
308: }
309:
310: public static void main(String argv[]) {
311: java.net.URL stoneURL = null;
312: java.net.URL skyURL = null;
313: // the path to the image for an application
314:
315: stoneURL = Resources.getResource("resources/images/stone.jpg");
316: if (stoneURL == null) {
317: System.err.println("resources/images/stone.jpg not found");
318: System.exit(1);
319: }
320:
321: skyURL = Resources.getResource("resources/images/bg.jpg");
322: if (skyURL == null) {
323: System.err.println("resources/images/bg.jpg not found");
324: System.exit(1);
325: }
326:
327: new MainFrame(new MultiTextureTest(stoneURL, skyURL), 750, 750);
328: }
329: }
|