001: /*
002: * $RCSfile: cgview.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:40 $
042: * $State: Exp $
043: */
044:
045: package org.jdesktop.j3d.examples.geometry_compression;
046:
047: import com.sun.j3d.utils.applet.MainFrame;
048: import com.sun.j3d.utils.behaviors.vp.OrbitBehavior;
049: import com.sun.j3d.utils.geometry.compression.*;
050: import com.sun.j3d.utils.universe.*;
051: import java.applet.Applet;
052: import java.awt.BorderLayout;
053: import java.io.IOException;
054: import javax.media.j3d.*;
055: import javax.vecmath.*;
056:
057: public class cgview extends Applet {
058:
059: private SimpleUniverse u = null;
060:
061: public BranchGroup createSceneGraph(CompressedGeometryData cg) {
062: // Create the root of the branch graph
063: BranchGroup objRoot = new BranchGroup();
064:
065: // Create a Transformgroup to scale all objects so they
066: // appear in the scene.
067: TransformGroup objScale = new TransformGroup();
068: Transform3D t3d = new Transform3D();
069: t3d.setScale(0.7);
070: objScale.setTransform(t3d);
071: objRoot.addChild(objScale);
072:
073: // Create the transform group node and initialize it to the
074: // identity. Enable the TRANSFORM_WRITE capability so that
075: // our behavior code can modify it at runtime. Add it to the
076: // root of the subgraph.
077: TransformGroup objTrans = new TransformGroup();
078: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
079: objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
080: objScale.addChild(objTrans);
081:
082: // Add compressed geometry to the scene graph.
083: CompressedGeometryData.Header hdr = new CompressedGeometryData.Header();
084: cg.getCompressedGeometryHeader(hdr);
085:
086: Shape3D[] shapes = cg.decompress();
087: if (shapes != null) {
088: for (int i = 0; i < shapes.length; i++) {
089: objTrans.addChild(shapes[i]);
090: }
091: }
092:
093: // Create mouse behavior scheduling bounds.
094: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
095: 0.0, 0.0), 100.0);
096:
097: // Set up the background
098: Color3f bgColor = new Color3f(0.05f, 0.05f, 0.5f);
099: Background bgNode = new Background(bgColor);
100: bgNode.setApplicationBounds(bounds);
101: objRoot.addChild(bgNode);
102:
103: // Set up the ambient light
104: Color3f ambientColor = new Color3f(0.1f, 0.1f, 0.1f);
105: AmbientLight ambientLightNode = new AmbientLight(ambientColor);
106: ambientLightNode.setInfluencingBounds(bounds);
107: objRoot.addChild(ambientLightNode);
108:
109: // Set up the directional lights
110: Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
111: Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
112: Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
113: Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -0.9f);
114:
115: DirectionalLight light1 = new DirectionalLight(light1Color,
116: light1Direction);
117: light1.setInfluencingBounds(bounds);
118: objRoot.addChild(light1);
119:
120: DirectionalLight light2 = new DirectionalLight(light2Color,
121: light2Direction);
122: light2.setInfluencingBounds(bounds);
123: objRoot.addChild(light2);
124:
125: return objRoot;
126: }
127:
128: private void usage() {
129: System.out.println("Usage: cgview <.cg file> <object index>");
130: System.exit(0);
131: }
132:
133: public cgview(String args[]) {
134: if (args.length < 1)
135: usage();
136:
137: int index;
138: if (args.length < 2)
139: index = 0;
140: else
141: index = Integer.parseInt(args[1]);
142:
143: String filename = args[0];
144: if (filename == null)
145: usage();
146:
147: // Read the compressed geometry.
148: CompressedGeometryData cg = null;
149: try {
150: CompressedGeometryFile cgf;
151: cgf = new CompressedGeometryFile(filename, false);
152:
153: if (cgf.getObjectCount() == 0) {
154: System.out.println("no objects were found in "
155: + filename);
156: System.exit(0);
157: }
158:
159: cg = cgf.read(index);
160: cgf.close();
161:
162: } catch (IOException e) {
163: System.out.println(e);
164: System.exit(0);
165: }
166:
167: setLayout(new BorderLayout());
168: Canvas3D c = new Canvas3D(SimpleUniverse
169: .getPreferredConfiguration());
170: add("Center", c);
171:
172: // Create a simple scene and attach it to the virtual universe
173: BranchGroup scene = createSceneGraph(cg);
174: u = new SimpleUniverse(c);
175:
176: // add mouse behaviors to the ViewingPlatform
177: ViewingPlatform viewingPlatform = u.getViewingPlatform();
178:
179: // This will move the ViewPlatform back a bit so the
180: // objects in the scene can be viewed.
181: viewingPlatform.setNominalViewingTransform();
182:
183: OrbitBehavior orbit = new OrbitBehavior(c,
184: OrbitBehavior.REVERSE_ALL);
185: BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,
186: 0.0, 0.0), 100.0);
187: orbit.setSchedulingBounds(bounds);
188: viewingPlatform.setViewPlatformBehavior(orbit);
189:
190: u.addBranchGraph(scene);
191: }
192:
193: public void destroy() {
194: u.cleanup();
195: }
196:
197: //
198: // The following allows cgview to be run as an application
199: // as well as an applet.
200: //
201: public static void main(String[] args) {
202: new MainFrame(new cgview(args), 700, 700);
203: }
204: }
|