001: /*
002: * $RCSfile: JInternalWorld.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.6 $
041: * $Date: 2007/02/09 17:21:42 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.jcanvas3d;
046:
047: import com.sun.j3d.exp.swing.JCanvas3D;
048: import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
049: import com.sun.j3d.utils.universe.SimpleUniverse;
050:
051: import java.awt.BorderLayout;
052: import java.awt.Component;
053: import java.awt.Dimension;
054: import java.awt.Font;
055:
056: import javax.media.j3d.Alpha;
057: import javax.media.j3d.AmbientLight;
058: import javax.media.j3d.Appearance;
059: import javax.media.j3d.BoundingSphere;
060: import javax.media.j3d.BranchGroup;
061: import javax.media.j3d.DirectionalLight;
062: import javax.media.j3d.Font3D;
063: import javax.media.j3d.FontExtrusion;
064: import javax.media.j3d.GraphicsConfigTemplate3D;
065: import javax.media.j3d.Material;
066: import javax.media.j3d.RotationInterpolator;
067: import javax.media.j3d.Shape3D;
068: import javax.media.j3d.Text3D;
069: import javax.media.j3d.Transform3D;
070: import javax.media.j3d.TransformGroup;
071:
072: import javax.swing.JInternalFrame;
073: import javax.vecmath.Color3f;
074:
075: import javax.vecmath.Point3d;
076: import javax.vecmath.Point3f;
077: import javax.vecmath.Vector3d;
078: import javax.vecmath.Vector3f;
079:
080: /**
081: * This is a JInternalFrame holding an universe, which can be configured to
082: * be interactive -that is, where user can interact with object- or automatic
083: * -where the object spins only-. When in automatic mode, spinning speed is
084: * changed so that they look less the same. Changing the spinning start angle
085: * helps unsynchronizing the rotations too.
086: *
087: * @author pepe
088: */
089: public class JInternalWorld extends JInternalFrame {
090: /** DOCUMENT ME! */
091: private Component comp;
092:
093: /**
094: * Creates a new JInternalWorld object.
095: *
096: * @param isInteractive tells the world to be constructed as interactive
097: * @param isDelayed tells the rotator to start at a random alpha.
098: */
099: public JInternalWorld(boolean isInteractive, boolean isDelayed,
100: boolean isRandom) {
101: super ();
102: setSize(256, 256);
103: setClosable(true);
104:
105: JCanvas3D canvas = new JCanvas3D(new GraphicsConfigTemplate3D());
106:
107: if (true == isDelayed) {
108: canvas.setResizeMode(canvas.RESIZE_DELAYED);
109: }
110:
111: comp = canvas;
112:
113: Dimension dim = new Dimension(256, 256);
114: comp.setPreferredSize(dim);
115: comp.setSize(dim);
116: getContentPane().setLayout(new BorderLayout());
117: getContentPane().add(comp, BorderLayout.CENTER);
118: pack();
119:
120: // Create a simple scene and attach it to the virtual universe
121: BranchGroup scene = createSceneGraph(isInteractive, isRandom);
122: SimpleUniverse universe = new SimpleUniverse(canvas
123: .getOffscreenCanvas3D()); //TODO: this is awful and must not be done like that in final version
124:
125: // This will move the ViewPlatform back a bit so the
126: // objects in the scene can be viewed.
127: universe.getViewingPlatform().setNominalViewingTransform();
128: universe.getViewer().getView().setMinimumFrameCycleTime(30);
129: universe.addBranchGraph(scene);
130: }
131:
132: /**
133: * Creates the world. Only exists to cleanup the source a bit
134: *
135: * @param isInteractive tells the world to be constructed as interactive
136: * @param isDelayed tells the rotator to start at a random alpha.
137: *
138: * @return a global branchgroup containing the world, as desired.
139: */
140: private BranchGroup createSceneGraph(boolean isInteractive,
141: boolean isRandom) {
142: // Create the root of the branch graph
143: BranchGroup objRoot = new BranchGroup();
144:
145: // Create the TransformGroup node and initialize it to the
146: // identity. Enable the TRANSFORM_WRITE capability so that
147: // our behavior code can modify it at run time. Add it to
148: // the root of the subgraph.
149: TransformGroup objTrans = new TransformGroup();
150: Transform3D t3dTrans = new Transform3D();
151: t3dTrans.setTranslation(new Vector3d(0, 0, -1));
152: objTrans.setTransform(t3dTrans);
153:
154: TransformGroup objRot = new TransformGroup();
155: objRot.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
156: objRoot.addChild(objTrans);
157: objTrans.addChild(objRot);
158:
159: // Create a simple Shape3D node; add it to the scene graph.
160: // issue 383: changed the cube to a text, so that any graphical problem related to Yup can be seen.
161: Font3D f3d = new Font3D(new Font("dialog", Font.PLAIN, 1),
162: new FontExtrusion());
163: Text3D text = new Text3D(f3d, "JCanvas3D", new Point3f(-2.3f,
164: -0.5f, 0.f));
165:
166: Shape3D sh = new Shape3D();
167: Appearance app = new Appearance();
168: Material mm = new Material();
169: mm.setLightingEnable(true);
170: app.setMaterial(mm);
171: sh.setGeometry(text);
172: sh.setAppearance(app);
173:
174: objRot.addChild(sh);
175:
176: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
177: 0.0, 0.0), 100.0);
178:
179: // Set up the ambient light
180: Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f);
181: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
182: ambientLightNode.setInfluencingBounds(bounds);
183: objRoot.addChild(ambientLightNode);
184:
185: // Set up the directional lights
186: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
187: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
188: Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
189: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
190:
191: DirectionalLight light1 = new DirectionalLight(light1Color,
192: light1Direction);
193: light1.setInfluencingBounds(bounds);
194: objRoot.addChild(light1);
195:
196: DirectionalLight light2 = new DirectionalLight(light2Color,
197: light2Direction);
198: light2.setInfluencingBounds(bounds);
199: objRoot.addChild(light2);
200:
201: if (true == isInteractive) {
202: MouseRotate mr = new MouseRotate(comp, objRot);
203: mr.setSchedulingBounds(bounds);
204: mr.setSchedulingInterval(1);
205: objRoot.addChild(mr);
206: } else {
207: // Create a new Behavior object that will perform the
208: // desired operation on the specified transform and add
209: // it into the scene graph.
210: Transform3D yAxis = new Transform3D();
211:
212: // rotation speed is randomized a bit so that it does not go at the same speed on every canvases,
213: // which will make it more natural and express the differences between every present universes
214: Alpha rotationAlpha = null;
215:
216: if (true == isRandom) {
217: int duration = Math.max(2000,
218: (int) (Math.random() * 8000.));
219: rotationAlpha = new Alpha(-1,
220: (int) ((double) duration * Math.random()), 0,
221: duration, 0, 0);
222: } else {
223: rotationAlpha = new Alpha(-1, 4000);
224: }
225:
226: RotationInterpolator rotator = new RotationInterpolator(
227: rotationAlpha, objRot, yAxis, 0.0f,
228: (float) Math.PI * 2.0f);
229:
230: rotator.setSchedulingBounds(bounds);
231: objRoot.addChild(rotator);
232: }
233:
234: return objRoot;
235: }
236:
237: }
|