📄 sphere.java
字号:
/* * $RCSfile: Sphere.java,v $ * * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistribution of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistribution in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * Neither the name of Sun Microsystems, Inc. or the names of * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * This software is provided "AS IS," without a warranty of any * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY * EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or * intended for use in the design, construction, operation or * maintenance of any nuclear facility. * * $Revision: 1.6 $ * $Date: 2007/04/24 18:50:59 $ * $State: Exp $ */package com.sun.j3d.utils.geometry;import com.sun.j3d.utils.geometry.*;import java.io.*;import java.util.*;import javax.media.j3d.*;import javax.vecmath.*;import java.math.*;/** * Sphere is a geometry primitive created with a given radius and resolution. * It is centered at the origin. * <p> * When a texture is applied to a Sphere, it is mapped CCW from the back * of the sphere. * <p> * By default all primitives with the same parameters share their * geometry (e.g., you can have 50 shperes in your scene, but the * geometry is stored only once). A change to one primitive will * effect all shared nodes. Another implication of this * implementation is that the capabilities of the geometry are shared, * and once one of the shared nodes is live, the capabilities cannot * be set. Use the GEOMETRY_NOT_SHARED flag if you do not wish to * share geometry among primitives with the same parameters. */public class Sphere extends Primitive { /** * Sphere shape identifier, used by <code>getShape</code>. * * @see Sphere#getShape */ public static final int BODY = 0; static final int MID_REZ_DIV = 16; float radius; int divisions; /** * Constructs a Sphere of a given radius. Normals are generated * by default, texture coordinates are not. The resolution defaults to * 15 divisions along sphere's axes. Appearance defaults to white. * @param radius Radius */ public Sphere (float radius) { this(radius, GENERATE_NORMALS, MID_REZ_DIV); } /** * Constructs a default Sphere of radius of 1.0. Normals are generated * by default, texture coordinates are not. * Resolution defaults to 15 divisions. Appearance defaults to white. */ public Sphere() { this(1.0f, GENERATE_NORMALS, MID_REZ_DIV); } /** * Constructs a Sphere of a given radius and appearance. * Normals are generated by default, texture coordinates are not. * @param radius Radius * @param ap Appearance */ public Sphere (float radius, Appearance ap) { this(radius, GENERATE_NORMALS, MID_REZ_DIV, ap); } /** * Constructs a Sphere of a given radius and appearance with * additional parameters specified by the Primitive flags. * @param radius Radius * @param primflags * @param ap appearance */ public Sphere(float radius, int primflags, Appearance ap) { this(radius, primflags, MID_REZ_DIV, ap); } /** * Constructs a Sphere of a given radius and number of divisions * with additional parameters specified by the Primitive flags. * Appearance defaults to white. * @param radius Radius * @param divisions Divisions * @param primflags Primflags */ public Sphere(float radius, int primflags, int divisions) { this(radius, primflags, divisions, null); } /** * Obtains Sphere's shape node that contains the geometry. * This allows users to modify the appearance or geometry. * @param partId The part to return (must be BODY for Spheres) * @return The Shape3D object associated with the partId. If an * invalid partId is passed in, null is returned. */ public Shape3D getShape(int partId) { if (partId != BODY) return null;// return (Shape3D)((Group)getChild(0)).getChild(BODY); return (Shape3D)getChild(BODY); } /** Obtains Sphere's shape node that contains the geometry. */ public Shape3D getShape() {// return (Shape3D)((Group)getChild(0)).getChild(BODY); return (Shape3D)getChild(BODY); } /** Sets appearance of the Sphere. */ public void setAppearance(Appearance ap) {// ((Shape3D)((Group)getChild(0)).getChild(BODY)).setAppearance(ap); ((Shape3D)getChild(BODY)).setAppearance(ap); } /** * Gets the appearance of the specified part of the sphere. * * @param partId identifier for a given subpart of the sphere * * @return The appearance object associated with the partID. If an * invalid partId is passed in, null is returned. * * @since Java 3D 1.2.1 */ public Appearance getAppearance(int partId) { if (partId != BODY) return null; return getShape(partId).getAppearance(); } /** * Constructs a customized Sphere of a given radius, * number of divisions, and appearance, with additional parameters * specified by the Primitive flags. The resolution is defined in * terms of number of subdivisions along the sphere's axes. More * divisions lead to more finely tesselated objects. * <p> * If the appearance is null, the sphere defaults to a white appearance. */ public Sphere(float radius, int primflags, int divisions, Appearance ap) { super(); int sign; int n, nstep; this.radius = radius; this.divisions = divisions; /* * The sphere algorithm evaluates spherical angles along regular * units. For each spherical coordinate, (theta, rho), a (x,y,z) is * evaluated (along with the normals and texture coordinates). * * The spherical angles theta varies from 0 to 2pi and rho from 0 * to pi. Sample points depends on the number of divisions. */ flags = primflags; boolean texCoordYUp = (flags & GENERATE_TEXTURE_COORDS_Y_UP) != 0; //Depending on whether normal inward bit is set. if ((flags & GENERATE_NORMALS_INWARD) != 0) { sign = -1; } else { sign = 1; } if (divisions < 4) { nstep = 1; n = 4; } else { int mod = divisions % 4; if (mod == 0) { n = divisions; } else { n = divisions + (4 - mod); } nstep = n/4; } GeomBuffer cache = getCachedGeometry(Primitive.SPHERE, radius, 0.0f, 0.0f, divisions, 0, primflags); Shape3D shape; if (cache != null) { shape = new Shape3D(cache.getComputedGeometry()); numVerts += cache.getNumVerts(); numTris += cache.getNumTris(); } else { // buffer size = 8*(1 + 2E{i} + (nstep+1)) // where E{i} = sum of i = 2 ... nstep GeomBuffer gbuf = new GeomBuffer(8*nstep*(nstep+2)); for (int i=0; i < 4; i++) { buildQuadrant(gbuf, i*Math.PI/2, (i+1)*Math.PI/2, sign, nstep, n, true); buildQuadrant(gbuf, i*Math.PI/2, (i+1)*Math.PI/2, sign, nstep, n, false); } // Fix to Issue 411. Java 3D prefers images used for texture mapping to be Y-up if (texCoordYUp) { TexCoord2f[] texCoords = gbuf.getTexCoords(); if (texCoords != null) { for (int ii=0; ii<texCoords.length; ii++) { texCoords[ii].y = 1.0f - texCoords[ii].y; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -