001: /*
002: * $RCSfile: SimpleGeometry.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.2 $
041: * $Date: 2007/02/09 17:21:50 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.platform_geometry;
046:
047: import com.sun.j3d.utils.geometry.ColorCube;
048: import com.sun.j3d.utils.geometry.*;
049: import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
050: import com.sun.j3d.utils.universe.*;
051: import javax.media.j3d.*;
052: import javax.vecmath.*;
053: import java.applet.Applet;
054: import com.sun.j3d.utils.applet.MainFrame;
055: import java.awt.*;
056:
057: /**
058: * This class demonstrates the use of the Universe builder for stand-alone
059: * applications along with the use of the PlatformGeometry node that is
060: * present in the Java 3D Universe Builder utility. The standard
061: * HelloWorld application is brought up. A transparent cylinder has been
062: * added to the PlatfromGeometry node of the ViewingPlatform and the
063: * MouseTranslate utility has been used to allow this sphere to be dragged
064: * around the canvas.
065: */
066: public class SimpleGeometry extends Applet {
067:
068: SimpleUniverse u = null;
069:
070: public BranchGroup createSceneGraph() {
071: // Create the root of the branch graph
072: BranchGroup objRoot = new BranchGroup();
073:
074: // Create a Transformgroup to scale all objects so they
075: // appear in the scene.
076: TransformGroup objScale = new TransformGroup();
077: Transform3D t3d = new Transform3D();
078: t3d.setScale(0.4);
079: objScale.setTransform(t3d);
080: objRoot.addChild(objScale);
081:
082: // Create the transform group node and initialize it to the
083: // identity. Enable the TRANSFORM_WRITE capability so that
084: // our behavior code can modify it at runtime. Add it to the
085: // root of the subgraph.
086: TransformGroup objTrans = new TransformGroup();
087: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
088: objScale.addChild(objTrans);
089:
090: // Create a simple shape leaf node, add it to the scene graph.
091: objTrans.addChild(new ColorCube());
092:
093: // Create a new Behavior object that will perform the desired
094: // operation on the specified transform object and add it into
095: // the scene graph.
096: Transform3D yAxis = new Transform3D();
097: Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE, 0,
098: 0, 4000, 0, 0, 0, 0, 0);
099:
100: RotationInterpolator rotator = new RotationInterpolator(
101: rotationAlpha, objTrans, yAxis, 0.0f,
102: (float) Math.PI * 2.0f);
103: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
104: 0.0, 0.0), 100.0);
105: rotator.setSchedulingBounds(bounds);
106: objTrans.addChild(rotator);
107:
108: // Have Java 3D perform optimizations on this scene graph.
109: objRoot.compile();
110:
111: return objRoot;
112: }
113:
114: /*
115: * Create the geometry to add to the platform geometry.
116: */
117: PlatformGeometry createAimer() {
118:
119: PlatformGeometry pg = new PlatformGeometry();
120:
121: // This TransformGroup will be used by the MouseTranslate
122: // utiltiy to move the cylinder around the canvas. when the
123: // the user holds down mouse button 3.
124: TransformGroup moveTG = new TransformGroup();
125: moveTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
126: moveTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
127: MouseTranslate mouseT = new MouseTranslate(moveTG);
128: moveTG.addChild(mouseT);
129: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
130: 0.0, 0.0), 100.0);
131: mouseT.setSchedulingBounds(bounds);
132: pg.addChild(moveTG);
133:
134: // This TransformGroup is used to place the cylinder in the scene.
135: // The cylinder will be rotated 90 degrees so it will appear as
136: // a circle on the screen (could be made into a nice gun site...).
137: // The cylinder is also displaced a little in Z so it is in front
138: // of the viewer.
139: Transform3D xForm = new Transform3D();
140: xForm.rotX(Math.PI / 2.0);
141: xForm.setTranslation(new Vector3d(0.0, 0.0, -0.7));
142: TransformGroup placementTG = new TransformGroup(xForm);
143: moveTG.addChild(placementTG);
144:
145: // Create the cylinder - make it thin and transparent.
146: Appearance cylinderAppearance = new Appearance();
147: TransparencyAttributes transAttrs = new TransparencyAttributes(
148: TransparencyAttributes.FASTEST, 0.5f);
149: // cylinderAppearance.setTransparencyAttributes(transAttrs);
150: Cylinder aimer = new Cylinder(0.06f, 0.005f, 0,
151: cylinderAppearance);
152: placementTG.addChild(aimer);
153:
154: return pg;
155: }
156:
157: public void init() {
158:
159: setLayout(new BorderLayout());
160: GraphicsConfiguration config = SimpleUniverse
161: .getPreferredConfiguration();
162:
163: Canvas3D c = new Canvas3D(config);
164: add("Center", c);
165:
166: // Create a simple scene and attach it to the virtual universe
167: BranchGroup scene = createSceneGraph();
168:
169: u = new SimpleUniverse(c);
170:
171: PlatformGeometry pg = createAimer();
172:
173: // Now set the just created PlatformGeometry.
174: ViewingPlatform vp = u.getViewingPlatform();
175: vp.setPlatformGeometry(pg);
176:
177: // This will move the ViewPlatform back a bit so the
178: // objects in the scene can be viewed.
179: u.getViewingPlatform().setNominalViewingTransform();
180:
181: // Add everthing to the scene graph - it will now be displayed.
182: u.addBranchGraph(scene);
183: }
184:
185: public SimpleGeometry(String[] args) {
186: }
187:
188: public SimpleGeometry() {
189: }
190:
191: public void destroy() {
192: u.cleanup();
193: }
194:
195: public static void main(String[] args) {
196: new MainFrame(new SimpleGeometry(args), 256, 256);
197: }
198: }
|