001: /*
002: * $RCSfile: ConfigObjLoad.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/02/09 17:21:34 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.configured_universe;
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.awt.event.*;
052: import com.sun.j3d.utils.universe.*;
053: import javax.media.j3d.*;
054: import javax.vecmath.*;
055: import java.io.*;
056: import com.sun.j3d.utils.behaviors.vp.*;
057: import com.sun.j3d.utils.behaviors.sensor.Mouse6DPointerBehavior;
058: import java.net.URL;
059: import java.net.MalformedURLException;
060: import java.util.Map;
061: import org.jdesktop.j3d.examples.Resources;
062:
063: public class ConfigObjLoad {
064:
065: private boolean spin = false;
066: private boolean noTriangulate = false;
067: private boolean noStripify = false;
068: private double creaseAngle = 60.0;
069: private URL filename = null;
070:
071: private ConfiguredUniverse u;
072:
073: public BranchGroup createSceneGraph() {
074: // Create the root of the branch graph
075: BranchGroup objRoot = new BranchGroup();
076:
077: // Create a Transformgroup to scale all objects so they
078: // appear in the scene.
079: TransformGroup objScale = new TransformGroup();
080: Transform3D t3d = new Transform3D();
081: t3d.setScale(0.7);
082: objScale.setTransform(t3d);
083: objRoot.addChild(objScale);
084:
085: // Create the transform group node and initialize it to the
086: // identity. Enable the TRANSFORM_WRITE capability so that
087: // our behavior code can modify it at runtime. Add it to the
088: // root of the subgraph.
089: TransformGroup objTrans = new TransformGroup();
090: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
091: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
092: objScale.addChild(objTrans);
093:
094: int flags = ObjectFile.RESIZE;
095: if (!noTriangulate)
096: flags |= ObjectFile.TRIANGULATE;
097: if (!noStripify)
098: flags |= ObjectFile.STRIPIFY;
099: ObjectFile f = new ObjectFile(flags, (float) (creaseAngle
100: * Math.PI / 180.0));
101: Scene s = null;
102: try {
103: s = f.load(filename);
104: } catch (FileNotFoundException e) {
105: System.err.println(e);
106: System.exit(1);
107: } catch (ParsingErrorException e) {
108: System.err.println(e);
109: System.exit(1);
110: } catch (IncorrectFormatException e) {
111: System.err.println(e);
112: System.exit(1);
113: }
114:
115: objTrans.addChild(s.getSceneGroup());
116:
117: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
118: 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: // Set up the ambient light
139: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
140: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
141: ambientLightNode.setInfluencingBounds(bounds);
142: objRoot.addChild(ambientLightNode);
143:
144: // Set up the directional lights
145: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
146: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
147: Color3f light2Color = new Color3f(1.0f, 1.0f, 1.0f);
148: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
149:
150: DirectionalLight light1 = new DirectionalLight(light1Color,
151: light1Direction);
152: light1.setInfluencingBounds(bounds);
153: objRoot.addChild(light1);
154:
155: DirectionalLight light2 = new DirectionalLight(light2Color,
156: light2Direction);
157: light2.setInfluencingBounds(bounds);
158: objRoot.addChild(light2);
159:
160: return objRoot;
161: }
162:
163: private void usage() {
164: System.out
165: .println("Usage: java ConfigObjLoad [-s] [-n] [-t] [-c degrees] <.obj file>");
166: System.out.println(" -s Spin (no user interaction)");
167: System.out.println(" -n No triangulation");
168: System.out.println(" -t No stripification");
169: System.out
170: .println(" -c Set crease angle for normal generation (default is 60 without");
171: System.out
172: .println(" smoothing group info, otherwise 180 within smoothing groups)");
173: System.exit(0);
174: } // End of usage
175:
176: public void init() {
177: if (filename == null) {
178: filename = Resources
179: .getResource("resources/geometry/galleon.obj");
180: if (filename == null) {
181: System.err
182: .println("resources/geometry/galleon.obj not found");
183: System.exit(1);
184: }
185: }
186:
187: // Get the config file URL from the j3d.configURL property or use the
188: // default config file "j3d1x1-window" in the current directory.
189: URL configURL = ConfiguredUniverse.getConfigURL(null);
190: if (configURL == null) {
191: configURL = Resources
192: .getResource("configured_universe/j3d1x1-window.cfg");
193: }
194:
195: // Create a simple scene and attach it to the virtual universe
196: BranchGroup scene = createSceneGraph();
197:
198: u = new ConfiguredUniverse(configURL);
199:
200: // Get the ViewingPlatform.
201: ViewingPlatform viewingPlatform = u.getViewingPlatform();
202:
203: // This will move the ViewPlatform back a bit so the objects in the
204: // scene can be viewed. This will only have an effect if the config
205: // file sets the window eyepoint policy to something other than
206: // RELATIVE_TO_COEXISTENCE, which is the default eyepoint policy
207: // applied by ConfiguredUniverse.
208: //
209: // The default view attach policy for ConfiguredUniverse applications
210: // is NOMINAL_SCREEN. This sets the view platform origin in the
211: // physical world to the center of coexistence, which allows eye
212: // positions expressed relative to coexistence to see the appropriate
213: // field of view automatically.
214: viewingPlatform.setNominalViewingTransform();
215:
216: // Add a ViewPlatformBehavior if not specified in the config file.
217: if (!spin && viewingPlatform.getViewPlatformBehavior() == null) {
218: OrbitBehavior orbit = new OrbitBehavior(u.getCanvas(),
219: OrbitBehavior.REVERSE_ALL);
220: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
221: 0.0, 0.0), 100.0);
222:
223: orbit.setSchedulingBounds(bounds);
224: viewingPlatform.setViewPlatformBehavior(orbit);
225: }
226:
227: // See if there's a 6 degree of freedom mouse in the environment.
228: // We look for one named "mouse6d".
229: Map sensorMap = null;
230: sensorMap = u.getNamedSensors();
231: if (sensorMap != null) {
232: Sensor mouse6d = (Sensor) sensorMap.get("mouse6d");
233: if (mouse6d != null) {
234: Mouse6DPointerBehavior behavior = new Mouse6DPointerBehavior(
235: mouse6d, 1.0, true);
236:
237: BoundingSphere bounds = new BoundingSphere(new Point3d(
238: 0.0, 0.0, 0.0), 100.0);
239: behavior.setSchedulingBounds(bounds);
240:
241: scene.addChild(behavior);
242: scene.addChild(behavior.getEcho());
243: }
244: }
245:
246: // Listen for a typed "q", "Q", or "Escape" key on each canvas to
247: // allow a convenient exit from full screen configurations.
248: Canvas3D[] canvases;
249: canvases = u.getViewer().getCanvas3Ds();
250:
251: class QuitListener extends KeyAdapter {
252: public void keyTyped(KeyEvent e) {
253: char c = e.getKeyChar();
254: if (c == 'q' || c == 'Q' || c == 27)
255: System.exit(0);
256: }
257: }
258:
259: QuitListener quitListener = new QuitListener();
260: for (int i = 0; i < canvases.length; i++)
261: canvases[i].addKeyListener(quitListener);
262:
263: // Make the scenegraph live.
264: u.addBranchGraph(scene);
265: }
266:
267: public ConfigObjLoad(String[] args) {
268: if (args.length != 0) {
269: for (int i = 0; i < args.length; i++) {
270: if (args[i].startsWith("-")) {
271: if (args[i].equals("-s")) {
272: spin = true;
273: } else if (args[i].equals("-n")) {
274: noTriangulate = true;
275: } else if (args[i].equals("-t")) {
276: noStripify = true;
277: } else if (args[i].equals("-c")) {
278: if (i < args.length - 1) {
279: creaseAngle = (new Double(args[++i]))
280: .doubleValue();
281: } else
282: usage();
283: } else {
284: usage();
285: }
286: } else {
287: try {
288: if ((args[i].indexOf("file:") == 0)
289: || (args[i].indexOf("http") == 0)) {
290: filename = new URL(args[i]);
291: } else if (args[i].charAt(0) != '/') {
292: filename = new URL("file:./" + args[i]);
293: } else {
294: filename = new URL("file:" + args[i]);
295: }
296: } catch (MalformedURLException e) {
297: System.err.println(e);
298: System.exit(1);
299: }
300: }
301: }
302: }
303: init();
304: }
305:
306: public static void main(String[] args) {
307: new ConfigObjLoad(args);
308: }
309: }
|