Reader small image

You're reading from  The Android Game Developer???s Handbook

Product typeBook
Published inAug 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785885860
Edition1st Edition
Languages
Right arrow
Author (1)
Avisekhar Roy
Avisekhar Roy
author image
Avisekhar Roy

Avisekhar Roy is a B.Tech engineer in computer science. He has had a passion for coding since his school days. However, he had no plans to become a game programmer. His fate landed him in the gaming industry in 2010. Since then, he fell in love with game development. Avisekhar has worked in many formats of game development environment, ranging from small companies and individual studios to corporate companies and full-scale game development studios. He recently started his own gaming start-up in 2016 and is currently working on games for the mobile platform. Avisekhar has also worked with some big companies, such as Reliance Games in India, as well as a small-scale studio called Nautilus Mobile. He is now trying to acquire a position in the gaming industry for his own venture, Funboat Games.
Read more about Avisekhar Roy

Right arrow

Chapter 7. Working with Shaders

Every game's success depends largely on its look and feel. This directly means that the game must have an eye-catching graphical display. It is not always possible to provide maximum quality graphical assets due to space and heap restrictions. So, there has to be a way to create or improvise the graphical assets at runtime for display. This necessity gave birth to the concept of shaders.

Shaders can operate on any visual element and can tweak every pixel of drawable elements before rendering. Mostly, shaders are optimized for a specific graphics processor. However, nowadays, shaders can be written to support multiple processors on multiple platforms.

Android accommodates the option to work with shaders in the Android framework itself. Additionally, OpenGL shaders can also be used and customized with the help of the Android NDK. There are many occasions where exquisite graphical quality is delivered with the help of shaders without excellent raw art assets.

We...

Introduction to shaders


Many developers develop games on Android, but do not possess much knowledge about shaders. In most cases, developers do not need to work with shaders, or there are some pre-defined shaders inside the game development framework or engines.

In 1988, the animation studio Pixar introduced the modern concept of shaders. However, GPUs were not capable of handling shaders at that point of time. OpenGL and Direct3D are the first two graphic libraries to support shaders. GPU started supporting shaders through pixel shading at the 2D level. Soon, it was enhanced to support vertex shaders. Nowadays, geometry shaders are also supported by OpenGL 3.2 and Direct3D 10 libraries.

Let's now dive a bit deeper into shaders to understand their definition, necessity, and scope for Android games.

What is a shader?

In simple words, a shader is an instruction set to manipulate the visual display of the input graphic assets.

Let's elaborate the definition a bit. All the instructions are basically...

How shaders work


We have already discussed that shaders process either vertices or pixels. So, the basic working principle is to change or manipulate data at runtime:

A shader process is a specific set of instructions to process vertices or fragments. Different shader programs can be written for various types of processing.

A vertex shader is used to change the shape of the model; it can also change the surface-formation system.

Pixel/fragment shaders can change the pixel color value along with opacity. Pixel data can be merged, modified, or replaced by a shader program to form a new digital image.

Types of shaders


There are many shaders used in the gaming industry. They are categorized on the basis of their behavior and features. Some of the shaders are as follows:

  • Pixel shaders

  • Vertex shaders

  • Geometry shaders

  • Tessellation shaders

Let's have a detailed look at these types.

Pixel shaders

Pixel shaders are 2D shaders that work on textures or digital images. Pixel shaders process colors and other attributes of a single pixel. Each single pixel is called a fragment. This is the reason pixel shaders are often called fragment shaders.

Vertex shaders

A vertex shader mainly operates on the vertices of a mesh or model. Every mesh of a model is made up of multiple vertices. A vertex shader can only be applied to 3D models. So, a vertex shader is a type of 3D shader.

Geometry shaders

Geometry shaders are used to create new primitive graphic elements. After applying a vertex shader in order to execute a rendering pipeline, geometry shaders are used to create points, lines, and triangles to form a surface...

Android library shaders


Android provides the shader option in its framework in the android.graphics package. A few well-known and widely used shaders are also in the Android library. Some of them are as follows:

  • BitmapShader: This can be used to draw a bitmap in the texture format. It also supports tiling or mirroring of the bitmap. It is very useful for creating terrain with tiling.

  • ComposeShader: This is used to merge two shaders. So, it is very useful for masking or merging colors for two different shaders.

  • LinearGradient: This is used to create a gradient along with the given line segment with a defined color set.

  • RadialGradient: This is used to create a gradient along with the given circle segment with a defined color set. A radial origin and radius are provided to create the gradient.

  • SweepGradient: This is used to create a sweep gradient color around a point with the given radius.

Here is an example:

@Override
protected void onDraw ( Canvas c)
{
  float px = 100.0f;
  float py = 100...

Writing custom shaders


A developer has the option to write a customized shader as per their requirements. Android provides the android.graphics.Shader class. It is easy to create your own shader class using the primitive shaders provided.

The custom shader may not include only one shader. It can be a combination of various shaders. For example, consider masking an image with a circular view port with a motion-touch event:

private float touchX;
private float touchY;
private boolean shouldMask = false;

private final float viewRadius;
private Paint customPaint;

@Override
public boolean onTouchEvent(MotionEvent motionEvent) 
{
  int pointerAction = motionEvent.getAction();
  if ( pointerAction == MotionEvent.ACTION_DOWN || 
  pointerAction == MotionEvent.ACTION_MOVE )
    shouldMask = true;
  else
    shouldMask = false;

  touchX = motionEvent.getX();
  touchY = motionEvent.getY();
  invalidate();
  return true;
}

@Override
protected void onDraw(Canvas canvas) 
{
  if (customPaint == null...

Shaders through OpenGL


In Android, OpenGL supports implementing shaders for Android 3D games. OpenGL ES 2.0 is the supporting platform in Android for shaders. It has two functional segments while manually creating the shader:

  • Shader

  • Program

The shader is converted into intermediate code to support the program to run on GPU. In the compiling stage, the shaders are converted. This is the reason why shaders need to be recompiled before the program execution.

We will work with GLSurfaceView of the android.opengl package in our example.

For 3D games, an Android developer can use this package to play with shaders on the Android SDK. This package provides the API to create and use an OpenGL shader with Java.

We will use GLSurfaceView instead of the normal Android View or SurfaceView. The implementation will look like this:

import android.opengl.GLSurfaceView;
import android.content.Context;

public class MyGLExampleView extends GLSurfaceView 
{
  private final GLRenderer mRenderer;

  public MyGLExampleView...

Use of shaders in games


Shaders are vastly used in games and animation, especially when creating dynamic lighting, changing tints, and making dynamic visual improvements. Sometimes, the world environment is created with shaders.

Shaders in a 2D game space

Only pixel shaders can be used in 2D games. Each pixel of a digital image is considered a fragment. This is the reason why pixel shaders are also called fragment shaders. Pixel shaders can only perform color changes, tiling, and masking.

BitmapShader, ComposeShader, LinearGradient, RadialGradient, and SweepGradient are the variants of Android 2D shaders.

A 2D game world is created with images. Developers often choose to create different assets to give the same object a different look and feel. In this process, developers end up making a bigger APK with almost the same use set.

Sprites can also be a field where shaders can hold a significant role. When using the same sprite to create different objects, the colors of certain fragments need to...

Summary


Since Android API level 15, the framework supports OpenGL ES 2.0. This gave immense flexibility to graphic programmers to implement shaders in Android games.

Almost every hardware configuration supports shaders to run on GPU. However, the scale of using shaders determines the performance. In modern day, this is not actually an issue.

Shaders are being used widely in games. In every aspect of graphical programming, shaders have already proven their place. All the famous and successful game developers have acknowledged the importance of shaders. Graphic artists need not worry about everything visual in the game, which reduces the development time significantly.

Shaders are, therefore, widely used in games. Newer shaders are coming up with additional features now. The upgrading cycle of shaders has become less. However, hardware is also being upgraded with newer technology to support the graphical updates.

It feels like magic to see a simple cube turn into anything that has the same orientation...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
The Android Game Developer???s Handbook
Published in: Aug 2016Publisher: PacktISBN-13: 9781785885860
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Avisekhar Roy

Avisekhar Roy is a B.Tech engineer in computer science. He has had a passion for coding since his school days. However, he had no plans to become a game programmer. His fate landed him in the gaming industry in 2010. Since then, he fell in love with game development. Avisekhar has worked in many formats of game development environment, ranging from small companies and individual studios to corporate companies and full-scale game development studios. He recently started his own gaming start-up in 2016 and is currently working on games for the mobile platform. Avisekhar has also worked with some big companies, such as Reliance Games in India, as well as a small-scale studio called Nautilus Mobile. He is now trying to acquire a position in the gaming industry for his own venture, Funboat Games.
Read more about Avisekhar Roy