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