001: /*
002: * $RCSfile: DistortGlyphTest.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.3 $
041: * $Date: 2007/02/09 17:21:36 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.distort_glyph;
046:
047: import com.sun.j3d.utils.behaviors.mouse.*;
048: import com.sun.j3d.utils.image.TextureLoader;
049: import com.sun.j3d.utils.universe.*;
050: import java.awt.Font;
051: import java.awt.GraphicsConfiguration;
052: import java.awt.GraphicsEnvironment;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import org.jdesktop.j3d.examples.Resources;
056:
057: public class DistortGlyphTest extends javax.swing.JFrame {
058:
059: private SimpleUniverse univ = null;
060: private BranchGroup scene = null;
061:
062: // get a nice graphics config
063: private static GraphicsConfiguration getGraphicsConfig() {
064: GraphicsConfigTemplate3D template = new GraphicsConfigTemplate3D();
065: template
066: .setSceneAntialiasing(GraphicsConfigTemplate3D.PREFERRED);
067: GraphicsConfiguration gcfg = GraphicsEnvironment
068: .getLocalGraphicsEnvironment().getDefaultScreenDevice()
069: .getBestConfiguration(template);
070: return gcfg;
071: }
072:
073: private void setupLights(BranchGroup root) {
074: // set up the BoundingSphere for all the lights
075: BoundingSphere bounds = new BoundingSphere(new Point3d(), 100.0);
076:
077: // Set up the ambient light
078: AmbientLight lightAmbient = new AmbientLight(new Color3f(0.37f,
079: 0.37f, 0.37f));
080: lightAmbient.setInfluencingBounds(bounds);
081: root.addChild(lightAmbient);
082:
083: // Set up the directional light
084: Vector3f lightDirection1 = new Vector3f(0.0f, 0.0f, -1.0f);
085: DirectionalLight lightDirectional1 = new DirectionalLight(
086: new Color3f(1.00f, 0.10f, 0.00f), lightDirection1);
087: lightDirectional1.setInfluencingBounds(bounds);
088: lightDirectional1.setCapability(Light.ALLOW_STATE_WRITE);
089: root.addChild(lightDirectional1);
090:
091: Point3f lightPos1 = new Point3f(-4.0f, 8.0f, 16.0f);
092: Point3f lightAttenuation1 = new Point3f(1.0f, 0.0f, 0.0f);
093: PointLight pointLight1 = new PointLight(new Color3f(0.37f,
094: 1.00f, 0.37f), lightPos1, lightAttenuation1);
095: pointLight1.setInfluencingBounds(bounds);
096: root.addChild(pointLight1);
097:
098: Point3f lightPos2 = new Point3f(-16.0f, 8.0f, 4.0f);
099: Point3f lightAttenuation2 = new Point3f(1.0f, 0.0f, 0.0f);
100: PointLight pointLight2 = new PointLight(new Color3f(0.37f,
101: 0.37f, 1.00f), lightPos2, lightAttenuation2);
102: pointLight2.setInfluencingBounds(bounds);
103: root.addChild(pointLight2);
104: }
105:
106: public BranchGroup createSceneGraph() {
107: // Create the root of the branch graph
108: BranchGroup objRoot = new BranchGroup();
109:
110: setupLights(objRoot);
111:
112: TransformGroup objTransform = new TransformGroup();
113: objTransform
114: .setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
115: objTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
116:
117: objRoot.addChild(objTransform);
118:
119: // setup a nice textured appearance
120: Appearance app = new Appearance();
121: Color3f objColor = new Color3f(1.0f, 0.7f, 0.8f);
122: Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
123: app.setMaterial(new Material(objColor, black, objColor, black,
124: 80.0f));
125: Texture txtr = new TextureLoader(Resources
126: .getResource("resources/images/gold.jpg"), this )
127: .getTexture();
128: app.setTexture(txtr);
129: TexCoordGeneration tcg = new TexCoordGeneration(
130: TexCoordGeneration.SPHERE_MAP,
131: TexCoordGeneration.TEXTURE_COORDINATE_2);
132: app.setTexCoordGeneration(tcg);
133:
134: // use a customized FontExtrusion object to control the depth of the text
135: java.awt.geom.GeneralPath gp = new java.awt.geom.GeneralPath();
136: gp.moveTo(0, 0);
137: gp.lineTo(.01f, .01f);
138: gp.lineTo(.2f, .01f);
139: gp.lineTo(.21f, 0f);
140: FontExtrusion fontEx = new FontExtrusion(gp);
141:
142: // our glyph
143: Font fnt = new Font("dialog", Font.BOLD, 1);
144: Font3D f3d = new Font3D(fnt, .001, fontEx);
145: GeometryArray geom = f3d.getGlyphGeometry('A');
146: Shape3D shape = new Shape3D(geom, app);
147: objTransform.addChild(shape);
148:
149: // the DistortBehavior
150: DistortBehavior eb = new DistortBehavior(shape, 1000, 1000);
151: eb.setSchedulingBounds(new BoundingSphere());
152: objTransform.addChild(eb);
153:
154: MouseRotate myMouseRotate = new MouseRotate();
155: myMouseRotate.setTransformGroup(objTransform);
156: myMouseRotate.setSchedulingBounds(new BoundingSphere());
157: objRoot.addChild(myMouseRotate);
158:
159: MouseTranslate myMouseTranslate = new MouseTranslate();
160: myMouseTranslate.setTransformGroup(objTransform);
161: myMouseTranslate.setSchedulingBounds(new BoundingSphere());
162: objRoot.addChild(myMouseTranslate);
163:
164: MouseZoom myMouseZoom = new MouseZoom();
165: myMouseZoom.setTransformGroup(objTransform);
166: myMouseZoom.setSchedulingBounds(new BoundingSphere());
167: objRoot.addChild(myMouseZoom);
168:
169: // Let Java 3D perform optimizations on this scene graph.
170: objRoot.compile();
171:
172: return objRoot;
173: }
174:
175: private Canvas3D createUniverse() {
176:
177: // Create a Canvas3D using a nice configuration
178: Canvas3D c = new Canvas3D(getGraphicsConfig());
179:
180: // Create simple universe with view branch
181: univ = new SimpleUniverse(c);
182:
183: // This will move the ViewPlatform back a bit so the
184: // objects in the scene can be viewed.
185: univ.getViewingPlatform().setNominalViewingTransform();
186:
187: // Ensure at least 5 msec per frame (i.e., < 200Hz)
188: univ.getViewer().getView().setMinimumFrameCycleTime(5);
189:
190: return c;
191: }
192:
193: /**
194: * Creates new form DistortGlyphTest2
195: */
196: public DistortGlyphTest() {
197: // Initialize the GUI components
198: initComponents();
199:
200: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
201: Canvas3D c = createUniverse();
202: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
203:
204: // Create the content branch and add it to the universe
205: scene = createSceneGraph();
206: univ.addBranchGraph(scene);
207: }
208:
209: // ----------------------------------------------------------------
210:
211: /** This method is called from within the constructor to
212: * initialize the form.
213: * WARNING: Do NOT modify this code. The content of this method is
214: * always regenerated by the Form Editor.
215: */
216: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
217: private void initComponents() {
218: drawingPanel = new javax.swing.JPanel();
219:
220: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
221: setTitle("DistortGlyphTest");
222: drawingPanel.setLayout(new java.awt.BorderLayout());
223:
224: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
225: getContentPane()
226: .add(drawingPanel, java.awt.BorderLayout.CENTER);
227:
228: pack();
229: }// </editor-fold>//GEN-END:initComponents
230:
231: /**
232: * @param args the command line arguments
233: */
234: public static void main(String args[]) {
235: java.awt.EventQueue.invokeLater(new Runnable() {
236: public void run() {
237: new DistortGlyphTest().setVisible(true);
238: }
239: });
240: }
241:
242: // Variables declaration - do not modify//GEN-BEGIN:variables
243: private javax.swing.JPanel drawingPanel;
244: // End of variables declaration//GEN-END:variables
245:
246: }
|