001: /*
002: * $RCSfile: BackgroundSoundTest.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.1 $
041: * $Date: 2007/02/09 18:10:26 $
042: * $State: Exp $
043: */
044: package org.jdesktop.j3d.examples.sound;
045:
046: import com.sun.j3d.utils.behaviors.keyboard.KeyNavigatorBehavior;
047: import com.sun.j3d.utils.geometry.Sphere;
048: import com.sun.j3d.utils.universe.*;
049: import com.sun.j3d.utils.geometry.ColorCube;
050: import java.net.URL;
051: import javax.swing.JCheckBoxMenuItem;
052: import javax.swing.JFrame;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import java.awt.GraphicsConfiguration;
056: import org.jdesktop.j3d.examples.Resources;
057:
058: /**
059: * This is a test for a BackgroundSound.
060: * This program is ported from an earlier version of BackgroundSoundTest, in the j3d-incubator project,
061: * contributed by David Grace (dave@dutchie.net).
062: *
063: */
064: public class BackgroundSoundTest extends javax.swing.JFrame {
065:
066: private URL url = null;
067: private SimpleUniverse univ = null;
068: private BranchGroup scene = null;
069:
070: //The activation radius for the ViewPlatform
071: private float activationRadius = 1;
072:
073: private Shape3D getDefaultGrid(int noOfLines, double size,
074: double height) {
075:
076: Shape3D shape = new Shape3D();
077: double lineLength = noOfLines * size / 2;
078: LineArray la = new LineArray(noOfLines * 4,
079: LineArray.COORDINATES);
080: int count = 0;
081: for (int i = 0; i < noOfLines; i++) {
082: la.setCoordinate(count, new Point3d(-lineLength, height, i
083: * size - lineLength));
084: count++;
085: la.setCoordinate(count, new Point3d(lineLength, height, i
086: * size - lineLength));
087: count++;
088: }
089: for (int i = 0; i < noOfLines; i++) {
090: la.setCoordinate(count, new Point3d(i * size - lineLength,
091: height, -lineLength));
092: count++;
093: la.setCoordinate(count, new Point3d(i * size - lineLength,
094: height, lineLength));
095: count++;
096: }
097: shape.setGeometry(la);
098: Appearance a = new Appearance();
099: ColoringAttributes ca = new ColoringAttributes();
100: ca.setColor(0.3f, 0.3f, 0.3f);
101: a.setColoringAttributes(ca);
102: LineAttributes sla = new LineAttributes();
103: sla.setLineWidth(1.0f);
104: a.setLineAttributes(sla);
105: shape.setAppearance(a);
106:
107: return shape;
108: }
109:
110: private Sphere createSoundBoundingGeometry(Sound sound) {
111: Bounds bounds = sound.getSchedulingBounds();
112: assert ((bounds != null) && (bounds instanceof BoundingSphere));
113: BoundingSphere bs = (BoundingSphere) bounds;
114: float radius = (float) bs.getRadius();
115:
116: return getSphere(radius);
117: }
118:
119: private Sphere getSphere(float radius) {
120:
121: Appearance a = new Appearance();
122: Material m = new Material();
123:
124: m.setDiffuseColor(1, 0, 0);
125: m.setAmbientColor(1, 0, 0);
126: m.setShininess(8);
127: a.setMaterial(m);
128:
129: PolygonAttributes pa = new PolygonAttributes();
130: pa.setPolygonMode(PolygonAttributes.POLYGON_LINE);
131: pa.setCullFace(PolygonAttributes.CULL_NONE);
132: a.setPolygonAttributes(pa);
133: return new Sphere(radius, a);
134: }
135:
136: private TransformGroup createSoundNodeGeometry(float x, float y,
137: float z) {
138:
139: TransformGroup rootTransformGroup = new TransformGroup();
140: Transform3D t3D = new Transform3D();
141: t3D.setTranslation(new Vector3f(x, y, z));
142: rootTransformGroup.setTransform(t3D);
143: ColorCube cc = new ColorCube(0.1);
144: rootTransformGroup.addChild(cc);
145: return rootTransformGroup;
146: }
147:
148: public BranchGroup createSceneGraph() {
149: // Create the root of the branch graph
150: BranchGroup objRoot = new BranchGroup();
151:
152: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
153: 0.0, 0.0), 100.0);
154:
155: AmbientLight al = new AmbientLight();
156: al.setInfluencingBounds(bounds);
157:
158: DirectionalLight dl = new DirectionalLight();
159: dl.setDirection(-1, -1, -1);
160: dl.setInfluencingBounds(bounds);
161: objRoot.addChild(al);
162: objRoot.addChild(dl);
163:
164: /*
165: * Create Sound and Behavior objects that will play the sound
166: */
167: BackgroundSound bgs = new BackgroundSound();
168: BackgroundSoundBehavior player = new BackgroundSoundBehavior(
169: bgs, url);
170: player.setSchedulingBounds(bounds);
171: objRoot.addChild(bgs);
172: objRoot.addChild(player);
173:
174: objRoot.addChild(getDefaultGrid(40, 1, -1));
175: objRoot.addChild(createSoundNodeGeometry(0, 0, 0));
176: objRoot.addChild(createSoundBoundingGeometry(bgs));
177: return objRoot;
178: }
179:
180: private Canvas3D createUniverse() {
181: // Get the preferred graphics configuration for the default screen
182: GraphicsConfiguration config = SimpleUniverse
183: .getPreferredConfiguration();
184:
185: // Create a Canvas3D using the preferred configuration
186: Canvas3D c = new Canvas3D(config);
187:
188: // Create simple universe with view branch
189: univ = new SimpleUniverse(c);
190:
191: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
192: TransformGroup viewingPlatformTransformGroup = viewingPlatform
193: .getViewPlatformTransform();
194:
195: // This will move the ViewPlatform back a bit so the
196: // objects in the scene can be viewed.
197: viewingPlatform.setNominalViewingTransform();
198:
199: Viewer viewer = univ.getViewer();
200:
201: viewer.createAudioDevice();
202: viewer.getView().setBackClipDistance(1000.0f);
203:
204: // Ensure at least 50 msec per frame.
205: viewer.getView().setMinimumFrameCycleTime(30);
206:
207: viewer.getView().getViewPlatform().setActivationRadius(
208: activationRadius);
209:
210: BranchGroup bg = new BranchGroup();
211: KeyNavigatorBehavior knb = new KeyNavigatorBehavior(c,
212: viewingPlatformTransformGroup);
213: Bounds b = new BoundingSphere(new Point3d(),
214: Double.POSITIVE_INFINITY);
215: knb.setSchedulingBounds(b);
216: bg.addChild(knb);
217: univ.addBranchGraph(bg);
218:
219: return c;
220: }
221:
222: /**
223: * Creates new form BackgroundSoundTest
224: */
225: public BackgroundSoundTest() {
226: // Initialize the GUI components
227: initComponents();
228:
229: url = Resources.getResource("resources/audio/magic_bells.wav");
230: if (url == null) {
231: System.err
232: .println("resources/audio/magic_bells.wav not found");
233: System.exit(1);
234: }
235:
236: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
237: Canvas3D c = createUniverse();
238: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
239:
240: // Create the content branch and add it to the universe
241: scene = createSceneGraph();
242: univ.addBranchGraph(scene);
243: }
244:
245: // ----------------------------------------------------------------
246:
247: /** This method is called from within the constructor to
248: * initialize the form.
249: * WARNING: Do NOT modify this code. The content of this method is
250: * always regenerated by the Form Editor.
251: */
252: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
253: private void initComponents() {
254: drawingPanel = new javax.swing.JPanel();
255:
256: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
257: setTitle("BackgroundSoundTest");
258: drawingPanel.setLayout(new java.awt.BorderLayout());
259:
260: drawingPanel.setPreferredSize(new java.awt.Dimension(800, 600));
261: getContentPane()
262: .add(drawingPanel, java.awt.BorderLayout.CENTER);
263:
264: pack();
265: }// </editor-fold>//GEN-END:initComponents
266:
267: /**
268: * @param args the command line arguments
269: */
270: public static void main(String args[]) {
271: java.awt.EventQueue.invokeLater(new Runnable() {
272: public void run() {
273: new BackgroundSoundTest().setVisible(true);
274: }
275: });
276: }
277:
278: // Variables declaration - do not modify//GEN-BEGIN:variables
279: private javax.swing.JPanel drawingPanel;
280: // End of variables declaration//GEN-END:variables
281:
282: }
|