001: /*
002: * $RCSfile: Text3DLoad.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:53 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.text3d;
046:
047: import com.sun.j3d.loaders.objectfile.*;
048: import java.applet.Applet;
049: import java.awt.*;
050: import java.awt.event.*;
051: import com.sun.j3d.utils.applet.MainFrame;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import java.io.*;
056: import com.sun.j3d.utils.behaviors.vp.*;
057: import com.sun.j3d.utils.behaviors.keyboard.*;
058:
059: public class Text3DLoad extends Applet implements ActionListener {
060:
061: private String fontName = "TestFont";
062: private String textString = null;
063: private double tessellation = 0.0;
064:
065: private SimpleUniverse u;
066:
067: private Button button;
068: private boolean behaviorsOn = false;
069: private OrbitBehavior orbit;
070:
071: public BranchGroup createSceneGraph() {
072: float sl = textString.length();
073: // Create the root of the branch graph
074: BranchGroup objRoot = new BranchGroup();
075:
076: // Create a Transformgroup to scale all objects so they
077: // appear in the scene.
078: TransformGroup objScale = new TransformGroup();
079: Transform3D t3d = new Transform3D();
080: // Assuming uniform size chars, set scale to fit string in view
081: t3d.setScale(1.2 / sl);
082: objScale.setTransform(t3d);
083: objRoot.addChild(objScale);
084:
085: // Create the transform group node and initialize it to the
086: // identity. Enable the TRANSFORM_WRITE capability so that
087: // our behavior code can modify it at runtime. Add it to the
088: // root of the subgraph.
089: TransformGroup objTrans = new TransformGroup();
090: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
091: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
092: objScale.addChild(objTrans);
093:
094: Font3D f3d;
095: if (tessellation > 0.0) {
096: f3d = new Font3D(new Font(fontName, Font.PLAIN, 2),
097: tessellation, new FontExtrusion());
098: } else {
099: f3d = new Font3D(new Font(fontName, Font.PLAIN, 2),
100: new FontExtrusion());
101: }
102: Text3D txt = new Text3D(f3d, textString, new Point3f(
103: -sl / 2.0f, -1.f, -1.f));
104: Shape3D sh = new Shape3D();
105: Appearance app = new Appearance();
106: Material mm = new Material();
107: mm.setLightingEnable(true);
108: app.setMaterial(mm);
109: sh.setGeometry(txt);
110: sh.setAppearance(app);
111: objTrans.addChild(sh);
112:
113: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
114: 0.0, 0.0), 100.0);
115:
116: if (false) {
117: Transform3D yAxis = new Transform3D();
118: Alpha rotationAlpha = new Alpha(-1,
119: Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
120:
121: RotationInterpolator rotator = new RotationInterpolator(
122: rotationAlpha, objTrans, yAxis, 0.0f,
123: (float) Math.PI * 2.0f);
124: rotator.setSchedulingBounds(bounds);
125: objTrans.addChild(rotator);
126: }
127:
128: // Set up the background
129: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
130: Background bgNode = new Background(bgColor);
131: bgNode.setApplicationBounds(bounds);
132: objRoot.addChild(bgNode);
133:
134: // Set up the ambient light
135: Color3f ambientColor = new Color3f(0.3f, 0.3f, 0.3f);
136: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
137: ambientLightNode.setInfluencingBounds(bounds);
138: objRoot.addChild(ambientLightNode);
139:
140: // Set up the directional lights
141: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
142: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
143: Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
144: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
145:
146: DirectionalLight light1 = new DirectionalLight(light1Color,
147: light1Direction);
148: light1.setInfluencingBounds(bounds);
149: objRoot.addChild(light1);
150:
151: DirectionalLight light2 = new DirectionalLight(light2Color,
152: light2Direction);
153: light2.setInfluencingBounds(bounds);
154: objRoot.addChild(light2);
155:
156: return objRoot;
157: }
158:
159: private void usage() {
160: System.out
161: .println("Usage: java Text3DLoad [-f fontname] [-t tessellation] [<text>]");
162: System.exit(0);
163: } // End of usage
164:
165: public Text3DLoad() {
166: }
167:
168: public Text3DLoad(String args[]) {
169:
170: if (args.length == 0) {
171: // usage();
172: textString = "Java3D";
173: } else {
174: for (int i = 0; i < args.length; i++) {
175: if (args[i].startsWith("-")) {
176: if (args[i].equals("-f")) {
177: if (i < args.length - 1) {
178: fontName = args[++i];
179: } else {
180: usage();
181: }
182: } else if (args[i].equals("-t")) {
183: if (i < args.length - 1) {
184: tessellation = Double
185: .parseDouble(args[++i]);
186: } else {
187: usage();
188: }
189: } else {
190: System.err.println("Argument '" + args[i]
191: + "' ignored.");
192: }
193: } else {
194: textString = args[i];
195: }
196: }
197: }
198:
199: if (textString == null) {
200: usage();
201: }
202:
203: }
204:
205: public void init() {
206:
207: if (textString == null) {
208: textString = "Java3D";
209: }
210: setLayout(new BorderLayout());
211:
212: button = new Button("remove behaviors");
213: button.addActionListener(this );
214: Panel p = new Panel();
215: p.add(button);
216: add("South", p);
217:
218: GraphicsConfiguration config = SimpleUniverse
219: .getPreferredConfiguration();
220:
221: Canvas3D c = new Canvas3D(config);
222: add("Center", c);
223:
224: // Create a simple scene and attach it to the virtual universe
225: BranchGroup scene = createSceneGraph();
226:
227: // create a SimpleUniverse with 4 TransformGroups for the mouse
228: // behaviors
229: u = new SimpleUniverse(c);
230:
231: // add the behaviors to the ViewingPlatform
232: ViewingPlatform viewingPlatform = u.getViewingPlatform();
233:
234: viewingPlatform.setNominalViewingTransform();
235:
236: // add orbit behavior to ViewingPlatform
237: orbit = new OrbitBehavior(c, OrbitBehavior.REVERSE_ALL
238: | OrbitBehavior.STOP_ZOOM);
239: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
240: 0.0, 0.0), 100.0);
241: orbit.setSchedulingBounds(bounds);
242: viewingPlatform.setViewPlatformBehavior(orbit);
243:
244: behaviorsOn = true;
245:
246: u.addBranchGraph(scene);
247: }
248:
249: public void destroy() {
250: u.cleanup();
251: }
252:
253: public void actionPerformed(ActionEvent e) {
254: if (e.getSource() == button) {
255: ViewingPlatform v = u.getViewingPlatform();
256: if (behaviorsOn) {
257: v.setViewPlatformBehavior(null);
258: button.setLabel("add behaviors");
259: behaviorsOn = false;
260: } else {
261: v.setViewPlatformBehavior(orbit);
262: button.setLabel("remove behaviors");
263: behaviorsOn = true;
264: }
265: }
266: }
267:
268: //
269: // The following allows Text3DLoad to be run as an application
270: // as well as an applet
271: //
272: public static void main(String[] args) {
273: new MainFrame(new Text3DLoad(args), 700, 700);
274: }
275: }
|