Lesson number 1This article is written for beginners who (like me) want to learn how to write programs for Android using OpenGL. The main ideas and code are taken from Kevin Brazalerās wonderful book āOpenGL ES 2 for Android. A Quick-Start Guide by Kevin Brothaler (1).
Why retell you ask? The fact is that before this book I read a dozen more articles on this topic and Kevinās code did not go straight away to me (in the book, the Eclipse development environment, and I have Android Studio). Therefore, I decided to write this article so that it was clear, first of all, to myself.
First, let's find out what OpenGL is. If you read Wikipedia (2), you will see the following:
āOpenGL (Open Graphics Library) is a specification that defines a programming-independent, platform-independent programming interface for writing applications using two-dimensional and three-dimensional computer graphics.
Includes more than 300 functions for drawing complex three-dimensional scenes from simple primitives. Used when creating computer games, CAD, virtual reality, visualization in scientific research. On the Windows platform, it competes with Direct3D. ā
We will study a shortened version of OpenGL ES 2 (hereinafter OpenGL). They shortened it for obvious reasons, Android has a small operating memory compared to desktop PCs, the presence of a Java virtual machine also imposes certain limitations. There are probably many more reasons why this was done, but we should not care. You just need to know that there is a great tool OpenGL and we must learn to use it!
To begin , you must have Android Studio (3) installed on your computer. It is desirable to connect a real device to it, as experience has shown that emulators do not work well with OpenGL. In my case, it will be the Samsung GT-P3113 Android 4.2.2 tablet, API 17.
Our task is to write and run a program that will cycle through the screen and fill it with red using the means of OpenGL.
Step one. Creating a project.
1.1 Open Android Studio and create a new project.

1.2. Select the folder where you will store the project and enter the name of the application.
First Open GL Project.

1.3. Choose the smallest version of the device your application can run on. OpenGL ES2 is supported starting with Android 2.3.3 (Gingerbread), so we cover about 100% of devices on Google Play.

1.4. Select an empty Activiti form. Name the main Activation FirstOpenGlProjectActivity, you can not create the Layout, but if you decide to create it, then name the activity_first_open_glproject. We are waiting for some time while the project is being built.
Step two. Launch of the project
2.1 Run the project on a real device.

2.2 Select the main Activiti for viewing and instead of what was automatically generated there, insert the following code. In the future, we analyze line by line what it does.
package com.adc2017gmail.firstopenglproject; import android.app.Activity; import android.app.ActivityManager; import android.content.Context; import android.content.pm.ConfigurationInfo; import android.opengl.GLSurfaceView; import android.os.Bundle; import android.widget.Toast; public class FirstOpenGLProjectActivity extends Activity { private GLSurfaceView glSurfaceView; private boolean rendererSet = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); glSurfaceView = new GLSurfaceView(this);
Android Studio should swear, because in the code we have a link to the new FirstOpenGLProjectRenderer class. The studio will offer you to create this class. Click on the red light bulb near the code with the right mouse button and select create class. Call it FirstOpenGLProjectRenderer.
As a result, your project structure will be like this.

Open the FirstOpenGLProjectRenderer document. Delete what was generated in it and paste the following code.
')
package com.adc2017gmail.firstopenglproject; import android.opengl.GLSurfaceView.Renderer; import javax.microedition.khronos.egl.EGLConfig; import javax.microedition.khronos.opengles.GL10; import static android.opengl.GLES20.glClearColor; import static android.opengl.GLES20.glClear; import static android.opengl.GLES20.glViewport; import static android.opengl.GLES20.GL_COLOR_BUFFER_BIT; public class FirstOpenGLProjectRenderer implements Renderer { @Override public void onSurfaceCreated(GL10 glUnused, EGLConfig config) {glClearColor(1.0f, 0.0f, 0.0f, 0.0f); } @Override
Now errors should not be. We start the project for execution.
The application icon should appear on the device screen, and the screen itself will fill in with red.
Step three. What happened?
Let's sort the code line by line. Let's start with the main Activiti.

Line (C) 1. The name of the package is declared. The collector of the project Android Studio for it collects our project.
C 3 - 9. The necessary libraries are imported for the operation of our main Activity and FirstOpenGLProjectActivity class.
On 11. Announce the creation of the class FirstOpenGLProjectActivity, which is inherited from the class Activity.
C 12. Create a GLSurfaseView object of the same name class.
GLSurfaceView is a special class that manages the OpenGL surface and converts it into an Android-friendly display environment. The class has many functions that make OpenGL easier to use. Here are some of the most common features:
- GLSurfaceView provides work in a separate thread (thread) to render OpenGL, so the main thread is not busy with us.
- supports continuous or on-demand rendering (update, redraw content on the screen).
- supports screen settings for EGL, the interface between OpenGL and our window system.
C 13. Create a boolean variable rendererSet, which defaults to false. It will control the operation of the GLSurfaceView class in the pause and standby modes of the main Activiti.

C 15. Annotation saying that the method is overridden.
C 16. Declaring the onCreate method (works when an application is started).
C 18. Create a new glSurfaseView object.
C 20 - 30. Check whether our device supports OpenGL ES 2? It appears that this check is outdated and not needed. All Android devices already support OpenGL ES 2, especially since we do not offer anything in return, except for the phrase āThis device does not support ...ā. Without it, everything works fine, you can leave only line 27.
From ActivityManager we receive information that allows us to pull out the device configuration from the system state. And already from it we get information that will help us understand whether our device supports OpenGL ES 2.
27. Set the surface and drawing parameters using the FirstOpenGLProjectRenderer () class.
C 32. Display the contents.

C 36 - 47 Two methods that pause the display of content on the screen if our Activity is not active and is in standby or pause mode.
GLSurfaceView requires that we call its onResume () and OnPause () methods when its parent Activate calls its onResume () and onPaused () methods. We add a challenge to these methods in order to properly resume and terminate our activities.
Now consider what the FirstOpenGLProjectRenderer class does.

C1. Declare the name of the application build package
C3-C9. Import the necessary libraries

C11. Class declaration
C13 A method that fills the surface in red. The first three parameters in the brackets of the glClearColor method set the weights for red, green and blue, and the last one sets the transparency of the layer. If we change the first coefficient to 0, and the second one to 1, we get a green screen. I am sure that at once you donāt tell me what color the screen will be if the first two coefficients are equal to 1. Play around. ļ
C18.19. The method sets the size of the layer. In this case, the surface will be the entire length and width of the screen of the device.
C25 The method cleans the surface from the contents. It is called when it's time to make a new frame. We still have to draw something, even if we just need to clear the screen. The render buffer will be replaced and the contents will be displayed on the screen after this method is called, so if we do not draw anything, then most likely we will get a flickering screen.
Why are we linking the GL10 in these methods? Developers borrow methods developed under the OpenGL ES 1.0 API. They work well in the ES2 version.
Summarize.
We created the project, initialized OpenGL ES2, started the stream of cleaning and drawing the screen. And most importantly - OpenGL is not so scary compared to what we drew :).
References:
1.
pragprog.com/book/kbogla/opengl-es-2-for-android2.
ru.wikipedia.org/wiki/OpenGL3.
developer.android.com/sdk/index.html