OpenGL ES with Android Tutorial- Switching from Canvas to OpenGL

It is about time we delve into the graphical capabilities of the Android platform. Android supports the OpenGL ES API. Needless to say that offloading graphics handling to a dedicated GPU is way more optimal than doing it in the CPU. Most android devices have such a dedicated GPU.
OpenGL is an API for writing 2D and 3D graphics that is rendered on the GPU. This will free up precious computing resources on the CPU to be used for more complex physics or more entities or anything not related to graphics.

There are a few notions that need to be understood but I will introduce them when we will bump into them during the course.

If you followed the articles related to displaying graphics on an android device, you already know that in order to display graphical elements, we need a surface and a renderer. We used a basic SurfaceView from which we obtained the Canvas and we drew everything onto it by calling the supported draw method from within our game loop.
Using OpenGL is not much different. Android comes with a dedicated implementation of the SurfaceView interface for displaying images rendered by OpenGL.

Let’s create the android project the usual way.

New Project Wizard

New Project Wizard

I call the activity simply Run. Check what the wizard has generated and it should look like this:

public class Run extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Nothing special.

Creating the OpenGL renderer

Let’s build the renderer. Create a class GlRenderer which implements the android.opengl.GLSurfaceView.Renderer interface.

It will look like this:


import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;

public class GlRenderer implements Renderer {

	@Override
	public void onDrawFrame(GL10 gl) {
	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
	}
}

We need to implement the above 3 methods. Currently they are empty and do nothing.

The methods

onSurfaceCreated(GL10 gl, EGLConfig config)created or recreated. It is important to bear the recreated bit in mind as it means every time the device goes to sleep for example and awakes, the surface gets recreated. Because the context which holds the resources gets destroyed too, this is the place where we will load our resources (images for textures, etc).

onSurfaceChanged(GL10 gl, int width, int height) is called whenever the surface size changes. This mainly affects our viewport. The viewport is just a rectangular region through which we see our game world.

onDrawFrame(GL10 gl) is called by the rendering thread to draw each frame. This is where all the drawing happens. We don't need to call it explicitly. A rendering thread is created by android for us and that will call it.

Let's switch to the OpenGL renderer. Check out the new Run activity.

package net.obviam.opengl;

import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class Run extends Activity {
	
	/** The OpenGL view */
	private GLSurfaceView glSurfaceView;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // requesting to turn the title OFF
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // making it full screen
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

        // Initiate the Open GL view and
        // create an instance with this activity
        glSurfaceView = new GLSurfaceView(this);
        
        // set our renderer to be the main renderer with
        // the current activity context
        glSurfaceView.setRenderer(new GlRenderer());
        setContentView(glSurfaceView);
    }

	/** Remember to resume the glSurface  */
	@Override
	protected void onResume() {
		super.onResume();
		glSurfaceView.onResume();
	}

	/** Also pause the glSurface  */
	@Override
	protected void onPause() {
		super.onPause();
		glSurfaceView.onPause();
	}
}

In line 12 we declare a GLSurfaceView member variable. This is our OpenGL view provided by android. When we instantiate it (line 27) we have to make it context aware. That is, for the view to have access to the application environment.
All we need to do is to add our renderer to this view. This we do in line 31.
Line 32 tells the activity to use our OpenGL view.

The onResume() and onPause() methods are being overridden and trigger the respective methods in our view.

You can run the application as an Android app and you should see a blank black screen.
That is it. We have switched from the canvas to the OpenGL renderer.

Download the source code and eclipse project here (obviam.opengl.p01.tgz)

To learn how to display primitives (triangles, rectangles) with OpenGL check out the next article.

LinkedInRedditTumblrDiggTechnorati FavoritesShare

10 Responses - Add Yours+

  1. Orbitus says:

    I am getting an error when opening the project. I seem to get this error for all opengl projects… is there something I have to configure for the compiler to know about openGL?

    There is a big x near package net.obviam.opengl;
    and the other sample code I was using said the same… the OpenGL java file was totally full of errors….

    Thanks!

  2. Myzifer says:

    Is the .p0 file like an iso so I have to mount it with some software in which case which or how else can I open it as a couple googles searches have yielded nothing.

  3. sailaja says:

    what is output of this program

  4. Nick says:

    Thanks a ton. I’ve been looking forward to learning how to use OpenGL on android. Actually bought two books to learn it, but this is nice to see while I wait for them to be shipped. Thanks for the head start and hopefully I can translate my JOGL code into an Android application!

  5. [...] going to try Switching from Canvas to OpenGL and toss in parts of [...]

  6. David.wan says:

    Nice introduction article to Android with brief and clear explanation.

  7. Dude says:

    Thanks a lot! I have searched a lot just for a simple “skeleton” code with openGL. You rock!

  8. vikram says:

    Hi thanks for this wonderful intro on OpenGL on android…

  9. Elliott says:

    I was looking for something like this, very nice intro to OpenGL on Android.

Leave a Reply

You must be logged in to post a comment.