⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spheregeometry.java

📁 Java 3D Desktop Environment旨在使用Java 3D来创建一个3D桌面环境。功能包括:分布式的应用程序
💻 JAVA
字号:
package org.j3de.ui.geometry;  

import javax.media.j3d.Bounds;
import javax.media.j3d.Geometry;   
import javax.media.j3d.QuadArray;

import javax.vecmath.Vector3f;

import org.j3de.ui.UIGeometry;

/** Implements a simple Sphere : radius=1.0, position=(0, 0, 0).
  * Most of this code is taken from : http://www.billday.com/Java3DArchive/Shapes/index.html
  * and originally by Matt Robinson.
  */
public class SphereGeometry implements UIGeometry { 
  private static final float RADIUS    = 1.0f;
  private static final float ARCLENGTH = 0.1f;
  private static final float XPOSITION = 1.0f;
  private static final float YPOSITION = 1.0f;
  private static final float ZPOSITION = 1.0f;
  
  private Geometry geometry;
  
  public SphereGeometry() {
    geometry = getSphereGeometry(RADIUS, ARCLENGTH, XPOSITION, YPOSITION, ZPOSITION); 
  }
  
  public Geometry getSphereGeometry(float r, float arclength, 
                                    float xpos, float ypos, 
                                    float zpos) {
    
    float[] qverts;
    float x, y, z, theta, t, previousx, previousz, 
          calct, rlower, rupper, num, mag, x1, x2, x3, x4, y1, y2, y3, y4, 
          z1, z2, z3, z4, xn, yn, zn;
    int half, numcirc, upcount = 1;
    int vertCount = 0;
    int normalcount = 0;
    Vector3f[] normals;
              
    t = arclength;
    num = ((float) (2*Math.PI*r)/t);
    numcirc = (int) num;

    // Change the arclength to the closest value that fits.
    calct = ((float) (2*Math.PI*r)/numcirc);
    t = calct;
    theta = t/r;
    half = (int) ((r*Math.PI/2)/t)+1;
            
    // In case theres too many quads...
    if ( 2*(half*(6*(2*numcirc))) > 600000) {
      System.out.println("Too detailed! Choose a bigger" + 
              " arclength or smaller radius.");
      System.exit(0);
    }
    qverts = new float[2*(half*(6*(2*numcirc)))];
    
    rlower = r;// radius of first loop
    rupper = r*((float) Math.cos(theta));// radius of second loop
    
    // upper hemisphere
    for (int k=0; k < half; k++) {
      for (int i=0 ; i < numcirc; i++) {   
        int j = i + 1;
        x1 =  rlower*((float) Math.cos(theta*i));
        qverts[vertCount] = x1;
        vertCount++;

        y1 =  r*((float) Math.sin(theta*k));
        if (y1 < 0) {
          y1 = -y1;
        }
        qverts[vertCount] = y1;
        vertCount++;
        
        z1 =  rlower*((float) Math.sin(theta*i));
        qverts[vertCount] = -z1;
        vertCount++;
        
        x4 =  rlower*((float) Math.cos(theta*j));
        qverts[vertCount] = x4;
        vertCount++;

        y4 =  r*((float) Math.sin(theta*k));
        if (y4 < 0) {
          y4 = -y4;
        }
        qverts[vertCount] = y4;
        vertCount++;
        
        z4 =  rlower*((float) Math.sin(theta*j));
        qverts[vertCount] = -z4;
        vertCount++;
        
        x3 =  rupper*((float) Math.cos(theta*j));
        qverts[vertCount] = x3;
        vertCount++;
        
        y3 =  r*((float) Math.sin(theta*(k+1)));
        if (y3 < 0) {
          y3 = -y3;
        }
        qverts[vertCount] = y3;
        vertCount++;
        
        z3 =  rupper*((float) Math.sin(theta*j));
        qverts[vertCount] = -z3;
        vertCount++;
        
                x2 =  rupper*((float) Math.cos(theta*i));
        qverts[vertCount] = x2;
        vertCount++;
        
        y2 =  r*((float) Math.sin(theta*(k+1)));
        if (y2 < 0) {
          y2 = -y2;
        }
        qverts[vertCount] = y2;
        vertCount++;
        
        z2 =  rupper*((float) Math.sin(theta*i));
        qverts[vertCount] = -z2;
        vertCount++;

      }
      rlower = rupper;
      upcount++;
      rupper = r *((float) Math.cos(theta*upcount));
    }
    rlower = r;
    rupper = r  *((float) Math.cos(theta));
    upcount = 1;

    // lower hemisphere 
    // (just mirror the upper half on y axis)
    int tempVertCount = vertCount;
    for (int k=0; k < tempVertCount; k = k+3) {
      qverts[vertCount] = qverts[k+2];
      vertCount++;
      qverts[vertCount] = -(qverts[k+1]);
      vertCount++;
      qverts[vertCount] = qverts[k];
      vertCount++;
    }

    //position the sphere corectly
    for (int j=0; j < vertCount; j = j+3) {
      qverts[j] = qverts[j] + xpos;
      qverts[j+1] = qverts[j+1] + ypos;
      qverts[j+2] = qverts[j+2] + zpos;
    }
    
    QuadArray sphereGeometry = new QuadArray( vertCount/3, 
            QuadArray.COORDINATES | QuadArray.NORMALS);
            
    sphereGeometry.setCapability( QuadArray.ALLOW_COLOR_WRITE );
    sphereGeometry.setCapability( QuadArray.ALLOW_COORDINATE_WRITE );
    sphereGeometry.setCoordinates( 0, qverts );
    
    // Calculate normals of all points.
    normals = new Vector3f[vertCount/3];
    for (int w = 0; w < vertCount; w = w + 3) {    
      Vector3f norm = new Vector3f(0.0f, 0.0f, 0.0f);
      mag = qverts[w] * qverts[w] + qverts[w+1] * qverts[w+1] 
              + qverts[w+2] * qverts[w+2];
      if (mag != 0.0) {       
        mag = 1.0f / ((float) Math.sqrt(mag));  
        xn = qverts[w]*mag;     
        yn = qverts[w+1]*mag;   
        zn = qverts[w+2]*mag;
        norm = new Vector3f(xn, yn, zn);
      }
      normals[normalcount] = norm;
      sphereGeometry.setNormal(normalcount, norm);
      normalcount++;
    }

    return sphereGeometry;
  }
  
  public Geometry getGeometry(Bounds bounds) {
    return geometry;
  }               
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -