Android-glsurfaceview Activity Framework Program

Source: Internet
Author: User

Two basic classes let us use the OpenGL ES API to create and manipulate graphics: Glsurfaceview and Glsurfaceview.renderer.

1. Glsurfaceview:

This is a view class, you can call OpenGL API to draw graphics and manipulate objects, functions and Surfaceview similar. We can create an instance of the Glsurfaceview class and add our own renderer. If we are going to implement some touchscreen operations on our own, we have to extend this class to implement touch listeners.

2. Glsurfaceview.renderer

This interface defines the methods required to draw a graphic in an OpenGL Glsurfaceview. We must provide implementations for these interfaces in a separate class and attach it to the Glsurfaceview instance object using the Glsurfaceview.setrenderer () method.

We need to implement the following methods of Glsurfaceview.renderer:

A) onsurfacecreated (): The system invokes it once when the Glsurfaceview is created. We can use it to set OpenGL environment variables, or initialize OpenGL graphics objects.

b) Ondrawframe (): The system calls this method each time the Glsurfaceview is redrawn. This method mainly completes the operation of drawing graphics.

c) onsurfacechanged (): The system calls this method when the geometry property of the Glsurfaceview changes, including the size or the orientation of the device screen. For example, the system changes the screen from upright to horizontal so that it is called. This method is mainly used to respond to changes in the Glsurfaceview container.

3. Steps to use

A. Defining the Render

 Public classSunnyglrenderImplementsGlsurfaceview.renderer { Public floatMAngle; floatone = 0.5f; PrivateFloatbuffer TriggerBuffer2 = Bufferutil.floattobuffer (New float []{                0,one,0,//Top Vertex-one,-one,0,//Lower left Pointone,-one,0,});//Lower right Point        PrivateFloatbuffer TriggerBuffer1 = Bufferutil.floattobuffer (New float []{                0,one,0,//Top Vertex-one,-one,0,//Lower left Pointone,-one,0,});//Lower right Point        Private float[] Mtrianglearray = {                //X, Y, Z This is a equilateral triangle .-0.5f, -0.25f, 0,                0.5f, -0.25f, 0,                0.0f, 0.559016994f, 0 }; @Override Public voidonsurfacecreated (GL10 gl, EGLConfig config) {//GLES30: For the OpenGL ES2.0 version, the corresponding//Gles30:opengl ES3.0//Black BackgroundGl.glclearcolor (0.0f, 0f, 1f, 0.5f); //gl.glclear (gl10.gl_color_buffer_bit); //enable vertex Array (otherwise gldrawarrays does not work)gl.glenableclientstate (Gl10.gl_vertex_array); } @Override Public voidOnsurfacechanged (GL10 GL,intWidthintheight) {            //Mprogram = Gles30.glcreateprogram (); //log.e ("Zcxgl", "Could not link Program:mprogram =" +mprogram);LOG.E ("Zcxgl", "onsurfacechanged"); floatRatio = (float) Width/height; Gl.glmatrixmode (gl10.gl_projection); //sets the current matrix as a projection matrixGl.glloadidentity ();//resets the matrix to the initial value//gl.glfrustumf (-ratio, ratio,-1, 1, 3, 7);//Setting the projection matrix based on the aspect ratioGL.GLFRUSTUMF (-ratio, ratio,-1, 1, 5, 6); }        PrivateFloatbuffer ColorBuffer2 = Bufferutil.floattobuffer (New float[]{One,0,0, one,0,one,0, one,0,0, One,one,}); @Override Public voidondrawframe (GL10 gl) {//Redraw Background ColorLOG.D ("Zcxgl", "Ondrawframew");            Gl.glclear (Gl10.gl_color_buffer_bit); GL.GLCOLOR4F (1.0f, 0.0f, 0.0f, 1.0f); /************ enable Modelview mode and use Glu.glulookat () to set the viewpoint ***************/            //set the current matrix to model view modeGl.glmatrixmode (Gl10.gl_modelview); Gl.glloadidentity (); //Reset the matrix to its default state//Set ViewpointsGlu.glulookat (GL, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f); /*****************************************/            LongTime = Systemclock.uptimemillis ()% 4000L; MAngle= 0.090f * ((int) time); //Resets the current model observation matrixgl.glloadidentity ();            //moving the plot origin coordinates with the above statement is equivalent to setting a new drawing far point coordinate,//gl.glfrustumf (-ratio, ratio,-1, 1, 1, ten);//the back 1-10 refers to the 1-10 layers of the image,            //The larger the image level, the smaller the display on the screen. The default is (0,0,1),            //Move left 1.5 units and move into screen 6.0. Gl.gltranslatef (0f, 0.0f, -5.0f); Gl.glrotatef (MAngle,0.0f, 0.0f, 1.0f);            //Enable smooth shadingGl.glenableclientstate (Gl10.gl_color_array);//             //gl.glcolor4f (1.0f, 0.0f, 0.0f, 1.0f);//you can set the monotone color of the drawing directly             //Set Triangle Point            //Gl.glvertexpointer (3, gl10.gl_fixed, 0, triggerbuffer);Gl.glvertexpointer (3,gl10.gl_float,0, triggerBuffer2);             //set the color matrix for smooth shadingGl.glcolorpointer (4,GL10.GL_FLOAT,0,COLORBUFFER2);//are all one-dimensional matrices, so the first parameter is a representation of the length of a color             //DrawingGl.gldrawarrays (gl10.gl_triangles, 0, 3); //Turn off Color smoothing shading Settingsgl.gldisableclientstate (Gl10.gl_color_array); //gl.glfinish ();        }    }

2. Use Glsurfaceview and set render

PrivateGlsurfaceview Mglsurfaceview; Static classBufferutil { Public StaticFloatbuffer Mbuffer;  Public StaticFloatbuffer Floattobuffer (float[] a) {//first initialize buffer, the length of the array, because a float takes up 4 bytesBytebuffer MBB = Bytebuffer.allocatedirect (A.length * 4); //array sorting with NativeorderMbb.order (Byteorder.nativeorder ()); Mbuffer=Mbb.asfloatbuffer ();            Mbuffer.put (a); Mbuffer.position (0); returnMbuffer; }} @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Initview (); Mglsurfaceview.setrenderer (NewSunnyglrender ());    Setcontentview (Mglsurfaceview); } @Overrideprotected voidOnresume () {Super. Onresume ();    Mglsurfaceview.onresume (); } @Overrideprotected voidOnPause () {Super. OnPause ();    Mglsurfaceview.onpause (); }    Private voidInitview () {Mglsurfaceview=NewGlsurfaceview ( This); }

3. Effects

Android-glsurfaceview Activity Framework Program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.