2017年11月8日 星期三

JavaFX 3D - Truncated Cube

以下是以JavaFX 8的TriangleMesh類別建構之Truncated Cube (截角立方體),屬於Archimedean Solid (阿基米德立體)。

float C0 = length * (float) (Math.sqrt(2.0)+1.0) / 2.0f;
float C1 = length * 0.5f;

// 建立TriangleMesh
TriangleMesh trianglemesh = new TriangleMesh();

// 設定頂點座標
trianglemesh.getPoints().addAll(
   C0,  C1,  C0,
   C0,  C1, -C0,
   C0, -C1,  C0,
   C0, -C1, -C0,
  -C0,  C1,  C0,
  -C0,  C1, -C0,
  -C0, -C1,  C0,
  -C0, -C1, -C0,
   C0,  C0,  C1,
   C0,  C0, -C1,
   C0, -C0,  C1,
   C0, -C0, -C1,
  -C0,  C0,  C1,
  -C0,  C0, -C1,
  -C0, -C0,  C1,
  -C0, -C0, -C1,
   C1,  C0,  C0,
   C1,  C0, -C0,
   C1, -C0,  C0,
   C1, -C0, -C0,
  -C1,  C0,  C0,
  -C1,  C0, -C0,
  -C1, -C0,  C0,
  -C1, -C0, -C0
);

// 設定貼圖座標
trianglemesh.getTexCoords().addAll(
  0.50f, 1.0f,
  0.75f, (float) (1.0-Math.sqrt(3.0)/4.0f),
  0.25f, (float) (1.0-Math.sqrt(3.0)/4.0f),
  1.00f, 1.0f,
  0.50f, (float) (1.0-Math.sqrt(3.0)/2.0f),
  0.00f, 1.0f
);

// 設定各三角形的面
trianglemesh.getFaces().addAll(
   0, 2,  8, 1, 16, 4,
   1, 2, 17, 1,  9, 4,
   2, 2, 18, 1, 10, 4,
   3, 2, 11, 1, 19, 4,
   4, 2, 20, 1, 12, 4,
   5, 2, 13, 1, 21, 4,
   6, 2, 14, 1, 22, 4,
   7, 2, 23, 1, 15, 4,
   0, 0,  2, 2, 10, 5,
   0, 0, 10, 2, 11, 5,
   0, 0, 11, 2,  3, 5,
   0, 0,  3, 2,  1, 5,
   0, 0,  1, 2,  9, 5,
   0, 0,  9, 2,  8, 5,
   0, 0, 16, 2, 20, 5,
   0, 0, 20, 2,  4, 5,
   0, 0,  4, 2,  6, 5,
   0, 0,  6, 2, 22, 5,
   0, 0, 22, 2, 18, 5,
   0, 0, 18, 2,  2, 5,
  12, 0, 13, 2,  5, 5,
  12, 0,  5, 2,  7, 5,
  12, 0,  7, 2, 15, 5,
  12, 0, 15, 2, 14, 5,
  12, 0, 14, 2,  6, 5,
  12, 0,  6, 2,  4, 5,
  12, 0, 20, 2, 16, 5,
  12, 0, 16, 2,  8, 5,
  12, 0,  8, 2,  9, 5,
  12, 0,  9, 2, 17, 5,
  12, 0, 17, 2, 21, 5,
  12, 0, 21, 2, 13, 5,
  19, 0, 23, 2,  7, 5,
  19, 0,  7, 2,  5, 5,
  19, 0,  5, 2, 21, 5,
  19, 0, 21, 2, 17, 5,
  19, 0, 17, 2,  1, 5,
  19, 0,  1, 2,  3, 5,
  19, 0, 11, 2, 10, 5,
  19, 0, 10, 2, 18, 5,
  19, 0, 18, 2, 22, 5,
  19, 0, 22, 2, 14, 5,
  19, 0, 14, 2, 15, 5,
  19, 0, 15, 2, 23, 5
);

// 設定各面的平滑參數
trianglemesh.getFaceSmoothingGroups().addAll(
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0,
  0, 0, 0, 0
);

// 建立MeshView
MeshView meshview;

meshview = new MeshView(createMesh(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

沒有留言:

張貼留言