001: /*
002: * $RCSfile: SphereMotion.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:52 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.sphere_motion;
046:
047: import com.sun.j3d.utils.geometry.Sphere;
048: import com.sun.j3d.utils.universe.*;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051: import java.awt.GraphicsConfiguration;
052:
053: public class SphereMotion extends javax.swing.JFrame {
054:
055: private SimpleUniverse univ = null;
056: private BranchGroup scene = null;
057:
058: // Constants for type of light to use
059: private static final int DIRECTIONAL_LIGHT = 0;
060: private static final int POINT_LIGHT = 1;
061: private static final int SPOT_LIGHT = 2;
062:
063: // Flag indicates type of lights: directional, point, or spot
064: // lights. This flag is set based on command line argument
065: private static int lightType = POINT_LIGHT;
066:
067: public BranchGroup createSceneGraph() {
068: Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
069: Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
070: Color3f objColor = new Color3f(0.6f, 0.6f, 0.6f);
071: Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
072: Color3f lColor2 = new Color3f(0.0f, 1.0f, 0.0f);
073: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
074: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
075:
076: Transform3D t;
077:
078: // Create the root of the branch graph
079: BranchGroup objRoot = new BranchGroup();
080:
081: // Create a Transformgroup to scale all objects so they
082: // appear in the scene.
083: TransformGroup objScale = new TransformGroup();
084: Transform3D t3d = new Transform3D();
085: t3d.setScale(0.4);
086: objScale.setTransform(t3d);
087: objRoot.addChild(objScale);
088:
089: // Create a bounds for the background and lights
090: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
091: 0.0, 0.0), 100.0);
092:
093: // Set up the background
094: Background bg = new Background(bgColor);
095: bg.setApplicationBounds(bounds);
096: objScale.addChild(bg);
097:
098: // Create a Sphere object, generate one copy of the sphere,
099: // and add it into the scene graph.
100: Material m = new Material(objColor, eColor, objColor, sColor,
101: 100.0f);
102: Appearance a = new Appearance();
103: m.setLightingEnable(true);
104: a.setMaterial(m);
105: Sphere sph = new Sphere(1.0f, Sphere.GENERATE_NORMALS, 80, a);
106: objScale.addChild(sph);
107:
108: // Create the transform group node for the each light and initialize
109: // it to the identity. Enable the TRANSFORM_WRITE capability so that
110: // our behavior code can modify it at runtime. Add them to the root
111: // of the subgraph.
112: TransformGroup l1RotTrans = new TransformGroup();
113: l1RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
114: objScale.addChild(l1RotTrans);
115:
116: TransformGroup l2RotTrans = new TransformGroup();
117: l2RotTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
118: objScale.addChild(l2RotTrans);
119:
120: // Create transformations for the positional lights
121: t = new Transform3D();
122: Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
123: t.set(lPos1);
124: TransformGroup l1Trans = new TransformGroup(t);
125: l1RotTrans.addChild(l1Trans);
126:
127: t = new Transform3D();
128: Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
129: t.set(lPos2);
130: TransformGroup l2Trans = new TransformGroup(t);
131: l2RotTrans.addChild(l2Trans);
132:
133: // Create Geometry for point lights
134: ColoringAttributes caL1 = new ColoringAttributes();
135: ColoringAttributes caL2 = new ColoringAttributes();
136: caL1.setColor(lColor1);
137: caL2.setColor(lColor2);
138: Appearance appL1 = new Appearance();
139: Appearance appL2 = new Appearance();
140: appL1.setColoringAttributes(caL1);
141: appL2.setColoringAttributes(caL2);
142: l1Trans.addChild(new Sphere(0.05f, appL1));
143: l2Trans.addChild(new Sphere(0.05f, appL2));
144:
145: // Create lights
146: AmbientLight aLgt = new AmbientLight(alColor);
147:
148: Light lgt1 = null;
149: Light lgt2 = null;
150:
151: Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
152: Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
153: Vector3f lDirect1 = new Vector3f(lPos1);
154: Vector3f lDirect2 = new Vector3f(lPos2);
155: lDirect1.negate();
156: lDirect2.negate();
157:
158: switch (lightType) {
159: case DIRECTIONAL_LIGHT:
160: lgt1 = new DirectionalLight(lColor1, lDirect1);
161: lgt2 = new DirectionalLight(lColor2, lDirect2);
162: break;
163: case POINT_LIGHT:
164: lgt1 = new PointLight(lColor1, lPoint, atten);
165: lgt2 = new PointLight(lColor2, lPoint, atten);
166: break;
167: case SPOT_LIGHT:
168: lgt1 = new SpotLight(lColor1, lPoint, atten, lDirect1,
169: 25.0f * (float) Math.PI / 180.0f, 10.0f);
170: lgt2 = new SpotLight(lColor2, lPoint, atten, lDirect2,
171: 25.0f * (float) Math.PI / 180.0f, 10.0f);
172: break;
173: }
174:
175: // Set the influencing bounds
176: aLgt.setInfluencingBounds(bounds);
177: lgt1.setInfluencingBounds(bounds);
178: lgt2.setInfluencingBounds(bounds);
179:
180: // Add the lights into the scene graph
181: objScale.addChild(aLgt);
182: l1Trans.addChild(lgt1);
183: l2Trans.addChild(lgt2);
184:
185: // Create a new Behavior object that will perform the desired
186: // operation on the specified transform object and add it into the
187: // scene graph.
188: Transform3D yAxis = new Transform3D();
189: Alpha rotor1Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
190: 0, 4000, 0, 0, 0, 0, 0);
191: RotationInterpolator rotator1 = new RotationInterpolator(
192: rotor1Alpha, l1RotTrans, yAxis, 0.0f,
193: (float) Math.PI * 2.0f);
194: rotator1.setSchedulingBounds(bounds);
195: l1RotTrans.addChild(rotator1);
196:
197: // Create a new Behavior object that will perform the desired
198: // operation on the specified transform object and add it into the
199: // scene graph.
200: Alpha rotor2Alpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
201: 0, 1000, 0, 0, 0, 0, 0);
202: RotationInterpolator rotator2 = new RotationInterpolator(
203: rotor2Alpha, l2RotTrans, yAxis, 0.0f, 0.0f);
204: bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
205: rotator2.setSchedulingBounds(bounds);
206: l2RotTrans.addChild(rotator2);
207:
208: // Create a position interpolator and attach it to the view
209: // platform
210: TransformGroup vpTrans = univ.getViewingPlatform()
211: .getViewPlatformTransform();
212: Transform3D axisOfTranslation = new Transform3D();
213: Alpha transAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE
214: | Alpha.DECREASING_ENABLE, 0, 0, 5000, 0, 0, 5000, 0, 0);
215: axisOfTranslation.rotY(-Math.PI / 2.0);
216: PositionInterpolator translator = new PositionInterpolator(
217: transAlpha, vpTrans, axisOfTranslation, 2.0f, 3.5f);
218: translator.setSchedulingBounds(bounds);
219: objScale.addChild(translator);
220:
221: // Let Java 3D perform optimizations on this scene graph.
222: objRoot.compile();
223:
224: return objRoot;
225: }
226:
227: private Canvas3D createUniverse() {
228: // Get the preferred graphics configuration for the default screen
229: GraphicsConfiguration config = SimpleUniverse
230: .getPreferredConfiguration();
231:
232: // Create a Canvas3D using the preferred configuration
233: Canvas3D c = new Canvas3D(config);
234:
235: // Create simple universe with view branch
236: univ = new SimpleUniverse(c);
237:
238: // This will move the ViewPlatform back a bit so the
239: // objects in the scene can be viewed.
240: univ.getViewingPlatform().setNominalViewingTransform();
241:
242: // Ensure at least 5 msec per frame (i.e., < 200Hz)
243: univ.getViewer().getView().setMinimumFrameCycleTime(5);
244:
245: return c;
246: }
247:
248: /**
249: * Creates new form SphereMotion
250: */
251: public SphereMotion(final String[] args) {
252:
253: // Parse the Input Arguments
254: String usage = "Usage: java SphereMotion [-point | -spot | -dir]";
255: for (int i = 0; i < args.length; i++) {
256: if (args[i].startsWith("-")) {
257: if (args[i].equals("-point")) {
258: System.out.println("Using point lights");
259: lightType = POINT_LIGHT;
260: } else if (args[i].equals("-spot")) {
261: System.out.println("Using spot lights");
262: lightType = SPOT_LIGHT;
263: } else if (args[i].equals("-dir")) {
264: System.out.println("Using directional lights");
265: lightType = DIRECTIONAL_LIGHT;
266: } else {
267: System.out.println(usage);
268: System.exit(0);
269: }
270: } else {
271: System.out.println(usage);
272: System.exit(0);
273: }
274: }
275:
276: // Initialize the GUI components
277: initComponents();
278:
279: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
280: Canvas3D c = createUniverse();
281: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
282:
283: // Create the content branch and add it to the universe
284: scene = createSceneGraph();
285: univ.addBranchGraph(scene);
286: }
287:
288: // ----------------------------------------------------------------
289:
290: /** This method is called from within the constructor to
291: * initialize the form.
292: * WARNING: Do NOT modify this code. The content of this method is
293: * always regenerated by the Form Editor.
294: */
295: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
296: private void initComponents() {
297: drawingPanel = new javax.swing.JPanel();
298:
299: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
300: setTitle("SphereMotion");
301: drawingPanel.setLayout(new java.awt.BorderLayout());
302:
303: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
304: getContentPane()
305: .add(drawingPanel, java.awt.BorderLayout.CENTER);
306:
307: pack();
308: }// </editor-fold>//GEN-END:initComponents
309:
310: /**
311: * @param args the command line arguments
312: */
313: public static void main(final String args[]) {
314: java.awt.EventQueue.invokeLater(new Runnable() {
315: public void run() {
316: SphereMotion sphereMotion = new SphereMotion(args);
317: sphereMotion.setVisible(true);
318: }
319: });
320: }
321:
322: // Variables declaration - do not modify//GEN-BEGIN:variables
323: private javax.swing.JPanel drawingPanel;
324: // End of variables declaration//GEN-END:variables
325:
326: }
|