Contents

Chapters

Sections

j3d.org

[ Previous ] [ Up ] [ Next ]

The QuadArray

© Justin Couch 2001 - 2004

QuadArrays provide the basic four-sided rendered primitive. You define the quad by providing four vertices that make up the extremities. The renderer then joins them in the order that you provide them.

Construction

When providing the points, the basic pattern follows the other simple primitive types - there is a flat array of vertices that has the components of each coordinate placed sequentially in the array. In the same way that a pair of vertices provided a line for the LineArray primitive, every four sets of coordinates (or vertices) represents one quad.

Constructing a set of quads starts by constructing an instance of the QuadArray class, which is also derived from the GeometryArray, so the same flags are used. Because quads always require four vertices (they wouldn't be quadrilaterals otherwise!), the value of the vertexCount parameter must always be a multiple of four.

    int format = GeometryArray.COORDINATE |
                 GeometryArray.COLOR_3;

    QuadArray quads = new QuadArray(8, format);
After the first four sets of vertices (12 items in the coordinate array), then the next quad is drawn, such as in Figure 6 that shows two quads being drawn.

A quad array image
Figure 6: A quad array showing the vertex numbering for two quads

The vertices can be specified in any order that you like. This leaves you a lot of options. Figure 7 gives you a variety of shapes that could be constructed using just four vertices.

Types of quads
Figure 7: The types of quads that can be constructed

While all these shapes are possible, it is usually not a good idea to construct most of these forms. Most rendering systems (both software and hardware) decompose the quad into a series of triangles. When this happens, shapes such as the two on the right never end up coming out right. Typically the renderer picks the wrong pair of vertices to make into triangles and the result is a very deformed looking shape. Besides, if you want a bowtie shape, why use a quad when two triangles would be more efficient anyway?

What is the front?

Because you can provide any four set of coordinates, this can lead to some apparently interesting issues. For example, your quad cannot be seen! One of the most common traps that programmers new to 3D graphics run into is this issue. This has a lot to do with understanding how 3D rendering systems work. So, let's take a couple of steps backwards and look at what happens during the process of putting your polygon onto the monitor.

In highly complex scenes, there will be many polygons. An often quoted cliche is that it reality is rendered in about 60 million polygons. Put that at about 120 frames a second, and you can see that is one hell of a fill-rate for your Geforce 20 to have to push out. When you look out the window to the big blue room, you will notice that of those 60 odd million polygons, you probably see less than a quarter of them. There's a lot behind you, a heap hidden behind other objects, and also a many that sit on the opposite side of the objects to where you are viewing them from. So, put this all down and you've trimmed that to maybe 10 million polygons.

Video cards have a hard time pushing through that many polygons at the best of times. Throw in a bit of interaction or animation and you're soon pushing the boundaries of your hardware. To make life easier, your rendering software does a series of checks on the polygons to remove as many as possible before they even get to your video card. Anything that could not possibly be seen is removed - this is termed culling. Apart from the spatial culling (such as the stuff behind you) then we start looking at the individual polygons. Anything that is not facing you doesn't need to be rendered, so can be culled. How does the rendering system decide which polygons face you and which don't?

Determining which way a polygon is facing is an important issue - that's why your polygon sometimes shows and sometimes doesn't depending on which way you've declared the vertices. Why does vertex ordering matter? Well, this is really a semi-arbitrary decision. Somehow, the rendering software must determine what constitutes the "front" of the polygon. To do this, the system must determine a direction and that direction is given by the normal to the polygon. In order to determine the normal, the system uses the points of two adjacent edges and does some maths (called a Cross-Product) to create a normal. The nature of this maths is such that depending on which way those two edges are put together, will determine the "direction" that your polygon is facing, and hence what is the front and back. Change the order that the points of your polygon is declared in and the "front" side of the polygon changes, as illustrated in Figure 8.

Determing the front of the polygon
Figure 8: How the declaration order effects the front side of the polygon

How does the rendering software determine which order? By convention, almost all rendering systems use an anti-clockwise order to determine the normal. That is, if the polygon is facing to you, the vertices must be declared in an anti-clockwise order. If they are declared in a clockwise order, the polygon will be facing away from you, and hence won't be visible. So, in order to make sure your polygon is always visible, ensure that the vertices are declared in an anti-clockwise order.