An Introduction to OpenGL for Google Android Programming Environment

An Introduction to OpenGL for Google Android Programming Environment
Page content

In this article, we will introduce a bit about OpenGL and its integration in Android. Let’s talk about OpenGL.

OpenGL, as I mentioned before, is a graphics library that helps us to create 2D and 3D environments. This library, opposite to Direct3D from Microsoft, is multiplatform, in that you can create applications that use OpenGL in a GNU/Linux system and in a Windows one.

In mobile devices we have a reduced version: OpenGL ES. It has less functionality that OpenGL (without ES) and it’s adapted to small devices with their limitations. In an Android phone we don’t have great graphics cards, so resources and implementation have to be optimized if we want to achieve good results.

On the Khronos page we can find a lot of information about this technology.

https://www.khronos.org/opengles/

OpenGL ES in Android

Android currently supports OpenGL ES 1.0, which corresponds to OpenGL 1.3 on a desktop system. The API we use to develop in Android is very similar to the J2ME OpenGL ES API (JSR 239). The OpenGL ES libraries and functionality can be found in the following packages:

javax.microedition.khronos.opengles

javax.microedition.khronos.egl

android.opengl

How to use the API

When I work with OpengGL I always use a “template” get from Android OpenGL examples. It’s a pseudo API that helps me to forget about how Android handles OpenGL and focus on implementation.

One of the main classes to handle OpenGL is GLSurfaceView

https://developer.android.com/guide/samples/ApiDemos/src/com/example/android/apis/graphics/GLSurfaceView.html

Then we have to create a class that implements the interface GLSurfaceView.Renderer. In further lessons I will explain a bit deeper about this class, for now, we need to know that:

  • In this class we initialize and specify the parameters of the OpenGL environment.
  • Here is the “draw” method. This function is always drawing, so code we put inside will be constantly drawn on the screen. If we are drawing a cube that moves on the screen, this is the place to put the routines that are going to drive the cube movement.
  • Another interesting method is the “surfaceCreated” method. This is called the first time the application is launched. Here we define the parameters of the OpenGL that are going to be rules, we can always create another class to load Textures.

Some Terminology

Talking this way about OpenGL and Android could be a bit shocking for people with no idea of what OpenGL is. To make things easier to understand, let’s define some terms to clarify these ideas.

SurfaceView: Provides a dedicated drawing surface embedded inside of a view hierarchy. With this we can create OpenGL objects over views and layouts. This is like the canvas we are going to use to draw our OpenGL environment.

SurfaceHolder: A client may implement this interface to receive information about changes to the surface.

GL10: Its an interface that implements the GL “object”. We will use this GL object to work with the OpenGL routines.

Textures: The textures are images we can add to a OpenGL object.

Follow up

If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev

This post is part of the series: How-to Develop Applications on Android: OpenGL

In this serie of articles we will enter in the 3D world of OpenGL ES and how to use it in Android.

  1. Developing in Android with OpenGL