Package com.sun.j3d.utils.geometry

Source Code of com.sun.j3d.utils.geometry.GeomBuffer

/*     */ package com.sun.j3d.utils.geometry;
/*     */
/*     */ import javax.media.j3d.GeometryArray;
/*     */ import javax.media.j3d.QuadArray;
/*     */ import javax.media.j3d.TriangleArray;
/*     */ import javax.media.j3d.TriangleFanArray;
/*     */ import javax.media.j3d.TriangleStripArray;
/*     */ import javax.vecmath.Point3f;
/*     */ import javax.vecmath.TexCoord2f;
/*     */ import javax.vecmath.Vector3f;
/*     */
/*     */ class GeomBuffer
/*     */ {
/*     */   static final int QUAD_STRIP = 1;
/*     */   static final int TRIANGLES = 2;
/*     */   static final int QUADS = 4;
/*     */   static final int TRIANGLE_FAN = 16;
/*     */   static final int TRIANGLE_STRIP = 32;
/*     */   private int flags;
/*     */   static final int GENERATE_NORMALS = 1;
/*     */   static final int GENERATE_TEXTURE_COORDS = 2;
/*  97 */   Point3f[] pts = null;
/*  98 */   Vector3f[] normals = null;
/*  99 */   TexCoord2f[] tcoords = null;
/*     */   int currVertCnt;
/*     */   int currPrimCnt;
/* 102 */   int[] currPrimType = null;
/* 103 */   int[] currPrimStartVertex = null;
/* 104 */   int[] currPrimEndVertex = null;
/*     */   GeometryArray geometry;
/* 106 */   int numVerts = 0;
/* 107 */   int numTris = 0;
/* 108 */   int numTexUnit = 1;
/* 109 */   int[] texCoordSetMap = null;
/*     */   static final int debug = 0;
/*     */
/*     */   GeomBuffer(int numVerts, int numTexUnit)
/*     */   {
/* 120 */     this.numTexUnit = numTexUnit;
/* 121 */     this.pts = new Point3f[numVerts];
/* 122 */     this.normals = new Vector3f[numVerts];
/* 123 */     this.tcoords = new TexCoord2f[numVerts];
/*     */
/* 125 */     this.currPrimType = new int[numVerts / 3];
/* 126 */     this.currPrimStartVertex = new int[numVerts / 3];
/* 127 */     this.currPrimEndVertex = new int[numVerts / 3];
/* 128 */     this.currVertCnt = 0;
/* 129 */     this.currPrimCnt = 0;
/*     */
/* 131 */     this.texCoordSetMap = new int[numTexUnit];
/* 132 */     for (int i = 0; i < numTexUnit; i++)
/* 133 */       this.texCoordSetMap[i] = 0;
/*     */   }
/*     */
/*     */   GeomBuffer(int numVerts)
/*     */   {
/* 139 */     this(numVerts, 1);
/*     */   }
/*     */
/*     */   GeometryArray getGeom(int format)
/*     */   {
/* 151 */     GeometryArray obj = null;
/* 152 */     this.flags = format;
/*     */
/* 154 */     this.numTris = 0;
/*     */
/* 157 */     switch (this.currPrimType[0]) {
/*     */     case 2:
/* 159 */       obj = processTriangles();
/* 160 */       break;
/*     */     case 4:
/* 162 */       obj = processQuads();
/* 163 */       break;
/*     */     case 1:
/*     */     case 32:
/* 166 */       obj = processQuadStrips();
/* 167 */       break;
/*     */     case 16:
/* 169 */       obj = processTriangleFan();
/*     */     }
/*     */
/* 172 */     if ((obj != null) && ((this.flags & 0x20) != 0)) {
/* 173 */       obj.setCapability(18);
/* 174 */       obj.setCapability(17);
/* 175 */       obj.setCapability(8);
/* 176 */       obj.setCapability(0);
/*     */     }
/* 178 */     return obj;
/*     */   }
/*     */
/*     */   void begin(int prim)
/*     */   {
/* 192 */     this.currPrimType[this.currPrimCnt] = prim;
/* 193 */     this.currPrimStartVertex[this.currPrimCnt] = this.currVertCnt;
/*     */   }
/*     */
/*     */   void end()
/*     */   {
/* 205 */     this.currPrimEndVertex[this.currPrimCnt] = this.currVertCnt;
/* 206 */     this.currPrimCnt += 1;
/*     */   }
/*     */
/*     */   void vertex3d(double x, double y, double z)
/*     */   {
/* 215 */     this.pts[this.currVertCnt] = new Point3f((float)x, (float)y, (float)z);
/* 216 */     this.currVertCnt += 1;
/*     */   }
/*     */
/*     */   void normal3d(double x, double y, double z)
/*     */   {
/* 223 */     double sum = x * x + y * y + z * z;
/* 224 */     if (Math.abs(sum - 1.0D) > 0.001D)
/*     */     {
/* 226 */       double root = Math.sqrt(sum);
/* 227 */       if (root > 1.0E-006D) {
/* 228 */         x /= root;
/* 229 */         y /= root;
/* 230 */         z /= root;
/*     */       } else {
/* 232 */         y = z = 0.0D; x = 1.0D;
/*     */       }
/*     */     }
/* 235 */     this.normals[this.currVertCnt] = new Vector3f((float)x, (float)y, (float)z);
/*     */   }
/*     */
/*     */   void texCoord2d(double s, double t)
/*     */   {
/* 243 */     this.tcoords[this.currVertCnt] = new TexCoord2f((float)s, (float)t);
/*     */   }
/*     */
/*     */   GeometryArray getComputedGeometry()
/*     */   {
/* 253 */     return this.geometry;
/*     */   }
/*     */
/*     */   int getNumTris()
/*     */   {
/* 258 */     return this.numTris;
/*     */   }
/*     */
/*     */   int getNumVerts()
/*     */   {
/* 263 */     return this.numVerts;
/*     */   }
/*     */
/*     */   private GeometryArray processQuadStrips()
/*     */   {
/* 269 */     GeometryArray obj = null;
/*     */
/* 271 */     int totalVerts = 0;
/*     */
/* 274 */     int[] stripCounts = new int[this.currPrimCnt];
/* 275 */     for (int i = 0; i < this.currPrimCnt; i++) {
/* 276 */       stripCounts[i] = (this.currPrimEndVertex[i] - this.currPrimStartVertex[i]);
/* 277 */       totalVerts += stripCounts[i];
/*     */     }
/*     */
/* 282 */     int tsaFlags = 1;
/* 283 */     if ((this.flags & 0x1) != 0)
/* 284 */       tsaFlags |= 2;
/* 285 */     if ((this.flags & 0x2) != 0) {
/* 286 */       tsaFlags |= 32;
/*     */     }
/*     */
/* 289 */     obj = new TriangleStripArray(totalVerts, tsaFlags, 1, this.texCoordSetMap, stripCounts);
/*     */
/* 293 */     Point3f[] newpts = new Point3f[totalVerts];
/* 294 */     Vector3f[] newnormals = new Vector3f[totalVerts];
/* 295 */     TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
/* 296 */     int currVert = 0;
/*     */
/* 299 */     for (i = 0; i < this.currPrimCnt; i++)
/*     */     {
/* 301 */       for (int j = this.currPrimStartVertex[i]; j < this.currPrimEndVertex[i]; j++) {
/* 302 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j);
/*     */       }
/*     */
/*     */     }
/*     */
/* 308 */     this.numVerts = currVert;
/* 309 */     this.numTris += totalVerts - this.currPrimCnt * 2;
/*     */
/* 311 */     obj.setCoordinates(0, newpts);
/* 312 */     if ((this.flags & 0x1) != 0)
/* 313 */       obj.setNormals(0, newnormals);
/* 314 */     if ((this.flags & 0x2) != 0) {
/* 315 */       obj.setTextureCoordinates(0, 0, newtcoords);
/*     */     }
/* 317 */     this.geometry = obj;
/* 318 */     return obj;
/*     */   }
/*     */
/*     */   private GeometryArray processQuads()
/*     */   {
/* 323 */     GeometryArray obj = null;
/*     */
/* 325 */     int totalVerts = 0;
/*     */
/* 327 */     for (int i = 0; i < this.currPrimCnt; i++) {
/* 328 */       totalVerts += this.currPrimEndVertex[i] - this.currPrimStartVertex[i];
/*     */     }
/*     */
/* 333 */     if (((this.flags & 0x1) != 0) && ((this.flags & 0x2) != 0))
/*     */     {
/* 335 */       obj = new QuadArray(totalVerts, 35, 1, this.texCoordSetMap);
/*     */     }
/* 342 */     else if (((this.flags & 0x1) == 0) && ((this.flags & 0x2) != 0))
/*     */     {
/* 344 */       obj = new QuadArray(totalVerts, 33, 1, this.texCoordSetMap);
/*     */     }
/* 350 */     else if (((this.flags & 0x1) != 0) && ((this.flags & 0x2) == 0))
/*     */     {
/* 352 */       obj = new QuadArray(totalVerts, 3);
/*     */     }
/*     */     else
/*     */     {
/* 357 */       obj = new QuadArray(totalVerts, 1);
/*     */     }
/*     */
/* 361 */     Point3f[] newpts = new Point3f[totalVerts];
/* 362 */     Vector3f[] newnormals = new Vector3f[totalVerts];
/* 363 */     TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
/* 364 */     int currVert = 0;
/*     */
/* 368 */     for (i = 0; i < this.currPrimCnt; i++)
/*     */     {
/* 371 */       for (int j = this.currPrimStartVertex[i]; j < this.currPrimEndVertex[i] - 3; j += 4) {
/* 372 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j);
/*     */
/* 374 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j + 1);
/*     */
/* 376 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j + 2);
/*     */
/* 378 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j + 3);
/*     */
/* 380 */         this.numTris += 2;
/*     */       }
/*     */     }
/* 383 */     this.numVerts = currVert;
/*     */
/* 385 */     obj.setCoordinates(0, newpts);
/* 386 */     if ((this.flags & 0x1) != 0)
/* 387 */       obj.setNormals(0, newnormals);
/* 388 */     if ((this.flags & 0x2) != 0) {
/* 389 */       obj.setTextureCoordinates(0, 0, newtcoords);
/*     */     }
/* 391 */     this.geometry = obj;
/* 392 */     return obj;
/*     */   }
/*     */
/*     */   private GeometryArray processTriangles()
/*     */   {
/* 397 */     GeometryArray obj = null;
/*     */
/* 399 */     int totalVerts = 0;
/*     */
/* 401 */     for (int i = 0; i < this.currPrimCnt; i++) {
/* 402 */       totalVerts += this.currPrimEndVertex[i] - this.currPrimStartVertex[i];
/*     */     }
/*     */
/* 407 */     if (((this.flags & 0x1) != 0) && ((this.flags & 0x2) != 0))
/*     */     {
/* 409 */       obj = new TriangleArray(totalVerts, 35, 1, this.texCoordSetMap);
/*     */     }
/* 416 */     else if (((this.flags & 0x1) == 0) && ((this.flags & 0x2) != 0))
/*     */     {
/* 418 */       obj = new TriangleArray(totalVerts, 33, 1, this.texCoordSetMap);
/*     */     }
/* 424 */     else if (((this.flags & 0x1) != 0) && ((this.flags & 0x2) == 0))
/*     */     {
/* 426 */       obj = new TriangleArray(totalVerts, 3);
/*     */     }
/*     */     else
/*     */     {
/* 431 */       obj = new TriangleArray(totalVerts, 1);
/*     */     }
/*     */
/* 435 */     Point3f[] newpts = new Point3f[totalVerts];
/* 436 */     Vector3f[] newnormals = new Vector3f[totalVerts];
/* 437 */     TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
/* 438 */     int currVert = 0;
/*     */
/* 440 */     for (i = 0; i < this.currPrimCnt; i++) {
/* 441 */       for (int j = this.currPrimStartVertex[i]; j < this.currPrimEndVertex[i] - 2; j += 3) {
/* 442 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j);
/*     */
/* 444 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j + 1);
/*     */
/* 446 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j + 2);
/*     */
/* 448 */         this.numTris += 1;
/*     */       }
/*     */     }
/* 451 */     this.numVerts = currVert;
/*     */
/* 453 */     obj.setCoordinates(0, newpts);
/* 454 */     if ((this.flags & 0x1) != 0)
/* 455 */       obj.setNormals(0, newnormals);
/* 456 */     if ((this.flags & 0x2) != 0) {
/* 457 */       obj.setTextureCoordinates(0, 0, newtcoords);
/*     */     }
/* 459 */     this.geometry = obj;
/* 460 */     return obj;
/*     */   }
/*     */
/*     */   private GeometryArray processTriangleFan()
/*     */   {
/* 467 */     GeometryArray obj = null;
/*     */
/* 469 */     int totalVerts = 0;
/*     */
/* 471 */     int[] stripCounts = new int[this.currPrimCnt];
/*     */
/* 474 */     for (int i = 0; i < this.currPrimCnt; i++) {
/* 475 */       stripCounts[i] = (this.currPrimEndVertex[i] - this.currPrimStartVertex[i]);
/* 476 */       totalVerts += stripCounts[i];
/*     */     }
/*     */
/* 480 */     int tfFlags = 1;
/* 481 */     if ((this.flags & 0x1) != 0) {
/* 482 */       tfFlags |= 2;
/*     */     }
/* 484 */     if ((this.flags & 0x2) != 0) {
/* 485 */       tfFlags |= 32;
/*     */     }
/*     */
/* 489 */     obj = new TriangleFanArray(totalVerts, tfFlags, 1, this.texCoordSetMap, stripCounts);
/*     */
/* 493 */     Point3f[] newpts = new Point3f[totalVerts];
/* 494 */     Vector3f[] newnormals = new Vector3f[totalVerts];
/* 495 */     TexCoord2f[] newtcoords = new TexCoord2f[totalVerts];
/*     */
/* 497 */     int currVert = 0;
/*     */
/* 500 */     for (i = 0; i < this.currPrimCnt; i++) {
/* 501 */       for (int j = this.currPrimStartVertex[i]; j < this.currPrimEndVertex[i]; j++) {
/* 502 */         outVertex(newpts, newnormals, newtcoords, currVert++, this.pts, this.normals, this.tcoords, j);
/*     */       }
/*     */
/*     */     }
/*     */
/* 507 */     for (i = 0; i < newpts.length; i++);
/* 511 */     this.numVerts = currVert;
/* 512 */     this.numTris = (totalVerts - this.currPrimCnt * 2);
/*     */
/* 515 */     obj.setCoordinates(0, newpts);
/*     */
/* 518 */     if ((this.flags & 0x1) != 0) {
/* 519 */       obj.setNormals(0, newnormals);
/*     */     }
/* 521 */     if ((this.flags & 0x2) != 0) {
/* 522 */       obj.setTextureCoordinates(0, 0, newtcoords);
/*     */     }
/* 524 */     this.geometry = obj;
/* 525 */     return obj;
/*     */   }
/*     */
/*     */   void outVertex(Point3f[] dpts, Vector3f[] dnormals, TexCoord2f[] dtcoords, int dloc, Point3f[] spts, Vector3f[] snormals, TexCoord2f[] stcoords, int sloc)
/*     */   {
/* 541 */     dpts[dloc] = new Point3f(spts[sloc]);
/*     */
/* 543 */     if ((this.flags & 0x1) != 0) {
/* 544 */       dnormals[dloc] = new Vector3f(snormals[sloc]);
/*     */     }
/* 546 */     if ((this.flags & 0x2) != 0)
/*     */     {
/* 548 */       dtcoords[dloc] = new TexCoord2f(stcoords[sloc]);
/*     */     }
/*     */   }
/*     */ }

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

Related Classes of com.sun.j3d.utils.geometry.GeomBuffer

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.