Contents

Chapters

Sections

j3d.org

[ Previous ] [ Up ] [ Next ]

The TriangleStripArray

© Justin Couch 2000-2004

A TriangleStripArray provides the most compact form of basic primitive when building geometry. Like the LineStripArray, the idea is to represent not just individual triangles, but a ribbon of triangles that all share common edges. To do this, you declare a starting triangle in the strip, and then for each additional triangle, you use the last two declared vertices as one edge of the triangle, and provide one more vertex to form the other two sides of your triangle. So, a strip of 2 triangles will declare a total of 4 vertices, a strip of 3 triangles uses 5 vertices and so on. This concept is illustrated in Figure 11 that shows three strips of length 3, 4 and 6.

A triangle strip array image
Figure 11: A triangle strip array showing strips of length 3, 4 and 6

Construction

To create an instance of TriangleStripArray you start with a combination of the TriangleArray and LineStripArray classes. You'll be needing the same vertex format and vertex count information suitable for triangles, and you'll need an extra variable that describes the number of vertices in the strip. Note that it is asking for the number of vertices, not the number of triangles in each strip, which means the minimum number for each count in the array will need to be at least 3 (ie 3 vertices to form a single triangle, more for each subsequent triangle added to the strip).

So, starting with a simple example that creates a strip with 2 triangles in it, you'll need the following code:

    int format = GeometryArray.COORDINATE |
                 GeometryArray.COLOR_3;

	int[] strip_counts = { 4 };

    TriangleStripArray tris = new TriangleStripArray(4, format, strip_counts);
The strip_count variable is an array of ints that describe how many vertices to use from the array for the given triangle strip. Since each strip must have at least a one triangle, the values in this array should always be greater than or equal to 3. Each additional value in the array indicates a new triangle to be added onto the previous, as showing in Figure 11. In the example above, we are telling Java3D to create a single strip that has two triangles - a starting triangle and a single additional point, to give you the second triangle (vertices 1 and 2 are reused for the base of the additional triangle).

After creating the basic primitive, all you need to do is fill in the vertex values through the usual setCoordinate() method. The coordinates must be placed in, in the order that they will be used in the strips as the renderer just starts from the first strip pulling coordinates from the array before moving onto the second strip etc.

    float[] vertices =
    {
        0,   0,  0,   // v1
        1,   5, -3,   // v2
        0,   2,  0,   // v3
        -1,  3,  1,   // v4
    };

    lines.setCoordinates(0, vertices);
Just remember that the number of vertices provided must be at least equivalent to the sum of the lengths of all the strips. For example, if you provided a strip count array with the values { 3, 5, 4 } your coordinate array must have at least 11 vertices declared. More than that really doesn't matter as Java3D will ignore the extras, but it must have at least the minimum amount.

Strips also follow the same rules for the individual triangles and quads when determining what is front facing and what is not. Since strips don't have any restriction on where you put vertices, it is possible for the strip to create a situation where a triangle folds backwards on the strip causing it to have the opposite winding as seen from the current viewpoint. When you look at this strip from the front, that folded back triangle would appear to be missing from the strip, as illustrated in Figure 12.

A tristrip after being folded
Figure 12: A triangle strip with a "missing" triangle due to winding rules to determine front facing for culling purposes