Skip to content

Half Dozen Hello Worlds Part 5 – Android and OpenGL

Before we look at our code for onDrawFrame I want to introduce an Enum to handle the different letters we will be drawing. Right click on your Project, go to New, then select Enum. Name the Enum Letters and make sure the package matches the package of your Activity; com.learnandroid.helloworld in my case. Now replace the code in Letters.java with the following code.

package com.learnandroid.helloworld;
 
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
 
import javax.microedition.khronos.opengles.GL10;
 
public enum Letters {
	D (new float[] {
			0.0f, 2.0f,
			0.0f, 0.0f,
 
			0.0f, 0.0f,
			1.0f, 0.0f,
 
			1.0f, 0.0f,
			1.25f, 0.25f,
 
			1.25f, 0.25f,
			1.25f, 1.75f,
 
			1.25f, 1.75f,
			1.0f, 2.0f,
 
			1.0f, 2.0f,
			0.0f, 2.0f
	}),
	E (new float[] {
			0.0f, 2.0f,
			0.0f, 0.0f,
 
			0.0f, 2.0f,
			1.0f, 2.0f,
 
			0.0f, 1.0f,
			1.0f, 1.0f,
 
			0.0f, 0.0f,
			1.0f, 0.0f
	}),
	H (new float[] {
			0.0f, 2.0f,
			0.0f, 0.0f,
 
			0.0f, 1.0f,
			1.5f, 1.0f,
 
			1.5f, 2.0f,
			1.5f, 0.0f
	}),
	L (new float[] {
			0.0f, 2.0f,
			0.0f, 0.0f,
 
			0.0f, 0.0f,
			1.0f, 0.0f
			}),
 
	O (new float[] {
			0.0f, 1.75f,
			0.0f, 0.25f,
 
			0.0f, 0.25f,
			0.25f, 0.0f,
 
			0.25f, 0.0f,
			1.25f, 0.0f,
 
			1.25f, 0.0f,
			1.5f, 0.25f,
 
			1.5f, 0.25f,
			1.5f, 1.75f,
 
			1.5f, 1.75f,
			1.25f, 2.0f,
 
			1.25f, 2.0f,
			0.25f, 2.0f,
 
			0.25f, 2.0f,
			0.0f, 1.75f
 
	}),
	R (new float[] {
			0.0f, 2.0f,
			0.0f, 0.0f,
 
			0.0f, 2.0f,
			1.0f, 2.0f,
 
			1.0f, 2.0f,
			1.5f, 1.5f,
 
			1.5f, 1.5f,
			1.5f, 1.25f,
 
			1.5f, 1.25f,
			1.0f, 1.0f,
 
			1.0f, 1.0f,
			0.0f, 1.0f,
 
			1.0f, 1.0f,
			1.5f, 0.0f
 
	}),
	W (new float[] {
			0.0f, 2.0f,
			0.75f, 0.0f,
 
			0.75f, 0.0f,
			1.0f, 1.0f,
 
			1.0f, 1.0f,
			1.25f, 0.0f,
 
			1.25f, 0.0f,
			2.0f, 2.0f
	});
 
	//Constructor - Load Vertices into a Buffer for OpenGL
	private Letters(float[] vertices)
	{
		this.vertices = vertices;
 
		ByteBuffer buffer = ByteBuffer.allocateDirect(vertices.length * 4);
		buffer.order(ByteOrder.nativeOrder());
		vertexBuffer = buffer.asFloatBuffer();
		vertexBuffer.put(this.vertices);
		vertexBuffer.position(0);
	}
 
	public void draw(GL10 gl)
	{
		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
 
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
 
		gl.glDrawArrays(GL10.GL_LINES, 0, vertices.length / 2);
 
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
	}
 
	private float[] vertices;
	private FloatBuffer vertexBuffer;
}

A full discussion on enums in Java is beyond the scope of this tutorial, but I will mention that enums in Java can have most of the features classes have. They can have methods, such as the draw method in our Letters enum. They can have constructors, but only private constructors that get called automatically. The enum class here allows us a simple way to define the points for each letter.

In the constructor I simply take an array of points (or vertices) and put them into a buffer than can be used by OpenGL. Each letter is defined by the set of verticies it passes to the constructor. That is, each pair of numbers represents a point (x, y) in our letter. I have put spaces between each pair of points to make it easier to read, because these define the start and end of a line segment.

Let’s look at how these points are being used in our draw method.

gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

These lines are telling OpenGL to use our vertexBuffer for the Vertex Array, and then telling OpenGL to enable the Vertex Array so we can use it.

gl.glDrawArrays(GL10.GL_LINES, 0, vertices.length / 2);

This line does the actual drawing. The first parameter tells it what to draw. GL_LINES will take the first two vertices in the array and draw a line between them, then it will take the next two verticies and draw a line between them, and so on. If there is an odd number of vertices the last vertex will be ignored.

Finally we tell OpenGL that it can disable the Vertex Array when we are done with it.

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

Now let’s go back to HelloRenderer.java and look at the onDrawFrame method.

{ 11 } Comments