Thursday, May 01, 2014

OpenGL|ES 2.0 on Android #1 - Creating a GL|ES 2.0 client

So I've been tinkering with OpenGL ES on Android recently. More specifically, I've been curious about OpenGL ES 2.0.

An empty OpenGL ES 2.0 client
Simply making an empty OpenGL ES 2.0 client in Android is simple, really. There's hardly anything need doing. We'll make this so we have a base template to use for future projects. All it does is create the client context. Nothing gets rendered to screen. It's just one blank screen with a title. Boring but we need to start somewhere.

Prerequisites

You'll, of course, need some familiarity with the Java language. If you haven't already then at least got through the Building you first app tutorial. You should be able to create a Hello World app at the very least.

I'm assuming you have an IDE (for coding) and a Android device that supports OpenGL ES 2.0 (for testing/running the app).

The GLES activity

I started from a Hello World project and made the required changes from there.

package com.example.gles;

import android.app.Activity;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;


public class GLActivity extends Activity implements GLSurfaceView.Renderer {
    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        GLSurfaceView GLView = new GLSurfaceView(this);
        GLView.setEGLContextClientVersion(2);                       // Request OpenGL ES 2.0 client context
        GLView.setRenderer(this);
        GLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);  // Only render when scene changes

        setContentView(GLView);
    }

    /**
     * Called when initializing GL surface and GL resources.
     */
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);    // Black background
    }

    /**
     * Called when setting screen dimensions when first created, or when changing orientation.
     */
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }

    /**
     * Called to render each frame.
     */
    @Override
    public void onDrawFrame(GL10 gl) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }
}

In onCreate we simply initialise a GL ES 2.0 client context using the provided GLSurfaceView class and set it as the view.

onSurfaceCreated is where we initialise OpenGL resources. Here we simply set the default color to black.

onSurfaceChanged gets called to set screen dimensions whether during startup, or anytime screen orientation changes. Anything to do with the view port should go here.

onDrawFrame does the actual rendering work. This is where the bulk of code will eventually land. Here we just clear the whole screen to the color we previously set in onSurfaceCreated.

That's it.