Skip to content

Half Dozen Hello Worlds Part 5 – Android and OpenGL

This is the last post of the series “Half Dozen Hello Worlds” where we explored different ways of communicating with Android users.

Part 1 used a simple TextView widget to display “Hello World” on the screen.

Part 2 explored using a Layout XML.

Part 3 showed you two different kinds of pop-up message.

Part 4 explored using Text to Speech to have Android actually say “Hello World” out loud.

When I was in college one of my favorite professors was Dr. Heath.  He taught Calculus and he would often spend ten or fifteen minutes in front of the board showing the steps to work out a single problem.  When he finished and the answer was 3 (or whatever) he would face the class with a big grin on his face and say, “That’s how you get 3, THE HARD WAY!”  Today we are going to display “Hello World” THE HARD WAY.

OpenGL is a very powerful Graphics Library (which is what GL stands for incidentally) which is intended to be hardware independent.  The version of OpenGL that is implemented on Android is called OpenGL ES 1.0 (The ES stands for Embedded Systems).  If you are unfamiliar with OpenGL all you really need to know right now is that it is a library for rendering 3D graphics.  We will be rendering our Hello World as a series of Line Segments in 3D space. Rendering, in case you don’t know, simply refers to displaying three dimensional objects on a two dimensional surface (i.e. your screen).

This is a more advanced topic and will, by necessity, be more involved than previous examples.  I will try my best to explain each part in detail, but please leave a comment if you have any questions as there is a lot to cover.  We are only going to scratch the surface of OpenGL on Android here, but it should be enough to get you started.

First we are going to create a new Android Project using Android 1.5 (Minimum SDK 3).  I will assume you know how to do this since it was covered in Part 1.  I named my main Activity Hello5.  Here is the source code from that activity.

package com.learnandroid.helloworld;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class Hello5 extends Activity {
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        GLSurfaceView drawSurface = new GLSurfaceView(this);
        drawSurface.setRenderer(new HelloRenderer());

The first thing you will probably notice here is the new import statement at the top.

import android.opengl.GLSurfaceView;

GLSurfaceView is a new kind of view that will provide us with a drawing surface on which to render our 3D objects. GLSurfaceView was introduced in Android 1.5 and is the reason we selected 1.5 for our minimum SDK for this project. GLSurfaceView will take care of providing us a place to display our graphics with very little effort on our part.

Inside on create we’ve added the following lines to the default code.

GLSurfaceView drawSurface = new GLSurfaceView(this);
drawSurface.setRenderer(new HelloRenderer());

The first line initialized the GLSurfaceView with a Context (our Activity). The second line tells our GLSurfaceView the name of the class that will perform the rendering. When you enter this code you will get an error and Eclipse will underline HelloRenderer in red. Hover over it, and select Create class ‘HelloRenderer’.

CreateHelloRendererOn the create screen that comes up you can leave all of the defaults and just click finish.  On the next page we will look at in detail.

{ 11 } Comments