001: /*
002: * $Header: /cvs/j3dfly/J3dEditor/src/org/jdesktop/j3dedit/j3ddelivery/DeliveryUniverse.java,v 1.1 2005/04/20 22:20:33 paulby Exp $
003: *
004: * Sun Public License Notice
005: *
006: * The contents of this file are subject to the Sun Public License Version
007: * 1.0 (the "License"). You may not use this file except in compliance with
008: * the License. A copy of the License is available at http://www.sun.com/
009: *
010: * The Original Code is the Java 3D(tm) Scene Graph Editor.
011: * The Initial Developer of the Original Code is Paul Byrne.
012: * Portions created by Paul Byrne are Copyright (C) 2002.
013: * All Rights Reserved.
014: *
015: * Contributor(s): Paul Byrne.
016: *
017: **/
018: package org.jdesktop.j3dedit.j3ddelivery;
019:
020: import java.awt.GraphicsConfiguration;
021: import javax.media.j3d.*;
022: import javax.vecmath.*;
023: import com.sun.j3d.utils.universe.SimpleUniverse;
024:
025: public class DeliveryUniverse extends javax.swing.JFrame {
026:
027: private Canvas3D canvas3D;
028: private BranchGroup userRoot; // The root of the users SceneGraph
029: private BranchGroup branchGraph;
030: private SimpleUniverse universe;
031: private boolean isLive;
032:
033: /** Creates new form UserUniverse
034: * SceneGraph is not live after creation, setSceneGraphLive(true) must
035: * be called to make the world visible
036: */
037: public DeliveryUniverse() {
038: initComponents();
039:
040: userRoot = new javax.media.j3d.BranchGroup();
041: userRoot.setCapability(BranchGroup.ALLOW_DETACH);
042: userRoot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
043: userRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
044:
045: branchGraph = new BranchGroup();
046: branchGraph.setCapability(BranchGroup.ALLOW_DETACH);
047: branchGraph.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
048: branchGraph.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
049: branchGraph.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
050:
051: GraphicsConfiguration config = SimpleUniverse
052: .getPreferredConfiguration();
053: canvas3D = new Canvas3D(config);
054: universe = new SimpleUniverse(canvas3D);
055: universe.getViewingPlatform().setNominalViewingTransform();
056: universe.getViewer().getView().setBackClipDistance(4000000);
057:
058: isLive = false;
059:
060: universe.addBranchGraph(branchGraph);
061:
062: this .getContentPane().add("Center", canvas3D);
063:
064: pack();
065: setSize(500, 500);
066: }
067:
068: /**
069: * Set the BranchGraph of the locale to be the
070: * tree root
071: *
072: * The previous graph will be discarded
073: *
074: *
075: */
076: public void setSceneGraph(BranchGroup root) {
077: userRoot.detach();
078: userRoot = root;
079: userRoot.setCapability(BranchGroup.ALLOW_DETACH);
080: userRoot.setCapability(BranchGroup.ALLOW_CHILDREN_READ);
081: userRoot.setCapability(BranchGroup.ALLOW_CHILDREN_WRITE);
082: userRoot.setCapability(BranchGroup.ALLOW_BOUNDS_READ);
083: }
084:
085: /**
086: * Makes the users SceneGraph Live or not
087: */
088: public void setLive(boolean live) {
089: if (live && userRoot.getParent() == null) {
090: branchGraph.addChild(userRoot);
091: messageLabel.setText("SceneGraph Live ");
092: } else if (!live) {
093: userRoot.detach();
094: messageLabel.setText("SceneGraph Detached");
095: }
096:
097: isLive = live;
098: }
099:
100: /**
101: * Return true is the scenegraph is live
102: */
103: public boolean isLive() {
104: return isLive;
105: }
106:
107: public void showWholeWorld() {
108: if (userRoot == null)
109: return;
110:
111: BoundingSphere worldBounds;
112: worldBounds = (BoundingSphere) userRoot.getBounds();
113:
114: // Increase bounds by 25% so everything fits in the view.
115: worldBounds.setRadius(worldBounds.getRadius() * 1.25);
116:
117: //System.out.println( worldBounds );
118: setViewpoint(universe.getViewingPlatform()
119: .getViewPlatformTransform(), worldBounds, universe
120: .getViewer().getView());
121: }
122:
123: /**
124: * Set the transform so that everything inside the BoundingSphere is
125: * visible
126: */
127: private void setViewpoint(TransformGroup tg,
128: BoundingSphere sceneBounds, View view) {
129: Transform3D viewTrans = new Transform3D();
130: Transform3D eyeTrans = new Transform3D();
131:
132: // point the view at the center of the object
133: Point3d center = new Point3d();
134: sceneBounds.getCenter(center);
135: double radius = sceneBounds.getRadius();
136: Vector3d temp = new Vector3d(center);
137: viewTrans.set(temp);
138:
139: // pull the eye back far enough to see the whole object
140: double eyeDist = 1.4 * radius
141: / Math.tan(view.getFieldOfView() / 2.0);
142: temp.x = 0.0;
143: temp.y = 0.0;
144: temp.z = eyeDist;
145: eyeTrans.set(temp);
146: viewTrans.mul(eyeTrans);
147:
148: // set the view transform
149: tg.setTransform(viewTrans);
150: }
151:
152: /** This method is called from within the constructor to
153: * initialize the form.
154: * WARNING: Do NOT modify this code. The content of this method is
155: * always regenerated by the FormEditor.
156: */
157: private void initComponents() {//GEN-BEGIN:initComponents
158: jMenuBar1 = new javax.swing.JMenuBar();
159: renderMenu = new javax.swing.JMenu();
160: menuSuspend = new javax.swing.JMenuItem();
161: menuContinue = new javax.swing.JMenuItem();
162: viewMenu = new javax.swing.JMenu();
163: menuShowAll = new javax.swing.JMenuItem();
164: messageLabel = new javax.swing.JLabel();
165: renderMenu.getPopupMenu().setLightWeightPopupEnabled(false);
166: viewMenu.getPopupMenu().setLightWeightPopupEnabled(false);
167:
168: renderMenu.setLabel("Render");
169: renderMenu.setText("Menu");
170:
171: menuSuspend.setText("Suspend");
172: menuSuspend
173: .addActionListener(new java.awt.event.ActionListener() {
174: public void actionPerformed(
175: java.awt.event.ActionEvent evt) {
176: suspendActionPerformed(evt);
177: }
178: });
179:
180: renderMenu.add(menuSuspend);
181: menuContinue.setLabel("Continue");
182: menuContinue.setText("Item");
183: menuContinue
184: .addActionListener(new java.awt.event.ActionListener() {
185: public void actionPerformed(
186: java.awt.event.ActionEvent evt) {
187: continueActionPerformed(evt);
188: }
189: });
190:
191: renderMenu.add(menuContinue);
192: jMenuBar1.add(renderMenu);
193: viewMenu.setText("View");
194:
195: menuShowAll.setText("Show All");
196: menuShowAll
197: .addActionListener(new java.awt.event.ActionListener() {
198: public void actionPerformed(
199: java.awt.event.ActionEvent evt) {
200: showAllActionPerformed(evt);
201: }
202: });
203:
204: viewMenu.add(menuShowAll);
205: jMenuBar1.add(viewMenu);
206: setName("User Universe");
207: setTitle("User Universe");
208: addWindowListener(new java.awt.event.WindowAdapter() {
209: public void windowClosing(java.awt.event.WindowEvent evt) {
210: exitForm(evt);
211: }
212: });
213:
214: messageLabel.setText(" ");
215:
216: getContentPane().add(messageLabel, java.awt.BorderLayout.SOUTH);
217:
218: setJMenuBar(jMenuBar1);
219:
220: }//GEN-END:initComponents
221:
222: private void continueActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_continueActionPerformed
223: // Add your handling code here:
224: setLive(true);
225: }//GEN-LAST:event_continueActionPerformed
226:
227: private void suspendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_suspendActionPerformed
228: // Add your handling code here:
229: setLive(false);
230: }//GEN-LAST:event_suspendActionPerformed
231:
232: /**
233: * Transform the view so that all geometry is visible
234: */
235: private void showAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllActionPerformed
236: // Add your handling code here:
237: showWholeWorld();
238: }//GEN-LAST:event_showAllActionPerformed
239:
240: /** Exit the Application */
241: private void exitForm(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_exitForm
242: System.exit(0);
243: }//GEN-LAST:event_exitForm
244:
245: // Variables declaration - do not modify//GEN-BEGIN:variables
246: private javax.swing.JMenuBar jMenuBar1;
247: private javax.swing.JMenu renderMenu;
248: private javax.swing.JMenuItem menuSuspend;
249: private javax.swing.JMenuItem menuContinue;
250: private javax.swing.JMenu viewMenu;
251: private javax.swing.JMenuItem menuShowAll;
252: private javax.swing.JLabel messageLabel;
253: // End of variables declaration//GEN-END:variables
254:
255: }
|