Package com.sun.j3d.loaders.lw3d

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

/*     */ package com.sun.j3d.loaders.lw3d;
/*     */
/*     */ import com.sun.j3d.loaders.IncorrectFormatException;
/*     */ import com.sun.j3d.utils.geometry.GeometryInfo;
/*     */ import com.sun.j3d.utils.geometry.NormalGenerator;
/*     */ import com.sun.j3d.utils.geometry.Stripifier;
/*     */ import java.io.FileNotFoundException;
/*     */ import java.net.URL;
/*     */ import java.util.Enumeration;
/*     */ import java.util.Vector;
/*     */ import javax.media.j3d.Appearance;
/*     */ import javax.media.j3d.ColoringAttributes;
/*     */ import javax.media.j3d.GeometryArray;
/*     */ import javax.media.j3d.LineArray;
/*     */ import javax.media.j3d.Material;
/*     */ import javax.media.j3d.PointArray;
/*     */ import javax.media.j3d.PointAttributes;
/*     */ import javax.media.j3d.Shape3D;
/*     */ import javax.media.j3d.Texture;
/*     */ import javax.media.j3d.TextureAttributes;
/*     */ import javax.media.j3d.TransparencyAttributes;
/*     */ import javax.media.j3d.TriangleFanArray;
/*     */ import javax.vecmath.Color3f;
/*     */ import javax.vecmath.Vector3f;
/*     */
/*     */ class J3dLwoParser extends LwoParser
/*     */ {
/*     */   float[] normalCoordsArray;
/*     */   int[] normalIndicesArray;
/*     */   Shape3D objectShape;
/*     */   Color3f color;
/*     */   Color3f diffuseColor;
/*     */   Color3f specularColor;
/*     */   Color3f emissiveColor;
/*     */   float shininess;
/*  80 */   Vector objectShapeList = new Vector();
/*     */
/*     */   J3dLwoParser(String fileName, int debugVals)
/*     */     throws FileNotFoundException
/*     */   {
/*  87 */     super(fileName, debugVals);
/*     */   }
/*     */
/*     */   J3dLwoParser(URL url, int debugVals) throws FileNotFoundException
/*     */   {
/*  92 */     super(url, debugVals);
/*     */   }
/*     */
/*     */   void getSurf(int length) throws FileNotFoundException {
/*  96 */     super.getSurf(length);
/*     */   }
/*     */
/*     */   void createJava3dGeometry()
/*     */     throws IncorrectFormatException
/*     */   {
/* 109 */     Enumeration e = this.shapeList.elements();
/* 110 */     while (e.hasMoreElements()) {
/* 111 */       int vertexFormat = 1;
/* 112 */       ShapeHolder shape = (ShapeHolder)e.nextElement();
/* 113 */       debugOutputLn(8, "about to create Arrays for Shape");
/* 114 */       debugOutputLn(2, "shape = " + shape);
/* 115 */       shape.createArrays(true);
/* 116 */       int vertexCount = shape.coordsArray.length / 3;
/* 117 */       int indexCount = 0;
/* 118 */       if (shape.facetIndices != null)
/* 119 */         indexCount = shape.facetIndices.length;
/* 120 */       debugOutputLn(2, "numSurf = " + shape.numSurf);
/*     */
/* 125 */       String surfName = (String)this.surfNameList.elementAt(shape.numSurf - 1);
/*     */
/* 127 */       LwoSurface surf = null;
/* 128 */       for (int surfNum = 0;
/* 129 */         surfNum < this.surfaceList.size();
/* 130 */         surfNum++) {
/* 131 */         LwoSurface tempSurf = (LwoSurface)this.surfaceList.elementAt(surfNum);
/*     */
/* 133 */         String tempSurfName = tempSurf.surfName;
/* 134 */         if (surfName.equals(tempSurfName)) {
/* 135 */           surf = tempSurf;
/* 136 */           break;
/*     */         }
/*     */       }
/* 139 */       if (surf == null) {
/* 140 */         throw new IncorrectFormatException("bad surf for surfnum/name = " + shape.numSurf + ", " + surfName);
/*     */       }
/*     */
/* 144 */       debugOutputLn(2, "surf = " + surf);
/*     */
/* 147 */       LwoTexture texture = surf.getTexture();
/*     */
/* 149 */       Appearance appearance = new Appearance();
/*     */       GeometryArray object;
/* 150 */       if (shape.facetSizes[0] == 1)
/*     */       {
/* 153 */         GeometryArray object = new PointArray(vertexCount, vertexFormat);
/*     */
/* 155 */         object.setCoordinates(0, shape.coordsArray);
/* 156 */         ColoringAttributes colorAtt = new ColoringAttributes(surf.getColor(), 0);
/*     */
/* 159 */         PointAttributes pointStyle = new PointAttributes();
/* 160 */         pointStyle.setPointSize(1.0F);
/*     */
/* 162 */         appearance.setColoringAttributes(colorAtt);
/* 163 */         appearance.setPointAttributes(pointStyle);
/*     */       }
/* 165 */       else if (shape.facetSizes[0] == 2)
/*     */       {
/* 168 */         debugOutputLn(8, "Creating IndexedLineArray");
/* 169 */         GeometryArray object = new LineArray(vertexCount, vertexFormat);
/*     */
/* 171 */         object.setCoordinates(0, shape.coordsArray);
/* 172 */         ColoringAttributes colorAtt = new ColoringAttributes(surf.getColor(), 0);
/*     */
/* 175 */         appearance.setColoringAttributes(colorAtt);
/*     */       }
/*     */       else
/*     */       {
/* 179 */         debugOutputLn(8, "Creating IndexedTriFanArray");
/*     */
/* 181 */         vertexFormat |= 2;
/*     */
/* 183 */         debugOutputLn(8, "about to process vertices/indices, facetIndices = " + shape.facetIndices);
/*     */
/* 185 */         if (shape.facetIndices != null) {
/* 186 */           float[] textureCoords = null;
/* 187 */           int[] textureIndices = null;
/*     */
/* 189 */           debugOutputLn(8, "setting vertexCount, normind = " + shape.normalIndices);
/*     */
/* 191 */           debugOutputLn(8, "vtxcount, format, indcount = " + vertexCount + ", " + vertexFormat + ", " + indexCount);
/*     */
/* 194 */           if (texture != null)
/*     */           {
/* 197 */             vertexFormat |= 32;
/* 198 */             textureCoords = new float[vertexCount * 2];
/* 199 */             textureIndices = new int[shape.facetIndices.length];
/* 200 */             calculateTextureCoords(texture, shape.coordsArray, shape.facetIndices, textureCoords, textureIndices);
/*     */
/* 203 */             debugOutputLn(8, "textureCoords:");
/* 204 */             debugOutputLn(8, "texture Coords, Indices.length = " + textureCoords.length + ", " + textureIndices.length);
/*     */           }
/* 206 */           debugOutputLn(8, "about to create GeometryInfo");
/*     */
/* 209 */           GeometryInfo gi = new GeometryInfo(3);
/*     */
/* 211 */           gi.setCoordinates(shape.coordsArray);
/* 212 */           gi.setCoordinateIndices(shape.facetIndices);
/* 213 */           gi.setStripCounts(shape.facetSizes);
/* 214 */           if (texture != null) {
/* 215 */             gi.setTextureCoordinateParams(1, 2);
/* 216 */             gi.setTextureCoordinates(0, textureCoords);
/* 217 */             gi.setTextureCoordinateIndices(0, textureIndices);
/*     */           }
/* 219 */           gi.recomputeIndices();
/* 220 */           NormalGenerator ng = new NormalGenerator(surf.getCreaseAngle());
/*     */
/* 222 */           ng.generateNormals(gi);
/* 223 */           Stripifier st = new Stripifier();
/* 224 */           st.stripify(gi);
/* 225 */           GeometryArray object = gi.getGeometryArray(true, true, false);
/* 226 */           debugOutputLn(8, "done.");
/*     */         }
/*     */         else
/*     */         {
/* 232 */           debugOutputLn(8, "about to create trifanarray with vertexCount, facetSizes.len = " + vertexCount + ", " + shape.facetSizes.length);
/*     */
/* 237 */           object = new TriangleFanArray(vertexCount, vertexFormat, shape.facetSizes);
/*     */
/* 241 */           object.setCoordinates(0, shape.coordsArray);
/* 242 */           object.setNormals(0, shape.normalCoords);
/* 243 */           debugOutputLn(2, "passed in normalCoords, length = " + shape.normalCoords.length);
/*     */         }
/*     */
/* 246 */         debugOutputLn(8, "created fan array");
/*     */
/* 249 */         Material material = new Material(surf.getColor(), surf.getEmissiveColor(), surf.getDiffuseColor(), surf.getSpecularColor(), surf.getShininess());
/*     */
/* 254 */         material.setLightingEnable(true);
/* 255 */         appearance.setMaterial(material);
/* 256 */         if (surf.getTransparency() != 0.0F) {
/* 257 */           TransparencyAttributes ta = new TransparencyAttributes();
/* 258 */           ta.setTransparency(surf.getTransparency());
/* 259 */           ta.setTransparencyMode(2);
/* 260 */           appearance.setTransparencyAttributes(ta);
/*     */         }
/* 262 */         if (texture != null) {
/* 263 */           debugOutputLn(8, "texture != null, enable texturing");
/* 264 */           Texture tex = texture.getTexture();
/* 265 */           tex.setEnable(true);
/* 266 */           appearance.setTexture(tex);
/* 267 */           TextureAttributes ta = new TextureAttributes();
/* 268 */           if (texture.getType().equals("DTEX"))
/* 269 */             ta.setTextureMode(2);
/* 270 */           else if (texture.getType().equals("CTEX"))
/* 271 */             ta.setTextureMode(3);
/* 272 */           appearance.setTextureAttributes(ta);
/*     */         }
/*     */         else {
/* 275 */           debugOutputLn(8, "texture == null, no texture to use");
/*     */         }
/*     */       }
/* 278 */       debugOutputLn(8, "done creating object");
/*     */
/* 281 */       shape.nullify();
/*     */
/* 283 */       this.objectShape = new Shape3D(object);
/*     */
/* 286 */       this.objectShape.setAppearance(appearance);
/* 287 */       this.objectShapeList.addElement(this.objectShape);
/*     */     }
/*     */   }
/*     */
/*     */   void calculateTextureCoords(LwoTexture texture, float[] verts, int[] indices, float[] textureCoords, int[] textureIndices)
/*     */   {
/* 404 */     debugOutputLn(1, "calculateTextureCoords()");
/*     */
/* 406 */     float sx = 0.0F; float sz = 0.0F; float ty = 0.0F; float tz = 0.0F;
/*     */
/* 408 */     int textureAxis = texture.getTextureAxis();
/* 409 */     Vector3f textureSize = texture.getTextureSize();
/* 410 */     Vector3f textureCenter = texture.getTextureCenter();
/*     */
/* 412 */     String mappingType = texture.getMappingType();
/* 413 */     if (mappingType.startsWith("Cylindrical")) {
/* 414 */       calculateCylindricalTextureCoords(textureAxis, textureSize, textureCenter, textureCoords, textureIndices, verts, indices);
/*     */     }
/* 418 */     else if (mappingType.startsWith("Spherical")) {
/* 419 */       calculateSphericalTextureCoords(textureAxis, textureCenter, textureCoords, textureIndices, verts, indices);
/*     */     }
/* 423 */     else if (mappingType.startsWith("Planar"))
/* 424 */       calculatePlanarTextureCoords(textureAxis, textureSize, textureCenter, textureCoords, textureIndices, verts, indices);
/*     */   }
/*     */
/*     */   double xyztoh(float x, float y, float z)
/*     */   {
/* 433 */     if ((x == 0.0D) && (z == 0.0D)) {
/* 434 */       return 0.0D;
/*     */     }
/* 436 */     if (z == 0.0D)
/* 437 */       return x < 0.0D ? 1.570796326794897D : -1.570796326794897D;
/* 438 */     if (z < 0.0D) {
/* 439 */       return -Math.atan(x / z) + 3.141592653589793D;
/*     */     }
/* 441 */     return -Math.atan(x / z);
/*     */   }
/*     */
/*     */   double xyztop(float x, float y, float z)
/*     */   {
/*     */     double p;
/*     */     double p;
/* 449 */     if ((x == 0.0D) && (z == 0.0D))
/*     */     {
/*     */       double p;
/* 450 */       if (y != 0.0D)
/* 451 */         p = y < 0.0D ? -1.570796326794897D : 1.570796326794897D;
/*     */       else
/* 453 */         p = 0.0D;
/*     */     }
/*     */     else {
/* 456 */       x = (float)Math.sqrt(x * x + z * z);
/*     */       double p;
/* 457 */       if (x == 0.0D)
/* 458 */         p = y < 0.0D ? -1.570796326794897D : 1.570796326794897D;
/*     */       else
/* 460 */         p = Math.atan(y / x);
/*     */     }
/* 462 */     return p;
/*     */   }
/*     */
/*     */   void calculateSphericalTextureCoords(int textureAxis, Vector3f textureCenter, float[] textureCoords, int[] textureIndices, float[] verts, int[] indices)
/*     */   {
/* 472 */     debugOutputLn(1, "calculateSphericalTextureCoords");
/*     */
/* 476 */     for (int i = 0; i < indices.length; i++) {
/* 477 */       float x = verts[(3 * indices[i])] - textureCenter.x;
/* 478 */       float y = verts[(3 * indices[i] + 1)] - textureCenter.y;
/* 479 */       float z = -(verts[(3 * indices[i] + 2)] + textureCenter.z);
/*     */       double t;
/* 480 */       if (textureAxis == 1) {
/* 481 */         double s = xyztoh(z, x, -y);
/* 482 */         t = xyztop(z, x, -y);
/*     */       }
/*     */       else
/*     */       {
/*     */         double t;
/* 484 */         if (textureAxis == 2) {
/* 485 */           double s = xyztoh(-x, y, z);
/* 486 */           t = xyztop(-x, y, z);
/*     */         }
/*     */         else {
/* 489 */           s = xyztoh(-x, z, -y);
/* 490 */           t = xyztop(-x, z, -y);
/*     */         }
/*     */       }
/* 492 */       double s = 1.0D - s / 6.283185307179586D;
/* 493 */       double t = -(0.5D - t / 3.141592653589793D);
/* 494 */       textureCoords[(indices[i] * 2)] = ((float)s);
/* 495 */       textureCoords[(indices[i] * 2 + 1)] = ((float)t);
/* 496 */       textureIndices[i] = indices[i];
/*     */     }
/*     */   }
/*     */
/*     */   void calculateCylindricalTextureCoords(int textureAxis, Vector3f textureSize, Vector3f textureCenter, float[] textureCoords, int[] textureIndices, float[] verts, int[] indices)
/*     */   {
/* 507 */     debugOutputLn(1, "calculateCylindricalTextureCoords");
/* 508 */     debugOutputLn(2, "axis, size, center, tc, ti, v, i = " + textureAxis + ", " + textureSize + ", " + textureCenter + ", " + textureCoords + ", " + textureIndices + ", " + verts + ", " + indices);
/*     */
/* 518 */     debugOutputLn(2, "Cyl Texture Coords:");
/* 519 */     for (int i = 0; i < indices.length; i++) {
/* 520 */       float x = verts[(3 * indices[i])] - textureCenter.x;
/* 521 */       float y = verts[(3 * indices[i] + 1)] - textureCenter.y;
/* 522 */       float z = -(verts[(3 * indices[i] + 2)] + textureCenter.z);
/*     */       double t;
/*     */       double t;
/* 524 */       if (textureAxis == 1) {
/* 525 */         double s = xyztoh(z, x, -y);
/* 526 */         t = x / textureSize.x + 0.5D;
/*     */       }
/*     */       else
/*     */       {
/*     */         double t;
/* 528 */         if (textureAxis == 2) {
/* 529 */           double s = xyztoh(-x, y, z);
/* 530 */           t = y / textureSize.y + 0.5D;
/*     */         }
/*     */         else {
/* 533 */           s = xyztoh(-x, z, -y);
/* 534 */           t = z / textureSize.z + 0.5D;
/*     */         }
/*     */       }
/* 536 */       double s = 1.0D - s / 6.283185307179586D;
/* 537 */       textureCoords[(indices[i] * 2)] = ((float)s);
/* 538 */       textureCoords[(indices[i] * 2 + 1)] = ((float)t);
/* 539 */       textureIndices[i] = indices[i];
/* 540 */       debugOutputLn(2, "x, y, z = " + x + ", " + y + ", " + z + "    " + "s, t = " + s + ", " + t);
/*     */     }
/*     */   }
/*     */
/*     */   void calculatePlanarTextureCoords(int textureAxis, Vector3f textureSize, Vector3f textureCenter, float[] textureCoords, int[] textureIndices, float[] verts, int[] indices)
/*     */   {
/* 552 */     debugOutputLn(1, "calculatePlanarTextureCoords");
/* 553 */     debugOutputLn(2, "size, center, axis = " + textureSize + textureCenter + ", " + textureAxis);
/*     */
/* 555 */     float sx = 0.0F; float sz = 0.0F; float ty = 0.0F; float tz = 0.0F;
/*     */
/* 558 */     if (textureAxis == 1) {
/* 559 */       sz = -1.0F / textureSize.z;
/* 560 */       ty = 1.0F / textureSize.y;
/*     */     }
/* 562 */     else if (textureAxis == 2) {
/* 563 */       sx = 1.0F / textureSize.x;
/* 564 */       tz = -1.0F / textureSize.z;
/*     */     }
/*     */     else {
/* 567 */       sx = 1.0F / textureSize.x;
/* 568 */       ty = 1.0F / textureSize.y;
/*     */     }
/*     */
/* 571 */     debugOutputLn(2, "Planar Texture Coords:");
/* 572 */     for (int i = 0; i < indices.length; i++) {
/* 573 */       float x = verts[(3 * indices[i])] - textureCenter.x;
/* 574 */       float y = verts[(3 * indices[i] + 1)] - textureCenter.y;
/* 575 */       float z = verts[(3 * indices[i] + 2)] + textureCenter.z;
/* 576 */       double s = x * sx + z * sz + 0.5D;
/* 577 */       double t = y * ty + z * tz + 0.5D;
/* 578 */       textureCoords[(indices[i] * 2)] = ((float)s);
/* 579 */       textureCoords[(indices[i] * 2 + 1)] = ((float)t);
/* 580 */       textureIndices[i] = indices[i];
/* 581 */       debugOutputLn(2, "x, y, z = " + x + ", " + y + ", " + z + "    " + "s, t = " + s + ", " + t);
/*     */     }
/*     */   }
/*     */
/*     */   Shape3D getJava3dShape()
/*     */   {
/* 589 */     return this.objectShape;
/*     */   }
/*     */
/*     */   Vector getJava3dShapeList() {
/* 593 */     return this.objectShapeList;
/*     */   }
/*     */ }

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

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

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.