Analysis of OpenGL drawing 2D graphics developed by Android

This paper describes the method of drawing 2D graphics with opengl developed by Android. Share with you for your reference, as follows:

Android provides OpenGL es support with the glsurviveview component, which is used to display 3D graphics. Glsurfaceview itself does not provide the function of drawing 3 graphics, but glsurfaceview.renderer completes the drawing of 3D graphics in surviveview.

To sum up, using OpenGL es in Android requires three steps.

1. Create glsurfaceview component and use activity to display glsurfaceview component.

2. Create a glsurviveview.renderer instance for the glsurviveview component. When implementing the glsurviveview.renderer class, you need to implement the three methods in the interface.

(1) Abstract void ondrawframe (GL 10 GL): the rener object calls this method to draw the current frame of glsurviveview.

(2) Abstract void onsurfacechanged (GL 10 GL, int width, int height): this method is called back when the size of glsurfaceview changes.

(3) Abstract void ondrawframe (GL 10 GL, eglconfig config): call back this method when glsurfaceview is created.

3. Call the setrebderer() method of glsurfaceview component to specify the renderer object, which will complete the rendering of 3D images in glsurfaceview.

It is not difficult to see from the above introduction that the difficulty in drawing 3D images is not how to use glsurface components, but how to implement the renderer class. Three methods are required to implement the render class. These three methods have a GL parameter, which represents the "drawing brush" of glopenes. We can think of it as graphics in swing 2D drawing or canvas component in Android 2D drawing - when we want renderer to draw 3D graphics, we actually call the method of gl10 to draw.

When the survival view is created, the system will call back the onsurfacecreated() method of the renderer object, which can perform some initialization of OpenGL es without any change, such as the following initialization code:

Gl10 is the drawing interface of OpenGL es. Although we see a gl10 here, it is actually an instance of gl11. Readers can judge whether it is an instance of gl11 interface through (GL instanceof gl11).

Some initialization methods of gl10 are used in the above methods. The descriptions of these methods are as follows:

(1) Gldisable (int cap): this method is used to disable a feature of an aspect of OpenGL es. The first line of code in this method is used to turn off anti jitter, which can improve performance. (2) Glhint (int target, int mode): this method is used to correct some aspects of OpenGL es. (3) Clearcolor (float red, float green, float blue, float alpha): this method sets the color used to clear the screen. The four parameters set the values of red, green, blue and transparency respectively: 0 is the minimum value and 1 is the maximum value. For example, set gl.glclearcolor (0,0); It's a black clear screen. (4) Glshademodel (int mode): this method is used to set the shadow mode of OpenGL es. Set shadow smoothing mode here. (5) GlEnable (int cap): this method is opposite to the gldisable (int cap) method. It is used to enable some features of OpenGL es. Here, it is used to start the depth test of OpenGL es, that is, OpenGL es is responsible for tracking the depth of each object on the Z axis, so as to avoid the rear object from blocking the front object.

When the size of the surviveview component changes, the system will call back the onsurfacechanged () method of the renderer object, so this method is usually used to initialize the 3D scene. For example, the following initialization code:

The above methods use some initialization methods of gl10. The descriptions of these methods are as follows:

1. Glviewport (int x, int y, int height): sets the position and size of the 3D window. The first two parameters specify the position of the window, and the last two parameters specify the width and height of the window.

2. GlMatrixMode (int mode): sets the matrix model of the view. Gl10.gl is generally acceptable_ PROJECTION、GL10.GL_ Modelview two constant values.

When calling glMatrixMode (GL 10. Gl_project); After the code, specify to set the screen as perspective, which means that the farther away things look smaller; When calling glMatrixMode (GL 10. Gl_modelview); After the code, set the current matrix mode as the mode view matrix, which means that any new transformation will affect all objects in the matrix.

3. Glloadidentity (): equivalent to the reset () method, used to initialize the identity matrix.

4. Glfrustumf (float left, float right, float bottom, float top, float znear, float zfar): used to set the space size of perspective projection. The first two parameters are used to set the minimum and maximum coordinate values on the X axis; The middle two parameters are used to set the minimum coordinate value and maximum coordinate value on the Y axis; The last two parameters are used to set the minimum and maximum coordinate values on the Z axis.

For example, we call the following code:

This means that if there is a two-dimensional rectangle, the coordinates of its four vertices are: (- 0.8, 1), (0.8,1), (0.8, - 1), (- 0.8, - 1), and the matrix will occupy the whole window.

All 3D graphics on glsurfaceview are drawn by the renderer's ondrawframe (gl10 GL) method. When rewriting this method, all 3D graphics must be drawn. This method usually starts in the following form:

More readers interested in Android related content can view the special topics of this site: summary of Android graphics and image processing skills, introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solving methods, summary of Android basic component usage, summary of Android view skills, summary of Android layout skills, and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>