001: /*
002: * $RCSfile: AppearanceMixed.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/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 AppearanceMixed 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: static class MyCanvas3D extends Canvas3D {
062: private GraphicsContext3D gc;
063:
064: private static final int vertIndices[] = { 0, 1, 2, 0, 2, 3 };
065: private static final int normalIndices[] = { 0, 0, 0, 1, 1, 1 };
066: private IndexedTriangleArray tri = new IndexedTriangleArray(4,
067: IndexedTriangleArray.COORDINATES
068: | IndexedTriangleArray.NORMALS, 6);
069:
070: private Point3f vert[] = { new Point3f(-0.12f, -0.12f, 0.0f),
071: new Point3f(0.12f, -0.12f, 0.0f),
072: new Point3f(0.12f, 0.12f, 0.0f),
073: new Point3f(-0.12f, 0.12f, 0.0f), };
074:
075: private Point3f min[] = { new Point3f(-0.24f, -0.24f, -0.20f),
076: new Point3f(0.04f, -0.28f, -0.24f),
077: new Point3f(0.00f, 0.00f, -0.24f),
078: new Point3f(-0.32f, 0.08f, -0.20f), };
079:
080: private Point3f max[] = { new Point3f(-0.04f, -0.04f, 0.12f),
081: new Point3f(0.32f, -0.04f, 0.16f),
082: new Point3f(0.36f, 0.28f, 0.20f),
083: new Point3f(-0.04f, 0.24f, 0.16f), };
084:
085: private Point3f delta[] = {
086: new Point3f(-0.0021f, -0.0017f, 0.0014f),
087: new Point3f(0.0025f, -0.0013f, -0.0018f),
088: new Point3f(0.0021f, 0.0017f, 0.0018f),
089: new Point3f(-0.0025f, 0.0013f, -0.0014f), };
090:
091: private Vector3f normals[];
092: private Vector3f v01 = new Vector3f();
093: private Vector3f v02 = new Vector3f();
094: private Vector3f v03 = new Vector3f();
095:
096: public void renderField(int fieldDesc) {
097: computeVert();
098: computeNormals();
099: gc.draw(tri);
100: }
101:
102: private void computeVert() {
103: for (int i = 0; i < 4; i++) {
104: vert[i].add(delta[i]);
105: if (vert[i].x > max[i].x) {
106: vert[i].x = max[i].x;
107: delta[i].x *= -1.0f;
108: }
109: if (vert[i].x < min[i].x) {
110: vert[i].x = min[i].x;
111: delta[i].x *= -1.0f;
112: }
113: if (vert[i].y > max[i].y) {
114: vert[i].y = max[i].y;
115: delta[i].y *= -1.0f;
116: }
117: if (vert[i].y < min[i].y) {
118: vert[i].y = min[i].y;
119: delta[i].y *= -1.0f;
120: }
121: if (vert[i].z > max[i].z) {
122: vert[i].z = max[i].z;
123: delta[i].z *= -1.0f;
124: }
125: if (vert[i].z < min[i].z) {
126: vert[i].z = min[i].z;
127: delta[i].z *= -1.0f;
128: }
129: }
130: tri.setCoordinates(0, vert);
131: }
132:
133: private void computeNormals() {
134: v01.sub(vert[1], vert[0]);
135: v02.sub(vert[2], vert[0]);
136: v03.sub(vert[3], vert[0]);
137: normals[0].cross(v01, v02);
138: normals[0].normalize();
139: normals[1].cross(v02, v03);
140: normals[1].normalize();
141: tri.setNormals(0, normals);
142: }
143:
144: public MyCanvas3D(GraphicsConfiguration gcfg) {
145: super (gcfg);
146:
147: // Allocate memory for normals
148: normals = new Vector3f[2];
149: normals[0] = new Vector3f();
150: normals[1] = new Vector3f();
151:
152: // Set up the indices
153: tri.setCoordinateIndices(0, vertIndices);
154: tri.setNormalIndices(0, normalIndices);
155:
156: // Set up the graphics context
157: gc = getGraphicsContext3D();
158:
159: // Create the appearance for the triangle fan
160: Appearance app = new Appearance();
161: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
162: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
163: Color3f objColor = new Color3f(0.0f, 0.0f, 0.8f);
164: app.setMaterial(new Material(objColor, black, objColor,
165: white, 80.0f));
166: gc.setAppearance(app);
167:
168: // Set up the global lights
169: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
170: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
171: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
172: gc.addLight(new AmbientLight(alColor));
173: gc.addLight(new DirectionalLight(lColor1, lDir1));
174: }
175: }
176:
177: private BranchGroup createSceneGraph() {
178: // Create the root of the branch graph
179: BranchGroup objRoot = new BranchGroup();
180:
181: // Create a bounds for the background and lights
182: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
183: 0.0, 0.0), 100.0);
184:
185: // Set up the background
186: TextureLoader bgTexture = new TextureLoader(bgImage, this );
187: Background bg = new Background(bgTexture.getImage());
188: bg.setApplicationBounds(bounds);
189: objRoot.addChild(bg);
190:
191: // Set up the global lights
192: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
193: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
194: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
195:
196: AmbientLight aLgt = new AmbientLight(alColor);
197: aLgt.setInfluencingBounds(bounds);
198: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
199: lgt1.setInfluencingBounds(bounds);
200: objRoot.addChild(aLgt);
201: objRoot.addChild(lgt1);
202:
203: // Create a bunch of objects with a behavior and add them
204: // into the scene graph.
205:
206: int row, col;
207: Appearance[][] app = new Appearance[3][3];
208:
209: for (row = 0; row < 3; row++)
210: for (col = 0; col < 3; col++)
211: app[row][col] = createAppearance(row * 3 + col);
212:
213: for (int i = 0; i < 3; i++) {
214: double ypos = (double) (i - 1) * 0.6;
215: for (int j = 0; j < 3; j++) {
216: double xpos = (double) (j - 1) * 0.6;
217: objRoot.addChild(createObject(app[i][j], 0.12, xpos,
218: ypos));
219: }
220: }
221:
222: // Let Java 3D perform optimizations on this scene graph.
223: objRoot.compile();
224:
225: return objRoot;
226: }
227:
228: private Appearance createAppearance(int idx) {
229: Appearance app = new Appearance();
230:
231: // Globally used colors
232: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
233: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
234:
235: switch (idx) {
236: // Unlit solid
237: case 0: {
238: // Set up the coloring properties
239: Color3f objColor = new Color3f(1.0f, 0.2f, 0.4f);
240: ColoringAttributes ca = new ColoringAttributes();
241: ca.setColor(objColor);
242: app.setColoringAttributes(ca);
243: break;
244: }
245:
246: // Unlit wire frame
247: case 1: {
248: // Set up the coloring properties
249: Color3f objColor = new Color3f(0.5f, 0.0f, 0.2f);
250: ColoringAttributes ca = new ColoringAttributes();
251: ca.setColor(objColor);
252: app.setColoringAttributes(ca);
253:
254: // Set up the polygon attributes
255: PolygonAttributes pa = new PolygonAttributes();
256: pa.setPolygonMode(pa.POLYGON_LINE);
257: pa.setCullFace(pa.CULL_NONE);
258: app.setPolygonAttributes(pa);
259: break;
260: }
261:
262: // Unlit points
263: case 2: {
264: // Set up the coloring properties
265: Color3f objColor = new Color3f(0.2f, 0.2f, 1.0f);
266: ColoringAttributes ca = new ColoringAttributes();
267: ca.setColor(objColor);
268: app.setColoringAttributes(ca);
269:
270: // Set up the polygon attributes
271: PolygonAttributes pa = new PolygonAttributes();
272: pa.setPolygonMode(pa.POLYGON_POINT);
273: pa.setCullFace(pa.CULL_NONE);
274: app.setPolygonAttributes(pa);
275:
276: // Set up point attributes
277: PointAttributes pta = new PointAttributes();
278: pta.setPointSize(5.0f);
279: app.setPointAttributes(pta);
280: break;
281: }
282:
283: // Lit solid
284: case 3: {
285: // Set up the material properties
286: Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
287: app.setMaterial(new Material(objColor, black, objColor,
288: white, 80.0f));
289: break;
290: }
291:
292: // Texture mapped, lit solid
293: case 4: {
294: // Set up the texture map
295: TextureLoader tex = new TextureLoader(texImage, this );
296: app.setTexture(tex.getTexture());
297:
298: TextureAttributes texAttr = new TextureAttributes();
299: texAttr.setTextureMode(TextureAttributes.MODULATE);
300: app.setTextureAttributes(texAttr);
301:
302: // Set up the material properties
303: app.setMaterial(new Material(white, black, white, black,
304: 1.0f));
305: break;
306: }
307:
308: // Transparent, lit solid
309: case 5: {
310: // Set up the transparency properties
311: TransparencyAttributes ta = new TransparencyAttributes();
312: ta.setTransparencyMode(ta.BLENDED);
313: ta.setTransparency(0.6f);
314: app.setTransparencyAttributes(ta);
315:
316: // Set up the polygon attributes
317: PolygonAttributes pa = new PolygonAttributes();
318: pa.setCullFace(pa.CULL_NONE);
319: app.setPolygonAttributes(pa);
320:
321: // Set up the material properties
322: Color3f objColor = new Color3f(0.7f, 0.8f, 1.0f);
323: app.setMaterial(new Material(objColor, black, objColor,
324: black, 1.0f));
325: break;
326: }
327:
328: // Lit solid, no specular
329: case 6: {
330: // Set up the material properties
331: Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
332: app.setMaterial(new Material(objColor, black, objColor,
333: black, 80.0f));
334: break;
335: }
336:
337: // Lit solid, specular only
338: case 7: {
339: // Set up the material properties
340: Color3f objColor = new Color3f(0.8f, 0.0f, 0.0f);
341: app.setMaterial(new Material(black, black, black, white,
342: 80.0f));
343: break;
344: }
345:
346: // Another lit solid with a different color
347: case 8: {
348: // Set up the material properties
349: Color3f objColor = new Color3f(0.8f, 0.8f, 0.0f);
350: app.setMaterial(new Material(objColor, black, objColor,
351: white, 80.0f));
352: break;
353: }
354:
355: default: {
356: ColoringAttributes ca = new ColoringAttributes();
357: ca.setColor(new Color3f(0.0f, 1.0f, 0.0f));
358: app.setColoringAttributes(ca);
359: }
360: }
361:
362: return app;
363: }
364:
365: private Group createObject(Appearance app, double scale,
366: double xpos, double ypos) {
367:
368: // Create a transform group node to scale and position the object.
369: Transform3D t = new Transform3D();
370: t.set(scale, new Vector3d(xpos, ypos, 0.0));
371: TransformGroup objTrans = new TransformGroup(t);
372:
373: // Create a second transform group node and initialize it to the
374: // identity. Enable the TRANSFORM_WRITE capability so that
375: // our behavior code can modify it at runtime.
376: TransformGroup spinTg = new TransformGroup();
377: spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
378:
379: // Create a simple shape leaf node and set the appearance
380: Shape3D shape = new Tetrahedron();
381: shape.setAppearance(app);
382:
383: // add it to the scene graph.
384: spinTg.addChild(shape);
385:
386: // Create a new Behavior object that will perform the desired
387: // operation on the specified transform object and add it into
388: // the scene graph.
389: Transform3D yAxis = new Transform3D();
390: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
391: 0, 5000, 0, 0, 0, 0, 0);
392:
393: RotationInterpolator rotator = new RotationInterpolator(
394: rotationAlpha, spinTg, yAxis, 0.0f,
395: (float) Math.PI * 2.0f);
396:
397: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
398: 0.0, 0.0), 100.0);
399:
400: rotator.setSchedulingBounds(bounds);
401:
402: // Add the behavior and the transform group to the object
403: objTrans.addChild(rotator);
404: objTrans.addChild(spinTg);
405:
406: return objTrans;
407: }
408:
409: private Canvas3D createUniverse() {
410: // Get the preferred graphics configuration for the default screen
411: GraphicsConfiguration config = SimpleUniverse
412: .getPreferredConfiguration();
413:
414: // Create a MyCanvas3D using the preferred configuration
415: MyCanvas3D c = new MyCanvas3D(config);
416:
417: // Create simple universe with view branch
418: univ = new SimpleUniverse(c);
419:
420: // This will move the ViewPlatform back a bit so the
421: // objects in the scene can be viewed.
422: univ.getViewingPlatform().setNominalViewingTransform();
423:
424: // Ensure at least 5 msec per frame (i.e., < 200Hz)
425: univ.getViewer().getView().setMinimumFrameCycleTime(5);
426:
427: return c;
428: }
429:
430: /**
431: * Creates new form AppearanceMixed
432: */
433: public AppearanceMixed() {
434:
435: if (bgImage == null) {
436: // the path to the image for an applet
437: bgImage = Resources.getResource("resources/images/bg.jpg");
438: if (bgImage == null) {
439: System.err.println("resources/images/bg.jpg not found");
440: System.exit(1);
441: }
442: }
443:
444: if (texImage == null) {
445: // the path to the image for an applet
446: texImage = Resources
447: .getResource("resources/images/stone.jpg");
448: if (texImage == null) {
449: System.err
450: .println("resources/images/stone.jpg not found");
451: System.exit(1);
452: }
453: }
454:
455: // Initialize the GUI components
456: initComponents();
457:
458: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
459: Canvas3D c = createUniverse();
460: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
461:
462: // Create the content branch and add it to the universe
463: scene = createSceneGraph();
464: univ.addBranchGraph(scene);
465: }
466:
467: // ----------------------------------------------------------------
468:
469: /** This method is called from within the constructor to
470: * initialize the form.
471: * WARNING: Do NOT modify this code. The content of this method is
472: * always regenerated by the Form Editor.
473: */
474: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
475: private void initComponents() {
476: drawingPanel = new javax.swing.JPanel();
477:
478: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
479: setTitle("AppearanceMixed");
480: drawingPanel.setLayout(new java.awt.BorderLayout());
481:
482: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
483: getContentPane()
484: .add(drawingPanel, java.awt.BorderLayout.CENTER);
485:
486: pack();
487: }// </editor-fold>//GEN-END:initComponents
488:
489: /**
490: * @param args the command line arguments
491: */
492: public static void main(String args[]) {
493: java.awt.EventQueue.invokeLater(new Runnable() {
494: public void run() {
495: new AppearanceMixed().setVisible(true);
496: }
497: });
498: }
499:
500: // Variables declaration - do not modify//GEN-BEGIN:variables
501: private javax.swing.JPanel drawingPanel;
502: // End of variables declaration//GEN-END:variables
503:
504: }
|