Source Code Cross Referenced for LeotoolLoader.java in  » 6.0-JDK-Modules » java-3d » org » jdesktop » j3dfly » utils » internalloaders » 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.j3dfly.utils.internalloaders 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  $Header: /cvs/j3dfly/J3dFly/src/org/jdesktop/j3dfly/utils/internalloaders/LeotoolLoader.java,v 1.1 2005/04/20 21:05:04 paulby Exp $
003:         *
004:         *                         Sun Public License Notice
005:         *
006:         *  The contents of this file are subject to the Sun Public License Version
007:         *  1.0 (the "License"). You may not use this file except in compliance with
008:         *  the License. A copy of the License is available at http://www.sun.com/
009:         *  
010:         *  The Original Code is Java 3D(tm) Fly Through.
011:         *  The Initial Developer of the Original Code is Paul Byrne.
012:         *  Portions created by Paul Byrne are Copyright (C) 2002.
013:         *  All Rights Reserved.
014:         *  
015:         *  Contributor(s): Paul Byrne.
016:         *  
017:         **/
018:        package org.jdesktop.j3dfly.utils.internalloaders;
019:
020:        import java.io.*;
021:        import java.util.Hashtable;
022:        import com.sun.j3d.loaders.Scene;
023:        import com.sun.j3d.loaders.objectfile.ObjectFile;
024:
025:        import javax.media.j3d.Material;
026:        import javax.media.j3d.Appearance;
027:        import javax.media.j3d.TransparencyAttributes;
028:        import javax.media.j3d.Shape3D;
029:        import javax.media.j3d.BranchGroup;
030:        import javax.media.j3d.AmbientLight;
031:        import javax.media.j3d.DirectionalLight;
032:        import javax.media.j3d.BoundingSphere;
033:        import javax.media.j3d.PolygonAttributes;
034:
035:        import javax.vecmath.Color3f;
036:        import javax.vecmath.Point3d;
037:        import javax.vecmath.Vector3f;
038:
039:        import com.sun.j3d.utils.scenegraph.io.SceneGraphFileWriter;
040:        import org.jdesktop.j3d.utils.scenegraph.traverser.ChangePolygonAttributes;
041:
042:        /**
043:         *
044:         * @author  paulby
045:         * @version 
046:         */
047:        public class LeotoolLoader extends com.sun.j3d.loaders.LoaderBase {
048:
049:            private Scene modelScene = null; // The scene of the obj 
050:
051:            /** Creates new LeoToolLoader */
052:            public LeotoolLoader() {
053:            }
054:
055:            public Scene load(java.io.Reader reader) {
056:                throw new RuntimeException("Not Implemented");
057:            }
058:
059:            public Scene load(String filename) {
060:                try {
061:                    String currentDir = filename.substring(0, filename
062:                            .lastIndexOf(File.separatorChar));
063:                    parseFile(new BufferedReader(new InputStreamReader(
064:                            new FileInputStream(filename))), currentDir);
065:                } catch (Exception e) {
066:                    e.printStackTrace();
067:                }
068:
069:                BranchGroup lightBG = new BranchGroup();
070:                BoundingSphere bounds = new BoundingSphere(new Point3d(),
071:                        Double.POSITIVE_INFINITY);
072:                Color3f light1Color = new Color3f(0.9f, 0.9f, 0.9f);
073:                Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
074:                light1Direction.normalize();
075:                Color3f light2Color = new Color3f(0.8f, 0.8f, 0.8f);
076:                Vector3f light2Direction = new Vector3f(-4.0f, 7.0f, 12.0f);
077:                light2Direction.normalize();
078:
079:                DirectionalLight light1 = new DirectionalLight(light1Color,
080:                        light1Direction);
081:                light1.setInfluencingBounds(bounds);
082:                lightBG.addChild(light1);
083:
084:                DirectionalLight light2 = new DirectionalLight(light2Color,
085:                        light2Direction);
086:                light2.setInfluencingBounds(bounds);
087:                lightBG.addChild(light2);
088:
089:                AmbientLight light3 = new AmbientLight(new Color3f(0.6f, 0.6f,
090:                        0.6f));
091:                light3.setInfluencingBounds(bounds);
092:                lightBG.addChild(light3);
093:
094:                modelScene.getSceneGroup().addChild(lightBG);
095:
096:                ChangePolygonAttributes.setCullFace(modelScene.getSceneGroup(),
097:                        PolygonAttributes.CULL_NONE, true);
098:
099:                return modelScene;
100:            }
101:
102:            public Scene load(java.net.URL url) {
103:                throw new RuntimeException("Not Implemented");
104:            }
105:
106:            private void parseFile(Reader reader, String currentDir) {
107:                StreamTokenizer tok = new StreamTokenizer(reader);
108:                tok.parseNumbers();
109:                tok.wordChars('_', '_');
110:                tok.commentChar('!');
111:
112:                try {
113:                    tok.nextToken(); // Read the first token
114:
115:                    while (tok.ttype != StreamTokenizer.TT_EOF) {
116:                        //System.out.println("Token "+tok.sval+"  "+tok.nval );
117:                        if (tok.sval.equals("load_object"))
118:                            parseLoadObject(tok, currentDir);
119:                        else if (tok.sval.equals("obj_colors.part_color"))
120:                            parseObjPartColor(tok);
121:                        else {
122:                            System.out.println("Ignoring Token " + tok.sval);
123:                            tok.eolIsSignificant(true);
124:                            while (tok.ttype != StreamTokenizer.TT_EOL)
125:                                tok.nextToken();
126:                            tok.eolIsSignificant(false);
127:                        }
128:                        tok.nextToken();
129:                    }
130:                } catch (Exception e) {
131:                    e.printStackTrace();
132:                }
133:            }
134:
135:            /**
136:             * Parse the load_object token 
137:             */
138:            private void parseLoadObject(StreamTokenizer tok, String currentDir)
139:                    throws java.io.IOException {
140:                tok.nextToken();
141:
142:                String filename = tok.sval;
143:                //filename = "Plymouth_Voyager_87_h.obj";
144:                if (filename.endsWith(".Z"))
145:                    filename = filename.substring(0, filename.length() - 2);
146:                System.out.println("Loading Obj file " + currentDir
147:                        + File.separatorChar + filename);
148:
149:                try {
150:                    Reader reader = new InputStreamReader(
151:                            new BufferedInputStream(new FileInputStream(
152:                                    currentDir + File.separatorChar + filename)));
153:                    ObjectFile objLoader = new ObjectFile(ObjectFile.STRIPIFY
154:                            | ObjectFile.TRIANGULATE | ObjectFile.RESIZE);
155:                    //ObjectFile objLoader = new ObjectFile( ObjectFile.STRIPIFY | ObjectFile.TRIANGULATE );
156:                    //reader = new InputStreamReader( new FileInputStream( currentDir+File.separatorChar+filename));
157:
158:                    modelScene = objLoader.load(reader);
159:                } catch (Exception e) {
160:                    e.printStackTrace();
161:                }
162:            }
163:
164:            /**
165:             * Parse the obj_colors.part_color token
166:             */
167:            private void parseObjPartColor(StreamTokenizer tok)
168:                    throws java.io.IOException {
169:                tok.nextToken();
170:
171:                String partName = tok.sval;
172:
173:                tok.nextToken(); // =
174:
175:                tok.nextToken();
176:                double red = tok.nval;
177:
178:                tok.nextToken();
179:                double green = tok.nval;
180:
181:                tok.nextToken();
182:                double blue = tok.nval;
183:
184:                Hashtable hash = modelScene.getNamedObjects();
185:                Object partObj = hash.get(partName);
186:
187:                Material mat = new Material();
188:                Color3f clr = new Color3f((float) red, (float) green,
189:                        (float) blue);
190:                mat.setDiffuseColor(clr);
191:                mat.setAmbientColor(clr);
192:
193:                TransparencyAttributes transAttr = null;
194:                PolygonAttributes polyAttr = new PolygonAttributes();
195:                polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
196:
197:                if (partName.equals("glass") || partName.equals("dkglass")) {
198:                    System.out.println(partName);
199:                    System.out.println("Geom "
200:                            + ((Shape3D) partObj).numGeometries());
201:                    transAttr = new TransparencyAttributes(
202:                            TransparencyAttributes.NICEST, 0.2f);
203:                    polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
204:
205:                    try {
206:                        File file = new File(partName + ".j3f");
207:                        BranchGroup bg = new BranchGroup();
208:                        Shape3D shape = new Shape3D();
209:                        shape.addGeometry(((Shape3D) partObj).getGeometry(0));
210:                        bg.addChild(shape);
211:
212:                        SceneGraphFileWriter writer = new SceneGraphFileWriter(
213:                                file, null, false, null, null);
214:                        writer.writeBranchGraph(bg);
215:                        writer.close();
216:                    } catch (Exception e) {
217:                        e.printStackTrace();
218:                    }
219:                }
220:
221:                Appearance app = new Appearance();
222:                app.setMaterial(mat);
223:                app.setTransparencyAttributes(transAttr);
224:                app.setPolygonAttributes(polyAttr);
225:
226:                if (partObj instanceof  Shape3D)
227:                    ((Shape3D) partObj).setAppearance(app);
228:
229:                //System.out.println( "Part "+partName+"  "+red+" "+green+" "+blue );
230:                //System.out.println( partObj );
231:
232:            }
233:
234:            public static void main(String args[]) {
235:                LeotoolLoader loader = new LeotoolLoader();
236:                loader
237:                        .load("/export/home/paulby/code/release/j3dTools/src/geometry/Dodge_Caravan_95.mdl");
238:            }
239:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.