Rendering using OpenGL

Android uses OpenGL for rendering. Android SDK libraries include the OpenGL libraries, specially optimized for Android. Android started supporting OpenGL from API level 4 and then increased its support as the level increased. Currently, the maximum supported version of OpenGL is OpenGL ES 3.1 from API level 21.

OpenGL versions

Different OpenGL versions have a different set of features. Versions 1.0 and 2.0 have a lot of differences in terms of coding style, API convenience, functionality, and feature support. Let's discuss the following OpenGL ES versions that are significant to Android development:

  • OpenGL ES 1.x
  • OpenGL ES 2.0
  • OpenGL ES 3.0
  • OpenGL ES 3.1

OpenGL 1.x

OpenGL version 1.x has been supported from Android API level 4 with a shared OpenGL ES 1.x library, libGLESv1.so. The headers gl.h and glext.h contain all the necessary APIs for OpenGL functionality.

OpenGL 2.0

In the current industry, a developer prefers to use OpenGL ES 2.0 for games, because almost every device supports this OpenGL version, and it provides vertex and fragment shaders useful for games. OpenGL ES 2.0 can be used in Android native development projects by including the libGLESv2.so shared library in the project, as follows:

LOCAL_LDLIBS := -lGLESv2

The headers are gl2.h and gl2ext.h. OpenGL ES 2.0 is supported from Android API level 5.

OpenGL 3.0

From Android API level 21, OpenGL ES 3.0 is supported. The developer can include libGLESv3.so to use OpenGL 3.1, as follows:

LOCAL_LDLIBS := -lGLESv3

The headers are gl3.h and gl3ext.h.

OpenGL 3.1

From Android API level 21, OpenGL ES 3.1 is supported. The developer can include libGLESv3.so to use OpenGL 3.1, as follows:

LOCAL_LDLIBS := -lGLESv3

The headers are gl31.h and gl3ext.h.

OpenGL ES 3.0 and OpenGL ES 3.1 are not supported by many Android devices. If a developer intends to use them, then there should be an OpenGL version check before using the version. Also, proper version of OpenGL ES must be used to run the game on that particular device. The latest Android N has support for OpenGL ES 3.2.

Detecting and setting the OpenGL version

This piece of Android Java code can be used to implement proper OpenGL ES support for an Android game:

private GLSurfaceView glSurfaceView;
void setOpenGLVersion()
{
  final boolean supportOpenGLEs3 = configurationInfo.reqGlEsVersion >= 0x30000;

  if (supportOpenGLEs3) 
  {
    glSurfaceView = new GLSurfaceView(this);
    glSurfaceView.setEGLContextClientVersion(3);
    glSurfaceView.setRenderer(new RendererWrapper());
    setContentView(glSurfaceView);
  }
  else
  {
  final boolean supportOpenGLEs2 = configurationInfo.reqGlEsVersion >= 0x20000;

  if (supportsOpenGLEs2) 
    {
      glSurfaceView = new GLSurfaceView(this);
      glSurfaceView.setEGLContextClientVersion(2);
      glSurfaceView.setRenderer(new RendererWrapper());
      setContentView(glSurfaceView);
    }
    else
    {
      glSurfaceView = new GLSurfaceView(this);
      glSurfaceView.setEGLContextClientVersion(1);
      glSurfaceView.setRenderer(new RendererWrapper());
      setContentView(glSurfaceView);
    }
  }
}

Texture compression and OpenGL

Texture compression has a significant effect on the rendering process handled by OpenGL. It can increase or decrease performance for different types of texture compression. Let's have a quick look at some of the important texture compression formats:

  • ATC
  • PVRTC
  • DXTC

ATC

ATI texture compression is often called ATITC. This compression supports RGB with and without an alpha channel. This is the most common and widely used compression technique for Android.

PVRTC

Power VR texture compression uses 2-bit and 4-bit pixel compression with or without an alpha channel. This is used by many game developers across the globe.

DXTC

DXTC is also called S3 texture compression, which is also used for OpenGL. This uses a 4-bit or 8-bit ARGB channel.

OpenGL manifest configuration

Android requires the version definition of OpenGL used in the application, along with other required options.

Here is the version declaration syntax for OpenGL ES:

<uses-feature android:glEsVersion=<Target version goes here> android:required="true" />

Here are the target version options:

  • 0x00010000 for version 1.0
  • 0x00010001 for version 1.1
  • 0x00020000 for version 2.0
  • 0x00030000 for version 3.0
  • 0x00030001 for version 3.1
  • 0x00030002 for version 3.2

Here is the optional setting for texture compression declaration:

<supports-gl-texture android:name=<Compression support type goes here> />

These are the compression type options:

  • GL_OES_compressed_ETC1_RGB8_texture
  • GL_OES_compressed_paletted_texture
  • GL_EXT_texture_compression_s3tc
  • GL_IMG_texture_compression_pvrtc
  • GL_EXT_texture_compression_dxt1
  • GL_EXT_texture_compression_dxt2
  • GL_EXT_texture_compression_dxt3
  • GL_EXT_texture_compression_dxt4
  • GL_EXT_texture_compression_dxt5
  • GL_AMD_compressed_3DC_texture
  • GL_EXT_texture_compression_latc
  • GL_AMD_compressed_ATC_texture
  • GL_ATI_texture_compression_atitc

However, not all texture compressions are supported by every device. The developer should always choose the target texture compression depending on the hardware and Android version requirement.

Note

Google does the filtration process of devices automatically if the target device does not support the declared texture format or formats.

Choosing the target OpenGL ES version

As you have already learned, not all devices support all OpenGL versions. So, it is very important to choose the correct OpenGL version before developing the game. Here are a few factors that should be evaluated while choosing the OpenGL version:

  • Performance
  • Texture support
  • Device support
  • Rendering feature
  • Programming comfort

Performance

It is noticed that OpenGL version 3.x is faster than OpenGL version 2.x, which is way faster than OpenGL 1.x. So, it is always better to use the latest possible version in the game.

Texture support

Texture compression support varies with OpenGL versions. Older versions support older texture compression factors. Also, Android version support is not universal for all OpenGL versions. Again, it is better to use the latest possible version for texture support.

Device support

This constraint keeps a developer's feet on the ground. The latest versions of OpenGL are not supported by all devices. So, in order to target a bigger range of devices, the user should change the OpenGL version to 2.0 as most devices support this version.

Rendering feature

As the version of OpenGL increases, the feature list becomes an important factor while choosing the OpenGL version. The developer must know the support required for developing the application and accordingly, they must choose the version.

Programming comfort

There is a huge coding style and API change among the versions of OpenGL. The developer should choose the version if it can actually be developed in the company with ease.

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

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