Contents

Chapters

Sections

j3d.org

[ Previous ] [ Up ] [ Next ]

The LineArray

© Justin Couch 2000

The line array presents a collection of single segment lines to the screen. Apart from the PointArray this is the simplest of the primitive types. For each pair of vertices, connect them together as a line.

Construction

As the line array is a very simple primitive the constructor does not change from the basic Geometry class. We supply the number of vertices and the flags indicating the sort of information to be supplied.
    int format = GeometryArray.COORDINATE |
                 GeometryArray.COLOR_3;

    LineArray lines = new LineArray(4, format);
The number of vertices supplied must always be a multiple of two. Why? Each line consists of a vertex for each end of it. No two lines share the same vertex information unless you create two verticies with the same values. This is illustrated in Figure 4. Note that while vertices 3 and 8 share the same position in space (the lines look like they join here), they are actually two independent entities.

A line array image
Figure 4: A line array showing the vertex numbering

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
        -1,  2, -8,   // v4
    };
Note that the provided array is a flat, one-dimensional array with each three values providing the three coordinates of each vertex.

To set the vertex array you now use one of the setCoordinate() methods. Because we've started with a simple array here that contains all of the vertex values, the simplest version is

    lines.setCoordinates(0, vertices);
The first parameter is the position in the array to start setting values for. Because we are starting with all of the vertex values, this index is zero. If you wanted to set a sub-range of the values, then the index will be set appropriately. However, make sure that the supplied array of vertex values is the right length because one that is too big will cause Java3D to throw an exception.

There are various other options available, depending on how you want to set the coordinate values. For example, if you want to change just one item, then use the setCoordinate() method that has just a single array or Point3f instance. If you need to change a large collection of points, calling set on individual values is not exact the most efficient of procedures. You are much better off setting the whole array again.

When you set colour values, the interpretation is the same for colours as the vertex values. Each pair of colours is assigned to the corresponding end points of the line. In between, the renderer generates a colour gradient that smoothly blends from the colour on one vertex to the colour on the other.