How to use OpenGL ES 2.0 APIs to draw shapes

Source: Internet
Author: User

Link: http://docs.eoeandroid.com/training/graphics/opengl/draw.html

After defining the shape to be drawn with OpenGL, you may want to draw them on the screen. Drawing these shapes based on OpenGL ES 2.0 requires a little more code than you think, because the 2.0 API provides a lot of control over the image rendering pipeline. This section describes how to use the OpenGL ES 2.0 API to draw the shape you have defined in the previous lesson.

Initial shape

Before plotting, you must initialize and load the shape you plan to draw. The onSurfaceCreated () method in the Renderer for inner memory and processing rate initializes the shape unless the original coordinate of the Shape Structure Changes During program execution.

 

12345678
public void onSurfachttp://wiki.eoeandroid.com/Drawing_ShapeseCreated(GL10 unused, EGLConfig config) {    ...    // initialize a triangle    mTriangle = new Triangle();    // initialize a square    mSquare = new Square();    }

 

Draw shape

Drawing shapes using OpenGL ES 2.0 requires a lot of code because you need to provide a lot of image Renderer pipeline details. Specifically, you need to define: * Vertex Shader)-code for rendering shape vertices in the OpenGL ES image * Fragment Shader) -OpenGL ES code for rendering shape surface color and texture * Program) -contains the OpenGL ES object that you want to use to draw the shape of the Shadow. You need at least one vertex shadow to draw the image, and one part shadow to color the image. These pasters must be defined and added to an OpenGL ES program, which will be used to draw shapes. The following is an example of a basic definition coloring tool:

 

 1 2 3 4 5 6 7 8 9101112
private final String vertexShaderCode =    "attribute vec4 vPosition;" +    "void main() {" +    "  gl_Position = vPosition;" +    "}";    private final String fragmentShaderCode =    "precision mediump float;" +    "uniform vec4 vColor;" +    "void main() {" +    "  gl_FragColor = vColor;" +    "}";

 

Before being used by the OpenGL ES environment, the shader that contains the OpenGL coloring language GLSL must be defined first. You can create a practical method in the rendering class to define these pasters:

 

 1 2 3 4 5 6 7 8 9101112
public static int loadShader(int type, String shaderCode){    // create a vertex shader type (GLES20.GL_VERTEX_SHADER)    // or a fragment shader type (GLES20.GL_FRAGMENT_SHADER)    int shader = GLES20.glCreateShader(type);    // add the source code to the shader and compile it    GLES20.glShaderSource(shader, shaderCode);    GLES20.glCompileShader(shader);    return shader;    }

 

To draw shapes, you must write the code of the shader and add them to the OpenGL ES program object to connect with the program. You can do this in the constructor of the Drawing Object so that it will run only once.

Note: defining the OpenGL ES shader and connecting the program requires a lot of CPU cycles and processing time, so you should avoid repeating this action. If you want to get the content of the coloring er during execution, you can create the Code only once and store it for later use.

 

 1 2 3 4 5 6 7 8 91011
public Triangle() {    ...    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);    mProgram = GLES20.glCreateProgram();             // create empty OpenGL ES Program    GLES20.glAttachShader(mProgram, vertexShader);   // add the vertex shader to program    GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program    GLES20.glLinkProgram(mProgram);                  // creates OpenGL ES program executables    }

 

Now, you can call the drawing method. When using OpenGL ES for plotting, You need to specify several parameters to tell the Renderer what and how to draw the pipeline. Since the shape will affect the drawing, the best way is to add their own logic to the shape class. You can create a draw () method to draw a shape. In the following example, the position of the Shape vertex shader and the color value of the fragment shader are set, and then the function method of drawing is executed.

 

 1 2 3 4 5 6 7 8 9101112131415161718192021222324252627
public void draw() {    // Add program to OpenGL ES environment    GLES20.glUseProgram(mProgram);    // get handle to vertex shader's vPosition member    mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");    // Enable a handle to the triangle vertices    GLES20.glEnableVertexAttribArray(mPositionHandle);    // Prepare the triangle coordinate data    GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,                                 GLES20.GL_FLOAT, false,                                 vertexStride, vertexBuffer);    // get handle to fragment shader's vColor member    mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");    // Set color for drawing the triangle    GLES20.glUniform4fv(mColorHandle, 1, color, 0);    // Draw the triangle    GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);    // Disable vertex array    GLES20.glDisableVertexAttribArray(mPositionHandle);    }

 

As long as you have complete code, you only need to call the draw () method in the onDrawFrame () method of the Renderer to draw an image. When the application is running, the following results should be obtained:

Figure 1. Triangle plot not used for projection and photography views

The above code still has some problems. First, it will not affect your friends very much. Second, when you change the screen direction of the mobile phone device, the triangle will be squashed and changed, this is because the fixed point coordinates of the drawn object are not set according to the ratio displayed on the GLSurfaceView screen. The projection and photography views can be used in the next lesson to solve this problem. Finally, the triangle does not move, it's boring. In Adding_Motion, you can rotate shapes and get in touch with more interesting usage of OpenGL ES image pipelines.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.