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