Reprint please indicate the source
Objective
OpenGL ES is a subset of OpenGL and is designed for embedded devices such as phones, PDAs, and game consoles. The API is defined by Khronos Group, Khronos is a graphic hardware and software industry Association, which focuses on the open standards of graphics and multimedia.
So OpenGL ES is used as a third-party library in Android.
So far, OpenGL ES has developed 3 versions, OpenGL ES 1.0, OpenGL ES 2.0, OpenGL ES 3.0. OpenGL ES 1.0 is based on the OpenGL 1.3 specification, OpenGL ES 2.0 is based on OpenGL 2.0, OpenGL ES 3.0 is mobile device-specific, OpenGL 4.3 is the standard.
Body: Creating simple triangles with OpenGL ES 2.0 First step: Create a Glsurfaceview object
There are 2 ways to generate Glsurfaceview, which can be written directly in XML, or new in code.
In this paper, we choose the latter one, eg:
Public class sunnyopenglactivity extends fragmentactivity { PrivateGlsurfaceview Mglsurfaceview;@Override protected void onCreate(@Nullable Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Initview (); }@Override protected void Onresume() {Super. Onresume (); Mglsurfaceview.onresume (); }@Override protected void OnPause() {Super. OnPause (); Mglsurfaceview.onpause (); }Private void Initview() {Mglsurfaceview =NewGlsurfaceview ( This); }
Step Two: Customize the render class to implement the Glsurfaceview.render interface
This step is the most important step, because shader generation, matrix transformation, texture loading, etc. are implemented here, Glsurfaceview.render has three important interfaces to implement:
onsurfacecreated (GL10 glunused,eglconfig config);
onsurfacechanged (GL10 glunused,int width,int height);
ondrawframe (GL10 glunused);
The timing of the above three callback methods is as follows:
Onsurfacecreated: Callback When Glsurfaceview instance is generated;
Onsurfacechanged: Callback when phone horizontal/vertical screen is switched;
Ondrawframe: is automatically callback by the system, it will call redraw view at a certain frame frequency.
When the render mode O for Glsurfaceview is set to glsurfaceview.rendermode_continuously or not set, the system will render actively, and it will callback the Ondrawframe () method
The Sunnyglrender class is as follows:
Public class sunnyglrender implements Glsurfaceview. Renderer { Private intMprogram;Private intMapostionhandle;PrivateFloatbuffer Mtrianglevb;//define vertex (vertex) coloring command statement Private FinalString Vertexshadercode ="Uniform mat4 Umvpmatrix; \ n "+"attribute Vec4 vposition; \ n "+"void Main () {\ n"+"gl_position = vposition; \ n "+"} \ n";Private FinalString Fragmentshadercode ="Precision Mediump float;\n"+"void Main () {\ n"+"Gl_fragcolor = VEC4 (0.63671875,0.76953125,0.22265625,1.0); \ n"+"}\n";//Application projection and camera view Private intMumvpmatrixhandle;//The total transformation matrix used to store the result of the transformation matrix [4*4] Private float[] Mmvpmatrix =New float[ -];//[4*4] View transformation matrix Private float[] Mvmatrix =New float[ -];//[4*4] Projection transformation matrix Private float[] Mprojmatrix =New float[ -];//[4*4] Model transformation matrix Private float[] Mmmatrix =New float[ -]; Public floatMAngle;@Override Public void onsurfacecreated(GL10 gl, EGLConfig config) {//GLES20: For the OpenGL ES2.0 version, the corresponding //gles30:opengl ES3.0 //Black backgroundGles20.glclearcolor (0.5F0.5F0.5F1.0f);//glclear: Clear buffer flag, this is: clear the color buffer and depth buffer, clear the entire window as a black backgroundGles20.glclear (Gles20.gl_depth_buffer_bit | Gles20.gl_color_buffer_bit); Initshapes ();intVertextshader = Loadshader (Gles20.gl_vertex_shader,vertexshadercode);intFragmentshader = Loadshader (Gles20.gl_fragment_shader,fragmentshadercode);//apply for a specific shader, program! = 0 Successful ApplicationMprogram = Gles20.glcreateprogram ();if(Mprogram! =0) {Gles20.glattachshader (Mprogram,vertextshader); Gles20.glattachshader (Mprogram,fragmentshader);//Connection shaderGles20.gllinkprogram (Mprogram);int[] Linkstatus =New int[1];//View shader Connection StatusGLES20.GLGETPROGRAMIV (program, Gles20.gl_link_status, Linkstatus,0);if(linkstatus[0] = gles20.gl_true) {//Connection failedLOG.E (TAG," Could not Link program:"); LOG.E (TAG, Gles20.glgetprograminfolog (program)); Gles20.gldeleteprogram (program); program =0; }//Gets the specific shader vposition parameter. Mapostionhandle = Gles20.glgetattriblocation (Mprogram,"Vposition"); } }/** * Initialize some parameters of the triangle * / Private void Initshapes() {/** * x, y, z axis coordinates * U,V is also the s,t, which is the coordinates of images, videos, etc. loaded into the glsurfaceview in the form of textures * u,v is no direction * * floatTrianlgecoords[] = {//x,y,z,u,v-1.0F,-0.5F0, -0.5F0.0F1.0F,-0.5F0,1.5F,-0.0F0.0F1.11803399F0,0.5F1.61803399f}; Bytebuffer VBB = Bytebuffer.allocatedirect (Trianlgecoords.length *4); Vbb.order (Byteorder.nativeorder ()); Mtrianglevb = Vbb.asfloatbuffer (); Mtrianglevb.put (trianlgecoords); Mtrianglevb.position (0); }/** * Load specified shader * @param type * @param shadercode * @return */ Private int Loadshader(intType,string Shadercode) {intShader = Gles20.glcreateshader (type);if(Shader! =0){//Load Shader script (this is the string variable command statement for this example)Gles20.glshadersource (Shader,shadercode);//Compile shader scriptGles20.glcompileshader (shader);int[] compiled =New int[1];//View compilation resultsGles20.glgetshaderiv (shader, Gles20.gl_compile_status, compiled,0);if(compiled[0] ==0) {//Compile failed, release the requested shaderGles20.gldeleteshader (shader); Shader =0; } }returnShader }@Override Public void onsurfacechanged(GL10 GL,intWidthintHeight) {Gles20.glviewport (0,0, width, height);floatRatio = (float) Width/height;//Call this method to calculate the build perspective projection matrixMatrix.frustumm (Mprojmatrix,0,-ratio,ratio,-1,1,3,7);//When sucrface is changed, gets the Umvpmatrix parameter of the specified shaderMumvpmatrixhandle = Gles20.glgetuniformlocation (Mprogram,"Umvpmatrix");//Set camera angle of view //Call this method to generate the camera 9 parameter position matrixMATRIX.SETLOOKATM (Mvmatrix,0,0,0,-3,//x, y coordinates of camera 0,0,0,//target x, y, Z coordinates 0,1.0F1.0F//Camera visual vector (UPX,UPY,UPZ, three vectors the direction of the final synthetic vector is the direction of the camera)); }@Override Public void Ondrawframe(GL10 GL) {//Redraw background color The result of the transformation of the left matrix projection matrix and the right matrix view matrix is stored in the total matrix MmvpmatrixMatrix.multiplymm (Mmvpmatrix,0, Mprojmatrix,0, Mvmatrix,0);//GLES20.GLUNIFORMMATRIX4FV (mumvpmatrixhandle,1,false,mmvpmatrix,0); //Create a rotation action for the Triangle /*long time = systemclock.uptimemillis ()% 4000L; MAngle = 0.090f * ((int) time); */ //Create a matrix that rotates a certain angle around the x, y, Z axisMatrix.setrotatem (Mmmatrix,0, MAngle,0,0,1.0f); Matrix.multiplymm (Mmvpmatrix,0, Mvmatrix,0, Mmmatrix,0); Matrix.multiplymm (Mmvpmatrix,0, Mprojmatrix,0, Mmvpmatrix,0); GLES20.GLUNIFORMMATRIX4FV (Mumvpmatrixhandle,1,false, Mmvpmatrix,0);//Use the program in OpenGL environmentGles20.gluseprogram (Mprogram);//Prepare to draw triangle data //Assign a value Gles20.glvertexattribpointer (MAPOSTIONHANDLE,3,GLES20.GL_FLOAT,FALSE,12,MTRIANGLEVB) to the specified shader's parameters; //Use the value of the parameter taken from the specified shaderGles20.glenablevertexattribarray (Mapostionhandle);//Start drawingGles20.gldrawarrays (Gles20.gl_triangles,0,3); }}
Step Three: Permission declarations
After the above two steps, the program can not run, you have to declare the appropriate permissions in the Androidmanifest.xml.
<!-- Tell the system this app requires OpenGL ES 2.0. --> <uses-feature android:glEsVersion="0x00020000" android:required="true" />
You can see the triangle by running the program.
Next section
Resources
Android OpenGL ES2.0 Development Documentation
Android OpenGL ES Application (ii) Textures
A Dangerous OpenGLES2.0
Google Apidemo (this looks at the official demo, it is not posted)
Android OpenGL ES 0 Basic series (i): Understanding the basic usage of Glsurfaceview,glsurfaceview.render