/* */ package com.sun.j3d.utils.geometry;
/* */
/* */ import javax.media.j3d.Appearance;
/* */ import javax.media.j3d.Node;
/* */ import javax.media.j3d.Shape3D;
/* */ import javax.vecmath.Point3f;
/* */ import javax.vecmath.TexCoord2f;
/* */ import javax.vecmath.Vector3f;
/* */
/* */ public class Sphere extends Primitive
/* */ {
/* */ public static final int BODY = 0;
/* */ static final int MID_REZ_DIV = 16;
/* */ float radius;
/* */ int divisions;
/* */
/* */ public Sphere(float radius)
/* */ {
/* 86 */ this(radius, 1, 16);
/* */ }
/* */
/* */ public Sphere()
/* */ {
/* 95 */ this(1.0F, 1, 16);
/* */ }
/* */
/* */ public Sphere(float radius, Appearance ap)
/* */ {
/* 106 */ this(radius, 1, 16, ap);
/* */ }
/* */
/* */ public Sphere(float radius, int primflags, Appearance ap)
/* */ {
/* 117 */ this(radius, primflags, 16, ap);
/* */ }
/* */
/* */ public Sphere(float radius, int primflags, int divisions)
/* */ {
/* 129 */ this(radius, primflags, divisions, null);
/* */ }
/* */
/* */ public Shape3D getShape(int partId)
/* */ {
/* 141 */ if (partId != 0) return null;
/* */
/* 143 */ return (Shape3D)getChild(0);
/* */ }
/* */
/* */ public Shape3D getShape()
/* */ {
/* 150 */ return (Shape3D)getChild(0);
/* */ }
/* */
/* */ public void setAppearance(Appearance ap)
/* */ {
/* 157 */ ((Shape3D)getChild(0)).setAppearance(ap);
/* */ }
/* */
/* */ public Appearance getAppearance(int partId)
/* */ {
/* 171 */ if (partId != 0) return null;
/* 172 */ return getShape(partId).getAppearance();
/* */ }
/* */
/* */ public Sphere(float radius, int primflags, int divisions, Appearance ap)
/* */ {
/* 191 */ this.radius = radius;
/* 192 */ this.divisions = divisions;
/* */
/* 203 */ this.flags = primflags;
/* */ int sign;
/* */ int sign;
/* 206 */ if ((this.flags & 0x4) != 0)
/* 207 */ sign = -1;
/* */ else
/* 209 */ sign = 1;
/* */ int n;
/* */ int n;
/* */ int nstep;
/* 212 */ if (divisions < 4) {
/* 213 */ int nstep = 1;
/* 214 */ n = 4;
/* */ } else {
/* 216 */ int mod = divisions % 4;
/* */ int n;
/* 217 */ if (mod == 0)
/* 218 */ n = divisions;
/* */ else {
/* 220 */ n = divisions + (4 - mod);
/* */ }
/* 222 */ nstep = n / 4;
/* */ }
/* */
/* 226 */ GeomBuffer cache = getCachedGeometry(1, radius, 0.0F, 0.0F, divisions, 0, primflags);
/* */ Shape3D shape;
/* 232 */ if (cache != null) {
/* 233 */ Shape3D shape = new Shape3D(cache.getComputedGeometry());
/* 234 */ this.numVerts += cache.getNumVerts();
/* 235 */ this.numTris += cache.getNumTris();
/* */ }
/* */ else
/* */ {
/* 239 */ GeomBuffer gbuf = new GeomBuffer(8 * nstep * (nstep + 2));
/* */
/* 241 */ for (int i = 0; i < 4; i++) {
/* 242 */ buildQuadrant(gbuf, i * 3.141592653589793D / 2.0D, (i + 1) * 3.141592653589793D / 2.0D, sign, nstep, n, true);
/* 243 */ buildQuadrant(gbuf, i * 3.141592653589793D / 2.0D, (i + 1) * 3.141592653589793D / 2.0D, sign, nstep, n, false);
/* */ }
/* */
/* 246 */ shape = new Shape3D(gbuf.getGeom(this.flags));
/* 247 */ this.numVerts = gbuf.getNumVerts();
/* 248 */ this.numTris = gbuf.getNumTris();
/* 249 */ if ((primflags & 0x10) == 0) {
/* 250 */ cacheGeometry(1, radius, 0.0F, 0.0F, divisions, 0, primflags, gbuf);
/* */ }
/* */
/* */ }
/* */
/* 256 */ if ((this.flags & 0x40) != 0) {
/* 257 */ shape.setCapability(14);
/* 258 */ shape.setCapability(15);
/* */ }
/* */
/* 261 */ if ((this.flags & 0x20) != 0) {
/* 262 */ shape.setCapability(12);
/* */ }
/* */
/* 265 */ addChild(shape);
/* */
/* 267 */ if (ap == null)
/* 268 */ setAppearance();
/* */ else
/* 270 */ setAppearance(ap);
/* */ }
/* */
/* */ public Node cloneNode(boolean forceDuplicate)
/* */ {
/* 297 */ Sphere s = new Sphere(this.radius, this.flags, this.divisions, getAppearance());
/* 298 */ s.duplicateNode(this, forceDuplicate);
/* */
/* 300 */ return s;
/* */ }
/* */
/* */ public void duplicateNode(Node originalNode, boolean forceDuplicate)
/* */ {
/* 330 */ super.duplicateNode(originalNode, forceDuplicate);
/* */ }
/* */
/* */ public float getRadius()
/* */ {
/* 339 */ return this.radius;
/* */ }
/* */
/* */ public int getDivisions()
/* */ {
/* 348 */ return this.divisions;
/* */ }
/* */
/* */ void buildQuadrant(GeomBuffer gbuf, double startDelta, double endDelta, int sign, int nstep, int n, boolean upperSphere)
/* */ {
/* */ boolean leftToRight;
/* */ double dt;
/* */ double theta;
/* */ double starth;
/* */ boolean leftToRight;
/* 366 */ if (upperSphere) {
/* 367 */ double dt = 3.141592653589793D / (2 * nstep);
/* 368 */ double theta = dt;
/* 369 */ double starth = 1.0D;
/* 370 */ leftToRight = sign > 0;
/* */ } else {
/* 372 */ dt = -3.141592653589793D / (2 * nstep);
/* 373 */ theta = 3.141592653589793D + dt;
/* 374 */ starth = -1.0D;
/* 375 */ leftToRight = sign < 0;
/* */ }
/* */
/* 379 */ for (int i = 1; i <= nstep; i++) {
/* 380 */ double h = Math.cos(theta);
/* 381 */ double r = Math.sin(theta);
/* */ double t;
/* */ double t;
/* 382 */ if (sign > 0)
/* 383 */ t = 1.0D - theta / 3.141592653589793D;
/* */ else {
/* 385 */ t = theta / 3.141592653589793D;
/* */ }
/* */
/* 388 */ int i2 = i << 1;
/* */
/* 390 */ double ds = (endDelta - startDelta) / i;
/* */
/* 392 */ gbuf.begin(32);
/* */
/* 394 */ if (leftToRight)
/* */ {
/* 396 */ double delta = startDelta;
/* */
/* 398 */ for (int j = 0; j < i; j++) {
/* 399 */ double vx = r * Math.cos(delta);
/* 400 */ double vz = r * Math.sin(delta);
/* */
/* 402 */ gbuf.normal3d(vx * sign, h * sign, vz * sign);
/* 403 */ gbuf.texCoord2d(0.75D - delta / 6.283185307179586D, t);
/* 404 */ gbuf.vertex3d(vx * this.radius, h * this.radius, vz * this.radius);
/* 405 */ if (i > 1)
/* */ {
/* 407 */ int index = gbuf.currVertCnt - i2;
/* 408 */ Point3f pt = gbuf.pts[index];
/* 409 */ Vector3f norm = gbuf.normals[index];
/* 410 */ TexCoord2f texCoord = gbuf.tcoords[index];
/* */
/* 412 */ gbuf.normal3d(norm.x, norm.y, norm.z);
/* 413 */ gbuf.texCoord2d(texCoord.x, texCoord.y);
/* 414 */ gbuf.vertex3d(pt.x, pt.y, pt.z);
/* */ } else {
/* 416 */ gbuf.normal3d(0.0D, sign * starth, 0.0D);
/* 417 */ if (sign > 0) {
/* 418 */ gbuf.texCoord2d(0.75D - (startDelta + endDelta) / 12.566370614359172D, 1.0D - (theta - dt) / 3.141592653589793D);
/* */ }
/* */ else {
/* 421 */ gbuf.texCoord2d(0.75D - (startDelta + endDelta) / 12.566370614359172D, (theta - dt) / 3.141592653589793D);
/* */ }
/* */
/* 424 */ gbuf.vertex3d(0.0D, starth * this.radius, 0.0D);
/* */ }
/* */
/* 427 */ delta += ds;
/* */ }
/* */
/* 433 */ delta = endDelta;
/* 434 */ double vx = r * Math.cos(delta);
/* 435 */ double vz = r * Math.sin(delta);
/* 436 */ gbuf.normal3d(vx * sign, h * sign, vz * sign);
/* 437 */ gbuf.texCoord2d(0.75D - delta / 6.283185307179586D, t);
/* 438 */ gbuf.vertex3d(vx * this.radius, h * this.radius, vz * this.radius);
/* */ } else {
/* 440 */ double delta = endDelta;
/* */
/* 442 */ for (int j = i; j > 0; j--) {
/* 443 */ double vx = r * Math.cos(delta);
/* 444 */ double vz = r * Math.sin(delta);
/* */
/* 446 */ gbuf.normal3d(vx * sign, h * sign, vz * sign);
/* */
/* 449 */ gbuf.texCoord2d(0.75D - delta / 6.283185307179586D, t);
/* 450 */ gbuf.vertex3d(vx * this.radius, h * this.radius, vz * this.radius);
/* 451 */ if (i > 1)
/* */ {
/* 453 */ int index = gbuf.currVertCnt - i2;
/* 454 */ Point3f pt = gbuf.pts[index];
/* 455 */ Vector3f norm = gbuf.normals[index];
/* 456 */ TexCoord2f texCoord = gbuf.tcoords[index];
/* 457 */ gbuf.normal3d(norm.x, norm.y, norm.z);
/* 458 */ gbuf.texCoord2d(texCoord.x, texCoord.y);
/* 459 */ gbuf.vertex3d(pt.x, pt.y, pt.z);
/* */ } else {
/* 461 */ gbuf.normal3d(0.0D, sign * starth, 0.0D);
/* 462 */ if (sign > 0) {
/* 463 */ gbuf.texCoord2d(0.75D - (startDelta + endDelta) / 12.566370614359172D, 1.0D - (theta - dt) / 3.141592653589793D);
/* */ }
/* */ else {
/* 466 */ gbuf.texCoord2d(0.75D - (startDelta + endDelta) / 12.566370614359172D, (theta - dt) / 3.141592653589793D);
/* */ }
/* */
/* 469 */ gbuf.vertex3d(0.0D, starth * this.radius, 0.0D);
/* */ }
/* */
/* 472 */ delta -= ds;
/* */ }
/* */
/* 478 */ delta = startDelta;
/* 479 */ double vx = r * Math.cos(delta);
/* 480 */ double vz = r * Math.sin(delta);
/* 481 */ gbuf.normal3d(vx * sign, h * sign, vz * sign);
/* 482 */ gbuf.texCoord2d(0.75D - delta / 6.283185307179586D, t);
/* 483 */ gbuf.vertex3d(vx * this.radius, h * this.radius, vz * this.radius);
/* */ }
/* */
/* 487 */ gbuf.end();
/* */
/* 489 */ if (i < nstep)
/* 490 */ theta += dt;
/* */ else
/* 492 */ theta = 1.570796326794897D;
/* */ }
/* */ }
/* */ }
/* Location: Z:\System\Library\Java\Extensions\j3dutils.jar
* Qualified Name: com.sun.j3d.utils.geometry.Sphere
* JD-Core Version: 0.6.2
*/