More Primitives Points Lines Strips and Fans

When I told you that OpenGL ES was a big, nasty triangle-rendering machine, I was not being 100 percent honest. In fact, OpenGL ES can also render points and lines. Best of all: these are also defined via vertices, and thus all of the above also applies to them (texturing, per-vertex colors, etc.). All we need to do to render these primitives is use something other than GL10.GL_TRIANGLES when we call glDrawArrays()/glDrawElements(). We can also perform indexed rendering with these primitives, although that's a bit redundant (in the case of points at least). Figure 7-18 shows a list of all the primitive types OpenGL ES offers us.

GL_POINTS GLJJNES GL LINE STRIP GL LINE LOOP

GL TRIANGLES GL TRIANGLE STRIP GL TRIANGLE FAN

Figure 7-18. All the primitives OpenGL ES can render

Let's go through all of these primitives really quickly: Point: With a point, each vertex is its own primitive.

Line: A line is made up of two vertices. As with triangles, we can just have 2 x n vertices to define n lines.

Line strip: All the vertices are interpreted as belonging to one long line.

Line loop: This is similar to a line strip, with the difference that OpenGL ES will automatically draw an additional line from the last vertex to the first vertex.

Triangle: This we already know. Each triangle is made up of three vertices.

Figure 7-18. All the primitives OpenGL ES can render

Triangle strip: Instead of specifying three vertices, we just specify number of triangles + 1 vertices. OpenGL ES will then construct the first triangle from vertices (v1,v2,v3), the next triangle from vertices (v2,v3,v4), and so on.

Triangle fan: This has one base vertex (v1) that is shared by all triangles. The first triangle will be (v1,v2,v3), the next triangle (v1,v3,v4), and so on.

Triangle strips and fans are a little bit less flexible than pure triangle lists. But they can give a little performance boost, as fewer vertices have to be multiplied by the projection and model-view matrices. We'll stick to triangle lists in all our code, though, as they are easier to use and can be made to achieve similar performance by using indices.

Points and lines are a little bit strange in OpenGL ES. When we use a pixel-perfect orthographic projection (e.g., our screen resolution is 320x480 pixels and our glOrthof() call uses those exact values), we still don't get pixel-perfect rendering in all cases. The positions of the point and line vertices have to be offset by 0.375f due to something called the diamond exit rule. Keep that in mind if you want to render pixel-perfect points and lines. We already saw that something similar applies to triangles. However, given that we usually draw rectangles in 2D, we don't run into that problem.

Given that all you have to do to render primitives other than GL10.GL_TRIANGLES is to use one of the other constants in Figure 7-17, I'll spare you an example program. We'll stick to triangle lists for the most part, especially when doing 2D graphics programming.

Let's now dive into one more thing OpenGL ES offers us: the almighty modelview matrix.

0 0

Post a comment