001: /*
002: * $RCSfile: Viewer.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.3 $
041: * $Date: 2007/02/09 17:21:43 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.lightwave;
046:
047: import java.applet.Applet;
048: import java.awt.*;
049:
050: import javax.media.j3d.BranchGroup;
051: import javax.media.j3d.Canvas3D;
052: import javax.media.j3d.Transform3D;
053: import javax.media.j3d.TransformGroup;
054: import javax.media.j3d.View;
055: import javax.vecmath.Matrix4d;
056:
057: import com.sun.j3d.loaders.lw3d.Lw3dLoader;
058: import com.sun.j3d.loaders.Loader;
059: import com.sun.j3d.loaders.Scene;
060: import com.sun.j3d.utils.applet.MainFrame;
061: import com.sun.j3d.utils.universe.SimpleUniverse;
062:
063: /**
064: * This class loads in a Lightwave3D file and displays it in an applet
065: * window. The application is fairly basic; a more complete version
066: * of a Lightwave 3D loader might incorporate features such as
067: * settable clip plane distances and animated views (these are both
068: * possible with the current Lightwave 3D loader, they just need to
069: * be implemented in the application).
070: */
071: public class Viewer extends Applet {
072:
073: private java.net.URL filename;
074: private SimpleUniverse u;
075:
076: public Viewer(java.net.URL url) {
077: filename = url;
078: }
079:
080: public Viewer() {
081: }
082:
083: public void init() {
084: if (filename == null) {
085: // the path to the file for an applet
086: try {
087: java.net.URL path = getCodeBase();
088: filename = new java.net.URL(path.toString()
089: + "./ballcone.lws");
090: } catch (java.net.MalformedURLException ex) {
091: System.err.println(ex.getMessage());
092: ex.printStackTrace();
093: System.exit(1);
094: }
095: }
096:
097: // Construct the Lw3d loader and load the file
098: Loader lw3dLoader = new Lw3dLoader(Loader.LOAD_ALL);
099: Scene loaderScene = null;
100: try {
101: loaderScene = lw3dLoader.load(filename);
102: } catch (Exception e) {
103: e.printStackTrace();
104: System.exit(1);
105: }
106:
107: // Construct the applet canvas
108: setLayout(new BorderLayout());
109: GraphicsConfiguration config = SimpleUniverse
110: .getPreferredConfiguration();
111:
112: Canvas3D c = new Canvas3D(config);
113: add("Center", c);
114:
115: // Create a basic universe setup and the root of our scene
116: u = new SimpleUniverse(c);
117: BranchGroup sceneRoot = new BranchGroup();
118:
119: // Change the back clip distance; the default is small for
120: // some lw3d worlds
121: View theView = u.getViewer().getView();
122: theView.setBackClipDistance(50000f);
123:
124: // Now add the scene graph defined in the lw3d file
125: if (loaderScene.getSceneGroup() != null) {
126: // Instead of using the default view location (which may be
127: // completely bogus for the particular file you're loading),
128: // let's use the initial view from the file. We can get
129: // this by getting the view groups from the scene (there's
130: // only one for Lightwave 3D), then using the inverse of the
131: // transform on that view as the transform for the entire scene.
132:
133: // First, get the view groups (shouldn't be null unless there
134: // was something wrong in the load
135: TransformGroup viewGroups[] = loaderScene.getViewGroups();
136:
137: // Get the Transform3D from the view and invert it
138: Transform3D t = new Transform3D();
139: viewGroups[0].getTransform(t);
140: Matrix4d m = new Matrix4d();
141: t.get(m);
142: m.invert();
143: t.set(m);
144:
145: // Now we've got the transform we want. Create an
146: // appropriate TransformGroup and parent the scene to it.
147: // Then insert the new group into the main BranchGroup.
148: TransformGroup sceneTransform = new TransformGroup(t);
149: sceneTransform.addChild(loaderScene.getSceneGroup());
150: sceneRoot.addChild(sceneTransform);
151: }
152:
153: // Make the scene graph live by inserting the root into the universe
154: u.addBranchGraph(sceneRoot);
155: }
156:
157: public void destroy() {
158: u.cleanup();
159: }
160:
161: private static void usage() {
162: System.out.println("Usage: java Viewer <.lws>");
163: System.exit(0);
164: }
165:
166: /**
167: * The main method of the application takes one argument in the
168: * args array; the filname that you want to load. Note that the
169: * file must be reachable from the directory in which you're running
170: * this application.
171: */
172: public static void main(String args[]) {
173: java.net.URL url = null;
174: java.net.URL pathUrl = null;
175: if (args.length > 0) {
176: try {
177: if ((args[0].indexOf("file:") == 0)
178: || (args[0].indexOf("http") == 0)) {
179: url = new java.net.URL(args[0]);
180: } else if (args[0].charAt(0) != '/') {
181: url = new java.net.URL("file:./" + args[0]);
182: } else {
183: url = new java.net.URL("file:" + args[0]);
184: }
185: } catch (java.net.MalformedURLException ex) {
186: System.err.println(ex.getMessage());
187: ex.printStackTrace();
188: System.exit(1);
189: }
190: } else {
191: usage();
192: }
193: new MainFrame(new Viewer(url), 500, 500);
194: }
195: }
|