001: /*
002: * $RCSfile: GearBox.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:38 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.gears;
046:
047: import com.sun.j3d.utils.behaviors.mouse.*;
048: import com.sun.j3d.utils.universe.*;
049: import javax.media.j3d.*;
050: import javax.vecmath.*;
051: import java.lang.Integer;
052: import com.sun.j3d.utils.behaviors.vp.*;
053: import java.awt.GraphicsConfiguration;
054:
055: /**
056: * Simple Java 3D example program to display a spinning cube.
057: */
058: public class GearBox extends javax.swing.JFrame {
059:
060: private SimpleUniverse univ = null;
061: private BranchGroup scene = null;
062: private int toothCount = 48;
063:
064: public BranchGroup createSceneGraph() {
065: Transform3D tempTransform = new Transform3D();
066:
067: // Create the root of the branch graph
068: BranchGroup branchRoot = createBranchEnvironment();
069:
070: // Create a Transformgroup to scale all objects so they
071: // appear in the scene.
072: TransformGroup objScale = new TransformGroup();
073: Transform3D t3d = new Transform3D();
074: t3d.setScale(0.4);
075: objScale.setTransform(t3d);
076: branchRoot.addChild(objScale);
077:
078: // Create an Appearance.
079: Appearance look = new Appearance();
080: Color3f objColor = new Color3f(0.5f, 0.5f, 0.6f);
081: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
082: Color3f white = new Color3f(1.0f, 1.0f, 1.0f);
083: look.setMaterial(new Material(objColor, black, objColor, white,
084: 100.0f));
085:
086: // Create the transform group node and initialize it to the
087: // identity. Enable the TRANSFORM_WRITE capability so that
088: // our behavior code can modify it at runtime. Add it to the
089: // root of the subgraph.
090: TransformGroup gearboxTrans = new TransformGroup();
091: gearboxTrans
092: .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
093: gearboxTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
094: objScale.addChild(gearboxTrans);
095:
096: // Create a bounds for the mouse behavior methods
097: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
098: 0.0, 0.0), 100.0);
099:
100: // Define the shaft base information
101: int shaftCount = 4;
102: int secondsPerRevolution = 8000;
103:
104: // Create the Shaft(s)
105: Shaft shafts[] = new Shaft[shaftCount];
106: TransformGroup shaftTGs[] = new TransformGroup[shaftCount];
107: Alpha shaftAlphas[] = new Alpha[shaftCount];
108: RotationInterpolator shaftRotors[] = new RotationInterpolator[shaftCount];
109: Transform3D shaftAxis[] = new Transform3D[shaftCount];
110:
111: // Note: the following arrays we're incorporated to make changing
112: // the gearbox easier.
113: float shaftRatios[] = new float[shaftCount];
114: shaftRatios[0] = 1.0f;
115: shaftRatios[1] = 0.5f;
116: shaftRatios[2] = 0.75f;
117: shaftRatios[3] = 5.0f;
118:
119: float shaftRadius[] = new float[shaftCount];
120: shaftRadius[0] = 0.2f;
121: shaftRadius[1] = 0.2f;
122: shaftRadius[2] = 0.2f;
123: shaftRadius[3] = 0.2f;
124:
125: float shaftLength[] = new float[shaftCount];
126: shaftLength[0] = 1.8f;
127: shaftLength[1] = 0.8f;
128: shaftLength[2] = 0.8f;
129: shaftLength[3] = 0.8f;
130:
131: float shaftDirection[] = new float[shaftCount];
132: shaftDirection[0] = 1.0f;
133: shaftDirection[1] = -1.0f;
134: shaftDirection[2] = 1.0f;
135: shaftDirection[3] = -1.0f;
136:
137: Vector3d shaftPlacement[] = new Vector3d[shaftCount];
138: shaftPlacement[0] = new Vector3d(-0.75, -0.9, 0.0);
139: shaftPlacement[1] = new Vector3d(0.75, -0.9, 0.0);
140: shaftPlacement[2] = new Vector3d(0.75, 0.35, 0.0);
141: shaftPlacement[3] = new Vector3d(-0.75, 0.60, -0.7);
142:
143: // Create the shafts.
144: for (int i = 0; i < shaftCount; i++) {
145: shafts[i] = new Shaft(shaftRadius[i], shaftLength[i], 25,
146: look);
147: }
148:
149: // Create a transform group node for placing each shaft
150: for (int i = 0; i < shaftCount; i++) {
151: shaftTGs[i] = new TransformGroup();
152: gearboxTrans.addChild(shaftTGs[i]);
153: shaftTGs[i].getTransform(tempTransform);
154: tempTransform.setTranslation(shaftPlacement[i]);
155: shaftTGs[i].setTransform(tempTransform);
156: shaftTGs[i].addChild(shafts[i]);
157: }
158:
159: // Add rotation interpolators to rotate the shaft in the appropriate
160: // direction and at the appropriate rate
161: for (int i = 0; i < shaftCount; i++) {
162: shaftAlphas[i] = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
163: 0, (long) (secondsPerRevolution * shaftRatios[i]),
164: 0, 0, 0, 0, 0);
165: shaftAxis[i] = new Transform3D();
166: shaftAxis[i].rotX(Math.PI / 2.0);
167: shaftRotors[i] = new RotationInterpolator(shaftAlphas[i],
168: shafts[i], shaftAxis[i], 0.0f, shaftDirection[i]
169: * (float) Math.PI * 2.0f);
170: shaftRotors[i].setSchedulingBounds(bounds);
171: shaftTGs[i].addChild(shaftRotors[i]);
172: }
173:
174: // Define the gear base information. Again, these arrays exist to
175: // make the process of changing the GearBox1 via an editor faster
176: int gearCount = 5;
177: float valleyToCircularPitchRatio = .15f;
178: float pitchCircleRadius = 1.0f;
179: float addendum = 0.05f;
180: float dedendum = 0.05f;
181: float gearThickness = 0.3f;
182: float toothTipThickness = 0.27f;
183:
184: // Create an array of gears and their associated information
185: SpurGear gears[] = new SpurGear[gearCount];
186: TransformGroup gearTGs[] = new TransformGroup[gearCount];
187:
188: int gearShaft[] = new int[gearCount];
189: gearShaft[0] = 0;
190: gearShaft[1] = 1;
191: gearShaft[2] = 2;
192: gearShaft[3] = 0;
193: gearShaft[4] = 3;
194:
195: float ratio[] = new float[gearCount];
196: ratio[0] = 1.0f;
197: ratio[1] = 0.5f;
198: ratio[2] = 0.75f;
199: ratio[3] = 0.25f;
200: ratio[4] = 1.25f;
201:
202: Vector3d placement[] = new Vector3d[gearCount];
203: placement[0] = new Vector3d(0.0, 0.0, 0.0);
204: placement[1] = new Vector3d(0.0, 0.0, 0.0);
205: placement[2] = new Vector3d(0.0, 0.0, 0.0);
206: placement[3] = new Vector3d(0.0, 0.0, -0.7);
207: placement[4] = new Vector3d(0.0, 0.0, 0.0);
208:
209: // Create the gears.
210: for (int i = 0; i < gearCount; i++) {
211: gears[i] = new SpurGearThinBody(
212: ((int) ((float) toothCount * ratio[i])),
213: pitchCircleRadius * ratio[i], shaftRadius[0],
214: addendum, dedendum, gearThickness,
215: toothTipThickness, valleyToCircularPitchRatio, look);
216: }
217:
218: // Create a transform group node for arranging the gears on a shaft
219: // and attach the gear to its associated shaft
220: for (int i = 0; i < gearCount; i++) {
221: gearTGs[i] = new TransformGroup();
222: gearTGs[i].getTransform(tempTransform);
223: tempTransform
224: .rotZ((shaftDirection[gearShaft[i]] == -1.0) ? gears[i]
225: .getCircularPitchAngle()
226: / -2.0f
227: : 0.0f);
228: tempTransform.setTranslation(placement[i]);
229: gearTGs[i].setTransform(tempTransform);
230: gearTGs[i].addChild(gears[i]);
231: shafts[gearShaft[i]].addChild(gearTGs[i]);
232: }
233:
234: // Have Java 3D perform optimizations on this scene graph.
235: branchRoot.compile();
236:
237: return branchRoot;
238: }
239:
240: BranchGroup createBranchEnvironment() {
241: // Create the root of the branch graph
242: BranchGroup branchRoot = new BranchGroup();
243:
244: // Create a bounds for the background and lights
245: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
246: 0.0, 0.0), 100.0);
247:
248: // Set up the background
249: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
250: Background bgNode = new Background(bgColor);
251: bgNode.setApplicationBounds(bounds);
252: branchRoot.addChild(bgNode);
253:
254: // Set up the ambient light
255: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
256: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
257: ambientLightNode.setInfluencingBounds(bounds);
258: branchRoot.addChild(ambientLightNode);
259:
260: // Set up the directional lights
261: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
262: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
263: Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
264: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
265:
266: DirectionalLight light1 = new DirectionalLight(light1Color,
267: light1Direction);
268: light1.setInfluencingBounds(bounds);
269: branchRoot.addChild(light1);
270:
271: DirectionalLight light2 = new DirectionalLight(light2Color,
272: light2Direction);
273: light2.setInfluencingBounds(bounds);
274: branchRoot.addChild(light2);
275:
276: return branchRoot;
277: }
278:
279: private Canvas3D createUniverse() {
280: // Get the preferred graphics configuration for the default screen
281: GraphicsConfiguration config = SimpleUniverse
282: .getPreferredConfiguration();
283:
284: // Create a Canvas3D using the preferred configuration
285: Canvas3D c = new Canvas3D(config);
286:
287: // Create simple universe with view branch
288: univ = new SimpleUniverse(c);
289:
290: // add mouse behaviors to the ViewingPlatform
291: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
292:
293: // add orbit behavior to the ViewingPlatform
294: OrbitBehavior orbit = new OrbitBehavior(c,
295: OrbitBehavior.REVERSE_ALL);
296: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
297: 0.0, 0.0), 100.0);
298: orbit.setSchedulingBounds(bounds);
299: viewingPlatform.setViewPlatformBehavior(orbit);
300:
301: // This will move the ViewPlatform back a bit so the
302: // objects in the scene can be viewed.
303: univ.getViewingPlatform().setNominalViewingTransform();
304:
305: // Ensure at least 5 msec per frame (i.e., < 200Hz)
306: univ.getViewer().getView().setMinimumFrameCycleTime(5);
307:
308: return c;
309: }
310:
311: /**
312: * Creates new form GearBox
313: */
314: public GearBox(String args[]) {
315: int value;
316:
317: if (args.length > 1) {
318: System.out.println("Usage: java GearBox #teeth (LCD 4)");
319: System.exit(0);
320: } else if (args.length == 1) {
321: {
322: try {
323: value = Integer.parseInt(args[0]);
324: } catch (NumberFormatException e) {
325: System.out.println("Illegal integer specified");
326: System.out
327: .println("Usage: java GearBox #teeth (LCD 4)");
328: value = 0;
329: System.exit(0);
330: }
331: if (value <= 0 | (value % 4) != 0) {
332: System.out
333: .println("Integer not a positive multiple of 4");
334: System.out
335: .println("Usage: java GearBox #teeth (LCD 4)");
336: System.exit(0);
337: }
338: toothCount = value;
339: }
340: }
341:
342: // Initialize the GUI components
343: initComponents();
344:
345: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
346: Canvas3D c = createUniverse();
347: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
348:
349: // Create the content branch and add it to the universe
350: scene = createSceneGraph();
351: univ.addBranchGraph(scene);
352: }
353:
354: // ----------------------------------------------------------------
355:
356: /** This method is called from within the constructor to
357: * initialize the form.
358: * WARNING: Do NOT modify this code. The content of this method is
359: * always regenerated by the Form Editor.
360: */
361: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
362: private void initComponents() {
363: drawingPanel = new javax.swing.JPanel();
364:
365: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
366: setTitle("GearBox");
367: drawingPanel.setLayout(new java.awt.BorderLayout());
368:
369: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
370: getContentPane()
371: .add(drawingPanel, java.awt.BorderLayout.CENTER);
372:
373: pack();
374: }// </editor-fold>//GEN-END:initComponents
375:
376: /**
377: * @param args the command line arguments
378: */
379: public static void main(final String args[]) {
380: java.awt.EventQueue.invokeLater(new Runnable() {
381: public void run() {
382: GearBox gb = new GearBox(args);
383: gb.setVisible(true);
384: }
385: });
386: }
387:
388: // Variables declaration - do not modify//GEN-BEGIN:variables
389: private javax.swing.JPanel drawingPanel;
390: // End of variables declaration//GEN-END:variables
391:
392: }
|