001: /*
002: * $RCSfile: LOD.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:43 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.lod;
046:
047: import com.sun.j3d.utils.geometry.*;
048: import com.sun.j3d.utils.behaviors.vp.*;
049: import com.sun.j3d.utils.universe.*;
050: import javax.media.j3d.*;
051: import javax.vecmath.*;
052: import java.awt.GraphicsConfiguration;
053:
054: /**
055: * Simple Java 3D example program to display a spinning cube.
056: */
057: public class LOD extends javax.swing.JFrame {
058:
059: private SimpleUniverse univ = null;
060: private BranchGroup scene = null;
061:
062: public BranchGroup createSceneGraph() {
063: // Create the root of the branch graph
064: BranchGroup objRoot = new BranchGroup();
065:
066: createLights(objRoot);
067:
068: // Create the transform group node and initialize it to the
069: // identity. Enable the TRANSFORM_WRITE capability so that
070: // our behavior code can modify it at runtime. Add it to the
071: // root of the subgraph.
072: TransformGroup objTrans = new TransformGroup();
073: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
074: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
075: objRoot.addChild(objTrans);
076:
077: // Create a switch to hold the different levels of detail
078: Switch sw = new Switch(0);
079: sw.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_READ);
080: sw.setCapability(javax.media.j3d.Switch.ALLOW_SWITCH_WRITE);
081:
082: // Create several levels for the switch, with less detailed
083: // spheres for the ones which will be used when the sphere is
084: // further away
085: sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 40));
086: sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 20));
087: sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 10));
088: sw.addChild(new Sphere(0.4f, Sphere.GENERATE_NORMALS, 3));
089:
090: // Add the switch to the main group
091: objTrans.addChild(sw);
092:
093: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
094: 0.0, 0.0), 100.0);
095:
096: // set up the DistanceLOD behavior
097: float[] distances = new float[3];
098: distances[0] = 5.0f;
099: distances[1] = 10.0f;
100: distances[2] = 25.0f;
101: DistanceLOD lod = new DistanceLOD(distances);
102: lod.addSwitch(sw);
103: lod.setSchedulingBounds(bounds);
104: objTrans.addChild(lod);
105:
106: // Have Java 3D perform optimizations on this scene graph.
107: objRoot.compile();
108:
109: return objRoot;
110: }
111:
112: private void createLights(BranchGroup graphRoot) {
113:
114: // Create a bounds for the light source influence
115: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
116: 0.0, 0.0), 100.0);
117:
118: // Set up the global, ambient light
119: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
120: AmbientLight aLgt = new AmbientLight(alColor);
121: aLgt.setInfluencingBounds(bounds);
122: graphRoot.addChild(aLgt);
123:
124: // Set up the directional (infinite) light source
125: Color3f lColor1 = new Color3f(0.9f, 0.9f, 0.9f);
126: Vector3f lDir1 = new Vector3f(1.0f, 1.0f, -1.0f);
127: DirectionalLight lgt1 = new DirectionalLight(lColor1, lDir1);
128: lgt1.setInfluencingBounds(bounds);
129: graphRoot.addChild(lgt1);
130: }
131:
132: private Canvas3D createUniverse() {
133: // Get the preferred graphics configuration for the default screen
134: GraphicsConfiguration config = SimpleUniverse
135: .getPreferredConfiguration();
136:
137: // Create a Canvas3D using the preferred configuration
138: Canvas3D c = new Canvas3D(config);
139:
140: // Create simple universe with view branch
141: univ = new SimpleUniverse(c);
142:
143: // only add zoom mouse behavior to viewingPlatform
144: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
145:
146: // This will move the ViewPlatform back a bit so the
147: // objects in the scene can be viewed.
148: viewingPlatform.setNominalViewingTransform();
149:
150: // add orbit behavior to the ViewingPlatform, but disable rotate
151: // and translate
152: OrbitBehavior orbit = new OrbitBehavior(c,
153: OrbitBehavior.REVERSE_ZOOM
154: | OrbitBehavior.DISABLE_ROTATE
155: | OrbitBehavior.DISABLE_TRANSLATE);
156: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
157: 0.0, 0.0), 100.0);
158: orbit.setSchedulingBounds(bounds);
159: viewingPlatform.setViewPlatformBehavior(orbit);
160:
161: // This will move the ViewPlatform back a bit so the
162: // objects in the scene can be viewed.
163: univ.getViewingPlatform().setNominalViewingTransform();
164:
165: // Ensure at least 5 msec per frame (i.e., < 200Hz)
166: univ.getViewer().getView().setMinimumFrameCycleTime(5);
167:
168: return c;
169: }
170:
171: /**
172: * Creates new form LOD
173: */
174: public LOD() {
175: // Initialize the GUI components
176: initComponents();
177:
178: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
179: Canvas3D c = createUniverse();
180: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
181:
182: // Create the content branch and add it to the universe
183: scene = createSceneGraph();
184: univ.addBranchGraph(scene);
185: }
186:
187: // ----------------------------------------------------------------
188:
189: /** This method is called from within the constructor to
190: * initialize the form.
191: * WARNING: Do NOT modify this code. The content of this method is
192: * always regenerated by the Form Editor.
193: */
194: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
195: private void initComponents() {
196: drawingPanel = new javax.swing.JPanel();
197:
198: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
199: setTitle("LOD");
200: drawingPanel.setLayout(new java.awt.BorderLayout());
201:
202: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
203: getContentPane()
204: .add(drawingPanel, java.awt.BorderLayout.CENTER);
205:
206: pack();
207: }// </editor-fold>//GEN-END:initComponents
208:
209: /**
210: * @param args the command line arguments
211: */
212: public static void main(String args[]) {
213: java.awt.EventQueue.invokeLater(new Runnable() {
214: public void run() {
215: new LOD().setVisible(true);
216: }
217: });
218: }
219:
220: // Variables declaration - do not modify//GEN-BEGIN:variables
221: private javax.swing.JPanel drawingPanel;
222: // End of variables declaration//GEN-END:variables
223:
224: }
|