Chapter 6. Rendering Graphics with OpenGL ES

Let's face the fact that one of the main interests of Android NDK is to write multimedia applications and games. Indeed, these programs consume lots of resources and need responsiveness. That is why one of the first available APIs (and almost the only one until recently) in Android NDK is an API for graphics: the Open Graphics Library for Embedded Systems (abbreviated as OpenGL ES).

OpenGL is a standard API created by Silicon Graphics and is now managed by the Khronos Group (see http://www.khronos.org/). OpenGL provides a common interface for all standard GPUs (Graphics Processing Unit like your graphics card, and so on) on desktops. OpenGL ES is a derivative API available on many embedded platforms, such as Android or iOS. It is your best hope to write portable and efficient graphics code. OpenGL can render both 2D and 3D graphics.

There are three main releases of OpenGL ES currently supported by Android:

  • OpenGL ES 1.0 and 1.1 are supported on all Android devices (except 1.1, which is supported on a few very old devices). It offers an old school graphic API with a fixed pipeline (that is, a fixed set of configurable operations to transform and render geometry). Specification is not completely implemented, but most features are available. This could still be a good choice for simple 2D or 3D graphics or to port legacy OpenGL code.
  • OpenGL ES 2 is supported on almost all phones nowadays, even older ones, starting from API Level 8. It replaces the fixed pipeline with a modern programmable pipeline with Vertex and Fragment Shaders. It is a bit more complex but also more powerful. It is a good choice for the more complex 2D or 3D games, while still maintaining a very good compatibility. Note that OpenGL ES 1.X is frequently emulated by an OpenGL 2 implementation behind the scenes.
  • OpenGL ES 3.0 is available on modern devices starting from API Level 18, and OpenGL ES 3.1 is available starting from API Level 21 (not all devices at these API level may support it though). They bring a set of new improvements to GLES 2 (Texture Compression as a standard feature, Occlusion Queries, Instanced Rendering, and others for 3.0, Compute Shaders, Indirect Draw commands, and others for 3.1) and a better compatibility with the desktop version of OpenGL. It is backward compatible with OpenGL ES 2.

This chapter teaches you how to create some basic 2D graphics using OpenGL ES 2. More specifically, you are going to discover how to:

  • Initialize OpenGL ES
  • Load a texture from a PNG file packaged in the assets
  • Draw sprites using vertex and fragment shaders
  • Render a particle effect
  • Adapt graphics to various resolutions

With OpenGL ES, and graphics in general, being a wide subject, this chapter covers only the basics to being with.

Initializing OpenGL ES

The first step to create awesome 2D and 3D graphics is to initialize OpenGL ES. Although not terribly complex, this task requires some boilerplate code to bind a rendering context to an Android window. These pieces are glued together with the help of the Embedded-System Graphics Library (EGL), a companion API of OpenGL ES.

For this first section, we are going to replace the raw drawing system implemented in the previous chapter with OpenGL ES. A black to white fading effect will demonstrate that the EGL initialization works properly.

Note

The resulting project is provided with this book under the name DroidBlaster_Part5.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.85.76