Package com.sun.j3d.loaders.lw3d

Source Code of com.sun.j3d.loaders.lw3d.Lw3dLoader

/*     */ package com.sun.j3d.loaders.lw3d;
/*     */
/*     */ import com.sun.j3d.loaders.IncorrectFormatException;
/*     */ import com.sun.j3d.loaders.Loader;
/*     */ import com.sun.j3d.loaders.ParsingErrorException;
/*     */ import com.sun.j3d.loaders.Scene;
/*     */ import com.sun.j3d.loaders.SceneBase;
/*     */ import java.io.BufferedInputStream;
/*     */ import java.io.BufferedReader;
/*     */ import java.io.File;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStreamReader;
/*     */ import java.io.Reader;
/*     */ import java.io.StreamTokenizer;
/*     */ import java.net.URL;
/*     */ import java.util.Enumeration;
/*     */ import java.util.StringTokenizer;
/*     */ import java.util.Vector;
/*     */ import javax.media.j3d.AmbientLight;
/*     */ import javax.media.j3d.Background;
/*     */ import javax.media.j3d.Behavior;
/*     */ import javax.media.j3d.BoundingSphere;
/*     */ import javax.media.j3d.BranchGroup;
/*     */ import javax.media.j3d.Fog;
/*     */ import javax.media.j3d.Light;
/*     */ import javax.media.j3d.TransformGroup;
/*     */ import javax.vecmath.Color3f;
/*     */ import javax.vecmath.Point3d;
/*     */
/*     */ public class Lw3dLoader extends TextfileParser
/*     */   implements Loader
/*     */ {
/*     */   Vector objectList;
/*     */   Vector lightList;
/*     */   BranchGroup sceneGroupNode;
/*     */   Color3f ambientColor;
/*  71 */   LwsCamera camera = null;
/*  72 */   LwsFog fog = null;
/*  73 */   LwsBackground background = null;
/*  74 */   int loadFlags = 0;
/*  75 */   int loadBehaviors = 0;
/*     */   Vector sceneBehaviors;
/*  77 */   SceneBase scene = null;
/*  78 */   String basePath = null;
/*  79 */   String internalBasePath = null;
/*  80 */   URL baseUrl = null;
/*  81 */   String internalBaseUrl = null;
/*     */   static final int FILE_TYPE_NONE = 0;
/*     */   static final int FILE_TYPE_URL = 1;
/*     */   static final int FILE_TYPE_FILENAME = 2;
/*     */   static final int FILE_TYPE_READER = 4;
/*  86 */   int fileType = 0;
/*     */
/*     */   public Lw3dLoader()
/*     */   {
/*  93 */     this.ambientColor = new Color3f(0.0F, 0.0F, 0.0F);
/*  94 */     this.objectList = new Vector();
/*  95 */     this.lightList = new Vector();
/*  96 */     this.debugPrinter.setValidOutput(0);
/*     */   }
/*     */
/*     */   public Lw3dLoader(int flags)
/*     */   {
/* 107 */     this();
/* 108 */     this.loadFlags = flags;
/* 109 */     this.loadBehaviors = (this.loadFlags & 0x8);
/*     */   }
/*     */
/*     */   public Scene load(URL url)
/*     */     throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
/*     */   {
/* 123 */     this.fileType = 1;
/* 124 */     setInternalBaseUrl(url);
/*     */     InputStreamReader reader;
/*     */     try
/*     */     {
/* 127 */       reader = new InputStreamReader(new BufferedInputStream(url.openStream()));
/*     */     }
/*     */     catch (IOException e)
/*     */     {
/* 131 */       throw new FileNotFoundException(e.getMessage());
/*     */     }
/* 133 */     Scene returnScene = load(reader);
/* 134 */     this.fileType = 0;
/* 135 */     return returnScene;
/*     */   }
/*     */
/*     */   public Scene load(String fileName)
/*     */     throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
/*     */   {
/* 148 */     this.fileType = 2;
/* 149 */     setInternalBasePath(fileName);
/* 150 */     Reader reader = new BufferedReader(new FileReader(fileName));
/* 151 */     Scene returnScene = load(reader);
/* 152 */     this.fileType = 0;
/* 153 */     return returnScene;
/*     */   }
/*     */
/*     */   public Scene load(Reader reader)
/*     */     throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
/*     */   {
/* 164 */     if (this.fileType == 0)
/* 165 */       this.fileType = 4;
/* 166 */     StreamTokenizer tokenizer = new StreamTokenizer(reader);
/* 167 */     setupTokenizer(tokenizer);
/*     */
/* 169 */     getAndCheckString(tokenizer, "LWSC");
/* 170 */     getNumber(tokenizer);
/* 171 */     getAndCheckString(tokenizer, "FirstFrame");
/* 172 */     int firstFrame = (int)getNumber(tokenizer);
/* 173 */     getAndCheckString(tokenizer, "LastFrame");
/* 174 */     int finalFrame = (int)getNumber(tokenizer);
/* 175 */     skipUntilString(tokenizer, "FramesPerSecond");
/* 176 */     double fps = getNumber(tokenizer);
/* 177 */     float totalTime = (finalFrame - firstFrame) / (float)fps;
/* 178 */     boolean done = false;
/* 179 */     while (!done) {
/*     */       int token;
/*     */       try {
/* 182 */         token = tokenizer.nextToken();
/*     */       }
/*     */       catch (IOException e) {
/* 185 */         throw new ParsingErrorException(e.getMessage());
/*     */       }
/* 187 */       switch (tokenizer.ttype) {
/*     */       case -1:
/* 189 */         done = true;
/* 190 */         break;
/*     */       case -3:
/* 192 */         debugOutputLn(2, "  String = " + tokenizer.sval);
/* 193 */         if (tokenizer.sval.equals("AddNullObject")) {
/* 194 */           LwsObject obj = new LwsObject(tokenizer, false, firstFrame, finalFrame, totalTime, this, this.debugPrinter.getValidOutput());
/*     */
/* 200 */           obj.createJava3dObject(null, this.loadBehaviors);
/* 201 */           this.objectList.addElement(obj);
/*     */         }
/* 203 */         else if (tokenizer.sval.equals("LoadObject")) {
/* 204 */           String filename = getString(tokenizer);
/* 205 */           tokenizer.pushBack();
/* 206 */           debugOutputLn(32, "loading " + filename + " at " + System.currentTimeMillis());
/*     */
/* 208 */           LwsObject obj = new LwsObject(tokenizer, true, firstFrame, finalFrame, totalTime, this, this.debugPrinter.getValidOutput());
/*     */
/* 213 */           debugOutputLn(32, "done loading at " + System.currentTimeMillis());
/*     */
/* 215 */           LwsObject cloneObject = null;
/* 216 */           Enumeration e = this.objectList.elements();
/* 217 */           while (e.hasMoreElements()) {
/* 218 */             LwsObject tmpObj = (LwsObject)e.nextElement();
/* 219 */             if ((tmpObj.fileName != null) && (tmpObj.fileName.equals(filename)))
/*     */             {
/* 221 */               cloneObject = tmpObj;
/* 222 */               break;
/*     */             }
/*     */           }
/* 225 */           obj.createJava3dObject(cloneObject, this.loadBehaviors);
/* 226 */           this.objectList.addElement(obj);
/*     */         }
/* 228 */         else if (tokenizer.sval.equals("AmbientColor")) {
/* 229 */           this.ambientColor.x = ((float)getNumber(tokenizer) / 255.0F);
/* 230 */           this.ambientColor.y = ((float)getNumber(tokenizer) / 255.0F);
/* 231 */           this.ambientColor.z = ((float)getNumber(tokenizer) / 255.0F);
/*     */         }
/* 233 */         else if (tokenizer.sval.equals("AmbIntensity"))
/*     */         {
/* 235 */           float intensity = (float)getNumber(tokenizer);
/* 236 */           this.ambientColor.x *= intensity;
/* 237 */           this.ambientColor.y *= intensity;
/* 238 */           this.ambientColor.z *= intensity;
/*     */         }
/* 240 */         else if (tokenizer.sval.equals("AddLight")) {
/* 241 */           LwsLight light = new LwsLight(tokenizer, finalFrame, totalTime, this.debugPrinter.getValidOutput());
/*     */
/* 245 */           light.createJava3dObject(this.loadBehaviors);
/* 246 */           this.lightList.addElement(light);
/*     */         }
/* 248 */         else if (tokenizer.sval.equals("ShowCamera")) {
/* 249 */           this.camera = new LwsCamera(tokenizer, firstFrame, finalFrame, totalTime, this.debugPrinter.getValidOutput());
/*     */
/* 252 */           this.camera.createJava3dObject(this.loadBehaviors);
/*     */         }
/* 254 */         else if (tokenizer.sval.equals("FogType")) {
/* 255 */           int fogType = (int)getNumber(tokenizer);
/* 256 */           if (fogType != 0) {
/* 257 */             this.fog = new LwsFog(tokenizer, this.debugPrinter.getValidOutput());
/*     */
/* 259 */             this.fog.createJava3dObject();
/*     */           }
/*     */         }
/* 262 */         else if (tokenizer.sval.equals("SolidBackdrop")) {
/* 263 */           this.background = new LwsBackground(tokenizer, this.debugPrinter.getValidOutput());
/*     */
/* 266 */           this.background.createJava3dObject(); } break;
/*     */       default:
/* 270 */         debugOutputLn(2, "  Unknown ttype, token = " + tokenizer.ttype + ", " + token);
/*     */       }
/*     */
/*     */     }
/*     */
/* 277 */     this.sceneGroupNode = new BranchGroup();
/* 278 */     this.sceneBehaviors = new Vector();
/* 279 */     parentObjects();
/* 280 */     constructScene();
/*     */
/* 282 */     return this.scene;
/*     */   }
/*     */
/*     */   void constructScene()
/*     */   {
/* 296 */     this.scene = new SceneBase();
/*     */
/* 298 */     if ((this.loadFlags & 0x1) != 0) {
/* 299 */       addLights();
/* 300 */       addAmbient();
/*     */     }
/*     */
/* 303 */     if ((this.loadFlags & 0x2) != 0) {
/* 304 */       addFog();
/*     */     }
/* 306 */     if ((this.loadFlags & 0x4) != 0) {
/* 307 */       addBackground();
/*     */     }
/* 309 */     if ((this.loadFlags & 0x10) != 0) {
/* 310 */       addCamera();
/*     */     }
/* 312 */     if (this.loadBehaviors != 0) {
/* 313 */       addBehaviors();
/*     */     }
/* 315 */     this.scene.setSceneGroup(this.sceneGroupNode);
/*     */
/* 318 */     for (Enumeration e = this.objectList.elements(); e.hasMoreElements(); )
/*     */     {
/* 320 */       LwsObject obj = (LwsObject)e.nextElement();
/* 321 */       if (obj.fileName != null)
/* 322 */         this.scene.addNamedObject(obj.fileName, obj.getObjectNode());
/* 323 */       else if (obj.objName != null)
/* 324 */         this.scene.addNamedObject(obj.objName, obj.getObjectNode());
/*     */     }
/*     */   }
/*     */
/*     */   void setInternalBaseUrl(URL url)
/*     */   {
/* 336 */     StringTokenizer stok = new StringTokenizer(url.toString(), "\\/");
/*     */
/* 340 */     int tocount = stok.countTokens() - 1;
/* 341 */     StringBuffer sb = new StringBuffer(80);
/* 342 */     for (int ji = 0; ji < tocount; ji++) {
/* 343 */       String a = stok.nextToken();
/* 344 */       if ((ji == 0) && (!a.regionMatches(true, 0, "file:", 0, 5)))
/*     */       {
/* 346 */         sb.append(a);
/*     */
/* 350 */         sb.append('/');
/* 351 */         sb.append('/');
/*     */       } else {
/* 353 */         sb.append(a);
/*     */
/* 356 */         sb.append('/');
/*     */       }
/*     */     }
/* 359 */     this.internalBaseUrl = sb.toString();
/*     */   }
/*     */
/*     */   void setInternalBasePath(String fileName)
/*     */   {
/* 367 */     StringTokenizer stok = new StringTokenizer(fileName, File.separator);
/*     */
/* 370 */     int tocount = stok.countTokens() - 1;
/* 371 */     StringBuffer sb = new StringBuffer(80);
/* 372 */     if ((fileName != null) && (fileName.startsWith(File.separator)))
/*     */     {
/* 374 */       sb.append(File.separator);
/* 375 */     }for (int ji = 0; ji < tocount; ji++) {
/* 376 */       String a = stok.nextToken();
/* 377 */       sb.append(a);
/* 378 */       sb.append(File.separator);
/*     */     }
/* 380 */     this.internalBasePath = sb.toString();
/*     */   }
/*     */
/*     */   String getInternalBasePath() {
/* 384 */     return this.internalBasePath;
/*     */   }
/*     */
/*     */   String getInternalBaseUrl() {
/* 388 */     return this.internalBaseUrl;
/*     */   }
/*     */
/*     */   int getFileType() {
/* 392 */     return this.fileType;
/*     */   }
/*     */
/*     */   void parentObjects()
/*     */   {
/* 402 */     debugOutputLn(1, "parentObjects()");
/* 403 */     for (Enumeration e = this.objectList.elements(); e.hasMoreElements(); )
/*     */     {
/* 405 */       LwsObject obj = (LwsObject)e.nextElement();
/* 406 */       if (obj.getParent() != -1)
/*     */       {
/* 408 */         LwsObject parent = (LwsObject)this.objectList.elementAt(obj.getParent() - 1);
/*     */
/* 410 */         parent.addChild(obj);
/* 411 */         debugOutputLn(2, "added child successfully");
/*     */       }
/* 415 */       else if (obj.getObjectNode() != null) {
/* 416 */         this.sceneGroupNode.addChild(obj.getObjectNode());
/*     */       }
/*     */
/* 421 */       if ((this.loadBehaviors != 0) &&
/* 422 */         (!obj.getObjectBehaviors().isEmpty())) {
/* 423 */         this.sceneBehaviors.addAll(obj.getObjectBehaviors());
/*     */       }
/*     */
/*     */     }
/*     */
/* 429 */     debugOutputLn(8, "Done with parentObjects()");
/*     */   }
/*     */
/*     */   public void setBaseUrl(URL url)
/*     */   {
/* 443 */     this.baseUrl = url;
/*     */   }
/*     */
/*     */   public void setBasePath(String pathName)
/*     */   {
/* 456 */     this.basePath = pathName;
/* 457 */     if ((this.basePath == null) || (this.basePath == ""))
/* 458 */       this.basePath = ("." + File.separator);
/* 459 */     this.basePath = this.basePath.replace('/', File.separatorChar);
/* 460 */     this.basePath = this.basePath.replace('\\', File.separatorChar);
/* 461 */     if (!this.basePath.endsWith(File.separator))
/* 462 */       this.basePath += File.separator;
/*     */   }
/*     */
/*     */   public URL getBaseUrl()
/*     */   {
/* 469 */     return this.baseUrl;
/*     */   }
/*     */
/*     */   public String getBasePath()
/*     */   {
/* 476 */     return this.basePath;
/*     */   }
/*     */
/*     */   public void setFlags(int flags)
/*     */   {
/* 484 */     this.loadFlags = flags;
/*     */   }
/*     */
/*     */   public int getFlags()
/*     */   {
/* 491 */     return this.loadFlags;
/*     */   }
/*     */
/*     */   public TransformGroup getObject(String name)
/*     */   {
/* 509 */     debugOutputLn(1, "getObject()");
/* 510 */     int indexNumber = -1;
/* 511 */     int currentObjectCount = 0;
/* 512 */     String subobjectName = name;
/* 513 */     if (name.indexOf("[") != -1)
/*     */     {
/* 515 */       int bracketsIndex = name.indexOf("[");
/* 516 */       subobjectName = name.substring(0, bracketsIndex);
/* 517 */       String bracketsString = name.substring(bracketsIndex);
/* 518 */       int bracketEndIndex = bracketsString.indexOf("]");
/* 519 */       String indexString = bracketsString.substring(1, bracketEndIndex);
/* 520 */       indexNumber = new Integer(indexString).intValue();
/*     */     }
/* 522 */     Enumeration e = this.objectList.elements();
/* 523 */     while (e.hasMoreElements()) {
/* 524 */       LwsObject tempObj = (LwsObject)e.nextElement();
/* 525 */       debugOutputLn(2, "tempObj, file, objname = " + tempObj + tempObj.fileName + tempObj.objName);
/*     */
/* 528 */       if (((tempObj.fileName != null) && (tempObj.fileName.indexOf(subobjectName) != -1)) || ((tempObj.objName != null) && (tempObj.objName.indexOf(subobjectName) != -1)))
/*     */       {
/* 532 */         if ((indexNumber < 0) || (indexNumber == currentObjectCount))
/*     */         {
/* 534 */           return tempObj.getObjectNode();
/*     */         }
/* 536 */         currentObjectCount++;
/*     */       }
/*     */     }
/* 539 */     debugOutputLn(2, " no luck - wanted " + name + " returning null");
/*     */
/* 541 */     return null;
/*     */   }
/*     */
/*     */   void setupTokenizer(StreamTokenizer tokenizer)
/*     */   {
/* 551 */     tokenizer.resetSyntax();
/* 552 */     tokenizer.wordChars(97, 122);
/* 553 */     tokenizer.wordChars(65, 90);
/* 554 */     tokenizer.wordChars(160, 255);
/* 555 */     tokenizer.whitespaceChars(0, 32);
/* 556 */     tokenizer.commentChar(47);
/* 557 */     tokenizer.quoteChar(34);
/* 558 */     tokenizer.quoteChar(39);
/* 559 */     tokenizer.wordChars(48, 57);
/* 560 */     tokenizer.wordChars(46, 46);
/* 561 */     tokenizer.wordChars(45, 45);
/* 562 */     tokenizer.wordChars(47, 47);
/* 563 */     tokenizer.wordChars(92, 92);
/* 564 */     tokenizer.wordChars(95, 95);
/* 565 */     tokenizer.wordChars(38, 38);
/* 566 */     tokenizer.ordinaryChar(40);
/* 567 */     tokenizer.ordinaryChar(41);
/* 568 */     tokenizer.whitespaceChars(13, 13);
/*     */
/* 571 */     tokenizer.wordChars(58, 58);
/*     */
/* 573 */     tokenizer.wordChars(126, 126);
/*     */   }
/*     */
/*     */   void addAmbient()
/*     */   {
/* 580 */     AmbientLight aLgt = new AmbientLight(this.ambientColor);
/* 581 */     BoundingSphere bounds = new BoundingSphere(new Point3d(0.0D, 0.0D, 0.0D), 100000.0D);
/*     */
/* 583 */     aLgt.setInfluencingBounds(bounds);
/* 584 */     this.sceneGroupNode.addChild(aLgt);
/*     */
/* 586 */     aLgt.addScope(this.sceneGroupNode);
/* 587 */     this.scene.addLightNode(aLgt);
/*     */   }
/*     */
/*     */   void addLights()
/*     */   {
/* 595 */     for (Enumeration e1 = this.lightList.elements(); e1.hasMoreElements(); )
/*     */     {
/* 597 */       debugOutputLn(8, "adding light to scene group");
/* 598 */       LwsLight light = (LwsLight)e1.nextElement();
/*     */
/* 600 */       if (light.getObjectNode() != null)
/*     */       {
/* 602 */         light.getLight().addScope(this.sceneGroupNode);
/*     */
/* 604 */         if (light.getParent() != -1) {
/* 605 */           LwsObject parent = (LwsObject)this.objectList.elementAt(light.getParent() - 1);
/*     */
/* 607 */           parent.addChild(light);
/*     */         }
/*     */         else {
/* 610 */           this.sceneGroupNode.addChild(light.getObjectNode());
/*     */         }
/*     */
/* 615 */         if ((this.loadBehaviors != 0) &&
/* 616 */           (!light.getObjectBehaviors().isEmpty())) {
/* 617 */           this.sceneBehaviors.addAll(light.getObjectBehaviors());
/*     */         }
/*     */
/* 621 */         this.scene.addLightNode(light.getLight());
/*     */       }
/*     */       else {
/* 624 */         debugOutputLn(8, "light object null?");
/*     */       }
/*     */     }
/*     */   }
/*     */
/*     */   void addCamera()
/*     */   {
/* 636 */     if (this.camera != null) {
/* 637 */       if (this.camera.getParent() != -1) {
/* 638 */         debugOutputLn(2, "camera parent = " + this.camera.getParent());
/*     */
/* 640 */         LwsObject parent = (LwsObject)this.objectList.elementAt(this.camera.getParent() - 1);
/*     */
/* 642 */         parent.addChild(this.camera);
/* 643 */         debugOutputLn(2, "added child successfully");
/*     */       }
/*     */       else {
/* 646 */         this.sceneGroupNode.addChild(this.camera.getObjectNode());
/*     */       }
/*     */
/* 651 */       if ((this.loadBehaviors != 0) &&
/* 652 */         (!this.camera.getObjectBehaviors().isEmpty())) {
/* 653 */         this.sceneBehaviors.addAll(this.camera.getObjectBehaviors());
/*     */       }
/*     */
/* 656 */       this.scene.addViewGroup(this.camera.getObjectNode());
/*     */     }
/*     */   }
/*     */
/*     */   void addFog()
/*     */   {
/* 664 */     if (this.fog != null) {
/* 665 */       Fog fogNode = this.fog.getObjectNode();
/* 666 */       if (fogNode != null) {
/* 667 */         this.sceneGroupNode.addChild(fogNode);
/* 668 */         this.scene.addFogNode(fogNode);
/*     */       }
/*     */     }
/*     */   }
/*     */
/*     */   void addBehaviors()
/*     */   {
/* 677 */     if (!this.sceneBehaviors.isEmpty()) {
/* 678 */       Enumeration e = this.sceneBehaviors.elements();
/* 679 */       while (e.hasMoreElements())
/* 680 */         this.scene.addBehaviorNode((Behavior)e.nextElement());
/*     */     }
/*     */   }
/*     */
/*     */   void addBackground()
/*     */   {
/* 692 */     if (this.background != null) {
/* 693 */       Background bgNode = this.background.getObjectNode();
/* 694 */       if (bgNode != null) {
/* 695 */         this.sceneGroupNode.addChild(bgNode);
/* 696 */         this.scene.addBackgroundNode(bgNode);
/*     */       }
/*     */     }
/*     */   }
/*     */ }

/* Location:           Z:\System\Library\Java\Extensions\j3dutils.jar
* Qualified Name:     com.sun.j3d.loaders.lw3d.Lw3dLoader
* JD-Core Version:    0.6.2
*/
TOP

Related Classes of com.sun.j3d.loaders.lw3d.Lw3dLoader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.