Android OpenGL ES development (two): OpenGL ES environment construction

Zero: the purpose of environment construction

In order to draw graphics using OpenGL ES in Android applications, a view container must be created for them. The most direct or common way is to implement a GLSurfaceView and a GLSurfaceView.Renderer. GLSurfaceView is a view container for drawing graphics with OpenGL, and GLSurfaceView.Renderer controls the content drawn in the view.

The following will explain how to use GLSurfaceView and GLSurfaceView.Renderer to make a minimal implementation on the Activity of a simple application.

One: Declare the use of OpenGL ES in the Manifest

In order for your application to use the OpenGL ES 2.0 API, you must add the following statement to the manifest:

<uses-feature android:glEsVersion="0x00020000" android:required="true" />

If your application needs to use texture compression, you also need to declare which compression format your application needs to support so that they can be installed on compatible devices.

<supports-gl-texture android:name="GL_OES_compressed_ETC1_RGB8_texture" />
<supports-gl-texture android:name="GL_OES_compressed_paletted_texture" />

For more knowledge of texture compression format, you can go to https://developer.android.com/guide/topics/graphics/opengl.html#textures for further understanding.

2: Create an Activity to display OpenGL ES graphics

The Activity of an application that uses OpenGL ES is the same as the Activity of other applications. The difference lies in the layout of the Activity you set. In many apps that use OpenGL ES, you can add TextView, Button and ListView, as well as GLSurfaceView.

The following code shows the basic implementation of using GLSurfaceView as the main view:

public class OpenGLES20Activity extends Activity {

    private GLSurfaceView mGLView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create a GLSurfaceView instance and set it
        // as the ContentView for this Activity.
        mGLView = new MyGLSurfaceView(this);
        setContentView(mGLView);
    }
}

Note: The Android version required for OpenGL ES 2.0 is 2.2 and above. Please make sure that the version targeted by your Android project is compatible.

Three, create a GLSurfaceView object

GLSurfaceView is a special View through which you can draw OpenGL images. But the View itself does not do much. The main drawing is controlled by the GLSurfaceView.Renderer set in the View. In fact, there is very little code to create this object. You might want to try to skip the extends operation and just create an unmodified GLSurfaceView instance, but this is not recommended. Because in some cases, you need to extend this class to capture touch events. The way to capture touch events will be introduced in a later article.
The basic code of GLSurfaceView is very small. In order to implement it quickly, an internal class is usually created in the Activity that uses it to implement it:

class MyGLSurfaceView extends GLSurfaceView {

    private final MyGLRenderer mRenderer;

    public MyGLSurfaceView(Context context){
        super(context);

        // Create an OpenGL ES 2.0 context
        setEGLContextClientVersion(2);

        mRenderer = new MyGLRenderer();

        // Set the Renderer for drawing on the GLSurfaceView
        setRenderer(mRenderer);
    }
}

You can set GLSurfaceView.RENDERMODE_WHEN_DIRTY to make your GLSurfaceView refresh when the data changes, that is, modify the rendering mode of GLSurfaceView. This setting can prevent GLSurfaceView from redrawing until you call requestRender(). This setting is more beneficial to your APP on the silent writing level.

Fourth, create a Renderer class

The realization of the GLSurfaceView.Renderer class is the real start to be able to use OpenGL ES in the application. This class controls the content drawn by the GLSurfaceView associated with it. There are three methods in the renderer that can be called by the Android system to know what to draw on GLSurfaceView and how to draw

  • onSurfaceCreated()-called when the View's OpenGL environment is created.
  • onDrawFrame()-Called every time the View is redrawn
  • onSurfaceChanged()-This method is called if the geometry of the view changes (for example, when the device's screen orientation changes).

The following is the basic implementation using the OpenGL ES renderer, the only thing to do is to draw a black background in GLSurfaceView.

public class MyGLRenderer implements GLSurfaceView.Renderer {

    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        // Set the background frame color
        GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    }

    public void onDrawFrame(GL10 unused) {
        // Redraw background color
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
    }

    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
    }
}

Five, summary

The above content is the basic OpenGL ES basic environment configuration. The code in this article is just to create a simple Android application and then use OpenGL to display a blackboard. Although not doing other more interesting things, but by creating these classes, you should already have the basis for drawing graphical elements using OpenGL.

Note: You may be curious why you see the GL10 parameters when using the OpenGL ES 2.0 API, because these method signatures are simply used in the 2.0 API so that the Android framework code can be kept simple.

If you are familiar with the OpenGL API, you should now be able to create an OpenGL ES environment in your APP and start drawing. But if you need more help to use OpenGL, please look forward to the following article.

Guess you like

Origin blog.csdn.net/xfb1989/article/details/115334149