2013-11-15 15 views
-5

我不确定数组在这个程序中的使用方式。任何人都可以向我解释这个程序中的两个数组是如何使用的?这个java3d代码是如何工作的?

import javax.vecmath.*; 
import javax.media.j3d.*; 

public class Tetrahedron extends IndexedTriangleArray { 
    public Tetrahedron() { 
     super(4, TriangleArray.COORDINATES | TriangleArray.NORMALS, 12); 
     setCoordinate(0, new Point3f(1f, 1f, 1f)); 
     setCoordinate(1, new Point3f(1f, -1, -1f)); 
     setCoordinate(2, new Point3f(-1f, 1f, -1f)); 
     setCoordinate(3, new Point3f(-1f, -1f, 1f)); 
     int[] coords = { 0, 1, 2, 0, 3, 1, 1, 3, 2, 2, 3, 0 }; 
     float n = (float) (1.0/Math.sqrt(3)); 
     setNormal(0, new Vector3f(n, n, -n)); 
     setNormal(1, new Vector3f(n, -n, n)); 
     setNormal(2, new Vector3f(-n, -n, -n)); 
     setNormal(3, new Vector3f(-n, n, n)); 
     int[] norms = { 0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3 }; 
     setCoordinateIndices(0, coords); 
     setNormalIndices(0, norms); 
    } 
} 

回答

0

代码工作通过与法线的阵列沿第一创建点的数组,然后稍后引用它们来创建该图。四个调用setCoordinate()设置每个顶点的位置。

int[] coords存储构成4个面的4个三角形的顶点位置(每个三角形总共有12个顶点有三个顶点)。第一个三角形是由第0,1日,2日和顶点,下一个三角形的第0,第3和第1顶点等

代码为法线以类似的方式工作到了顶点的

+0

谢谢。我会投你一票,但我没有足够的代表点。 – user2363161