001: /*
002: * $RCSfile: Morphing.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.4 $
041: * $Date: 2007/02/09 17:21:44 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.morphing;
046:
047: import com.sun.j3d.utils.universe.*;
048: import javax.media.j3d.*;
049: import javax.vecmath.*;
050: import java.awt.GraphicsConfiguration;
051: import java.io.*;
052: import com.sun.j3d.loaders.objectfile.ObjectFile;
053: import com.sun.j3d.loaders.Scene;
054: import com.sun.j3d.loaders.ParsingErrorException;
055: import com.sun.j3d.loaders.IncorrectFormatException;
056: import org.jdesktop.j3d.examples.Resources;
057:
058: public class Morphing extends javax.swing.JFrame {
059:
060: private SimpleUniverse univ = null;
061: private BranchGroup scene = null;
062: private java.net.URL[] objFiles = null;
063:
064: private BranchGroup createSceneGraph() {
065: // Create the root of the branch graph
066: BranchGroup objRoot = new BranchGroup();
067:
068: // Create a Transformgroup to scale all objects so they
069: // appear in the scene.
070: TransformGroup objScale = new TransformGroup();
071: Transform3D t3d = new Transform3D();
072: t3d.setScale(0.4);
073: objScale.setTransform(t3d);
074: objRoot.addChild(objScale);
075:
076: // Create a bounds for the background and lights
077: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
078: 0.0, 0.0), 100.0);
079:
080: // Set up the background
081: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
082: Background bg = new Background(bgColor);
083: bg.setApplicationBounds(bounds);
084: objScale.addChild(bg);
085:
086: // Set up the global lights
087: Color3f lColor1 = new Color3f(0.7f, 0.7f, 0.7f);
088: Vector3f lDir1 = new Vector3f(-1.0f, -1.0f, -1.0f);
089: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
090:
091: AmbientLight aLgt = new AmbientLight(alColor);
092: aLgt.setInfluencingBounds(bounds);
093: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
094: lgt1.setInfluencingBounds(bounds);
095: objScale.addChild(aLgt);
096: objScale.addChild(lgt1);
097:
098: //
099: // Create the transform group nodes for the 3 original objects
100: // and the morphed object. Add them to the root of the
101: // branch graph.
102: //
103: TransformGroup objTrans[] = new TransformGroup[4];
104:
105: for (int i = 0; i < 4; i++) {
106: objTrans[i] = new TransformGroup();
107: objScale.addChild(objTrans[i]);
108: }
109:
110: Transform3D tr = new Transform3D();
111: Transform3D rotX90 = new Transform3D();
112: rotX90.rotX(90.0 * Math.PI / 180.0);
113:
114: objTrans[0].getTransform(tr);
115: tr.setTranslation(new Vector3d(-2.0, 1.5, -2.0));
116: tr.mul(rotX90);
117: objTrans[0].setTransform(tr);
118:
119: objTrans[1].getTransform(tr);
120: tr.setTranslation(new Vector3d(0.0, 1.5, -2.0));
121: tr.mul(rotX90);
122: objTrans[1].setTransform(tr);
123:
124: objTrans[2].getTransform(tr);
125: tr.setTranslation(new Vector3d(2.0, 1.5, -2.0));
126: tr.mul(rotX90);
127: objTrans[2].setTransform(tr);
128:
129: objTrans[3].getTransform(tr);
130: tr.setTranslation(new Vector3d(0.0, -2.0, -2.0));
131: tr.mul(rotX90);
132: objTrans[3].setTransform(tr);
133:
134: // Now load the object files
135: Scene s[] = new Scene[3];
136: GeometryArray g[] = new GeometryArray[3];
137: Shape3D shape[] = new Shape3D[3];
138: ObjectFile loader = new ObjectFile(ObjectFile.RESIZE);
139: for (int i = 0; i < 3; i++) {
140: s[i] = null;
141: g[i] = null;
142: shape[i] = null;
143: }
144:
145: for (int i = 0; i < 3; i++) {
146: try {
147: s[i] = loader.load(objFiles[i]);
148: } catch (FileNotFoundException e) {
149: System.err.println(e);
150: System.exit(1);
151: } catch (ParsingErrorException e) {
152: System.err.println(e);
153: System.exit(1);
154: } catch (IncorrectFormatException e) {
155: System.err.println(e);
156: System.exit(1);
157: }
158:
159: BranchGroup b = s[i].getSceneGroup();
160: shape[i] = (Shape3D) b.getChild(0);
161: g[i] = (GeometryArray) shape[i].getGeometry();
162:
163: shape[i].setGeometry(g[i]);
164: objTrans[i].addChild(b);
165: }
166:
167: //
168: // Create a Morph node, and set the appearance and input geometry
169: // arrays. Set the Morph node's capability bits to allow the weights
170: // to be modified at runtime.
171: //
172: Appearance app = new Appearance();
173: Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
174: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
175: app.setMaterial(new Material(objColor, black, objColor, black,
176: 80.0f));
177: Morph morph = new Morph(g, app);
178: morph.setCapability(Morph.ALLOW_WEIGHTS_READ);
179: morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
180:
181: objTrans[3].addChild(morph);
182:
183: // Now create the Alpha object that controls the speed of the
184: // morphing operation.
185: Alpha morphAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
186: | Alpha.DECREASING_ENABLE, 0, 0, 2000, 1000, 200, 2000,
187: 1000, 200);
188:
189: // Finally, create the morphing behavior
190: MorphingBehavior mBeh = new MorphingBehavior(morphAlpha, morph);
191: mBeh.setSchedulingBounds(bounds);
192: objScale.addChild(mBeh);
193:
194: return objRoot;
195: }
196:
197: private Canvas3D createUniverse() {
198: // Get the preferred graphics configuration for the default screen
199: GraphicsConfiguration config = SimpleUniverse
200: .getPreferredConfiguration();
201:
202: // Create a Canvas3D using the preferred configuration
203: Canvas3D c = new Canvas3D(config);
204:
205: // Create simple universe with view branch
206: univ = new SimpleUniverse(c);
207:
208: // This will move the ViewPlatform back a bit so the
209: // objects in the scene can be viewed.
210: univ.getViewingPlatform().setNominalViewingTransform();
211:
212: // Ensure at least 5 msec per frame (i.e., < 200Hz)
213: univ.getViewer().getView().setMinimumFrameCycleTime(5);
214:
215: return c;
216: }
217:
218: /**
219: * Creates new form LOD
220: */
221: public Morphing(String args[]) {
222:
223: objFiles = new java.net.URL[3];
224: for (int i = 0; i < 3; i++) {
225: objFiles[i] = Resources
226: .getResource("resources/geometry/hand" + (i + 1)
227: + ".obj");
228: if (objFiles[i] == null) {
229: System.err.println("resources/geometry/hand" + (i + 1)
230: + ".obj not found");
231: System.exit(1);
232: }
233: }
234:
235: // Initialize the GUI components
236: initComponents();
237:
238: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
239: Canvas3D c = createUniverse();
240: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
241:
242: // Create the content branch and add it to the universe
243: scene = createSceneGraph();
244: univ.addBranchGraph(scene);
245: }
246:
247: // ----------------------------------------------------------------
248:
249: /** This method is called from within the constructor to
250: * initialize the form.
251: * WARNING: Do NOT modify this code. The content of this method is
252: * always regenerated by the Form Editor.
253: */
254: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
255: private void initComponents() {
256: drawingPanel = new javax.swing.JPanel();
257:
258: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
259: setTitle("Morphing");
260: drawingPanel.setLayout(new java.awt.BorderLayout());
261:
262: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
263: getContentPane()
264: .add(drawingPanel, java.awt.BorderLayout.CENTER);
265:
266: pack();
267: }// </editor-fold>//GEN-END:initComponents
268:
269: /**
270: * @param args the command line arguments
271: */
272: public static void main(final String args[]) {
273: java.awt.EventQueue.invokeLater(new Runnable() {
274: public void run() {
275: Morphing morphing = new Morphing(args);
276: morphing.setVisible(true);
277: }
278: });
279: }
280:
281: // Variables declaration - do not modify//GEN-BEGIN:variables
282: private javax.swing.JPanel drawingPanel;
283: // End of variables declaration//GEN-END:variables
284:
285: }
|