Pergunta

I'm a unsure how the arrays are being used in this program. Can anyone explain to me how the two arrays in this program are being used?

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);
    }
}
Foi útil?

Solução

The code works by first creating an array of points along with an array of normals and then referencing them later to create the figure. The four calls to setCoordinate() sets the position of the each vertex.

The int[] coords store the positions of the vertices for the 4 triangles that make up the 4 faces (each triangle has three vertices for a total of 12 vertices). The first triangle is made up of the 0th , 1st, and 2nd vertices, the next triangle the 0th, 3rd and 1st vertex etc.

The code for the normals works in a similar fashion to that that of the vertices

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top