001: /*
002: * $RCSfile: Pyramid2Cube.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.3 $
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:
052: public class Pyramid2Cube extends javax.swing.JFrame {
053:
054: private SimpleUniverse univ = null;
055: private BranchGroup scene = null;
056:
057: private BranchGroup createSceneGraph() {
058: // Create the root of the branch graph
059: BranchGroup objRoot = new BranchGroup();
060:
061: // Create a Transformgroup to scale all objects so they
062: // appear in the scene.
063: TransformGroup objScale = new TransformGroup();
064: Transform3D t3d = new Transform3D();
065: t3d.setScale(0.4);
066: objScale.setTransform(t3d);
067: objRoot.addChild(objScale);
068:
069: // Create a bounds for the background and behavior
070: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
071: 0.0, 0.0), 100.0);
072:
073: // Set up the background
074: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
075: Background bg = new Background(bgColor);
076: bg.setApplicationBounds(bounds);
077: objScale.addChild(bg);
078:
079: //
080: // Create the transform group nodes for the 3 original objects
081: // and the morphed object. Add them to the root of the
082: // branch graph.
083: //
084: TransformGroup objTrans[] = new TransformGroup[4];
085:
086: for (int i = 0; i < 4; i++) {
087: objTrans[i] = new TransformGroup();
088: objScale.addChild(objTrans[i]);
089: }
090:
091: Transform3D tr = new Transform3D();
092: Transform3D rotY15 = new Transform3D();
093: rotY15.rotY(15.0 * Math.PI / 180.0);
094:
095: objTrans[0].getTransform(tr);
096: tr.setTranslation(new Vector3d(-3.0, 1.5, -6.5));
097: tr.mul(rotY15);
098: objTrans[0].setTransform(tr);
099:
100: objTrans[1].getTransform(tr);
101: tr.setTranslation(new Vector3d(0.0, 1.5, -6.5));
102: tr.mul(rotY15);
103: objTrans[1].setTransform(tr);
104:
105: objTrans[2].getTransform(tr);
106: tr.setTranslation(new Vector3d(3.0, 1.5, -6.5));
107: tr.mul(rotY15);
108: objTrans[2].setTransform(tr);
109:
110: objTrans[3].getTransform(tr);
111: tr.setTranslation(new Vector3d(0.0, -2.0, -5.0));
112: tr.mul(rotY15);
113: objTrans[3].setTransform(tr);
114:
115: // Now create simple geometries.
116:
117: QuadArray g[] = new QuadArray[3];
118: Shape3D shape[] = new Shape3D[3];
119: for (int i = 0; i < 3; i++) {
120: g[i] = null;
121: shape[i] = null;
122: }
123:
124: g[0] = new ColorPyramidUp();
125: g[1] = new ColorCube();
126: g[2] = new ColorPyramidDown();
127:
128: Appearance a = new Appearance();
129:
130: for (int i = 0; i < 3; i++) {
131: shape[i] = new Shape3D(g[i], a);
132: objTrans[i].addChild(shape[i]);
133: }
134:
135: //
136: // Create a Morph node, and set the appearance and input geometry
137: // arrays. Set the Morph node's capability bits to allow the weights
138: // to be modified at runtime.
139: //
140: Morph morph = new Morph((GeometryArray[]) g, a);
141: morph.setCapability(Morph.ALLOW_WEIGHTS_READ);
142: morph.setCapability(Morph.ALLOW_WEIGHTS_WRITE);
143:
144: objTrans[3].addChild(morph);
145:
146: // Now create the Alpha object that controls the speed of the
147: // morphing operation.
148: Alpha morphAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
149: | Alpha.DECREASING_ENABLE, 0, 0, 4000, 1000, 500, 4000,
150: 1000, 500);
151:
152: // Finally, create the morphing behavior
153: MorphingBehavior mBeh = new MorphingBehavior(morphAlpha, morph);
154: mBeh.setSchedulingBounds(bounds);
155: objScale.addChild(mBeh);
156:
157: return objRoot;
158: }
159:
160: private Canvas3D createUniverse() {
161: // Get the preferred graphics configuration for the default screen
162: GraphicsConfiguration config = SimpleUniverse
163: .getPreferredConfiguration();
164:
165: // Create a Canvas3D using the preferred configuration
166: Canvas3D c = new Canvas3D(config);
167:
168: // Create simple universe with view branch
169: univ = new SimpleUniverse(c);
170:
171: // This will move the ViewPlatform back a bit so the
172: // objects in the scene can be viewed.
173: univ.getViewingPlatform().setNominalViewingTransform();
174:
175: // Ensure at least 5 msec per frame (i.e., < 200Hz)
176: univ.getViewer().getView().setMinimumFrameCycleTime(5);
177:
178: return c;
179: }
180:
181: /**
182: * Creates new form Pyramid2Cube
183: */
184: public Pyramid2Cube() {
185: // Initialize the GUI components
186: initComponents();
187:
188: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
189: Canvas3D c = createUniverse();
190: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
191:
192: // Create the content branch and add it to the universe
193: scene = createSceneGraph();
194: univ.addBranchGraph(scene);
195: }
196:
197: // ----------------------------------------------------------------
198:
199: /** This method is called from within the constructor to
200: * initialize the form.
201: * WARNING: Do NOT modify this code. The content of this method is
202: * always regenerated by the Form Editor.
203: */
204: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
205: private void initComponents() {
206: drawingPanel = new javax.swing.JPanel();
207:
208: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
209: setTitle("Pyramid2Cube");
210: drawingPanel.setLayout(new java.awt.BorderLayout());
211:
212: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
213: getContentPane()
214: .add(drawingPanel, java.awt.BorderLayout.CENTER);
215:
216: pack();
217: }// </editor-fold>//GEN-END:initComponents
218:
219: /**
220: * @param args the command line arguments
221: */
222: public static void main(String args[]) {
223: java.awt.EventQueue.invokeLater(new Runnable() {
224: public void run() {
225: new Pyramid2Cube().setVisible(true);
226: }
227: });
228: }
229:
230: // Variables declaration - do not modify//GEN-BEGIN:variables
231: private javax.swing.JPanel drawingPanel;
232: // End of variables declaration//GEN-END:variables
233:
234: }
|