Skip to content

Half Dozen Hello Worlds Part 5 – Android and OpenGL

Here is the code in my onDrawFrame method.

	public void onDrawFrame(GL10 gl) {
 
	    //Reset Drawing Surface
	    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
	    gl.glLoadIdentity();
 
	    gl.glTranslatef(-4.0f, 5.0f, -20.0f);
	    Letters.H.draw(gl);
 
	    gl.glTranslatef(2.0f, 0.0f, 0.0f);
	    Letters.E.draw(gl);
 
	    gl.glTranslatef(1.5f, 0.0f, 0.0f);
	    Letters.L.draw(gl);
 
	    gl.glTranslatef(1.5f, 0.0f, 0.0f);
	    Letters.L.draw(gl);
 
	    gl.glTranslatef(1.5f, 0.0f, 0.0f);
	    Letters.O.draw(gl);
 
	    gl.glTranslatef(-7.0f, -5.0f, 0.0f);
	    Letters.W.draw(gl);
 
	    gl.glTranslatef(2.5f, 0.0f, 0.0f);
	    Letters.O.draw(gl);
 
	    gl.glTranslatef(2.0f, 0.0f, 0.0f);
	    Letters.R.draw(gl);
 
	    gl.glTranslatef(2.0f, 0.0f, 0.0f);
	    Letters.L.draw(gl);
 
	    gl.glTranslatef(1.5f, 0.0f, 0.0f);
	    Letters.D.draw(gl);
	}

The first two lines reset the drawing surface.

gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glLoadIdentity();

First clearing the surface to the background color, and then loading the identity matrix so no previous commands will affect our next set of commands.

Next I issue this command:

gl.glTranslatef(-4.0f, 5.0f, -20.0f);

This tells OpenGL to translate (move without rotating) the position where the next object will be drawn by (-4, 5, -20) along the x axis, y axis, and z axis respectively. This is the only time I will translate along the Z axis, but I need to make sure we are between the near and far planes that we set to 0.1 and 100. After performing my translation I will call the draw method on the letter H from my enum, causing it to draw itself.

Letters.H.draw(gl);

Now the next line of code will move the position from where we drew the letter H.

gl.glTranslatef(2.0f, 0.0f, 0.0f);

This is why we called gl.glLoadIdentity() at the very beginning of this method. If we had not done so OpenGL would begin where we left off the last time we drew a frame. Now, however, we use this to our advantage since we want to draw our letter E to the right of our letter H. The rest of this method follow this procedure, moving the position and then calling the draw function on the desired letter. I got the numbers used both for the vertices for each letter and for the distance used in the translations through trial and error, so they are based on how I thought the letters should look rather than any system. You should, by all means, try out different numbers to see what results you get. Here is my final result.

Hello World OpenGL

I know there was a lot of new material here, covered very quickly.  If you have any specific questions please leave a comment and I will be happy to answer to the best of my ability.  Also, since we have reached the end of the Hello World series please let me know if there is any specific topic you would like to see covered in a future article.

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