In our next example, you’ll use OpenGL ES to create a simple drawing, a rectangle, using OpenGL primitives, which are pixels, polygons, and triangles. When you draw the square, you’ll use a primitive called the GL_Triangle_Strip, which takes three vertices (the x, y, and z points in an array of vertices) and draws a triangle. The last two vertices become the first two vertices for the next triangle, with the next vertex in the array being the final point. This process repeats for as many vertices as there are in the array, where two triangles are drawn. OpenGL supports a small set of primitives, that allow you to build anything using simple geometric shapes, from a rectangle to 3D models of animated characters.

OpenGL primitives and their descriptions:

GL_LINE_LOOP: Draws a continuous set of lines. After the first vertex, it draws a line between every successive vertex and the vertex before it. Then it connects the start and end vertices.

GL_LINE_STRIP: Draws a continuous set of lines. After the first vertex, it draws a line between every successive vertex and the vertex before it.

GL_LINES: Draws a line for every pair of vertices given.

GL_POINTS: Places a point at each vertex.

GL_TRIANGLE_FAN: After the first two vertices, every successive vertex uses the previous vertex and the first vertex to draw a triangle. This flag is used to draw cone-like shapes.

GL_TRIANGLE_STRIP: After the first two vertices, every successive vertex uses the previous two vertices to draw a triangle.

GL_TRIANGLES: For every triplet of vertices, it draws a triangle with corners specified by the coordinates of the vertices.

In the next listing, we use an array of vertices to define a square to paint on our surface. To use the code, insert it directly into the code for immediately below the commented line:

// do whatever drawing here

gl.glClear(GL10.GL_COLOR_BUFFER_BIT |

GL10.GL_DEPTH_BUFFER_BIT);

float[] square = new float[] {

0.25f, 0.25f, 0.0f,

0.75f, 0.25f, 0.0f,

0.25f, 0.75f, 0.0f,

0.75f, 0.75f, 0.0f };

FloatBuffer squareBuff;

ByteBuffer bb =

ByteBuffer.allocateDirect(square.length*4);

bb.order(ByteOrder.nativeOrder());

squareBuff = bb.asFloatBuffer();

squareBuff.put(square);

squareBuff.position(0);

gl.glMatrixMode(GL10.GL_PROJECTION);

gl.glLoadIdentity();

GLU.gluOrtho2D(gl, 0.0f,1.2f,0.0f,1.0f);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, squareBuff);

gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

gl.glColor4f(0,1,1,1);

gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

This code is dense with OpenGL commands. The first thing we do is clear the screen using glClear, which you want to do before every drawing. Then we build the array that’ll represent the set of vertices that make up our square. As we explained before, we’ll be using the OpenGL primitive GL_TRANGLE_STRIP to create the rectangle, where the first set of three vertices (points 1, 2, and 3) represent the first triangle. The last vertex represents the third vertex (point 4) in the second triangle, which reuses vertices 2 and 3 from the first triangle as its first two to make the triangle described by points 2, 3, and 4. To put it more succinctly, Open GL takes one triangle and flips it over on its third side (in this case, the hypotenuse). We then create a buffer to hold that same square data. We also tell the system that we’ll be using a GL_PROJECTION for our matrix mode, which is a type of matrix transformation that’s applied to every point in the matrix stack. The next things we do are more related to setup. We load the identity matrix and then use the gluOrtho2D(GL10 gl, float left, float right, float bottom, float top) command to set the clipping planes that are mapped to the lower-left and upperright corners of the window.

Now we’re ready to start drawing our image. First, we use the glVertex-Pointer(int size, int type, int stride, pointer to array) method, which indicates the location of vertices for our triangle strip. The method has four attributes: size, type, stride, and pointer. The size attribute specifies the number of coordinates per vertex (for example, a 2D shape might ignore the z axis and use only two coordinates per vertex), type defines the data type to be used (GL_BYTE, GL_SHORT, GL_FLOAT, and so on). stride specifies the offset between consecutive vertices (how many unused values exist between the end of the current vertex and the beginning of the next), and pointer is a reference to the array. Though most drawing in OpenGL ES is performed by using various forms of arrays such as the vertex array, they’re all disabled by default to save system resources. To enable them, we use the OpenGL command glEnableClientState(array type), which accepts an array type; in our case the type is GL_VERTEX_ARRAY. Finally, we use the glDrawArrays function to render our arrays into the OpenGL primitives and create our simple drawing. The glDrawArrays (mode, first, count) function has three attributes:

mode indicates which primitive to render, such as GL_TRIANGLE_STRIP; first is the starting index of the array, which we set to 0 because we

want it to render all the vertices in the array; and count specifies the number of indices to be rendered, which for us is 4.