final int nPoints = divisions * 2 + 2;
final int nTexCoords = (divisions + 1) * 4 + 1;
float textureDelta = 1.f / 256;
float dA = 1.f / divisions;
height *= .5f;
float points[] = new float[nPoints * 3];
float texcoords[] = new float[nTexCoords * 2];
int pPos = 0, tPos = 0;
for (int i = 0; i < divisions; ++i) {
double a = dA * i * 2 * Math.PI;
points[pPos + 0] = (float) (Math.sin(a) * radius);
points[pPos + 2] = (float) (Math.cos(a) * radius);
points[pPos + 1] = height;
texcoords[tPos + 0] = 1 - dA * i;
texcoords[tPos + 1] = 1 - textureDelta;
pPos += 3; tPos += 2;
}
// Top Edge
texcoords[tPos + 0] = 0;
texcoords[tPos + 1] = 1 - textureDelta;
tPos += 2;
for (int i = 0; i < divisions; ++i) {
double a = dA * i * 2 * Math.PI;
points[pPos + 0] = (float) (Math.sin(a) * radius);
points[pPos + 2] = (float) (Math.cos(a) * radius);
points[pPos + 1] = -height;
texcoords[tPos + 0] = 1 - dA * i;
texcoords[tPos + 1] = textureDelta;
pPos += 3;
tPos += 2;
}
// Bottom Edge
texcoords[tPos + 0] = 0;
texcoords[tPos + 1] = textureDelta;
tPos += 2;
// Add Cap Central Points
points[pPos + 0] = 0;
points[pPos + 1] = height;
points[pPos + 2] = 0;
points[pPos + 3] = 0;
points[pPos + 4] = -height;
points[pPos + 5] = 0;
pPos += 6;
// Add Cap Central Points
// Bottom Cap
for (int i = 0; i <= divisions; ++i) {
double a = (i < divisions) ? (dA * i * 2) * Math.PI: 0;
texcoords[tPos + 0] = (float) (Math.sin(a) * 0.5f) + 0.5f;
texcoords[tPos + 1] = (float) (Math.cos(a) * 0.5f) + 0.5f;
tPos += 2;
}
// Top Cap
for (int i = 0; i <= divisions; ++i) {
double a = (i < divisions) ? (dA * i * 2) * Math.PI: 0;
texcoords[tPos + 0] = 0.5f + (float) (Math.sin(a) * 0.5f);
texcoords[tPos + 1] = 0.5f - (float) (Math.cos(a) * 0.5f);
tPos += 2;
}
texcoords[tPos + 0] = .5f;
texcoords[tPos + 1] = .5f;
tPos += 2;
// 建立TriangleMesh
TriangleMesh trianglemesh = new TriangleMesh();
// 設定頂點座標
trianglemesh.getPoints().addAll(points);
// 設定貼圖座標
trianglemesh.getTexCoords().addAll(texcoords);
final int nFaces = divisions * 4;
int faces[] = new int[nFaces * 6];
int fIndex = 0;
// Faces
for (int p0 = 0; p0 < divisions; ++p0) {
int p1 = p0 + 1;
int p2 = p0 + divisions;
int p3 = p1 + divisions;
faces[fIndex+0] = p0;
faces[fIndex+1] = p0;
faces[fIndex+2] = p2;
faces[fIndex+3] = p2 + 1;
faces[fIndex+4] = p1 == divisions ? 0 : p1;
faces[fIndex+5] = p1;
fIndex += 6;
faces[fIndex+0] = p3 % divisions == 0 ? p3 - divisions : p3;
faces[fIndex+1] = p3 + 1;
faces[fIndex+2] = p1 == divisions ? 0 : p1;
faces[fIndex+3] = p1;
faces[fIndex+4] = p2;
faces[fIndex+5] = p2 + 1;
fIndex += 6;
}
// build cap faces
int tStart = (divisions + 1) * 2;
int t1 = (divisions + 1) * 4;
int p1 = divisions * 2;
...
// 設定各三角形的面
trianglemesh.getFaces().addAll(faces);
// 設定各面的平滑參數
int smoothing[] = new int[nFaces];
for (int i = 0; i < divisions * 2; ++i) {
smoothing[i] = 1;
}
for (int i = divisions * 2; i < divisions * 4; ++i) {
smoothing[i] = 2;
}
trianglemesh.getFaceSmoothingGroups().addAll(smoothing);
// 建立MeshView
MeshView meshview;
meshview = new MeshView(createMesh(120, 250, 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
[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

沒有留言:
張貼留言