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

  1. sabs | December 24, 2009 at 1:01 am | Permalink

    Hii,
    Thanks for your information on openGL. I would like to know more about the 3d pictures means how to create the 3d pictures and also how to add effects to that pictures.
    I have read all the parts of your material it is very informative, iam just beginner to android i would like to know what are the different kinds of UI available in android.
    Thanks once again for your information especially on openGL. please share with me if you have any kind of material regarding the 3d pictures

    Any kind of help is appreciable.

  2. Shrey | January 2, 2010 at 7:56 am | Permalink

    Hi Sheridan,

    Thank u for ur hello world series it was very informative, that helped me a lot.
    Can u post some topics on listeners and layouts.

  3. Mike | March 30, 2010 at 12:12 am | Permalink

    Nice example. How can I add color to the letters? I tried using this in a program that I have but the letters disappear after I execute these 2 lines in a texture mapped object.

    // Blend image’s alpha with background
    gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);

    gl.glEnable(GL10.GL_BLEND);

  4. Ted Hughes | October 12, 2010 at 1:20 pm | Permalink

    Excellent explanation! Hello5 was just my speed. Thank you.

  5. Chris | April 19, 2011 at 1:06 pm | Permalink

    I love your tutorial. Help me alot. Very good structure, brilliant content. Easy for beginners. Thanks a lot.

  6. Daniel Kahn | June 9, 2011 at 11:49 am | Permalink

    Thank you for this direct tutorial. I have a book (Beginning OpenGL Game Programming) but was struggling to get OpenGL loaded up in an android framework. I think you got me there.

  7. Amit Raj | August 23, 2011 at 12:30 am | Permalink

    Hi
    i am beginer of Andriod . i enjoyed with this tutorial . i need some more tutorial along with example to develop application in andriod .kindly help me in this .

    thanks
    amit

  8. Amit Raj | August 23, 2011 at 12:36 am | Permalink

    hi

    i need some more tutorial on OpenGL and List,Layout,Widget,Listener,3Dpictures etc

    thanks
    amit

  9. pavan shinde | October 3, 2011 at 7:58 am | Permalink

    I’m getting the run time error in my android emulator as “The application hello5 has stopped working unexpectedly. Please try again.” How do I resolve this error?? Please HELP!!

  10. parthi | June 18, 2012 at 12:29 am | Permalink

    nice buddy can u post like more animation frames….

  11. parthi | June 18, 2012 at 12:44 am | Permalink

    hi dude my emulator nothing display …what reason .

Post a Comment

Your email is never published nor shared. Required fields are marked *