001: /*
002: * $RCSfile: ObjLoadAxis.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:16:59 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.axisbehavior;
046:
047: import com.sun.j3d.loaders.objectfile.ObjectFile;
048: import com.sun.j3d.loaders.ParsingErrorException;
049: import com.sun.j3d.loaders.IncorrectFormatException;
050: import com.sun.j3d.loaders.Scene;
051: import java.applet.Applet;
052: import java.awt.*;
053: import java.awt.event.*;
054: import com.sun.j3d.utils.applet.MainFrame;
055: import com.sun.j3d.utils.universe.*;
056: import javax.media.j3d.*;
057: import javax.vecmath.*;
058: import java.io.*;
059: import com.sun.j3d.utils.behaviors.vp.*;
060: import org.jdesktop.j3d.utils.behaviors.vp.AxisBehavior;
061: import java.net.URL;
062: import java.net.MalformedURLException;
063:
064: public class ObjLoadAxis extends Applet {
065:
066: private boolean spin = false;
067: private boolean noTriangulate = false;
068: private boolean noStripify = false;
069: private double creaseAngle = 60.0;
070: private URL filename = null;
071: private SimpleUniverse u;
072: private BoundingSphere bounds;
073:
074: public BranchGroup createSceneGraph() {
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.7);
083: objScale.setTransform(t3d);
084: objRoot.addChild(objScale);
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: objScale.addChild(objTrans);
094:
095: int flags = ObjectFile.RESIZE;
096: if (!noTriangulate)
097: flags |= ObjectFile.TRIANGULATE;
098: if (!noStripify)
099: flags |= ObjectFile.STRIPIFY;
100: ObjectFile f = new ObjectFile(flags, (float) (creaseAngle
101: * Math.PI / 180.0));
102: Scene s = null;
103: try {
104: s = f.load(filename);
105: } catch (FileNotFoundException e) {
106: System.err.println(e);
107: System.exit(1);
108: } catch (ParsingErrorException e) {
109: System.err.println(e);
110: System.exit(1);
111: } catch (IncorrectFormatException e) {
112: System.err.println(e);
113: System.exit(1);
114: }
115:
116: objTrans.addChild(s.getSceneGroup());
117:
118: bounds = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
119:
120: if (spin) {
121: Transform3D yAxis = new Transform3D();
122: Alpha rotationAlpha = new Alpha(-1,
123: Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
124:
125: RotationInterpolator rotator = new RotationInterpolator(
126: rotationAlpha, objTrans, yAxis, 0.0f,
127: (float) Math.PI * 2.0f);
128: rotator.setSchedulingBounds(bounds);
129: objTrans.addChild(rotator);
130: }
131:
132: // Set up the background
133: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
134: Background bgNode = new Background(bgColor);
135: bgNode.setApplicationBounds(bounds);
136: objRoot.addChild(bgNode);
137:
138: return objRoot;
139: }
140:
141: private void usage() {
142: System.out
143: .println("Usage: java ObjLoadAxis [-s] [-n] [-t] [-c degrees] <.obj file>");
144: System.out.println(" -s Spin (no user interaction)");
145: System.out.println(" -n No triangulation");
146: System.out.println(" -t No stripification");
147: System.out
148: .println(" -c Set crease angle for normal generation (default is 60 without");
149: System.out
150: .println(" smoothing group info, otherwise 180 within smoothing groups)");
151: System.exit(0);
152: } // End of usage
153:
154: public void init() {
155: if (filename == null) {
156: // Applet
157: try {
158: URL path = getCodeBase();
159: filename = new URL(path.toString() + "./galleon.obj");
160: } catch (MalformedURLException e) {
161: System.err.println(e);
162: System.exit(1);
163: }
164: }
165:
166: setLayout(new BorderLayout());
167: GraphicsConfiguration config = SimpleUniverse
168: .getPreferredConfiguration();
169:
170: Canvas3D c = new Canvas3D(config);
171: add("Center", c);
172:
173: // Create a simple scene and attach it to the virtual universe
174: BranchGroup scene = createSceneGraph();
175: u = new SimpleUniverse(c);
176:
177: // Limit updates to 100 fps
178: View view = u.getViewer().getView();
179: view.setMinimumFrameCycleTime(10);
180:
181: // add mouse behaviors to the ViewingPlatform
182: ViewingPlatform viewingPlatform = u.getViewingPlatform();
183:
184: PlatformGeometry pg = new PlatformGeometry();
185:
186: // Set up the ambient light
187: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
188: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
189: ambientLightNode.setInfluencingBounds(bounds);
190: pg.addChild(ambientLightNode);
191:
192: // Set up the directional lights
193: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
194: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
195: Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
196: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
197:
198: DirectionalLight light1 = new DirectionalLight(light1Color,
199: light1Direction);
200: light1.setInfluencingBounds(bounds);
201: pg.addChild(light1);
202:
203: DirectionalLight light2 = new DirectionalLight(light2Color,
204: light2Direction);
205: light2.setInfluencingBounds(bounds);
206: pg.addChild(light2);
207:
208: //
209: // Create axis with associated behavior
210: //
211: BranchGroup axisRoot = new BranchGroup();
212:
213: // Position the axis
214: Transform3D t = new Transform3D();
215: t.set(new Vector3d(-0.5, -0.5, -1.5));
216: TransformGroup axisTranslation = new TransformGroup(t);
217: axisRoot.addChild(axisTranslation);
218:
219: // Create transform group to orient the axis and make it
220: // readable & writable (this will be the target of the axis
221: // behavior)
222: TransformGroup axisTG = new TransformGroup();
223: axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
224: axisTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
225: axisTranslation.addChild(axisTG);
226:
227: // Create the axis geometry
228: Axis axis = new Axis();
229: axisTG.addChild(axis);
230:
231: // Add axis into BG
232: pg.addChild(axisRoot);
233:
234: // Create the axis behavior
235: TransformGroup viewPlatformTG = viewingPlatform
236: .getViewPlatformTransform();
237: AxisBehavior axisBehavior = new AxisBehavior(axisTG,
238: viewPlatformTG);
239: axisBehavior.setSchedulingBounds(bounds);
240: pg.addChild(axisBehavior);
241:
242: viewingPlatform.setPlatformGeometry(pg);
243:
244: // This will move the ViewPlatform back a bit so the
245: // objects in the scene can be viewed.
246: viewingPlatform.setNominalViewingTransform();
247:
248: if (true /*!spin*/) {
249: OrbitBehavior orbit = new OrbitBehavior(c,
250: OrbitBehavior.REVERSE_ALL);
251: orbit.setSchedulingBounds(bounds);
252: viewingPlatform.setViewPlatformBehavior(orbit);
253: }
254:
255: u.addBranchGraph(scene);
256: }
257:
258: // Caled if running as a program
259: public ObjLoadAxis(String[] args) {
260: if (args.length != 0) {
261: for (int i = 0; i < args.length; i++) {
262: if (args[i].startsWith("-")) {
263: if (args[i].equals("-s")) {
264: spin = true;
265: } else if (args[i].equals("-n")) {
266: noTriangulate = true;
267: } else if (args[i].equals("-t")) {
268: noStripify = true;
269: } else if (args[i].equals("-c")) {
270: if (i < args.length - 1) {
271: creaseAngle = (new Double(args[++i]))
272: .doubleValue();
273: } else
274: usage();
275: } else {
276: usage();
277: }
278: } else {
279: try {
280: if ((args[i].indexOf("file:") == 0)
281: || (args[i].indexOf("http") == 0)) {
282: filename = new URL(args[i]);
283: } else if (args[i].charAt(0) != '/') {
284: filename = new URL("file:./" + args[i]);
285: } else {
286: filename = new URL("file:" + args[i]);
287: }
288: } catch (MalformedURLException e) {
289: System.err.println(e);
290: System.exit(1);
291: }
292: }
293: }
294: }
295: }
296:
297: // Running as an applet
298: public ObjLoadAxis() {
299: }
300:
301: public void destroy() {
302: u.cleanup();
303: }
304:
305: //
306: // The following allows ObjLoadAxis to be run as an application
307: // as well as an applet
308: //
309: public static void main(String[] args) {
310: new MainFrame(new ObjLoadAxis(args), 700, 700);
311: }
312:
313: static class Axis extends Group {
314: private static final float[] axisCoords = { 0.0f, 0.0f, 0.0f,
315: 0.10f, 0.00f, 0.00f, // X-axis
316: 0.1f, 0.0f, 0.0f, 0.09f, 0.01f, 0.00f, // X-axis arrow
317: 0.1f, 0.0f, 0.0f, 0.09f, -0.01f, 0.00f, // X-axis arrow
318: 0.1f, 0.0f, 0.0f, 0.09f, 0.00f, 0.01f, // X-axis arrow
319: 0.1f, 0.0f, 0.0f, 0.09f, 0.00f, -0.01f, // X-axis arrow
320: };
321:
322: Axis() {
323: Transform3D t = new Transform3D();
324:
325: // Turn off face culling so we can see the back side of the labels
326: // (since we're not using font extrusion)
327: PolygonAttributes polygonAttributes = new PolygonAttributes();
328: polygonAttributes.setCullFace(PolygonAttributes.CULL_NONE);
329:
330: // Make the axis lines 2 pixels wide
331: LineAttributes lineAttributes = new LineAttributes();
332: lineAttributes.setLineWidth(2.0f);
333:
334: // Create red appearance for X-axis
335: ColoringAttributes redColoringAttributes = new ColoringAttributes();
336: redColoringAttributes
337: .setColor(new Color3f(1.0f, 0.0f, 0.0f));
338: Appearance redAppearance = new Appearance();
339: redAppearance.setColoringAttributes(redColoringAttributes);
340: redAppearance.setPolygonAttributes(polygonAttributes);
341: redAppearance.setLineAttributes(lineAttributes);
342:
343: // Create green appearance for Y-axis
344: ColoringAttributes greenColoringAttributes = new ColoringAttributes();
345: greenColoringAttributes.setColor(new Color3f(0.0f, 1.0f,
346: 0.0f));
347: Appearance greenAppearance = new Appearance();
348: greenAppearance
349: .setColoringAttributes(greenColoringAttributes);
350: greenAppearance.setPolygonAttributes(polygonAttributes);
351: greenAppearance.setLineAttributes(lineAttributes);
352:
353: // Create blue appearance for Z-axis
354: ColoringAttributes blueColoringAttributes = new ColoringAttributes();
355: blueColoringAttributes.setColor(new Color3f(0.0f, 0.0f,
356: 1.0f));
357: Appearance blueAppearance = new Appearance();
358: blueAppearance
359: .setColoringAttributes(blueColoringAttributes);
360: blueAppearance.setPolygonAttributes(polygonAttributes);
361: blueAppearance.setLineAttributes(lineAttributes);
362:
363: // Define 3D font
364: Font font = new Font(null, Font.PLAIN, 2);
365: FontExtrusion extrude = null;
366: Font3D f3d = new Font3D(font, extrude);
367:
368: //
369: // Build X-axis (red)
370: //
371:
372: t = new Transform3D();
373: TransformGroup xAxisTG = new TransformGroup(t); // Identity transform
374:
375: // X-axis lines
376: LineArray xAxisLineArr = new LineArray(10,
377: GeometryArray.COORDINATES);
378: xAxisLineArr.setCoordinates(0, axisCoords);
379: Shape3D xAxisLines = new Shape3D(xAxisLineArr,
380: redAppearance);
381: xAxisTG.addChild(xAxisLines);
382:
383: // X-axis label
384: Text3D xAxisText = new Text3D(f3d, "+X");
385: Shape3D xAxisLabel = new Shape3D(xAxisText, redAppearance);
386: Transform3D xTextScale = new Transform3D();
387: xTextScale.set(0.015);
388: xTextScale.setTranslation(new Vector3d(0.11, 0.0, 0.0));
389: TransformGroup xAxisLabelTG = new TransformGroup(xTextScale);
390: xAxisLabelTG.addChild(xAxisLabel);
391: xAxisTG.addChild(xAxisLabelTG);
392:
393: this .addChild(xAxisTG);
394:
395: //
396: // Build Y-axis (green)
397: //
398:
399: t = new Transform3D();
400: t.rotZ(Math.PI / 2.0); // rotate about Z-axis to create Y-axis
401: TransformGroup yAxisTG = new TransformGroup(t);
402:
403: // Y-axis lines
404: LineArray yAxisLineArr = new LineArray(10,
405: GeometryArray.COORDINATES);
406: yAxisLineArr.setCoordinates(0, axisCoords);
407: Shape3D yAxisLines = new Shape3D(yAxisLineArr,
408: greenAppearance);
409: yAxisTG.addChild(yAxisLines);
410:
411: // Y-axis label
412: Text3D yAxisText = new Text3D(f3d, "+Y");
413: Shape3D yAxisLabel = new Shape3D(yAxisText, greenAppearance);
414: Transform3D yTextScale = new Transform3D();
415: yTextScale.set(0.015);
416: yTextScale.setTranslation(new Vector3d(0.11, 0.0, 0.0));
417: TransformGroup yAxisLabelTG = new TransformGroup(yTextScale);
418: yAxisLabelTG.addChild(yAxisLabel);
419: yAxisTG.addChild(yAxisLabelTG);
420:
421: this .addChild(yAxisTG);
422:
423: //
424: // Build Z-axis (blue)
425: //
426:
427: t = new Transform3D();
428: t.rotY(-Math.PI / 2.0); // rotate about Y-axis to create Z-axis
429: TransformGroup zAxisTG = new TransformGroup(t);
430:
431: // Z-axis lines
432: LineArray zAxisLineArr = new LineArray(10,
433: GeometryArray.COORDINATES);
434: zAxisLineArr.setCoordinates(0, axisCoords);
435: Shape3D zAxisLines = new Shape3D(zAxisLineArr,
436: blueAppearance);
437: zAxisTG.addChild(zAxisLines);
438:
439: // Z-axis label
440: Text3D zAxisText = new Text3D(f3d, "+Z");
441: Shape3D zAxisLabel = new Shape3D(zAxisText, blueAppearance);
442: Transform3D zTextScale = new Transform3D();
443: zTextScale.set(0.015);
444: zTextScale.setTranslation(new Vector3d(0.11, 0.0, 0.0));
445: TransformGroup zAxisLabelTG = new TransformGroup(zTextScale);
446: zAxisLabelTG.addChild(zAxisLabel);
447: zAxisTG.addChild(zAxisLabelTG);
448:
449: this.addChild(zAxisTG);
450:
451: }
452: }
453:
454: }
|