001: /*
002: * $RCSfile: PickText3DGeometry.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 PickText3DGeometry 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:
103: Text3D text3D = new Text3D(f3d, new String("TEXT3D"),
104: new Point3f(-2.0f, 0.7f, 0.0f));
105: text3D.setCapability(Geometry.ALLOW_INTERSECT);
106: Shape3D s3D1 = new Shape3D();
107: s3D1.setGeometry(text3D);
108: s3D1.setAppearance(a);
109:
110: // Create a transform group node and initialize it to the
111: // identity. Enable the TRANSFORM_WRITE capability so that
112: // our behavior code can modify it at runtime.
113: TransformGroup spinTg1 = new TransformGroup();
114: spinTg1.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
115: spinTg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
116: spinTg1.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
117:
118: spinTg1.addChild(s3D1);
119: objScale.addChild(spinTg1);
120:
121: Text3D pick = new Text3D(f3d, new String("Pick me"),
122: new Point3f(-2.0f, -0.7f, 0.0f));
123: pick.setCapability(Geometry.ALLOW_INTERSECT);
124: Shape3D s3D2 = new Shape3D();
125: s3D2.setGeometry(pick);
126: s3D2.setAppearance(a);
127:
128: // Create a transform group node and initialize it to the
129: // identity. Enable the TRANSFORM_WRITE capability so that
130: // our behavior code can modify it at runtime.
131: TransformGroup spinTg2 = new TransformGroup();
132: spinTg2.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
133: spinTg2.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
134: spinTg2.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
135:
136: spinTg2.addChild(s3D2);
137: objScale.addChild(spinTg2);
138:
139: // Create the transform group node for the each light and initialize
140: // it to the identity. Enable the TRANSFORM_WRITE capability so that
141: // our behavior code can modify it at runtime. Add them to the root
142: // of the subgraph.
143:
144: // Create transformations for the positional lights
145: t = new Transform3D();
146: Vector3d lPos1 = new Vector3d(0.0, 0.0, 2.0);
147: t.set(lPos1);
148: TransformGroup l1Trans = new TransformGroup(t);
149: l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
150: l1Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
151: l1Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
152: objScale.addChild(l1Trans);
153:
154: t = new Transform3D();
155: Vector3d lPos2 = new Vector3d(0.5, 1.2, 2.0);
156: t.set(lPos2);
157: TransformGroup l2Trans = new TransformGroup(t);
158: l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
159: l2Trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
160: l2Trans.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
161: objScale.addChild(l2Trans);
162:
163: // Create Geometry for point lights
164: ColoringAttributes caL1 = new ColoringAttributes();
165: ColoringAttributes caL2 = new ColoringAttributes();
166: caL1.setColor(lColor1);
167: caL2.setColor(lColor2);
168: Appearance appL1 = new Appearance();
169: Appearance appL2 = new Appearance();
170: appL1.setColoringAttributes(caL1);
171: appL2.setColoringAttributes(caL2);
172: l1Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS
173: | Sphere.ENABLE_GEOMETRY_PICKING, 15, appL1));
174: l2Trans.addChild(new Sphere(0.05f, Sphere.GENERATE_NORMALS
175: | Sphere.ENABLE_GEOMETRY_PICKING, 15, appL2));
176:
177: // Create lights
178: AmbientLight aLgt = new AmbientLight(alColor);
179:
180: Light lgt1;
181: Light lgt2;
182:
183: Point3f lPoint = new Point3f(0.0f, 0.0f, 0.0f);
184: Point3f atten = new Point3f(1.0f, 0.0f, 0.0f);
185: lgt1 = new PointLight(lColor1, lPoint, atten);
186: lgt2 = new PointLight(lColor2, lPoint, atten);
187:
188: // Set the influencing bounds
189: aLgt.setInfluencingBounds(bounds);
190: lgt1.setInfluencingBounds(bounds);
191: lgt2.setInfluencingBounds(bounds);
192:
193: // Add the lights into the scene graph
194: objScale.addChild(aLgt);
195: l1Trans.addChild(lgt1);
196: l2Trans.addChild(lgt2);
197:
198: PickRotateBehavior behavior1 = new PickRotateBehavior(objRoot,
199: canvas, bounds);
200: behavior1.setMode(PickTool.GEOMETRY);
201: behavior1.setTolerance(0.0f);
202: objRoot.addChild(behavior1);
203:
204: PickZoomBehavior behavior2 = new PickZoomBehavior(objRoot,
205: canvas, bounds);
206: behavior2.setMode(PickTool.GEOMETRY);
207: behavior2.setTolerance(0.0f);
208: objRoot.addChild(behavior2);
209:
210: PickTranslateBehavior behavior3 = new PickTranslateBehavior(
211: objRoot, canvas, bounds);
212: behavior3.setMode(PickTool.GEOMETRY);
213: behavior3.setTolerance(0.0f);
214: objRoot.addChild(behavior3);
215:
216: // Let Java 3D perform optimizations on this scene graph.
217: objRoot.compile();
218:
219: return objRoot;
220: }
221:
222: public PickText3DGeometry() {
223: }
224:
225: public void init() {
226: setLayout(new BorderLayout());
227: GraphicsConfiguration config = SimpleUniverse
228: .getPreferredConfiguration();
229: Canvas3D c = new Canvas3D(config);
230: add("Center", c);
231:
232: u = new SimpleUniverse(c);
233: BranchGroup scene = createSceneGraph(c);
234:
235: // This will move the ViewPlatform back a bit so the
236: // objects in the scene can be viewed.
237: u.getViewingPlatform().setNominalViewingTransform();
238:
239: u.addBranchGraph(scene);
240: }
241:
242: public void destroy() {
243: u.cleanup();
244: }
245:
246: //
247: // The following allows Text3DMotion to be run as an application
248: // as well as an applet
249: //
250: public static void main(String[] args) {
251: new MainFrame(new PickText3DGeometry(), 700, 700);
252: }
253: }
|