001: /*
002: * $RCSfile: ObjLoad.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.4 $
041: * $Date: 2007/02/09 17:21:45 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.objload;
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:
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import java.awt.GraphicsConfiguration;
056: import java.net.MalformedURLException;
057: import java.net.URL;
058: import com.sun.j3d.utils.behaviors.vp.*;
059: import java.io.FileNotFoundException;
060: import org.jdesktop.j3d.examples.Resources;
061:
062: /**
063: * Simple Java 3D example program to display an .obj object.
064: */
065: public class ObjLoad extends javax.swing.JFrame {
066:
067: private boolean spin = false;
068: private boolean noTriangulate = false;
069: private boolean noStripify = false;
070: private double creaseAngle = 60.0;
071: private URL filename = null;
072:
073: private SimpleUniverse univ = null;
074: private BranchGroup scene = null;
075:
076: public BranchGroup createSceneGraph() {
077: // Create the root of the branch graph
078: BranchGroup objRoot = new BranchGroup();
079:
080: // Create a Transformgroup to scale all objects so they
081: // appear in the scene.
082: TransformGroup objScale = new TransformGroup();
083: Transform3D t3d = new Transform3D();
084: t3d.setScale(0.7);
085: objScale.setTransform(t3d);
086: objRoot.addChild(objScale);
087:
088: // Create the transform group node and initialize it to the
089: // identity. Enable the TRANSFORM_WRITE capability so that
090: // our behavior code can modify it at runtime. Add it to the
091: // root of the subgraph.
092: TransformGroup objTrans = new TransformGroup();
093: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
094: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
095: objScale.addChild(objTrans);
096:
097: int flags = ObjectFile.RESIZE;
098: if (!noTriangulate)
099: flags |= ObjectFile.TRIANGULATE;
100: if (!noStripify)
101: flags |= ObjectFile.STRIPIFY;
102: ObjectFile f = new ObjectFile(flags, (float) (creaseAngle
103: * Math.PI / 180.0));
104: Scene s = null;
105: try {
106: s = f.load(filename);
107: } catch (FileNotFoundException e) {
108: System.err.println(e);
109: System.exit(1);
110: } catch (ParsingErrorException e) {
111: System.err.println(e);
112: System.exit(1);
113: } catch (IncorrectFormatException e) {
114: System.err.println(e);
115: System.exit(1);
116: }
117:
118: objTrans.addChild(s.getSceneGroup());
119:
120: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
121: 0.0, 0.0), 100.0);
122:
123: if (spin) {
124: Transform3D yAxis = new Transform3D();
125: Alpha rotationAlpha = new Alpha(-1,
126: Alpha.INCREASING_ENABLE, 0, 0, 4000, 0, 0, 0, 0, 0);
127:
128: RotationInterpolator rotator = new RotationInterpolator(
129: rotationAlpha, objTrans, yAxis, 0.0f,
130: (float) Math.PI * 2.0f);
131: rotator.setSchedulingBounds(bounds);
132: objTrans.addChild(rotator);
133: }
134:
135: // Set up the background
136: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
137: Background bgNode = new Background(bgColor);
138: bgNode.setApplicationBounds(bounds);
139: objRoot.addChild(bgNode);
140:
141: return objRoot;
142: }
143:
144: private Canvas3D createUniverse() {
145: // Get the preferred graphics configuration for the default screen
146: GraphicsConfiguration config = SimpleUniverse
147: .getPreferredConfiguration();
148:
149: // Create a Canvas3D using the preferred configuration
150: Canvas3D canvas3d = new Canvas3D(config);
151:
152: // Create simple universe with view branch
153: univ = new SimpleUniverse(canvas3d);
154: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
155: 0.0, 0.0), 100.0);
156:
157: // add mouse behaviors to the ViewingPlatform
158: ViewingPlatform viewingPlatform = univ.getViewingPlatform();
159:
160: PlatformGeometry pg = new PlatformGeometry();
161:
162: // Set up the ambient light
163: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
164: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
165: ambientLightNode.setInfluencingBounds(bounds);
166: pg.addChild(ambientLightNode);
167:
168: // Set up the directional lights
169: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
170: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
171: Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
172: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
173:
174: DirectionalLight light1 = new DirectionalLight(light1Color,
175: light1Direction);
176: light1.setInfluencingBounds(bounds);
177: pg.addChild(light1);
178:
179: DirectionalLight light2 = new DirectionalLight(light2Color,
180: light2Direction);
181: light2.setInfluencingBounds(bounds);
182: pg.addChild(light2);
183:
184: viewingPlatform.setPlatformGeometry(pg);
185:
186: // This will move the ViewPlatform back a bit so the
187: // objects in the scene can be viewed.
188: viewingPlatform.setNominalViewingTransform();
189:
190: if (!spin) {
191: OrbitBehavior orbit = new OrbitBehavior(canvas3d,
192: OrbitBehavior.REVERSE_ALL);
193: orbit.setSchedulingBounds(bounds);
194: viewingPlatform.setViewPlatformBehavior(orbit);
195: }
196:
197: // Ensure at least 5 msec per frame (i.e., < 200Hz)
198: univ.getViewer().getView().setMinimumFrameCycleTime(5);
199:
200: return canvas3d;
201: }
202:
203: private void usage() {
204: System.out
205: .println("Usage: java ObjLoad [-s] [-n] [-t] [-c degrees] <.obj file>");
206: System.out.println(" -s Spin (no user interaction)");
207: System.out.println(" -n No triangulation");
208: System.out.println(" -t No stripification");
209: System.out
210: .println(" -c Set crease angle for normal generation (default is 60 without");
211: System.out
212: .println(" smoothing group info, otherwise 180 within smoothing groups)");
213: System.exit(0);
214: } // End of usage
215:
216: /**
217: * Creates new form ObjLoad
218: */
219: public ObjLoad(String args[]) {
220: if (args.length != 0) {
221: for (int i = 0; i < args.length; i++) {
222: if (args[i].startsWith("-")) {
223: if (args[i].equals("-s")) {
224: spin = true;
225: } else if (args[i].equals("-n")) {
226: noTriangulate = true;
227: } else if (args[i].equals("-t")) {
228: noStripify = true;
229: } else if (args[i].equals("-c")) {
230: if (i < args.length - 1) {
231: creaseAngle = (new Double(args[++i]))
232: .doubleValue();
233: } else
234: usage();
235: } else {
236: usage();
237: }
238: } else {
239: try {
240: if ((args[i].indexOf("file:") == 0)
241: || (args[i].indexOf("http") == 0)) {
242: filename = new URL(args[i]);
243: } else if (args[i].charAt(0) != '/') {
244: filename = new URL("file:./" + args[i]);
245: } else {
246: filename = new URL("file:" + args[i]);
247: }
248: } catch (MalformedURLException e) {
249: System.err.println(e);
250: System.exit(1);
251: }
252: }
253: }
254: }
255:
256: if (filename == null) {
257: filename = Resources
258: .getResource("resources/geometry/galleon.obj");
259: if (filename == null) {
260: System.err
261: .println("resources/geometry/galleon.obj not found");
262: System.exit(1);
263: }
264: }
265:
266: // Initialize the GUI components
267: initComponents();
268:
269: // Create Canvas3D and SimpleUniverse; add canvas to drawing panel
270: Canvas3D c = createUniverse();
271: drawingPanel.add(c, java.awt.BorderLayout.CENTER);
272:
273: // Create the content branch and add it to the universe
274: scene = createSceneGraph();
275: univ.addBranchGraph(scene);
276: }
277:
278: // ----------------------------------------------------------------
279:
280: /** This method is called from within the constructor to
281: * initialize the form.
282: * WARNING: Do NOT modify this code. The content of this method is
283: * always regenerated by the Form Editor.
284: */
285: // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
286: private void initComponents() {
287: drawingPanel = new javax.swing.JPanel();
288:
289: setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
290: setTitle("ObjLoad");
291: drawingPanel.setLayout(new java.awt.BorderLayout());
292:
293: drawingPanel.setPreferredSize(new java.awt.Dimension(700, 700));
294: getContentPane()
295: .add(drawingPanel, java.awt.BorderLayout.CENTER);
296:
297: pack();
298: }// </editor-fold>//GEN-END:initComponents
299:
300: /**
301: * @param args the command line arguments
302: */
303: public static void main(final String args[]) {
304: java.awt.EventQueue.invokeLater(new Runnable() {
305: public void run() {
306: ObjLoad objLoad = new ObjLoad(args);
307: objLoad.setVisible(true);
308: }
309: });
310: }
311:
312: // Variables declaration - do not modify//GEN-BEGIN:variables
313: private javax.swing.JPanel drawingPanel;
314: // End of variables declaration//GEN-END:variables
315:
316: }
|