2017年11月8日 星期三

JavaFX 3D - Sphere

以下是以JavaFX 8的TriangleMesh類別建構之Sphere (球體),屬於Platonic Solid (柏拉圖多面體)。

int divisions = ((divisions + 3) / 4) * 4;

final int div2 = divisions / 2;

final int nPoints = divisions * (div2 - 1) + 2;
final int nTexCoords = (divisions + 1) * (div2 - 1) + divisions * 2;

final float rDiv = 1.f / divisions;

float points[] = new float[nPoints * 3];
float texcoords[] = new float[nTexCoords * 2];

int pPos = 0, tPos = 0;

for (int y = 0; y < div2 - 1; ++y) {
  float va = rDiv * (y + 1 - div2 / 2) * 2 * (float) Math.PI;
  float sin_va = (float) Math.sin(va);
  float cos_va = (float) Math.cos(va);

  float ty = 0.5f + sin_va * 0.5f;
  for (int i = 0; i < divisions; ++i) {
    double a = rDiv * i * 2 * (float) Math.PI;
    float hSin = (float) Math.sin(a);
    float hCos = (float) Math.cos(a);
    points[pPos + 0] = hSin * cos_va * radius;
    points[pPos + 2] = hCos * cos_va * radius;
    points[pPos + 1] = sin_va * radius;
    texcoords[tPos + 0] = 1 - rDiv * i;
    texcoords[tPos + 1] = ty;
    pPos += 3;
    tPos += 2;
  }
  texcoords[tPos + 0] = 0;
  texcoords[tPos + 1] = ty;
  tPos += 2;
}

points[pPos + 0] = 0;
points[pPos + 1] = -radius;
points[pPos + 2] = 0;
points[pPos + 3] = 0;
points[pPos + 4] = radius;
points[pPos + 5] = 0;
pPos += 6;

int pS = (div2 - 1) * divisions;

float textureDelta = 1.f / 256;
for (int i = 0; i < divisions; ++i) {
  texcoords[tPos + 0] = rDiv * (0.5f + i);
  texcoords[tPos + 1] = textureDelta;
  tPos += 2;
}

for (int i = 0; i < divisions; ++i) {
  texcoords[tPos + 0] = rDiv * (0.5f + i);
  texcoords[tPos + 1] = 1 - textureDelta;
  tPos += 2;
}

// 建立TriangleMesh
TriangleMesh trianglemesh = new TriangleMesh();
// 設定頂點座標
trianglemesh.getPoints().addAll(points);
// 設定貼圖座標
trianglemesh.getTexCoords().addAll(texcoords);

final int nFaces = divisions * (div2 - 2) * 2 + divisions * 2;
int faces[] = new int[nFaces * 6];

int fIndex = 0;
for (int y = 0; y < div2 - 2; ++y) {
  for (int x = 0; x < divisions; ++x) {
    int p0 = y * divisions + x;
    int p1 = p0 + 1;
    int p2 = p0 + divisions;
    int p3 = p1 + divisions;
    int t0 = p0 + y;
    int t1 = t0 + 1;
    int t2 = t0 + (divisions + 1);
    int t3 = t1 + (divisions + 1);

    // add p0, p1, p2
    faces[fIndex + 0] = p0;
    faces[fIndex + 1] = t0;
    faces[fIndex + 2] = p1 % divisions == 0 ? p1 - divisions : p1;
    faces[fIndex + 3] = t1;
    faces[fIndex + 4] = p2;
    faces[fIndex + 5] = t2;
    fIndex += 6;

    // add p3, p2, p1
    faces[fIndex + 0] = p3 % divisions == 0 ? p3 - divisions : p3;
    faces[fIndex + 1] = t3;
    faces[fIndex + 2] = p2;
    faces[fIndex + 3] = t2;
    faces[fIndex + 4] = p1 % divisions == 0 ? p1 - divisions : p1;
    faces[fIndex + 5] = t1;
    fIndex += 6;
  }
}

// 設定各三角形的面
trianglemesh.getFaces().addAll(faces);

// 建立MeshView
MeshView meshview;

meshview = new MeshView(createMesh(150, 100));
...
【參考資料】

[1] Java Official Web Site:http://www.oracle.com/technetwork/java/index.html
[2] JavaFX:http://www.oracle.com/technetwork/java/javafx
[3] JavaFX 8.0 API Specification.
[4] Java Platform, Standard Edition 8 API Specification.
[5] JDK 8 Certified System Configurations.
[6] H. M. Cundy, A. P. Rollett, Mathematical Models, Tarquin Publications, 1981.
[7] G. Sellers, R. S. Wright, N. Haemel, OpenGL SuperBible: Comprehensive Tutorial and Reference (6th Edition), Addison-Wesley Professional, 2013.
[8] 黃嘉輝, JavaFX 8技術手冊, ISBN: 9789863474050, 碁峰資訊, 2014.

© Chia-Hui Huang

沒有留言:

張貼留言