Package com.sun.j3d.loaders.lw3d

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

/*     */ package com.sun.j3d.loaders.lw3d;
/*     */
/*     */ import com.sun.j3d.loaders.IncorrectFormatException;
/*     */ import com.sun.j3d.loaders.ParsingErrorException;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.net.URL;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Vector;
/*     */
/*     */ class LwoParser extends ParserObject
/*     */ {
/*     */   LWOBFileReader theReader;
/*     */   int currLength;
/*     */   float[] coordsArray;
/*     */   float[] normalCoordsArray;
/*     */   int[] facetIndicesArray;
/*     */   int[] facetSizesArray;
/*     */   int[] normalIndicesArray;
/*  80 */   int red = 255; int green = 255; int blue = 255;
/*  81 */   float diffuse = 0.0F; float specular = 0.0F; float transparency = 0.0F; float luminosity = 0.0F;
/*  82 */   int gloss = 128;
/*  83 */   Vector surfNameList = null;
/*  84 */   Vector surfaceList = new Vector(200);
/*  85 */   Vector shapeList = new Vector(200);
/*     */
/*     */   LwoParser(String fileName, int debugVals)
/*     */     throws FileNotFoundException
/*     */   {
/*  94 */     super(debugVals);
/*  95 */     debugOutputLn(1, "parser()");
/*  96 */     long start = System.currentTimeMillis();
/*  97 */     this.theReader = new LWOBFileReader(fileName);
/*  98 */     debugOutputLn(32, " file opened in " + (System.currentTimeMillis() - start));
/*     */
/* 100 */     parseFile();
/*     */   }
/*     */
/*     */   LwoParser(URL url, int debugVals) throws FileNotFoundException
/*     */   {
/* 105 */     super(debugVals);
/* 106 */     debugOutputLn(1, "parser()");
/*     */     try {
/* 108 */       long start = System.currentTimeMillis();
/* 109 */       this.theReader = new LWOBFileReader(url);
/* 110 */       debugOutputLn(32, " file opened in " + (System.currentTimeMillis() - start));
/*     */     }
/*     */     catch (IOException ex)
/*     */     {
/* 114 */       throw new FileNotFoundException(url.toString());
/*     */     }
/* 116 */     parseFile();
/*     */   }
/*     */
/*     */   int skipDetailPolygons(int numPolys)
/*     */     throws ParsingErrorException
/*     */   {
/* 127 */     debugOutputLn(1, "skipDetailPolygons(), numPolys = " + numPolys);
/* 128 */     int lengthRead = 0;
/*     */     try
/*     */     {
/* 132 */       for (int polyNum = 0; polyNum < numPolys; polyNum++) {
/* 133 */         debugOutputLn(2, "polyNum = " + polyNum);
/* 134 */         int numVerts = this.theReader.getShortInt();
/* 135 */         this.theReader.skip(numVerts * 2 + 2);
/* 136 */         lengthRead += numVerts * 2 + 4;
/*     */       }
/*     */     }
/*     */     catch (IOException e) {
/* 140 */       debugOutputLn(16, "Exception in reading detail polys: " + e);
/* 141 */       throw new ParsingErrorException(e.getMessage());
/*     */     }
/* 143 */     return lengthRead;
/*     */   }
/*     */
/*     */   ShapeHolder getAppropriateShape(int numSurf, int numVerts)
/*     */   {
/* 151 */     Enumeration e = this.shapeList.elements();
/* 152 */     while (e.hasMoreElements()) {
/* 153 */       ShapeHolder shape = (ShapeHolder)e.nextElement();
/* 154 */       if ((shape.numSurf == numSurf) && (
/* 155 */         (shape.numVerts == numVerts) || ((shape.numVerts > 3) && (numVerts > 3))))
/*     */       {
/* 158 */         return shape;
/*     */       }
/*     */     }
/* 160 */     return null;
/*     */   }
/*     */
/*     */   void getPols(int length)
/*     */   {
/* 169 */     debugOutputLn(1, "getPols(len), len = " + length);
/*     */
/* 171 */     int lengthRead = 0;
/* 172 */     int prevNumVerts = -1;
/* 173 */     int prevNumSurf = 0;
/*     */
/* 176 */     Vector facetSizesList = new Vector(length / 6);
/*     */
/* 180 */     int[] facetIndicesArray = new int[length / 2];
/* 181 */     ShapeHolder shape = new ShapeHolder(this.debugPrinter.getValidOutput());
/* 182 */     debugOutputLn(2, "new shape = " + shape);
/* 183 */     shape.coordsArray = this.coordsArray;
/* 184 */     shape.facetSizesList = facetSizesList;
/*     */
/* 186 */     shape.facetIndicesArray = facetIndicesArray;
/* 187 */     this.shapeList.addElement(shape);
/*     */
/* 190 */     boolean firstTime = true;
/* 191 */     while (lengthRead < length) {
/* 192 */       int numVerts = this.theReader.getShortInt();
/* 193 */       lengthRead += 2;
/* 194 */       int[] intArray = new int[numVerts];
/* 195 */       for (int i = 0; i < numVerts; i++) {
/* 196 */         intArray[i] = this.theReader.getShortInt();
/* 197 */         lengthRead += 2;
/*     */       }
/*     */
/* 200 */       int numSurf = this.theReader.getShortInt();
/* 201 */       lengthRead += 2;
/* 202 */       long startTimeBuff = 0L; long startTimeList = 0L;
/* 203 */       if ((!firstTime) && ((numSurf != prevNumSurf) || ((numVerts != prevNumVerts) && ((prevNumVerts < 3) || (numVerts < 3)))))
/*     */       {
/* 209 */         shape = getAppropriateShape(numSurf, numVerts);
/* 210 */         if (shape == null)
/*     */         {
/* 212 */           facetSizesList = new Vector(length / 6);
/* 213 */           facetIndicesArray = new int[length / 2];
/* 214 */           shape = new ShapeHolder(this.debugPrinter.getValidOutput());
/* 215 */           shape.coordsArray = this.coordsArray;
/* 216 */           shape.facetSizesList = facetSizesList;
/*     */
/* 218 */           shape.facetIndicesArray = facetIndicesArray;
/* 219 */           shape.numSurf = numSurf;
/* 220 */           shape.numVerts = numVerts;
/* 221 */           this.shapeList.addElement(shape);
/*     */         }
/*     */         else {
/* 224 */           facetSizesList = shape.facetSizesList;
/* 225 */           facetIndicesArray = shape.facetIndicesArray;
/*     */         }
/*     */       }
/*     */       else {
/* 229 */         shape.numSurf = numSurf;
/* 230 */         shape.numVerts = numVerts;
/*     */       }
/* 232 */       prevNumVerts = numVerts;
/* 233 */       prevNumSurf = numSurf;
/* 234 */       facetSizesList.addElement(new Integer(numVerts));
/*     */
/* 236 */       int currPtr = 0;
/* 237 */       System.arraycopy(intArray, 0, facetIndicesArray, shape.currentNumIndices, numVerts);
/*     */
/* 240 */       shape.currentNumIndices += numVerts;
/* 241 */       if (numSurf < 0) {
/* 242 */         int numPolys = this.theReader.getShortInt();
/* 243 */         lengthRead += skipDetailPolygons(numPolys);
/* 244 */         shape.numSurf = ((shape.numSurf ^ 0xFFFFFFFF) & 0xFFFF);
/* 245 */         if (shape.numSurf == 0)
/* 246 */           shape.numSurf = 1;
/*     */       }
/* 248 */       firstTime = false;
/*     */     }
/*     */   }
/*     */
/*     */   void getSrfs(int length)
/*     */   {
/* 258 */     String surfName = new String();
/* 259 */     this.surfNameList = new Vector(length / 2);
/* 260 */     int lengthRead = 0;
/* 261 */     int stopMarker = this.theReader.getMarker() + length;
/*     */
/* 263 */     int surfIndex = 0;
/* 264 */     while (this.theReader.getMarker() < stopMarker) {
/* 265 */       debugOutputLn(2, "marker, stop = " + this.theReader.getMarker() + ", " + stopMarker);
/*     */
/* 267 */       debugOutputLn(8, "About to call getString");
/* 268 */       surfName = this.theReader.getString();
/* 269 */       debugOutputLn(2, "Surfname = " + surfName);
/* 270 */       this.surfNameList.addElement(surfName);
/*     */     }
/*     */   }
/*     */
/*     */   void getPnts(int length)
/*     */     throws ParsingErrorException
/*     */   {
/* 278 */     int numVerts = length / 12;
/*     */
/* 281 */     this.coordsArray = new float[numVerts * 3];
/* 282 */     this.theReader.getVerts(this.coordsArray, numVerts);
/*     */   }
/*     */
/*     */   void getSurf(int length)
/*     */     throws FileNotFoundException
/*     */   {
/* 290 */     debugOutputLn(1, "getSurf()");
/*     */
/* 295 */     LwoSurface surf = new LwoSurface(this.theReader, length, this.debugPrinter.getValidOutput());
/*     */
/* 297 */     this.surfaceList.addElement(surf);
/*     */   }
/*     */
/*     */   int parseFile()
/*     */     throws FileNotFoundException, IncorrectFormatException
/*     */   {
/* 306 */     debugOutputLn(1, "parseFile()");
/* 307 */     int length = 0;
/* 308 */     int lengthRead = 0;
/* 309 */     int fileLength = 100000;
/*     */
/* 311 */     long loopStartTime = System.currentTimeMillis();
/*     */
/* 313 */     String tokenString = this.theReader.getToken();
/*     */
/* 316 */     while ((tokenString != null) && (lengthRead < fileLength)) {
/* 317 */       long startTime = System.currentTimeMillis();
/*     */
/* 319 */       length = this.theReader.getInt();
/*     */
/* 321 */       lengthRead += 4;
/*     */
/* 326 */       if (tokenString.equals("FORM"))
/*     */       {
/* 328 */         fileLength = length + 4;
/* 329 */         length = 0;
/* 330 */         tokenString = this.theReader.getToken();
/* 331 */         lengthRead += 4;
/* 332 */         if (!tokenString.equals("LWOB")) {
/* 333 */           throw new IncorrectFormatException("File not of FORM-length-LWOB format");
/*     */         }
/*     */       }
/* 336 */       else if (tokenString.equals("PNTS"))
/*     */       {
/* 338 */         getPnts(length);
/* 339 */         debugOutputLn(32, "done with " + tokenString + " in " + (System.currentTimeMillis() - startTime));
/*     */       }
/* 342 */       else if (tokenString.equals("POLS"))
/*     */       {
/* 344 */         getPols(length);
/* 345 */         debugOutputLn(32, "done with " + tokenString + " in " + (System.currentTimeMillis() - startTime));
/*     */       }
/* 348 */       else if (tokenString.equals("SRFS"))
/*     */       {
/* 350 */         getSrfs(length);
/* 351 */         debugOutputLn(32, "done with " + tokenString + " in " + (System.currentTimeMillis() - startTime));
/*     */       }
/* 354 */       else if (tokenString.equals("CRVS"))
/*     */       {
/* 356 */         this.theReader.skipLength(length);
/*     */       }
/* 360 */       else if (tokenString.equals("PCHS"))
/*     */       {
/* 362 */         this.theReader.skipLength(length);
/*     */       }
/* 366 */       else if (tokenString.equals("SURF"))
/*     */       {
/* 368 */         getSurf(length);
/*     */
/* 370 */         debugOutputLn(32, "done with " + tokenString + " in " + (System.currentTimeMillis() - startTime));
/*     */       }
/* 373 */       else if (!tokenString.equals("LWOB"))
/*     */       {
/* 378 */         this.theReader.skipLength(length);
/*     */       }
/*     */
/* 382 */       lengthRead += length;
/* 383 */       if (lengthRead < fileLength)
/*     */       {
/* 386 */         tokenString = this.theReader.getToken();
/* 387 */         lengthRead += 4;
/*     */       }
/*     */     }
/*     */
/* 391 */     debugOutputLn(32, "done with parseFile in " + (System.currentTimeMillis() - loopStartTime));
/*     */
/* 393 */     return 0;
/*     */   }
/*     */
/*     */   static void main(String[] args)
/*     */   {
/*     */     String fileName;
/*     */     String fileName;
/* 401 */     if (args.length == 0)
/* 402 */       fileName = "cube.obj";
/*     */     else
/* 404 */       fileName = args[0];
/*     */     try
/*     */     {
/* 407 */       theParser = new LwoParser(fileName, 0);
/*     */     }
/*     */     catch (FileNotFoundException e)
/*     */     {
/*     */       LwoParser theParser;
/* 410 */       System.err.println(e.getMessage());
/* 411 */       e.printStackTrace();
/* 412 */       System.exit(1);
/*     */     }
/*     */   }
/*     */ }

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

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

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.