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.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
 
public class Hello5 extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GLSurfaceView drawSurface = new GLSurfaceView(this);
        drawSurface.setRenderer(new HelloRenderer());
        setContentView(drawSurface);
    }
}

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 HelloRenderer.java in detail.

{ 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 *