GlDraw Elements

Once you specify the series of points through glVertexPointer, you use the glDrawElements method to draw those points with one of the primitive shapes that OpenGL ES allows. Note that OpenGL is a state machine, meaning that it remembers the values set by one method when it invokes the next method in a cumulative manner. So you don't need to explicitly pass the points set by glVertexPointer to glDrawElements. glDrawElements will implicitly use those points. Listing 10-1 shows an example of this method with possible arguments.

Listing 10-1. Example of glDrawElements glDrawElements(

// type of shape GL10.GL_TRIANGLE_STRIP, // Number of indices 3,

// How big each index is


// buffer containing the 3 indices mIndexBuffer);

The first argument indicates the type of geometrical shape to draw: GL_TRIANGLE_STRIP signifies a triangle strip. Other possible options for this argument are points only (GL_POINTS), line strips (GL_LINE_STRIP), lines only (GL_LINES), line loops (GL_LINE_LOOP), triangles only (GL_TRIANGLES), and triangle fans (GL_TRIANGLE_FAN).

The concept of a STRIP in GL_LINE_STRIP and GL_TRIANGLE_STRIP is to add new points while making use of the old ones. This way, you can avoid specifying all the points for each new object. For example, if you specify four points in an array, you can use strips to draw the first triangle out of (1,2,3) and the second one out of (2,3,4). Each new point will add a new triangle. (You can refer to the OpenGL red book for more details.) You can also vary these parameters to see how the triangles are drawn as you add new points.

The idea of a FAN in GL_TRIANGLE_FAN applies to triangles where the first point is used as a starting point for all subsequent triangles. So you're essentially making a FAN- or circle-like object with the first vertex in the middle. Suppose you have six points in your array: (1,2,3,4,5,6). With a FAN, the triangles will be drawn at (1,2,3), (1,3,4), (1,4,5), and (1,5,6). Every new point adds an extra triangle, similar to the process of extending a fan or unfolding a pack of cards.

The rest of the arguments of glDrawElements involve the method's ability to let you reuse point specification. For example, a square contains four points. Each square can be drawn as a combination of two triangles. If you want to draw two triangles to make up the square, do you have to specify six points? No. You can specify only four points and refer to them six times to draw two triangles. This process is called indexing into the point buffer.

Here is an example:

Notice how the first triangle comprises p1,p2,p3 and the second one comprises p2,p3,p4. With this knowledge, the second argument identifies how many indices there are in the index buffer.

The third argument to glDrawElements (see Listing 10-1) points to the type of values in the index array, whether they are unsigned shorts (GL_UNSIGNED_SHORT) or unsigned bytes (GL_UNSIGNED_BYTE).

The last argument points to the index buffer. To fill up the index buffer, you need to do something similar to what you did with the vertex buffer. Start with a Java array and use the java.nio package to convert that array into a native buffer.

Here is some sample code that converts a short array of {0,1,2} into a native buffer suitable to be passed to glDrawElements:

//Figure out how you want to arrange your points short[] myIndecesArray = {0,1,2};

//get a short buffer java.nio.ShortBuffer mIndexBuffer;

//Allocate 2 bytes each for each index value ByteBuffer ibb = ByteBuffer.allocateDirect(3 * 2); ibb.order(ByteOrder.nativeOrder()); mIndexBuffer = ibb.asShortBuffer();

//stuff that into the buffer for (int i=0;i<3;i++) {


Now that you've seen mIndexBuffer at work in the preceding snippet, you can revisit Listing 10-1 and better understand what's going on.

Note Rather than create any new points, the index buffer merely indexes into the array of points indicated through the glVertexPointer. This is possible because OpenGL remembers the assets set by the previous calls in a stateful fashion.

Now we'll look at two commonly used OpenGL ES methods: glClear and glColor. We'll use each of these in our test harness.

0 0

Post a comment