Package com.sun.j3d.loaders.objectfile

Source Code of com.sun.j3d.loaders.objectfile.ObjectFile

/*      */ package com.sun.j3d.loaders.objectfile;
/*      */
/*      */ 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 com.sun.j3d.utils.geometry.GeometryInfo;
/*      */ import com.sun.j3d.utils.geometry.NormalGenerator;
/*      */ import com.sun.j3d.utils.geometry.Stripifier;
/*      */ 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.net.MalformedURLException;
/*      */ import java.net.URL;
/*      */ import java.util.ArrayList;
/*      */ import java.util.HashMap;
/*      */ import java.util.Iterator;
/*      */ import java.util.Set;
/*      */ import javax.media.j3d.BranchGroup;
/*      */ import javax.media.j3d.Shape3D;
/*      */ import javax.vecmath.Point3f;
/*      */ import javax.vecmath.TexCoord2f;
/*      */ import javax.vecmath.Vector3f;
/*      */
/*      */ public class ObjectFile
/*      */   implements Loader
/*      */ {
/*      */   private static final int DEBUG = 1;
/*      */   public static final int RESIZE = 64;
/*      */   public static final int TRIANGULATE = 128;
/*      */   public static final int REVERSE = 256;
/*      */   public static final int STRIPIFY = 512;
/*      */   private static final char BACKSLASH = '\\';
/*      */   private int flags;
/*  262 */   private String basePath = null;
/*  263 */   private URL baseUrl = null;
/*  264 */   private boolean fromUrl = false;
/*      */   private float radians;
/*      */   private ArrayList coordList;
/*      */   private ArrayList texList;
/*      */   private ArrayList normList;
/*      */   private ArrayList coordIdxList;
/*      */   private ArrayList texIdxList;
/*      */   private ArrayList normIdxList;
/*      */   private ArrayList stripCounts;
/*      */   private HashMap groups;
/*      */   private String curGroup;
/*      */   private HashMap sGroups;
/*      */   private String curSgroup;
/*      */   private HashMap groupMaterials;
/*      */   private HashMap triGroups;
/*      */   private ArrayList curTriGroup;
/*      */   private HashMap triSgroups;
/*      */   private ArrayList curTriSgroup;
/*  311 */   private Point3f[] coordArray = null;
/*  312 */   private Vector3f[] normArray = null;
/*  313 */   private TexCoord2f[] texArray = null;
/*      */   private long time;
/*  318 */   private ObjectFileMaterials materials = null;
/*      */
/*      */   void readVertex(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  324 */     Point3f p = new Point3f();
/*      */
/*  326 */     st.getNumber();
/*  327 */     p.x = ((float)st.nval);
/*  328 */     st.getNumber();
/*  329 */     p.y = ((float)st.nval);
/*  330 */     st.getNumber();
/*  331 */     p.z = ((float)st.nval);
/*      */
/*  336 */     st.skipToNextLine();
/*      */
/*  339 */     this.coordList.add(p);
/*      */   }
/*      */
/*      */   void readNormal(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  349 */     Vector3f p = new Vector3f();
/*      */
/*  351 */     st.getNumber();
/*  352 */     p.x = ((float)st.nval);
/*  353 */     st.getNumber();
/*  354 */     p.y = ((float)st.nval);
/*  355 */     st.getNumber();
/*  356 */     p.z = ((float)st.nval);
/*      */
/*  361 */     st.skipToNextLine();
/*      */
/*  364 */     this.normList.add(p);
/*      */   }
/*      */
/*      */   void readTexture(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  374 */     TexCoord2f p = new TexCoord2f();
/*      */
/*  376 */     st.getNumber();
/*  377 */     p.x = ((float)st.nval);
/*  378 */     st.getNumber();
/*  379 */     p.y = ((float)st.nval);
/*      */
/*  384 */     st.skipToNextLine();
/*      */
/*  387 */     this.texList.add(p);
/*      */   }
/*      */
/*      */   void readFace(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  403 */     int texIndex = 0; int normIndex = 0;
/*  404 */     int count = 0;
/*      */
/*  410 */     st.getToken();
/*      */
/*  412 */     while (st.ttype != 10)
/*      */     {
/*  414 */       st.pushBack();
/*  415 */       st.getNumber();
/*  416 */       int vertIndex = (int)st.nval - 1;
/*  417 */       if (vertIndex < 0) vertIndex += this.coordList.size() + 1;
/*  418 */       this.coordIdxList.add(new Integer(vertIndex));
/*      */
/*  421 */       st.getToken();
/*  422 */       if (st.ttype == 47)
/*      */       {
/*  425 */         st.getToken();
/*  426 */         if (st.ttype == -3)
/*      */         {
/*  428 */           st.pushBack();
/*  429 */           st.getNumber();
/*  430 */           texIndex = (int)st.nval - 1;
/*  431 */           if (texIndex < 0) texIndex += this.texList.size() + 1;
/*  432 */           this.texIdxList.add(new Integer(texIndex));
/*  433 */           st.getToken();
/*      */         }
/*      */
/*  437 */         if (st.ttype == 47)
/*      */         {
/*  440 */           st.getNumber();
/*  441 */           normIndex = (int)st.nval - 1;
/*  442 */           if (normIndex < 0) normIndex += this.normList.size() + 1;
/*  443 */           this.normIdxList.add(new Integer(normIndex));
/*  444 */           st.getToken();
/*      */         }
/*      */
/*      */       }
/*      */
/*  451 */       count++;
/*      */     }
/*      */
/*  454 */     Integer faceNum = new Integer(this.stripCounts.size());
/*  455 */     this.stripCounts.add(new Integer(count));
/*      */
/*  458 */     this.groups.put(faceNum, this.curGroup);
/*  459 */     if (this.curSgroup != null) this.sGroups.put(faceNum, this.curSgroup);
/*      */
/*  462 */     st.skipToNextLine();
/*      */   }
/*      */
/*      */   void readPartName(ObjectFileParser st)
/*      */   {
/*  472 */     st.getToken();
/*      */
/*  475 */     String curMat = (String)this.groupMaterials.get(this.curGroup);
/*      */
/*  478 */     if (st.ttype != -3) this.curGroup = "default"; else {
/*  479 */       this.curGroup = st.sval;
/*      */     }
/*      */
/*  483 */     if (this.groupMaterials.get(this.curGroup) == null)
/*      */     {
/*  485 */       this.groupMaterials.put(this.curGroup, curMat);
/*      */     }
/*      */
/*  488 */     st.skipToNextLine();
/*      */   }
/*      */
/*      */   void readMaterialName(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  498 */     st.getToken();
/*  499 */     if (st.ttype == -3) {
/*  500 */       this.groupMaterials.put(this.curGroup, new String(st.sval));
/*      */     }
/*      */
/*  506 */     st.skipToNextLine();
/*      */   }
/*      */
/*      */   void loadMaterialFile(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  521 */     String s = null;
/*      */
/*  524 */     st.lowerCaseMode(false);
/*      */     do
/*      */     {
/*  528 */       st.getToken();
/*  529 */       if (st.ttype == -3) s = st.sval;
/*      */     }
/*  530 */     while (st.ttype != 10);
/*      */
/*  532 */     this.materials.readMaterialFile(this.fromUrl, this.fromUrl ? this.baseUrl.toString() : this.basePath, s);
/*      */
/*  535 */     st.lowerCaseMode(true);
/*  536 */     st.skipToNextLine();
/*      */   }
/*      */
/*      */   void readSmoothingGroup(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  546 */     st.getToken();
/*  547 */     if (st.ttype != -3) {
/*  548 */       st.skipToNextLine();
/*  549 */       return;
/*      */     }
/*  551 */     if (st.sval.equals("off")) this.curSgroup = "0"; else {
/*  552 */       this.curSgroup = st.sval;
/*      */     }
/*  554 */     st.skipToNextLine();
/*      */   }
/*      */
/*      */   void readFile(ObjectFileParser st)
/*      */     throws ParsingErrorException
/*      */   {
/*  568 */     st.getToken();
/*  569 */     while (st.ttype != -1)
/*      */     {
/*  580 */       if (st.ttype == -3) {
/*  581 */         if (st.sval.equals("v"))
/*  582 */           readVertex(st);
/*  583 */         else if (st.sval.equals("vn"))
/*  584 */           readNormal(st);
/*  585 */         else if (st.sval.equals("vt"))
/*  586 */           readTexture(st);
/*  587 */         else if (st.sval.equals("f"))
/*  588 */           readFace(st);
/*  589 */         else if (st.sval.equals("fo"))
/*  590 */           readFace(st);
/*  591 */         else if (st.sval.equals("g"))
/*  592 */           readPartName(st);
/*  593 */         else if (st.sval.equals("s"))
/*  594 */           readSmoothingGroup(st);
/*  595 */         else if (st.sval.equals("p"))
/*  596 */           st.skipToNextLine();
/*  597 */         else if (st.sval.equals("l"))
/*  598 */           st.skipToNextLine();
/*  599 */         else if (st.sval.equals("mtllib"))
/*  600 */           loadMaterialFile(st);
/*  601 */         else if (st.sval.equals("usemtl"))
/*  602 */           readMaterialName(st);
/*  603 */         else if (st.sval.equals("maplib"))
/*  604 */           st.skipToNextLine();
/*  605 */         else if (st.sval.equals("usemap"))
/*  606 */           st.skipToNextLine();
/*      */         else {
/*  608 */           throw new ParsingErrorException("Unrecognized token, line " + st.lineno());
/*      */         }
/*      */
/*      */       }
/*      */
/*  613 */       st.skipToNextLine();
/*      */
/*  616 */       st.getToken();
/*      */     }
/*      */   }
/*      */
/*      */   public ObjectFile(int flags, float radians)
/*      */   {
/*  633 */     setFlags(flags);
/*  634 */     this.radians = radians;
/*      */   }
/*      */
/*      */   public ObjectFile(int flags)
/*      */   {
/*  647 */     this(flags, -1.0F);
/*      */   }
/*      */
/*      */   public ObjectFile()
/*      */   {
/*  659 */     this(0, -1.0F);
/*      */   }
/*      */
/*      */   private void setBasePathFromFilename(String fileName)
/*      */   {
/*  670 */     if (fileName.lastIndexOf(File.separator) == -1)
/*      */     {
/*  672 */       setBasePath("." + File.separator);
/*      */     }
/*  674 */     else setBasePath(fileName.substring(0, fileName.lastIndexOf(File.separator)));
/*      */   }
/*      */
/*      */   public Scene load(String filename)
/*      */     throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
/*      */   {
/*  693 */     setBasePathFromFilename(filename);
/*      */
/*  695 */     Reader reader = new BufferedReader(new FileReader(filename));
/*  696 */     return load(reader);
/*      */   }
/*      */
/*      */   private void setBaseUrlFromUrl(URL url)
/*      */     throws FileNotFoundException
/*      */   {
/*  703 */     String u = url.toString();
/*      */     String s;
/*      */     String s;
/*  705 */     if (u.lastIndexOf('/') == -1)
/*  706 */       s = url.getProtocol() + ":";
/*      */     else
/*  708 */       s = u.substring(0, u.lastIndexOf('/') + 1);
/*      */     try
/*      */     {
/*  711 */       this.baseUrl = new URL(s);
/*      */     }
/*      */     catch (MalformedURLException e) {
/*  714 */       throw new FileNotFoundException(e.getMessage());
/*      */     }
/*      */   }
/*      */
/*      */   public Scene load(URL url)
/*      */     throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
/*      */   {
/*  733 */     if (this.baseUrl == null) setBaseUrlFromUrl(url); BufferedReader reader;
/*      */     try
/*      */     {
/*  736 */       reader = new BufferedReader(new InputStreamReader(url.openStream()));
/*      */     }
/*      */     catch (IOException e) {
/*  739 */       throw new FileNotFoundException(e.getMessage());
/*      */     }
/*  741 */     this.fromUrl = true;
/*  742 */     return load(reader);
/*      */   }
/*      */
/*      */   private Point3f[] getLimits()
/*      */   {
/*  754 */     Point3f cur_vtx = new Point3f();
/*      */
/*  757 */     Point3f[] limit = new Point3f[2];
/*  758 */     limit[0] = new Point3f(3.4028235E+38F, 3.4028235E+38F, 3.4028235E+38F);
/*  759 */     limit[1] = new Point3f(1.4E-45F, 1.4E-45F, 1.4E-45F);
/*  760 */     for (int i = 0; i < this.coordList.size(); i++)
/*      */     {
/*  762 */       cur_vtx = (Point3f)this.coordList.get(i);
/*      */
/*  765 */       if (cur_vtx.x < limit[0].x) limit[0].x = cur_vtx.x;
/*  766 */       if (cur_vtx.x > limit[1].x) limit[1].x = cur_vtx.x;
/*  767 */       if (cur_vtx.y < limit[0].y) limit[0].y = cur_vtx.y;
/*  768 */       if (cur_vtx.y > limit[1].y) limit[1].y = cur_vtx.y;
/*  769 */       if (cur_vtx.z < limit[0].z) limit[0].z = cur_vtx.z;
/*  770 */       if (cur_vtx.z > limit[1].z) limit[1].z = cur_vtx.z;
/*      */
/*      */     }
/*      */
/*  779 */     return limit;
/*      */   }
/*      */
/*      */   private void resize()
/*      */   {
/*  790 */     Point3f cur_vtx = new Point3f();
/*      */
/*  793 */     Point3f[] limit = getLimits();
/*      */
/*  796 */     Vector3f offset = new Vector3f(-0.5F * (limit[0].x + limit[1].x), -0.5F * (limit[0].y + limit[1].y), -0.5F * (limit[0].z + limit[1].z));
/*      */
/*  806 */     float biggest_dif = limit[1].x - limit[0].x;
/*  807 */     if (biggest_dif < limit[1].y - limit[0].y)
/*  808 */       biggest_dif = limit[1].y - limit[0].y;
/*  809 */     if (biggest_dif < limit[1].z - limit[0].z)
/*  810 */       biggest_dif = limit[1].z - limit[0].z;
/*  811 */     biggest_dif /= 2.0F;
/*      */
/*  813 */     for (int i = 0; i < this.coordList.size(); i++)
/*      */     {
/*  815 */       cur_vtx = (Point3f)this.coordList.get(i);
/*      */
/*  817 */       cur_vtx.add(cur_vtx, offset);
/*      */
/*  819 */       cur_vtx.x /= biggest_dif;
/*  820 */       cur_vtx.y /= biggest_dif;
/*  821 */       cur_vtx.z /= biggest_dif;
/*      */     }
/*      */   }
/*      */
/*      */   private int[] objectToIntArray(ArrayList inList)
/*      */   {
/*  831 */     int[] outList = new int[inList.size()];
/*  832 */     for (int i = 0; i < inList.size(); i++) {
/*  833 */       outList[i] = ((Integer)inList.get(i)).intValue();
/*      */     }
/*  835 */     return outList;
/*      */   }
/*      */
/*      */   private Point3f[] objectToPoint3Array(ArrayList inList)
/*      */   {
/*  842 */     Point3f[] outList = new Point3f[inList.size()];
/*  843 */     for (int i = 0; i < inList.size(); i++) {
/*  844 */       outList[i] = ((Point3f)inList.get(i));
/*      */     }
/*  846 */     return outList;
/*      */   }
/*      */
/*      */   private TexCoord2f[] objectToTexCoord2Array(ArrayList inList)
/*      */   {
/*  853 */     TexCoord2f[] outList = new TexCoord2f[inList.size()];
/*  854 */     for (int i = 0; i < inList.size(); i++) {
/*  855 */       outList[i] = ((TexCoord2f)inList.get(i));
/*      */     }
/*  857 */     return outList;
/*      */   }
/*      */
/*      */   private Vector3f[] objectToVectorArray(ArrayList inList)
/*      */   {
/*  864 */     Vector3f[] outList = new Vector3f[inList.size()];
/*  865 */     for (int i = 0; i < inList.size(); i++) {
/*  866 */       outList[i] = ((Vector3f)inList.get(i));
/*      */     }
/*  868 */     return outList;
/*      */   }
/*      */
/*      */   private int[] groupIndices(ArrayList sourceList, ArrayList group)
/*      */   {
/*  879 */     int[] indices = new int[group.size() * 3];
/*  880 */     for (int i = 0; i < group.size(); i++) {
/*  881 */       int j = ((Integer)group.get(i)).intValue();
/*  882 */       indices[(i * 3 + 0)] = ((Integer)sourceList.get(j + 0)).intValue();
/*  883 */       indices[(i * 3 + 1)] = ((Integer)sourceList.get(j + 1)).intValue();
/*  884 */       indices[(i * 3 + 2)] = ((Integer)sourceList.get(j + 2)).intValue();
/*      */     }
/*  886 */     return indices;
/*      */   }
/*      */
/*      */   private void smoothingGroupNormals()
/*      */   {
/*  902 */     NormalGenerator ng = new NormalGenerator(this.radians == -1.0F ? 3.141592653589793D : this.radians);
/*      */
/*  904 */     NormalGenerator ng0 = new NormalGenerator(0.0D);
/*  905 */     this.normList.clear();
/*  906 */     this.normIdxList = null;
/*  907 */     int[] newNormIdxArray = new int[this.coordIdxList.size()];
/*      */
/*  909 */     Iterator e = this.triSgroups.keySet().iterator();
/*  910 */     while (e.hasNext()) {
/*  911 */       String curname = (String)e.next();
/*  912 */       ArrayList triList = (ArrayList)this.triSgroups.get(curname);
/*      */
/*  915 */       if (triList.size() > 0)
/*      */       {
/*  917 */         GeometryInfo gi = new GeometryInfo(1);
/*      */
/*  919 */         gi.setCoordinateIndices(groupIndices(this.coordIdxList, triList));
/*  920 */         gi.setCoordinates(this.coordArray);
/*      */
/*  922 */         if (curname.equals("0")) ng0.generateNormals(gi); else {
/*  923 */           ng.generateNormals(gi);
/*      */         }
/*      */
/*  926 */         Vector3f[] genNorms = gi.getNormals();
/*  927 */         int[] genNormIndices = gi.getNormalIndices();
/*      */
/*  934 */         int normIdx = 0;
/*      */
/*  936 */         for (int i = 0; i < triList.size(); i++)
/*      */         {
/*  939 */           int idx = ((Integer)triList.get(i)).intValue();
/*      */
/*  942 */           for (int j = 0; j < 3; j++)
/*      */           {
/*  945 */             newNormIdxArray[(idx + j)] = this.normList.size();
/*      */
/*  948 */             this.normList.add(genNorms[genNormIndices[(normIdx++)]]);
/*      */           }
/*      */         }
/*      */       }
/*      */     }
/*  953 */     this.normIdxList = new ArrayList(this.coordIdxList.size());
/*  954 */     for (int i = 0; i < this.coordIdxList.size(); i++) {
/*  955 */       this.normIdxList.add(new Integer(newNormIdxArray[i]));
/*      */     }
/*  957 */     this.normArray = objectToVectorArray(this.normList);
/*      */   }
/*      */
/*      */   private void convertToTriangles()
/*      */   {
/*  983 */     boolean triangulate = (this.flags & 0x80) != 0;
/*  984 */     boolean textures = (!this.texList.isEmpty()) && (!this.texIdxList.isEmpty()) && (this.texIdxList.size() == this.coordIdxList.size());
/*      */
/*  986 */     boolean normals = (!this.normList.isEmpty()) && (!this.normIdxList.isEmpty()) && (this.normIdxList.size() == this.coordIdxList.size());
/*      */
/*  988 */     int numFaces = this.stripCounts.size();
/*  989 */     boolean haveSgroups = this.curSgroup != null;
/*      */
/*  991 */     this.triGroups = new HashMap(50);
/*  992 */     if (haveSgroups) this.triSgroups = new HashMap(50);
/*      */
/*  994 */     ArrayList newCoordIdxList = null;
/*  995 */     ArrayList newTexIdxList = null;
/*  996 */     ArrayList newNormIdxList = null;
/*      */
/*  998 */     if (triangulate) {
/*  999 */       GeometryInfo gi = new GeometryInfo(5);
/* 1000 */       gi.setStripCounts(objectToIntArray(this.stripCounts));
/* 1001 */       gi.setCoordinates(this.coordArray);
/* 1002 */       gi.setCoordinateIndices(objectToIntArray(this.coordIdxList));
/* 1003 */       if (textures) {
/* 1004 */         gi.setTextureCoordinateParams(1, 2);
/* 1005 */         gi.setTextureCoordinates(0, this.texArray);
/* 1006 */         gi.setTextureCoordinateIndices(0, objectToIntArray(this.texIdxList));
/*      */       }
/* 1008 */       if (normals) {
/* 1009 */         gi.setNormals(this.normArray);
/* 1010 */         gi.setNormalIndices(objectToIntArray(this.normIdxList));
/*      */       }
/* 1012 */       gi.convertToIndexedTriangles();
/*      */
/* 1017 */       int[] coordIndicesArray = gi.getCoordinateIndices();
/*      */
/* 1021 */       int tris = 0;
/* 1022 */       for (int i = 0; i < numFaces; i++) {
/* 1023 */         tris += ((Integer)this.stripCounts.get(i)).intValue() - 2;
/*      */       }
/* 1025 */       if (coordIndicesArray.length != tris * 3)
/*      */       {
/* 1028 */         triangulate = false;
/*      */       }
/*      */       else {
/* 1031 */         int[] texIndicesArray = gi.getTextureCoordinateIndices();
/* 1032 */         int[] normIndicesArray = gi.getNormalIndices();
/*      */
/* 1035 */         this.coordIdxList.clear();
/* 1036 */         this.texIdxList.clear();
/* 1037 */         this.normIdxList.clear();
/* 1038 */         for (int i = 0; i < coordIndicesArray.length; i++) {
/* 1039 */           this.coordIdxList.add(new Integer(coordIndicesArray[i]));
/* 1040 */           if (textures) this.texIdxList.add(new Integer(texIndicesArray[i]));
/* 1041 */           if (normals) this.normIdxList.add(new Integer(normIndicesArray[i]));
/*      */         }
/*      */       }
/*      */     }
/*      */
/* 1046 */     if (!triangulate) {
/* 1047 */       newCoordIdxList = new ArrayList();
/* 1048 */       if (textures) newTexIdxList = new ArrayList();
/* 1049 */       if (normals) newNormIdxList = new ArrayList();
/*      */
/*      */     }
/*      */
/* 1054 */     int baseVertex = 0;
/* 1055 */     for (int f = 0; f < numFaces; f++) {
/* 1056 */       int faceSize = ((Integer)this.stripCounts.get(f)).intValue();
/*      */
/* 1059 */       Integer curFace = new Integer(f);
/* 1060 */       this.curGroup = ((String)this.groups.get(curFace));
/*      */
/* 1063 */       this.curTriGroup = ((ArrayList)this.triGroups.get(this.curGroup));
/* 1064 */       if (this.curTriGroup == null) {
/* 1065 */         this.curTriGroup = new ArrayList();
/* 1066 */         this.triGroups.put(this.curGroup, this.curTriGroup);
/*      */       }
/*      */
/* 1070 */       if (haveSgroups) {
/* 1071 */         this.curSgroup = ((String)this.sGroups.get(curFace));
/* 1072 */         if (this.curSgroup == null)
/*      */         {
/* 1076 */           this.curSgroup = "0";
/*      */         }
/* 1078 */         this.curTriSgroup = ((ArrayList)this.triSgroups.get(this.curSgroup));
/* 1079 */         if (this.curTriSgroup == null) {
/* 1080 */           this.curTriSgroup = new ArrayList();
/* 1081 */           this.triSgroups.put(this.curSgroup, this.curTriSgroup);
/*      */         }
/*      */       }
/*      */
/* 1085 */       if (triangulate)
/*      */       {
/* 1088 */         for (int t = 0; t < faceSize - 2; t++)
/*      */         {
/* 1091 */           Integer triBaseVertex = new Integer(baseVertex);
/* 1092 */           this.curTriGroup.add(triBaseVertex);
/* 1093 */           if (haveSgroups) this.curTriSgroup.add(triBaseVertex);
/*      */
/* 1095 */           baseVertex += 3;
/*      */         }
/*      */       }
/*      */       else {
/* 1099 */         for (int v = 0; v < faceSize - 2; v++)
/*      */         {
/* 1101 */           Integer triBaseVertex = new Integer(newCoordIdxList.size());
/* 1102 */           this.curTriGroup.add(triBaseVertex);
/* 1103 */           if (haveSgroups) this.curTriSgroup.add(triBaseVertex);
/*      */
/* 1105 */           newCoordIdxList.add(this.coordIdxList.get(baseVertex));
/* 1106 */           newCoordIdxList.add(this.coordIdxList.get(baseVertex + v + 1));
/* 1107 */           newCoordIdxList.add(this.coordIdxList.get(baseVertex + v + 2));
/*      */
/* 1109 */           if (textures) {
/* 1110 */             newTexIdxList.add(this.texIdxList.get(baseVertex));
/* 1111 */             newTexIdxList.add(this.texIdxList.get(baseVertex + v + 1));
/* 1112 */             newTexIdxList.add(this.texIdxList.get(baseVertex + v + 2));
/*      */           }
/*      */
/* 1115 */           if (normals) {
/* 1116 */             newNormIdxList.add(this.normIdxList.get(baseVertex));
/* 1117 */             newNormIdxList.add(this.normIdxList.get(baseVertex + v + 1));
/* 1118 */             newNormIdxList.add(this.normIdxList.get(baseVertex + v + 2));
/*      */           }
/*      */         }
/* 1121 */         baseVertex += faceSize;
/*      */       }
/*      */
/*      */     }
/*      */
/* 1126 */     this.stripCounts = null;
/* 1127 */     this.groups = null;
/* 1128 */     this.sGroups = null;
/*      */
/* 1130 */     if (!triangulate) {
/* 1131 */       this.coordIdxList = newCoordIdxList;
/* 1132 */       this.texIdxList = newTexIdxList;
/* 1133 */       this.normIdxList = newNormIdxList;
/*      */     }
/*      */   }
/*      */
/*      */   private SceneBase makeScene()
/*      */   {
/* 1142 */     SceneBase scene = new SceneBase();
/* 1143 */     BranchGroup group = new BranchGroup();
/* 1144 */     scene.setSceneGroup(group);
/*      */
/* 1146 */     boolean gen_norms = (this.normList.isEmpty()) || (this.normIdxList.isEmpty()) || (this.normIdxList.size() != this.coordIdxList.size());
/*      */
/* 1148 */     boolean do_tex = (!this.texList.isEmpty()) && (!this.texIdxList.isEmpty()) && (this.texIdxList.size() == this.coordIdxList.size());
/*      */
/* 1152 */     this.coordArray = objectToPoint3Array(this.coordList);
/* 1153 */     if (!gen_norms) this.normArray = objectToVectorArray(this.normList);
/* 1154 */     if (do_tex) this.texArray = objectToTexCoord2Array(this.texList);
/*      */
/* 1156 */     convertToTriangles();
/*      */
/* 1164 */     if ((gen_norms) && (this.curSgroup != null)) {
/* 1165 */       smoothingGroupNormals();
/* 1166 */       gen_norms = false;
/*      */     }
/*      */
/* 1174 */     NormalGenerator ng = null;
/* 1175 */     if (gen_norms) ng = new NormalGenerator(this.radians);
/*      */
/* 1177 */     Stripifier strippy = null;
/* 1178 */     if ((this.flags & 0x200) != 0) strippy = new Stripifier();
/*      */
/* 1180 */     long t1 = 0L; long t2 = 0L; long t3 = 0L; long t4 = 0L;
/*      */
/* 1183 */     Iterator e = this.triGroups.keySet().iterator();
/* 1184 */     while (e.hasNext())
/*      */     {
/* 1186 */       String curname = (String)e.next();
/* 1187 */       ArrayList triList = (ArrayList)this.triGroups.get(curname);
/*      */
/* 1190 */       if (triList.size() > 0)
/*      */       {
/* 1192 */         GeometryInfo gi = new GeometryInfo(1);
/*      */
/* 1194 */         gi.setCoordinateIndices(groupIndices(this.coordIdxList, triList));
/* 1195 */         gi.setCoordinates(this.coordArray);
/*      */
/* 1197 */         if (do_tex) {
/* 1198 */           gi.setTextureCoordinateParams(1, 2);
/* 1199 */           gi.setTextureCoordinates(0, this.texArray);
/* 1200 */           gi.setTextureCoordinateIndices(0, groupIndices(this.texIdxList, triList));
/*      */         }
/*      */
/* 1204 */         if (gen_norms) {
/* 1205 */           if ((this.flags & 0x100) != 0) gi.reverse();
/* 1206 */           ng.generateNormals(gi);
/*      */         }
/*      */         else
/*      */         {
/* 1213 */           gi.setNormalIndices(groupIndices(this.normIdxList, triList));
/* 1214 */           gi.setNormals(this.normArray);
/* 1215 */           if ((this.flags & 0x100) != 0) gi.reverse();
/*      */         }
/*      */
/* 1218 */         if ((this.flags & 0x200) != 0) {
/* 1219 */           strippy.stripify(gi);
/*      */         }
/*      */
/* 1228 */         Shape3D shape = new Shape3D();
/*      */
/* 1230 */         shape.setGeometry(gi.getGeometryArray(true, true, false));
/*      */
/* 1232 */         String matName = (String)this.groupMaterials.get(curname);
/* 1233 */         this.materials.assignMaterial(matName, shape);
/*      */
/* 1235 */         group.addChild(shape);
/* 1236 */         scene.addNamedObject(curname, shape);
/*      */       }
/*      */
/*      */     }
/*      */
/* 1246 */     return scene;
/*      */   }
/*      */
/*      */   public Scene load(Reader reader)
/*      */     throws FileNotFoundException, IncorrectFormatException, ParsingErrorException
/*      */   {
/* 1263 */     ObjectFileParser st = new ObjectFileParser(reader);
/*      */
/* 1265 */     this.coordList = new ArrayList();
/* 1266 */     this.texList = new ArrayList();
/* 1267 */     this.normList = new ArrayList();
/* 1268 */     this.coordIdxList = new ArrayList();
/* 1269 */     this.texIdxList = new ArrayList();
/* 1270 */     this.normIdxList = new ArrayList();
/* 1271 */     this.groups = new HashMap(50);
/* 1272 */     this.curGroup = "default";
/* 1273 */     this.sGroups = new HashMap(50);
/* 1274 */     this.curSgroup = null;
/* 1275 */     this.stripCounts = new ArrayList();
/* 1276 */     this.groupMaterials = new HashMap(50);
/* 1277 */     this.groupMaterials.put(this.curGroup, "default");
/* 1278 */     this.materials = new ObjectFileMaterials();
/*      */
/* 1280 */     this.time = 0L;
/*      */
/* 1285 */     readFile(st);
/*      */
/* 1293 */     if ((this.flags & 0x40) != 0) resize();
/*      */
/* 1295 */     return makeScene();
/*      */   }
/*      */
/*      */   public void setBaseUrl(URL url)
/*      */   {
/* 1308 */     this.baseUrl = url;
/*      */   }
/*      */
/*      */   public URL getBaseUrl()
/*      */   {
/* 1319 */     return this.baseUrl;
/*      */   }
/*      */
/*      */   public void setBasePath(String pathName)
/*      */   {
/* 1332 */     this.basePath = pathName;
/* 1333 */     if ((this.basePath == null) || (this.basePath == ""))
/* 1334 */       this.basePath = ("." + File.separator);
/* 1335 */     this.basePath = this.basePath.replace('/', File.separatorChar);
/* 1336 */     this.basePath = this.basePath.replace('\\', File.separatorChar);
/* 1337 */     if (!this.basePath.endsWith(File.separator))
/* 1338 */       this.basePath += File.separator;
/*      */   }
/*      */
/*      */   public String getBasePath()
/*      */   {
/* 1349 */     return this.basePath;
/*      */   }
/*      */
/*      */   public void setFlags(int flags)
/*      */   {
/* 1363 */     this.flags = flags;
/*      */   }
/*      */
/*      */   public int getFlags()
/*      */   {
/* 1378 */     return this.flags;
/*      */   }
/*      */ }

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

Related Classes of com.sun.j3d.loaders.objectfile.ObjectFile

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.