OpenGL is Open Graphics Library (open graphics library interface), mainly used for three-dimensional graphics programming.
OpenGL ES: a subset of OpenGL, embedded open graphics library interface. OpenGL ES provides the GLSurfaceView component.
GLSurfaceView is used to display 3D images. It does not provide the function of drawing 3D graphics. The drawing function is completed by GLSurfaceView.Renderer.
The steps to use OpenGL ES are as follows:
1. Create a GLSurfaceView component (you can also inherit the component), and use Activity to display the GLSurfaceView component;
p>
GLSurfaceView glSurfaceView=new GLSurfaceView(this);
2. Create a GLSurfaceView.Renderer instance for GLSurfaceView and implement it Three methods of GLSurfaceView.Renderer interface:
Among them, GL10 represents OpenGL drawing brush
MyRenderer implements Renderer{//onSurfaceCreated method is mainly used to perform some initialization operations @Override public void onSurfaceCreated(GL10 gl, EGLConfig config) {gl.glDisable(GL10.GL_DITHER);// gl.glDisable is used to disable OpenGL A certain aspect of the feature, which means turning off anti-jitter, can improve performance gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);//This method is used for correction, here is used to set the perspective correction gl.glClearColor(0 , 0, 0, 0);//Set the color used by OpenGL to clear the screen. The four parameters respectively represent red, green, blue and transparency values, the range is 0-1, here it means black gl.glEnable(GL10.GL_DEPTH_TEST) ;//Enable certain aspects of performance, here is to start the depth test, which is responsible for tracking the depth of each object on the Z axis, and avoiding objects behind obstructing the objects in front gl.glDepthFunc(GL 10.GL_LEQUAL);//Set the type of depth test, here is if the input depth value is less than or equal to the reference value, pass gl.glShadeModel(GL10.GL_SMOOTH);//Set the shadow mode to smooth mode} //When Callback when the SurfaceView size changes, usually used to initialize the 3D scene @Override public void onSurfaceChanged(GL10 gl, int width, int height) {gl.glViewport(0, 0, width, height);//Set the position and size of the 3D window gl.glMatrixMode(GL10.GL_PROJECTION);//Set the matrix mode to the projection matrix, which means that the farther things look smaller, GL10.GL_MODELVIEW: model view matrix: any new transformation will affect all objects in the matrix float ratio=(float)width/height; gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);//Set the space size of the perspective projection, the first two parameters are used to set the minimum value of the X axis And the maximum value, the middle two parameters are used to set the minimum and maximum values of the y-axis, and the last two parameters are used to set the minimum and maximum values of the Z-axis} //Used to draw 3D graphics @Override public void onDrawFrame(GL10 gl) {//Clear the screen buffer and depth buffer (usually must be set) gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//Enable the vertex coordinate array gl.glEnableClientState(GL10. GL_COLOR_ARRAY);//Enable the vertex color array gl.glMatrixMode(GL10.GL_MODELVIEW); Set the matrix mode to the model view matrix gl.glLoadIdentity();//Equivalent to the reset() method, used to initialize the identity matrix gl.glTranslatef(- 0.32f, 0 .35f, -1.1f);//Mobile Graphics Center
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleFloatBuffer);//Settings The position data of the vertex, the first parameter specifies how many elements determine a vertex, usually 3; the second parameter specifies the type of the vertex coordinate value, triangleFloatBuffer is a one-dimensional array, such as (x1,y1,z1,x2, y2,z2,...), used to specify vertex coordinates
gl.glColorPointer(4, GL10.GL_FIXED, 0, triangleColorBuffer);//Set the color data of the verticesgl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);//Draw plane graphics according to the vertices, the first parameter indicates the type of graphics drawn, GL10.GL_TRIANGLES: draw simple triangles, GL10.GL_TRIANG LE_STRIP: give The output vertex data draws triangles to form a plane figure. OpenGL can only draw triangles to form 3D graphics; the second parameter specifies which vertex to start from, and the third parameter indicates the number of vertices gl.glFinish();//Draw end gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); }} pre>Note: When inputting a vertex array, the order of each vertex in the array is different, which may cause the drawn graphics to be different
3, use the setRenderer method as GLSurfaceView adds Renderer.