Source Code Cross Referenced for ConfigObjLoad.java in  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3d » examples » configured_universe » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » java 3d » org.jdesktop.j3d.examples.configured_universe 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.