Write a few articles to record my study of OpenGL process, this is the first article. This piece of article through the implementation of a clear screen operation, first understand some basic knowledge.
1. Create an activity as the Display page:
/** * View Display page, only realize the operation of clear screen <br/> * 1. Create glsurfaceview<br/> * 2. Call Glsurfaceview Setrenderer method, set renderer<br/ > * 3. Implement renderer<br/> * 4. Handling Activity Lifecycle Events <br/> */public class Firstopenglactivity extends appcompatactivity {private static final String TAG = FirstOpenGLActivity.class.getSimpleName (); Private Glsurfaceview Mglsurfaceview; /** * is render seted */private Boolean isrendererset = false; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Mglsurfaceview = new Glsurfaceview (this); Does the prosecutorial system support OpenGL ES 2.0 Activitymanager Activitymanager = (activitymanager) getsystemservice (Context.activity_servi CE); Configurationinfo configurationinfo = Activitymanager.getdeviceconfigurationinfo (); Boolean isSupportsEs2 = Configurationinfo.reqglesversion >= 0x20000; LOG.D (TAG, "isSupportsEs2 =" + isSupportsEs2); if (ISSUPPORTSES2) {Request an OpenGL ES 2.0 compatible context mglsurfaceview.seteglcontextclientversion (2); Assign our renderer//when surface is created or changed, and when a new frame is drawn, the renderer is glsurfaceview called Mglsurfaceview.setrende RER (New Firstopenglrenderer ()); Isrendererset = true; } else {Toast.maketext (this, "This device dose not the support OpenGL ES 2.0", Toast.length_short); } setcontentview (Mglsurfaceview); }//page recovery, continue the background rendering thread, continued with OpenGL context @Override protected void Onresume () {super.onresume (); if (isrendererset) {mglsurfaceview.onresume (); }}//page is not visible, pause background rendering thread, release OpenGL context @Override protected void OnPause () {super.onpause (); if (isrendererset) {mglsurfaceview.onpause (); } }}
2. Create a renderer class to implement the renderer interface:
The/** * Renderer class implements the Renderer interface * Created by Hsji on 16/1/10. */public class Firstopenglrenderer implements Glsurfaceview.renderer {/** * When surface is created, Glsurfaceview calls this method; * This happens when the application first runs, and this method may be called when the device is woken up or when the user switches back from another activity. * In practice, this means that this method may be called multiple times when the program is running. * * @param GL10 * @param eglConfig */@Override public void onsurfacecreated (GL10 gl10, EglConfig Eglcon FIG) {//Set the color used to empty the screen Rgba gles20.glclearcolor (1.0f, 0.0f, 0.0f, 0.0f); /** * This method will be called by Glsurfaceview every time the surface size changes after surface is created. * The size of your surface changes when you switch back and forth between horizontal and vertical screens. * * @param GL10 * @param width * @param height */@Override public void onsurfacechanged (GL10 gl10, int width, int height) {//set The OpenGL viewport to fill the entire surface gles20.glviewport (0, 0, wid th, height); }/** * This method is called by Glsurfaceview when a frame is drawn. * In this method we must draw something, even if it just empties the screen; * because, after this method returns, the render buffer is swapped and displayed on the screen, * if nothing is drawn, it may lookto flicker Effect * * @param GL10 */@Override public void Ondrawframe (GL10 gl10) {//clear the Rendering Sur Face//Clears the screen, erases all colors on the screen, and calls before Glclearcolor () defines the color to fill the entire screen gles20.glclear (gles20.gl_color_buffer_bit); }}
A little explanation: how can there be an unused parameter type GL10?
* He is a legacy of OpenGL ES 1.0 API;
* If you want to write a renderer that uses OpenGL ES 1.0, use this parameter,
* However, the OpenGL ES 2.0,GLES20 class provides a static method to access.
3. Interaction between the render thread and the main thread (which is then covered in detail, here is just a summary)
Glsurfaceview will invoke the renderer's method in a separate thread. By default, Glsurfaceview is rendered continuously with the refresh frequency of the display device, and of course it can also be configured to render on request, with only Glsurfaceview.rendermode_when_ Dirty Call Glsurfaceview.setrendermode () as a parameter.
Since Android's Glsurfaceview performs rendering in the background county, it must be careful to invoke OpenGL in this render thread, using UI-related calls in the Android main threads The communication between two threads can use the following method: The Glsurfaceview instance in the main thread can call the Queueevent () method to pass a runnable to the background render thread, and the render thread can invoke the activity's Runonuithread ( ) to pass events to the main thread.
4. Run the program to see a pure red screen.
Android OpenGL (i) Start preparing