This article describes the polygon function on the drawing plane developed by Android. Share it for your reference, as follows:
The 3D graphics in the computer are actually composed of many planes. The so-called "drawing 3D" graphics is actually formed through multiple plane graphics. The steps to call GL10 graphics to draw 2D graphics are as follows:
i. Calling GL10glEnableClientState(GL10.GL_VERTEX_ARRAY);
Method enables vertex coordinate arrays.
ii. Calling GL10glEnableClientState(GL10.GL_COLOR_ARRAY);
Method enables vertex color arrays.
iii. Calling GL10glVertex (int size,int type,int stride,Buffer pointer);
Method sets the position data of the vertex. In this method, the pointer parameter is used to specify the vertex coordinate value, but the three-dimensional array is not used here to specify the value of each vertex X, Y, and Z coordinates. The pointer is still a one-dimensional array with the format of(x1,y1,z1,x2,y2,z2,x3,y3,z3…xN,yN,zN); That is to say, the array will contain 3N values, and each three values specify the type of X, Y, and Z coordinate values of a vertex. If the vertex coordinate value is float type, it is specified asGL10.GL_FLOAT;If the vertex coordinate value is an integer, specify asGL10.GL_FIXED。
iv. Calling GL10glColorPointer(int size,int type,int stride,Buffer pointer)
Method sets the color data of the vertex. In this method, the pointer parameter is used to specify the color value of the vertex. The pointer is still a one-dimensional array, and its format is(r1,g1,b1,a1,x2,y2,z2,a2,x3,y3,z3,a3…xN,yN,zN,aN);That is, the array will contain 4N values, and each 4 values specify the color value of the red, green and blue transparency of a vertex. The first parameter size specifies how many elements specify a vertex position. The size parameter is usually always 4. The type parameter specifies the type of the vertex coordinate value. If the vertex coordinate value is float type, it is specified asGL10.GL_FLOAT;If the vertex coordinate value is an integer, specify asGL10.GL_FIXED。
v. Calling GL10glDrawArrays(int mode,int first,int count)
Method to draw a plane. The first parameter of this method is used to specify the drawing type, the second parameter specifies which vertex to start drawing, and the third parameter specifies the total number of fixed points to be drawn.
vi. After drawing is completed, call GL10'sglFinish()
The method ends drawing; and callsglDisableClientState(int)
Method to disable vertex coordinate data and vertex color data.
After mastering the above steps, use the sample program to draw a few simple graphics.
public class MyRenderer implements Renderer { float[] triangleData = new float []{ 0.1f,0.6f,0.0f,//Up to the vertex -0.3f,0.0f,0.0f,//Left vertex 0.3f,0.1f,0.0f//Right vertex }; int[] triangleColor = new int []{ 65535,0,0,0,//The upper vertex is red 0,65535,0,0,//The left vertex is green 0,0,65535,0//The right vertex is blue }; float[] rectData = new float[]{ 0.4f,0.4f,0.0f,//The upper right vertex 0.4f,-0.4f,0.0f,//The lower right vertex -0.4f,0.4f,0.0f,//The upper left vertex -0.4f,-0.4f,0.0f//The lower left vertex }; int[] rectColor = new int []{ 0,65535,0,0,//The upper right vertex is green 0,0,65535,0,//The lower right vertex is blue 65535,0,0,0,//The upper left vertex is red 65535,65535,0,0//The lower left vertex is yellow }; float[] rectData2 = new float[]{ -0.4f,0.4f,0.0f,//The upper left vertex 0.4f,0.4f,0.0f, 0.4f,-0.4f,0.0f, -0.4f,-0.4f,0.0f rectColorBuffer }; float[] pentacle = new float[]{ 0.4f,0.4f,0.0f, -0.2f,0.3f,0.0f, 0.5f,0.0f,0f, -0.4f,0.0f,0f -0.1f,-0.3f,0f }; FloatBuffer triangleDataBuffer; IntBuffer triangleColorBuffer; FloatBuffer rectDataBuffer; FloatBuffer rectDataBuffer2; FloatBuffer pentacleBuffer; IntBuffer rectDataBuffer; public MyRenderer() { //Package the vertex position data array into a FloatBuffer; triangleDataBuffer = (triangleData); rectDataBuffer =(rectData); rectDataBuffer2 =(rectData2); pentacleBuffer = (pentacle); //Package the vertex color data array into an IntBuffer; triangleColorBuffer=(triangleColor); rectColorBuffer= (rectColor); }; //Close anti-jitter (GL10.GL_DITHER); //Setting system to correct perspective (GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); (0, 0, 0, 0); //Set shadow smoothing mode (GL10.GL_SMOOTH); //Enable in-depth testing (GL10.GL_DEPTH_TEST); //Set the type of depth test (GL10.GL_LEQUAL); } public void onSurfaceChanged(GL10 gl,int width,int height) { //Set the size and position of the 3D window (0,0,width,height); //Set the current matrix mode as projection matrix (GL10.GL_PROJECTION); //Initialize the unit matrix (); //Calculate the width and height ratio of the perspective view window float ratio = (float)width/height; //Call this method to set the space size of the perspective view window (-ratio,ratio,-1,1,1,10); } public void onDrawFrame(GL10 gl) { //Clear screen cache and depth cache (GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT); //Enable vertex coordinate data (GL10.GL_VERTEX_ARRAY); //Enable vertex color data (GL10.GL_COLOR_ARRAY); //Set the current matrix stack as the model stack (GL10.GL_MODELVIEW); //Draw the first figure and reset the current model view matrix (); (-0.32f, 0.35f, -1f); //Set the position data of the vertex (3, GL10.GL_FLOAT, 0, triangleDataBuffer); //Set the color data of the vertex (4, GL10.GL_FIXED, 0, triangleColorBuffer); //Draw a plan shape based on vertex data (GL10.GL_TRIANGLES, 0, 3); //Draw the second figure (); (0.6f, 0.8f, -1.5f); (3, GL10.GL_FLOAT, 0, rectDataBuffer); (4, GL10.GL_FIXED, 0, rectColorBuffer); (GL10.GL_TRIANGLE_STRIP,0,4); //Draw the third graphic (); (-0.4f, -0.5f, -1.5f); (3,GL10.GL_FLOAT, 0, rectDataBuffer2); (GL10.GL_TRIANGLE_STRIP,0,4); // (); (0.4f, -0.5f, -1.5f); //Set to fill with solid color gl.glColor4f(1.0f,0.2f,0.2f,0.0f); (GL10.GL_COLOR_ARRAY); (3, GL10.GL_FLOAT, 0,penTacleBuffer); (GL10.GL_TRIANGLE_STRIP,0,5); //Drawing ends (); (GL10.GL_VERTEX_ARRAY); } }
The above program uses GL10 to draw the key code for drawing the graph: load the vertex position data; load the vertex color data; call GL10glDrawArraysdraw.
Define aGLSurfaceView, and use the Renderer above to draw, the program is as follows:
public void Polygon extends Activity { public void onCreate(Bundle savedInstanceState) { (savedInstanceState); //Create a GLSurfaceView to display the graphics drawn by OpenGL GLSurfaceView glView = new GLSurfaceView(this); //Create the content drawer for GLSurfaceView MyRenderer myRender = new MyRenderer(); //Set the drawer for GLSurfaceView (myRender); setContentView(glView); } }
You may find it strange, why the second and third figures only define the order of 4 coordinate points slightly different, and why are the differences in the figures so big? Should beglDrawArrays
The first parameter of the method specifies the drawing mode.GL10.GL_TRIANGLESIt is to draw a triangle,GL10.GL_TRIANGLE_STRIPIt is to use multiple triangles to draw polygons.
For the second figure, when calledglDrawArrays(int mode,int first,int count )
When specifying the first parameter isGL10.GL_TRIANGLE_STRIPWhen the system starts from the first vertices, a triangle will be drawn for every 3 vertices.
For more information about Android related content, please check out the topic of this site:Summary of Android graphics and image processing skills》、《Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.