001: /*
002: * $RCSfile: OrientedPtTest.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.5 $
041: * $Date: 2007/04/24 18:55:58 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.oriented_shape3d;
046:
047: import java.applet.Applet;
048: import java.awt.BorderLayout;
049: import com.sun.j3d.utils.image.TextureLoader;
050: import com.sun.j3d.utils.applet.MainFrame;
051: import com.sun.j3d.utils.geometry.ColorCube;
052: import com.sun.j3d.utils.geometry.Cone;
053: import com.sun.j3d.utils.geometry.Cylinder;
054: import com.sun.j3d.utils.geometry.Sphere;
055: import com.sun.j3d.utils.universe.*;
056: import javax.media.j3d.*;
057: import javax.vecmath.*;
058: import java.awt.*;
059: import com.sun.j3d.utils.behaviors.vp.*;
060: import org.jdesktop.j3d.examples.Resources;
061:
062: public class OrientedPtTest extends Applet {
063:
064: // setup font stuff
065: private String fontName = "TestFont";
066: private String textString = "OrientedShape3D";
067: float sl = textString.length();
068:
069: // paths to texture image files
070: private java.net.URL earthImage = null;
071: private java.net.URL stoneImage = null;
072:
073: private SimpleUniverse u;
074:
075: public BranchGroup createSceneGraph() {
076:
077: // Create the root of the branch graph
078: BranchGroup objRoot = new BranchGroup();
079:
080: TransformGroup objScale = new TransformGroup();
081: Transform3D textMat = new Transform3D();
082: // Assuming uniform size chars, set scale to fit string in view
083: textMat.setScale(1.2 / sl);
084: objScale.setTransform(textMat);
085:
086: // Create the transform group node and initialize it to the
087: // identity. Enable the TRANSFORM_WRITE capability so that
088: // our behavior code can modify it at runtime. Add it to the
089: // root of the subgraph.
090: TransformGroup objTrans = new TransformGroup();
091: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
092: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
093: objRoot.addChild(objTrans);
094:
095: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
096: 0.0, 0.0), 100.0);
097:
098: Appearance apText = new Appearance();
099: Material m = new Material();
100: m.setLightingEnable(true);
101: apText.setMaterial(m);
102:
103: Appearance apEarth = new Appearance();
104: Material mm = new Material();
105: mm.setLightingEnable(true);
106: apEarth.setMaterial(mm);
107:
108: Appearance apStone = new Appearance();
109: apStone.setMaterial(mm);
110:
111: // create 3D text
112: Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2),
113: new FontExtrusion());
114: Point3f textPt = new Point3f(-sl / 2.0f, 3.0f, 0.0f);
115: Text3D txt = new Text3D(f3d, textString, textPt);
116: OrientedShape3D textShape = new OrientedShape3D();
117: textShape.setGeometry(txt);
118: textShape.setAppearance(apText);
119:
120: textShape.setAlignmentMode(OrientedShape3D.ROTATE_ABOUT_POINT);
121: // text is centered around 0, 3, 0. Make it rotate around 0,5,0
122: Point3f rotationPt = new Point3f(0.0f, 5.0f, 0.0f);
123: textShape.setRotationPoint(rotationPt);
124: objScale.addChild(textShape);
125:
126: // also add a small Sphere at the rotation point to
127: // show that we are rotating around the right point
128: Sphere sphere = new Sphere(0.2f);
129: TransformGroup sphereGroup = new TransformGroup();
130: Transform3D sphereXform = new Transform3D();
131: sphereXform.set(new Vector3f(rotationPt));
132: sphereGroup.setTransform(sphereXform);
133: sphereGroup.addChild(sphere);
134: objScale.addChild(sphereGroup);
135:
136: // Create a simple shape leaf node, add it to the scene graph.
137:
138: Transform3D cubeMat = new Transform3D();
139: TransformGroup cubeTrans = new TransformGroup(cubeMat);
140: cubeMat.set(new Vector3d(0.9, 0.0, -1.0));
141: cubeTrans.setTransform(cubeMat);
142: cubeTrans.addChild(new ColorCube(0.3));
143: objTrans.addChild(cubeTrans);
144:
145: TextureLoader stoneTex = new TextureLoader(stoneImage,
146: new String("RGB"), TextureLoader.BY_REFERENCE
147: | TextureLoader.Y_UP, this );
148: if (stoneTex != null)
149: apStone.setTexture(stoneTex.getTexture());
150:
151: TextureAttributes texAttr = new TextureAttributes();
152: texAttr.setTextureMode(TextureAttributes.REPLACE);
153: apStone.setTextureAttributes(texAttr);
154:
155: Transform3D coneMat = new Transform3D();
156: TransformGroup coneTrans = new TransformGroup(coneMat);
157: coneMat.set(new Vector3d(0.0, 0.0, 0.0));
158: coneTrans.setTransform(coneMat);
159: coneTrans.addChild(new Cone(.2f, 0.8f, Cone.GENERATE_NORMALS
160: | Cone.GENERATE_TEXTURE_COORDS
161: | Cone.GENERATE_TEXTURE_COORDS_Y_UP, apStone));
162: objTrans.addChild(coneTrans);
163:
164: TextureLoader earthTex = new TextureLoader(earthImage,
165: new String("RGB"), TextureLoader.BY_REFERENCE
166: | TextureLoader.Y_UP, this );
167: if (earthTex != null)
168: apEarth.setTexture(earthTex.getTexture());
169: apEarth.setTextureAttributes(texAttr);
170:
171: Transform3D cylinderMat = new Transform3D();
172: TransformGroup cylinderTrans = new TransformGroup(cylinderMat);
173: cylinderMat.set(new Vector3d(-0.9, 0.5, -1.0));
174: cylinderTrans.setTransform(cylinderMat);
175: cylinderTrans.addChild(new Cylinder(.35f, 2.0f,
176: Cylinder.GENERATE_NORMALS
177: | Cylinder.GENERATE_TEXTURE_COORDS
178: | Cylinder.GENERATE_TEXTURE_COORDS_Y_UP,
179: apEarth));
180: objTrans.addChild(cylinderTrans);
181:
182: objTrans.addChild(objScale);
183:
184: // Set up the background
185: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
186: Background bgNode = new Background(bgColor);
187: bgNode.setApplicationBounds(bounds);
188: objRoot.addChild(bgNode);
189:
190: // Set up the ambient light
191: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
192: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
193: ambientLightNode.setInfluencingBounds(bounds);
194: objRoot.addChild(ambientLightNode);
195:
196: // Set up the directional lights
197: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
198: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
199: Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
200: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
201:
202: DirectionalLight light1 = new DirectionalLight(light1Color,
203: light1Direction);
204: light1.setInfluencingBounds(bounds);
205: objRoot.addChild(light1);
206:
207: DirectionalLight light2 = new DirectionalLight(light2Color,
208: light2Direction);
209: light2.setInfluencingBounds(bounds);
210: objRoot.addChild(light2);
211:
212: apText.setMaterial(mm);
213:
214: // Have Java 3D perform optimizations on this scene graph.
215: objRoot.compile();
216:
217: return objRoot;
218: }
219:
220: public OrientedPtTest() {
221: }
222:
223: public OrientedPtTest(java.net.URL earthURL, java.net.URL stoneURL) {
224: earthImage = earthURL;
225: stoneImage = stoneURL;
226: }
227:
228: public void init() {
229: // the paths to the image files for an applet
230: earthImage = Resources
231: .getResource("resources/images/earth.jpg");
232: if (earthImage == null) {
233: System.err.println("resources/images/earth.jpg not found");
234: System.exit(1);
235: }
236:
237: stoneImage = Resources
238: .getResource("resources/images/stone.jpg");
239: if (stoneImage == null) {
240: System.err.println("resources/images/stone.jpg not found");
241: System.exit(1);
242: }
243:
244: setLayout(new BorderLayout());
245: Canvas3D c = new Canvas3D(SimpleUniverse
246: .getPreferredConfiguration());
247: add("Center", c);
248:
249: // Create a simple scene and attach it to the virtual universe
250: BranchGroup scene = createSceneGraph();
251: u = new SimpleUniverse(c);
252:
253: // add mouse behaviors to the ViewingPlatform
254: ViewingPlatform viewingPlatform = u.getViewingPlatform();
255:
256: // This will move the ViewPlatform back a bit so the
257: // objects in the scene can be viewed.
258: u.getViewingPlatform().setNominalViewingTransform();
259:
260: // add orbit behavior to the viewing platform
261: OrbitBehavior orbit = new OrbitBehavior(c,
262: OrbitBehavior.REVERSE_ALL);
263: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
264: 0.0, 0.0), 100.0);
265: orbit.setSchedulingBounds(bounds);
266: viewingPlatform.setViewPlatformBehavior(orbit);
267:
268: u.addBranchGraph(scene);
269: }
270:
271: public void destroy() {
272: u.cleanup();
273: }
274:
275: //
276: // The following allows OrientedPtTest to be run as an application
277: // as well as an applet
278: //
279: public static void main(String[] args) {
280: java.net.URL earthURL = null;
281: java.net.URL stoneURL = null;
282: earthURL = Resources.getResource("resources/images/earth.jpg");
283: if (earthURL == null) {
284: System.err.println("resources/images/earth.jpg not found");
285: System.exit(1);
286: }
287:
288: stoneURL = Resources.getResource("resources/images/stone.jpg");
289: if (stoneURL == null) {
290: System.err.println("resources/images/stone.jpg not found");
291: System.exit(1);
292: }
293:
294: new MainFrame(new OrientedPtTest(earthURL, stoneURL), 400, 400);
295: }
296: }
|