Search Projects

Sunday, March 28, 2021

Getting Started with OpenGL ES for Android

All Android devices have graphics processing unit (GPU), which used for handling the graphics processing. OpenGL is one of the API used in Android which support all devices, other is Vulkan which support only Android 7.0 and above. In this post we will getting stated with OpenGL ES for Android. This is simple Android OpenGL ES tutorial for beginners.

Prerequisite

Before we get started with OpenGL ES Tutorial for Android, we should know about the prerequisite like pre-learning, software, tools, plugins required and other information and definition about graphics APIs.

What is OpenGL ES?

As defined in our many posts, OpenGL is acronym of Open Graphics Library which is cross platform which means, platform-independent graphics API. OpenGL ES means OpenGL for Embedded Systems, API for computer graphics in embedded systems like mobile phones.

As we proceed further in this OpenGL ES Tutorial for Android, we assume that reader are aware of basics of Java, xml, Opengl and Android ecosystem. If you have some idea about Android you must have Android Studio installed which have Android SDK, AVD and all other tools required for Android App development.

Project Setup

Create a new project in android studio, name it OpenGLESTutorial choose basic template. Android  studio will create the all required code including the java files and xml files. 

1. OpenGL ES Check

As we are working with OpenGL ES we need to check if android device the support OpenGL ES 2.0 or not. 

final ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
    final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
 
    if (supportsEs2)
    {
        // Request an OpenGL ES 2.0 compatible context.
        mGLSurfaceView.setEGLContextClientVersion(2);
 
        // Set the renderer to our demo renderer, defined below.
        mGLSurfaceView.setRenderer(new LessonOneRenderer());
    }
    else
    {
        // This is where you could create an OpenGL ES 1.x compatible
        // renderer if you wanted to support both ES 1 and ES 2.
        return;
    }
 
    setContentView(mGLSurfaceView);



Earlier Android emulator does not support OpenGL ES 2.0, to test real device was required, but now now Android emulator does support OpenGL ES 2.0. Hence we can test our tutorial on the Android emulator as well as real Android phones.  

Moreover, to stop Android user who don't have phone/device supporting OpenGL ES Version, <uses-feature> tag need to be added in the project <uses-feature> tag. Add the following code in AndroidManifest.xml file.
<uses-feature android:glEsVersion="0x00020000"
android:required="true" />

2. Create Class

First create a class name OpenGLView Overide the class with GLSurfaceView. Add all the overide methord. Create init methord as shown below.

public class OpenGLView extends GLSurfaceView {

public OpenGLView(Context context) {
super(context);
init();
}

public OpenGLView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init(){
setEGLContextClientVersion(2);
setPreserveEGLContextOnPause(true);
setRenderer(new OpenGLRenderer());
}
}

Now create a class OpenGLRenderer and implement interface GLSurfaceView.Renderer and write all methord.
public class OpenGLRenderer implements GLSurfaceView.Renderer {
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(1f,0,0,1f);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {

}

@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
}
}

3. Main Activity XML

In activity_main layout create an widget for creating canvas. Add the following code- 

<com.india.myapplication.OpenGLView
android:id="@+id/openglview"
android:layout_height="match_parent"
android:layout_width="match_parent"/>

Now call the widget in Main Activity class
OpenGLView openGLView;
Initalize the OpenGL View in main activity class
openGLView =(OpenGLView) findViewById(R.id.openglview);

Now the app, test it on emulator or on real device. You will see a red color surface which is rendered by OpenGL ES. 
You can change the color of surface by changing in the -  GLES20.glClearColor(1f,0,0,1f);

No comments:

Post a Comment