001: /*
002: * $RCSfile: AppearanceTest.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/02/09 17:21:31 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.appearance;
046:
047: import com.sun.j3d.utils.image.TextureLoader;
048: import com.sun.j3d.utils.universe.*;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051: import java.awt.GraphicsConfiguration;
052: import org.jdesktop.j3d.examples.Resources;
053:
054: public class AppearanceTest extends javax.swing.JFrame {
055:
056: private java.net.URL texImage = null;
057: private java.net.URL bgImage = null;
058: private SimpleUniverse univ = null;
059: private BranchGroup scene = null;
060:
061: private BranchGroup createSceneGraph() {
062: // Create the root of the branch graph
063: BranchGroup objRoot = new BranchGroup();
064:
065: // Create a bounds for the background and lights
066: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
067: 0.0, 0.0), 100.0);
068:
069: // Set up the background
070: TextureLoader bgTexture = new TextureLoader(bgImage, this );
071: Background bg = new Background(bgTexture.getImage());
072: bg.setApplicationBounds(bounds);
073: objRoot.addChild(bg);
074:
075: // Set up the global lights
076: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
077: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
078: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
079:
080: AmbientLight aLgt = new AmbientLight(alColor);
081: aLgt.setInfluencingBounds(bounds);
082: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
083: lgt1.setInfluencingBounds(bounds);
084: objRoot.addChild(aLgt);
085: objRoot.addChild(lgt1);
086:
087: // Create a bunch of objects with a behavior and add them
088: // into the scene graph.
089:
090: int row, col;
091: Appearance[][] app = new Appearance[3][3];
092:
093: for (row = 0; row < 3; row++)
094: for (col = 0; col < 3; col++)
095: app[row][col] = createAppearance(row * 3 + col);
096:
097: for (int i = 0; i < 3; i++) {
098: double ypos = (double) (i - 1) * 0.6;
099: for (int j = 0; j < 3; j++) {
100: double xpos = (double) (j - 1) * 0.6;
101: objRoot.addChild(createObject(app[i][j], 0.12, xpos,
102: ypos));
103: }
104: }
105:
106: // Let Java 3D perform optimizations on this scene graph.
107: objRoot.compile();
108:
109: return objRoot;
110: }
111:
112: private Appearance createAppearance(int idx) {
113: Appearance app = new Appearance();
114:
115: // Globally used colors
116: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
117: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
118:
119: switch (idx) {
120: // Unlit solid
121: case 0: {
122: // Set up the coloring properties
123: Color3f objColor = new Color3f(1.0f, 0.2f, 0.4f);
124: ColoringAttributes ca = new ColoringAttributes();
125: ca.setColor(objColor);
126: app.setColoringAttributes(ca);
127: break;
128: }
129:
130: // Unlit wire frame
131: case 1: {
132: // Set up the coloring properties
133: Color3f objColor = new Color3f(0.5f, 0.0f, 0.2f);
134: ColoringAttributes ca = new ColoringAttributes();
135: ca.setColor(objColor);
136: app.setColoringAttributes(ca);
137:
138: // Set up the polygon attributes
139: PolygonAttributes pa = new PolygonAttributes();
140: pa.setPolygonMode(pa.POLYGON_LINE);
141: pa.setCullFace(pa.CULL_NONE);
142: app.setPolygonAttributes(pa);
143: break;
144: }
145:
146: // Unlit points
147: case 2: {
148: // Set up the coloring properties
149: Color3f objColor = new Color3f(0.2f, 0.2f, 1.0f);
150: ColoringAttributes ca = new ColoringAttributes();
151: ca.setColor(objColor);
152: app.setColoringAttributes(ca);
153:
154: // Set up the polygon attributes
155: PolygonAttributes pa = new PolygonAttributes();
156: pa.setPolygonMode(pa.POLYGON_POINT);
157: pa.setCullFace(pa.CULL_NONE);
158: app.setPolygonAttributes(pa);
159:
160: // Set up point attributes
161: PointAttributes pta = new PointAttributes();
162: pta.setPointSize(5.0f);
163: app.setPointAttributes(pta);
164: break;
165: }
166:
167: // Lit solid
168: case 3: {
169: // Set up the material properties
170: Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
171: app.setMaterial(new Material(objColor, black, objColor,
172: white, 80.0f));
173: break;
174: }
175:
176: // Texture mapped, lit solid
177: case 4: {
178: // Set up the texture map
179: TextureLoader tex = new TextureLoader(texImage, this );
180: app.setTexture(tex.getTexture());
181:
182: TextureAttributes texAttr = new TextureAttributes();
183: texAttr.setTextureMode(TextureAttributes.MODULATE);
184: app.setTextureAttributes(texAttr);
185:
186: // Set up the material properties
187: app.setMaterial(new Material(white, black, white, black,
188: 1.0f));
189: break;
190: }
191:
192: // Transparent, lit solid
193: case 5: {
194: // Set up the transparency properties
195: TransparencyAttributes ta = new TransparencyAttributes();
196: ta.setTransparencyMode(ta.BLENDED);
197: ta.setTransparency(0.6f);
198: app.setTransparencyAttributes(ta);
199:
200: // Set up the polygon attributes
201: PolygonAttributes pa = new PolygonAttributes();
202: pa.setCullFace(pa.CULL_NONE);
203: app.setPolygonAttributes(pa);
204:
205: // Set up the material properties
206: Color3f objColor = new Color3f(0.7f, 0.8f, 1.0f);
207: app.setMaterial(new Material(objColor, black, objColor,
208: black, 1.0f));
209: break;
210: }
211:
212: // Lit solid, no specular
213: case 6: {
214: // Set up the material properties
215: Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
216: app.setMaterial(new Material(objColor, black, objColor,
217: black, 80.0f));
218: break;
219: }
220:
221: // Lit solid, specular only
222: case 7: {
223: // Set up the material properties
224: Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
225: app.setMaterial(new Material(black, black, black, white,
226: 80.0f));
227: break;
228: }
229:
230: // Another lit solid with a different color
231: case 8: {
232: // Set up the material properties
233: Color3f objColor = new Color3f(0.8f, 0.8f, 0.0f);
234: app.setMaterial(new Material(objColor, black, objColor,
235: white, 80.0f));
236: break;
237: }
238:
239: default: {
240: ColoringAttributes ca = new ColoringAttributes();
241: ca.setColor(new Color3f(0.0f, 1.0f, 0.0f));
242: app.setColoringAttributes(ca);
243: }
244: }
245:
246: return app;
247: }
248:
249: private Group createObject(Appearance app, double scale,
250: double xpos, double ypos) {
251:
252: // Create a transform group node to scale and position the object.
253: Transform3D t = new Transform3D();
254: t.set(scale, new Vector3d(xpos, ypos, 0.0));
255: TransformGroup objTrans = new TransformGroup(t);
256:
257: // Create a second transform group node and initialize it to the
258: // identity. Enable the TRANSFORM_WRITE capability so that
259: // our behavior code can modify it at runtime.
260: TransformGroup spinTg = new TransformGroup();
261: spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
262:
263: // Create a simple shape leaf node and set the appearance
264: Shape3D shape = new Tetrahedron();
265: shape.setAppearance(app);
266:
267: // add it to the scene graph.
268: spinTg.addChild(shape);
269:
270: // Create a new Behavior object that will perform the desired
271: // operation on the specified transform object and add it into
272: // the scene graph.
273: Transform3D yAxis = new Transform3D();
274: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
275: 0, 5000, 0, 0, 0, 0, 0);
276:
277: RotationInterpolator rotator = new RotationInterpolator(
278: rotationAlpha, spinTg, yAxis, 0.0f,
279: (float) Math.PI * 2.0f);
280:
281: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
282: 0.0, 0.0), 100.0);
283:
284: rotator.setSchedulingBounds(bounds);
285:
286: // Add the behavior and the transform group to the object
287: objTrans.addChild(rotator);
288: objTrans.addChild(spinTg);
289:
290: return objTrans;
291: }
292:
293: private Canvas3D createUniverse() {
294: // Get the preferred graphics configuration for the default screen
295: GraphicsConfiguration config = SimpleUniverse
296: .getPreferredConfiguration();
297:
298: // Create a Canvas3D using the preferred configuration
299: Canvas3D c = new Canvas3D(config);
300:
301: // Create simple universe with view branch
302: univ = new SimpleUniverse(c);
303:
304: // This will move the ViewPlatform back a bit so the
305: // objects in the scene can be viewed.
306: univ.getViewingPlatform().setNominalViewingTransform();
307:
308: // Ensure at least 5 msec per frame (i.e., < 200Hz)
309: univ.getViewer().getView().setMinimumFrameCycleTime(5);
310:
311: return c;
312: }
313:
314: /**
315: * Creates new form AppearanceTest
316: */
317: public AppearanceTest() {
318:
319: if (bgImage == null) {
320: // the path to the image for an applet
321: bgImage = Resources.getResource("resources/images/bg.jpg");
322: if (bgImage == null) {
323: System.err.println("resources/images/bg.jpg not found");
324: System.exit(1);
325: }
326: }
327:
328: if (texImage == null) {
329: // the path to the image for an applet
330: texImage = Resources
331: .getResource("resources/images/stone.jpg");
332: if (texImage == null) {
333: System.err
334: .println("resources/images/stone.jpg not found");
335: System.exit(1);
336: }
337: }
338:
339: // Initialize the GUI components
340: initComponents();
341:
342: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
343: Canvas3D c = createUniverse();
344: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
345:
346: // Create the content branch and add it to the universe
347: scene = createSceneGraph();
348: univ.addBranchGraph(scene);
349: }
350:
351: // ----------------------------------------------------------------
352:
353: /** This method is called from within the constructor to
354: * initialize the form.
355: * WARNING: Do NOT modify this code. The content of this method is
356: * always regenerated by the Form Editor.
357: */
358: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
359: private void initComponents() {
360: drawingPanel = new javax.swing.JPanel();
361:
362: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
363: setTitle("AppearanceTest");
364: drawingPanel.setLayout(new java.awt.BorderLayout());
365:
366: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
367: getContentPane()
368: .add(drawingPanel, java.awt.BorderLayout.CENTER);
369:
370: pack();
371: }// </editor-fold>//GEN-END:initComponents
372:
373: /**
374: * @param args the command line arguments
375: */
376: public static void main(String args[]) {
377: java.awt.EventQueue.invokeLater(new Runnable() {
378: public void run() {
379: new AppearanceTest().setVisible(true);
380: }
381: });
382: }
383:
384: // Variables declaration - do not modify//GEN-BEGIN:variables
385: private javax.swing.JPanel drawingPanel;
386: // End of variables declaration//GEN-END:variables
387:
388: }
|