This article illustrates the OpenGL graphics techniques of Android programming. Share to everyone for your reference, specific as follows:
For a long time without OpenGL ES drawing, afraid of their own forgotten, so to review again, by the way the original rational things summed up as follows:
1. Android 3D coordinate system
As shown in figure:
Android's three-dimensional coordinate system:
The coordinate origin is located in the center,
The x-axis extends from left to right with a negative value to the left of the origin and a positive number on the right;
The y-axis extends from bottom to bottom with a negative value and a positive number above the origin.
The z-axis screen is extended to the outside, with negative numbers on the screen and positive numbers outside.
2. Introduction to Development tools (OpenGL and OpenGL ES)
Opengl:open Graphics Libraries, open graphics library interfaces, cross programming languages, and Cross-platform programming interface specifications. Apply to diagram new workstation, personal pc.
A subset of OpenGL Es:opengl for Embedded System,opengl, based on OpenGL, eliminates many of the non-essential features of Glbegin/glend, quadrilateral, and polygon. Suitable for a variety of embedded systems for small devices.
3. Using OpenGL ES to draw the graphics principle
(1), the OpenGL es brush GL10 has two modes when drawing graphics:
①gl10.gl_triangles: Drawing Triangles
②gl10.gl_triangle_strip: Drawing polygons with multiple triangles
That is, OpenGL es can only draw a 3D shape that is composed of triangles.
(2), when drawing 2D graphics, Gl10 the method of drawing the graph gldrawarrays (int mode,int first,int count);
When the first argument is the second pattern above, it represents the system with multiple triangles to draw the polygon, starting at first vertex and drawing a triangle for every three vertices. For example, we first give the point data source contains point (0,1,2,3) four points, our rendering method is Gldrawarrays (gl10.gl_triangle_strip,0,4), on behalf of which we will draw two triangles, respectively, by 0, 1, 23 vertices of the triangle, and 1,2,3 three vertices composed of triangles. The drawing is the following figure:
So, if the location of the source data that we give is changed, then the graph also changes, for example, we are given the point of the data source contains points (0,1,2,3) Four points, the other we are the same as above, but the position of the point has changed, the drawing is as follows:
(3), drawing 3D graphics, drawing 3D graphics and the steps of the 2D graphics are basically similar, the difference is only to draw 3D graphics not only need to define the coordinates of each vertex position, but also to define the 3D graph of which vertices of each triangle.
Like 2D, GL Draws a 3D method: gldrawelements (int mode,int count,int type,buffer indices). Just imagine that when we put the point of drawing 2D graphics out of the same plane, this is not the shape of the 3D graphics, 3D graphics, but also need to give us the drawing surface of the combination of points of the data source, that is, must give a few faces, each face is composed of those points.
4. Using OpenGL ES steps in Android
(1), create Glsurfaceview component, use activity to display Glsurfaceview component
(2) To create a Glsurfaceview.renderer instance for the Glsurfaceview component, the implementation of the Glsurfaceview.renderer class needs to implement the interface three methods.
A, abstract void Ondrawframe (GL10 gl);
b, abstract void onsurfacechanged (GL10 gl,int width,int height);
C, abstract void onsurfacecreated (GL10 gl,eglconfig config);
(3) The Setrenderer () method of the Glsurfaceview component is invoked to make the renderer object, which will complete the drawing of 3D graphics in Glsurfaceview.
5. The difference between the three-dimensional coordinate system and the two-dimensional coordinate system
The coordinate values on a two-dimensional coordinate system usually directly use the number of pixels in the system.
The coordinate value of a three-bit coordinate system depends on the setting of the GLFRUSTUMF () method.
This method glfrustumf (float left,float right,float bottom,float top,float znear,float)
This method is used to set the space size of the perspective projection, the first two parameters are used to set the minimum and maximum coordinate values on the x-axis, and the middle two parameters are used to set the minimum and maximum coordinate values on the y-axis, and the following two parameters are used to set the minimum and maximum coordinate values on the z-axis.
I hope this article will help you with the Android program.