Skip to content

Half Dozen Hello Worlds Part 5 – Android and OpenGL should look like this by default.

package com.learnandroid.helloworld;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class HelloRenderer implements Renderer {
	public void onDrawFrame(GL10 arg0) {
		// TODO Auto-generated method stub
	public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
		// TODO Auto-generated method stub
	public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
		// TODO Auto-generated method stub

The Renderer Interface provides three methods: onDrawFrame, onSurfaceChanged, and onSurfaceCreated. One thing to note is the Renderer in Android will continually draw frames. It will call onDrawFrame repeatedly, allowing you to perform any animations through the logic in this method. In our example program we will be drawing the same thing over and over again, so it will look like a static image, but it is important to realize that frames are constantly being drawn.

onSurfaceCreated provides a place to do any initialization that doesn’t depend on the screen size or orientation. We won’t be using this method in our example.

onSurfaceChanged is called every time there is a new orientation or screen size (including the first time). We are going to put the following code in this method.

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glViewport(0, 0, width, height);
		GLU.gluPerspective(gl, 60.0f, (float)width / (float)height, 0.1f, 100.0f);

glViewPort describes the actual flat representation of the 3D objects. If we were using a camera we would call this the photograph. The first two parameters are the x and y position of the bottom left of the viewport. The last two parameters are the width and height of the viewport. We could have a viewport displayed in a window, which would have values other than (0,0) for the position of its bottom left corner and window height and window width instead of screen height and screen width. In our case, however, we are just going to use the screen values.

glMatrixMode tells OpenGL whether your commands are affecting the projection or the models. Using the camera analogy, we need to be able to specify if we are moving the camera or an object in front of the camera.


will allow us to issue commands to our camera.
OpenGL provides ways to reuse transformations. Our next command:


simply resets the projection matrix so we know that no previous commands sent to OpenGL will be compounded with our commands.

Finally, we get to the actual command we are trying to issue to our projection matrix (camera).

GLU.gluPerspective(gl, 60.0f, (float)width / (float)height, 0.1f, 100.0f);

The values that are passed to gluPerspective are our GL10 object, the angle of view, the aspect ratio, and the distance to the near and far planes.  I’ve provided links to information about angle of view and aspect ratio.  The near plane how close an object can be (beyond the near plane) before it is included in the picture.  The far plane is the greatest distance an object can be from the camera before it is no longer included.  You can think of this is setting your range of vision.  The values I picked here are largely arbitrary for everything except aspect ratio.  Feel free to play around with these numbers and note the affect on the rendered image.

Finally we call these lines of code to put us back into ModelView (so our commands will affect the objects instead of the camera) and reset the Model matrix.


You will frequently see this pattern in OpenGL where you setup your camera and then leave it fixed while manipulating the objects.

{ 11 } Comments

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

    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


  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

    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 .


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


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


  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 *