Contents

Chapters

Sections

j3d.org

[ Previous ] [ Up ] [ Next ]

The TriangleArray

© Justin Couch 2000-2004

TriangleArrays provide the most basic of all graphics primitives - a collection of individual triangles. Triangles, unsurprisingly, are described by three vertices. As an array of values, the rendering will take each collection of 3 vertices from the array and render them as a triangle.

Construction

Once again, as a simple primitive type, providing vertex values is just a flat array of values with each coordinate placed sequentially in the array. For this case, every three sets of coordinates in that array represents a triangle.

    int format = GeometryArray.COORDINATE |
                 GeometryArray.COLOR_3;

    TriangleArray tris = new TriangleArray(6, format);
After the first three sets of vertices (9 items in the coordinate array) are drawn as the first triangle, then the next triangle is drawn, such as in Figure 9 that shows two triangles being drawn.

A triangle array image
Figure 9: A triangle array showing the vertex numbering for two triangles

To complete the construction of your line array, you need to now specify a set of vertices. The primitive looks at each pair of vertices in the provided array and connects those as lines. So, to make a line you need to provide the array of vertices like this:
    float[] vertices =
    {
        0,   0,  0,   // v1
        0,   2,  0,   // v2
        1,   5, -3,   // v3
        2,   0,  0,   // v4
        4,   1,  0,   // v5
        5,   5,  1,   // v6
    };
Setting the array of vertices is just like Point/Line/QuadArrays. Use the setCoordinates() method to change the values to the new array.
    tris.setCoordinates(0, vertices);

It's impossible to go wrong with triangles. There's only one way of arranging 3 vertices, unlike quads and polygons. However, like quads, there is a clear definition of what is the "front" of the triangle. Just as with quads, if you get the ordering incorrect, you will not be able to see the triangles from your current viewing position. The definition of the "front" of a triangle is the same as that for a quad. If you've forgotten, or not read the Quad intro page then the vertices must be ordered in an anti-clockwise order relative to your current viewing position to see that front face.