This article describes the method of OpenGL in Android development to draw 2D graphics. Share it for your reference, as follows:
Android provides GLSurviceView assembly for OpenGL ES support, which is used to display 3D graphics. GLSurviceView itself does not provide the function of drawing 3 graphics, but rather completes the drawing of 3D graphics in SurviceView.
In summary, using OpenGL ES in android requires 3 steps.
1. Create a GLSurviceView component and use Activity to display the GLSurfaceView component.
2. Create a GLSurviceView componentExamples: When implementing a class, three methods in this interface need to be implemented.
(1) abstract void onDrawFrame(GL 10 gl): The Renerer object calls this method to draw the current frame of the GLSurviceView.
(2) abstract void onSurfaceChanged(GL 10 gl,int width,int height): Callback this method when the size of the GLSurfaceView changes.
(3) abstract void onDrawFrame(GL 10 gl,EGLConfig config): Callback this method when the GLSurfaceView is created.
3. Call the GLSurfaceView componentsetRebderer()
Method specifies the Renderer object, which will complete the drawing of the 3D image in the GLSurfaceView.
From the above introduction, it is not difficult to see that the difficulty in drawing 3D images is actually not how to use the GLSurface component, but how to implement the Renderer class. There are 3 methods that need to be implemented when implementing the Render class. These three methods have a GL parameter, which represents the "drawing brush" of GLOpenES. We can imagine it as Graphics in Swing 2D drawing, or as Canvas component in Android 2D drawing. When we want renderer to draw 3D graphics, we actually call the GL10 method to draw.
When the Survice View is created, the system will call back the onSurfaceCreated() method of the Renderer object, which will perform some initializations without any changes to OpenGL ES, such as the following initialization code:
public void OnSurfaceCreated(GL10 gl,EGLConfig config) { //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); }
GL10 is the drawing interface of OpenGL ES. Although you see a GL10 here, it is actually an instance of GL11. Readers can determine whether it is an instance of GL11 through (gl instance of GL11).
Some initialization methods of GL10 are used in the above method. The explanation of these methods is as follows:
(1) GlDisable(int cap)
: This method is used to disable some aspect of OpenGL ES features. 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 for clearing the screen, and sets the red, green, blue and transparency values for the four parameters: 0 is the minimum value and 1 is the maximum value. For example, setting (0,0,0,0); is to use a black screen to clear.
(4)glShadeModel(int mode)
: This method is used to set the shadow mode of OpenGL ES. Set to Shadow Smooth Mode here.
(5)glEnable(int cap)
: This method is opposite to the glDisable(int cap) method and is used to enable some aspects of OpenGL ES. This is used to start the depth test of OpenGL ES, which is to let OpenGL ES be responsible for tracking the depth of each object on the Z axis, so that the objects behind can avoid the objects blocking the objects in front.
When the size of the SurviceView assembly changes, the system will call back the onSurfaceChanged() method of the Renderer object, so this method is usually used to initialize a 3D scene. For example, the initialization code is as follows:
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); }
The above method uses some initialization methods of GL10. The explanation of these methods is as follows:
1、 glViewport(int x,int y,int width,int height)
: Set 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)
: Set the matrix model of the view. Usually, two constant values of GL10.GL_PROJECTION and GL10.GL_MODELVIEW can be accepted.
When calledglMatrixMode(GL 10.GL_PROJECTION);
After the code, specify that the screen is set to perspective, which means that the farther away things look smaller; when calledglMatrixMode(GL 10. GL_MODELVIEW);
After the code, the current matrix mode is set to the mode view matrix, which means that any new transformation will affect all objects in the matrix.
3、glLoadIdentity()
:Equivalent to reset() method, used to initialize the unit 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 coordinate value and maximum coordinate value 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 coordinate value and maximum coordinate value on the Z axis.
For example, we call the following code:
(-0.8,0.8,-1,1,1,10);
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), and (-0.8, -1), and this matrix will fill the entire window.
All 3D graphics on GLSurfaceView are made fromRenderer's onDrawFrame(GL10 gl)
When drawing the method, all 3D graphics must be drawn when rewriting the method. The method usually starts with the following form:
public void onDrawFrame(GL10 gl) { //Clear screen cache and depth cache (GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT); ... }
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.