Contents

Chapters

Sections

j3d.org

[ Previous ] [ Up ] [ Next ]

The TriangleFanArray

© Justin Couch 2000 - 2004

The TriangleFanArray provides a different way of providing efficient primitive description. In the previous section the TriangleStripArray used a base triangle and then added each new triangle by using the last two vertices of the previous triangle and adding a new vertex to complete the shape. For this representation, all of the triangles in a single fan use one common vertex and then build the structure around that, much like taking the top of a cone, slicing down one side and unfurling it into a flat shape (though flatness is not a requirement for fans).

To describe the geometry for a single fan, you start with the common vertex. This is the first vertex of the fan. Next declare two more vertices to create your first triangle. To add another triangle to the fan, take the last vertex declared, the initial vertex and the new vertex triangle and that is now the new triangle. As with the strip version, each fan must consist of a minimum of 3 vertices and then an additional vertex for each triangle added after the first. Figure 13 illustrates 3 fans with 3, 4 and 6 vertices.

A triangle fan array image
Figure 13: A triangle fan array showing strips of length 3, 4 and 6

Construction

To create an instance of TriangleFanArray you start with the same basic setup as the LineStripArray and TriangleStripArray. The third parameter, although it is called stripVertexCounts in the documentation, defines the number of vertices to use in each fan. As with the triangle strip, each value in this array must be greater than or equal to three. So, starting with a simple example that creates a fan with 2 triangles in it, you'll need the following code:
    int format = GeometryArray.COORDINATE |
                 GeometryArray.COLOR_3;

	int[] fan_counts = { 4 };

    TriangleFanArray tris = new TriangleFanArray(4, format, fan_counts);
The fan_count variable is an array of ints that describe how many vertices to use from the array for the given triangle fan. Since each fan 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 13. In the example above, we are telling Java3D to create a single fan that has two triangles - a starting triangle and a single additional point, to give you the second 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 fans as the renderer just starts from the first fan pulling coordinates from the array before moving onto the second fan etc.

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

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

A trifan after being folded
Figure 14: A triangle fan with a "missing" triangle due to winding rules to determine front facing for culling purposes