001: /*
002: * $RCSfile: PickText3DBounds.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:48 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.picking;
046:
047: import java.applet.Applet;
048: import java.awt.BorderLayout;
049: import com.sun.j3d.utils.picking.PickTool;
050: import com.sun.j3d.utils.picking.behaviors.*;
051: import com.sun.j3d.utils.geometry.Sphere;
052: import com.sun.j3d.utils.applet.MainFrame;
053: import com.sun.j3d.utils.universe.*;
054: import javax.media.j3d.*;
055: import javax.vecmath.*;
056: import java.util.Enumeration;
057: import java.awt.*;
058: import java.lang.String;
059:
060: public class PickText3DBounds extends Applet {
061:
062: private SimpleUniverse u = null;
063:
064: public BranchGroup createSceneGraph(Canvas3D canvas) {
065: Color3f eColor = new Color3f(0.0f, 0.0f, 0.0f);
066: Color3f sColor = new Color3f(1.0f, 1.0f, 1.0f);
067: Color3f objColor = new Color3f(0.6f, 0.6f, 0.6f);
068: Color3f lColor1 = new Color3f(1.0f, 0.0f, 0.0f);
069: Color3f lColor2 = new Color3f(0.0f, 1.0f, 0.0f);
070: Color3f alColor = new Color3f(0.2f, 0.2f, 0.2f);
071: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
072:
073: Transform3D t;
074:
075: // Create the root of the branch graph
076: BranchGroup objRoot = new BranchGroup();
077:
078: // Create a Transformgroup to scale all objects so they
079: // appear in the scene.
080: TransformGroup objScale = new TransformGroup();
081: Transform3D t3d = new Transform3D();
082: t3d.setScale(0.4);
083: objScale.setTransform(t3d);
084: objRoot.addChild(objScale);
085:
086: // Create a bounds for the background and lights
087: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
088: 0.0, 0.0), 100.0);
089:
090: // Set up the background
091: Background bg = new Background(bgColor);
092: bg.setApplicationBounds(bounds);
093: objScale.addChild(bg);
094:
095: Material m = new Material(objColor, eColor, objColor, sColor,
096: 100.0f);
097: Appearance a = new Appearance();
098: m.setLightingEnable(true);
099: a.setMaterial(m);
100: Font3D f3d = new Font3D(new Font("TestFont", Font.PLAIN, 1),
101: new FontExtrusion());
102: Text3D txt = new Text3D(f3d, new String("TEXT3D"), new Point3f(
103: -2.0f, 0.0f, 0.0f));
104: // txt.setCapability(Geometry.ALLOW_INTERSECT);
105: Shape3D s3D = new Shape3D();
106: s3D.setGeometry(txt);
107: s3D.setAppearance(a);
108:
109: // Create a transform group node and initialize it to the
110: // identity. Enable the TRANSFORM_WRITE capability so that
111: // our behavior code can modify it at runtime.
112: TransformGroup spinTg = new TransformGroup();
113: spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
114: spinTg.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
115: spinTg.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
116:
117: spinTg.addChild(s3D);
118: objScale.addChild(spinTg);
119:
120: // Create the transform group node for the each light and initialize
121: // it to the identity. Enable the TRANSFORM_WRITE capability so that
122: // our behavior code can modify it at runtime. Add them to the root
123: // of the subgraph.
124:
125: // Create transformations for the positional lights
126: t = new Transform3D();
127: Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
128: t.set(lPos1);
129: TransformGroup l1Trans = new TransformGroup(t);
130: l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
131: l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
132: l1Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
133: objScale.addChild(l1Trans);
134:
135: t = new Transform3D();
136: Vector3d lPos2 = new Vector3d(0.5, 0.8, 2.0);
137: t.set(lPos2);
138: TransformGroup l2Trans = new TransformGroup(t);
139: l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
140: l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
141: l2Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
142:
143: objScale.addChild(l2Trans);
144:
145: // Create Geometry for point lights
146: ColoringAttributes caL1 = new ColoringAttributes();
147: ColoringAttributes caL2 = new ColoringAttributes();
148: caL1.setColor(lColor1);
149: caL2.setColor(lColor2);
150: Appearance appL1 = new Appearance();
151: Appearance appL2 = new Appearance();
152: appL1.setColoringAttributes(caL1);
153: appL2.setColoringAttributes(caL2);
154: l1Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS, 15,
155: appL1));
156: l2Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS, 15,
157: appL2));
158:
159: // Create lights
160: AmbientLight aLgt = new AmbientLight(alColor);
161:
162: Light lgt1;
163: Light lgt2;
164:
165: Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
166: Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
167: lgt1 = new PointLight(lColor1, lPoint, atten);
168: lgt2 = new PointLight(lColor2, lPoint, atten);
169:
170: // Set the influencing bounds
171: aLgt.setInfluencingBounds(bounds);
172: lgt1.setInfluencingBounds(bounds);
173: lgt2.setInfluencingBounds(bounds);
174:
175: // Add the lights into the scene graph
176: objScale.addChild(aLgt);
177: l1Trans.addChild(lgt1);
178: l2Trans.addChild(lgt2);
179:
180: PickRotateBehavior behavior1 = new PickRotateBehavior(objRoot,
181: canvas, bounds);
182: behavior1.setMode(PickTool.BOUNDS);
183: objRoot.addChild(behavior1);
184:
185: PickZoomBehavior behavior2 = new PickZoomBehavior(objRoot,
186: canvas, bounds);
187: behavior2.setMode(PickTool.BOUNDS);
188: objRoot.addChild(behavior2);
189:
190: PickTranslateBehavior behavior3 = new PickTranslateBehavior(
191: objRoot, canvas, bounds);
192: behavior3.setMode(PickTool.BOUNDS);
193: objRoot.addChild(behavior3);
194:
195: // Let Java 3D perform optimizations on this scene graph.
196: objRoot.compile();
197:
198: return objRoot;
199: }
200:
201: public PickText3DBounds() {
202: }
203:
204: public void init() {
205: setLayout(new BorderLayout());
206: GraphicsConfiguration config = SimpleUniverse
207: .getPreferredConfiguration();
208:
209: Canvas3D c = new Canvas3D(config);
210: add("Center", c);
211:
212: u = new SimpleUniverse(c);
213: BranchGroup scene = createSceneGraph(c);
214:
215: // This will move the ViewPlatform back a bit so the
216: // objects in the scene can be viewed.
217: u.getViewingPlatform().setNominalViewingTransform();
218:
219: u.addBranchGraph(scene);
220: }
221:
222: public void destroy() {
223: u.cleanup();
224: }
225:
226: //
227: // The following allows Text3DMotion to be run as an application
228: // as well as an applet
229: //
230: public static void main(String[] args) {
231: new MainFrame(new PickText3DBounds(), 700, 700);
232: }
233: }
|