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: }
|