Java Reference
In-Depth Information
Understanding Triangle Strips
It is very important to understand what a triangle strip is and why it is used in M3G. As the
name implies, a triangle strip is a strip of triangles. In TriangleCanvas , this strip contains just
one single triangle. M3G allows for a strip with an arbitrary number of triangles.
Recall from basic geometry that any polygon can be decomposed into a number of triangles.
This means that a triangle strip can be used to create any arbitrary polygon.
Because all triangles in a strip share common side(s) with others, TriangleStripArray in
M3G can use a compact way of specifying vertices for multiple triangles. To specify one triangle,
you will need three vertices; to specify two adjoining triangles, you will need only four vertices;
to specify three, you need five vertices, and so on.
Take as an example a square composed of two triangles, as shown in Figure 15-7.
Figure 15-7. Using four vertices to describe two triangles forming a square
In Figure 15-7, you can see how four vertices— (0,0,0) , (3,0,0) , (0,3,0) , and (3,3,0) —
are sufficient to describe this strip of two triangles.
Modifying TriangleCanvas to Rotate a Square
We'll continue our experimentation with M3G APIs using TriangleCanvas by modifying it to
display a rotating square instead of a triangle.
You will need to specify one more vertex for the triangle strip:
short[] vertices = { 0, 0, 0, 3, 0, 0, 0, 3, 0 , 3,3,0 };
VertexArray vertexArray = new VertexArray(vertices.length / 3, 3, 2);
vertexArray.set(0, vertices.length/3, vertices);
One more vertex means an additional normal:
byte[] normals = { 0, 0, 127, 0, 0, 127, 0, 0, 127, 0,0, 127 };
VertexArray normalsArray = new VertexArray(normals.length / 3, 3, 1);
normalsArray.set(0, normals.length/3, normals);
 
Search WWH ::




Custom Search