Android OpenGL es Drawing Tutorial Three: Drawing graphics

Source: Internet
Author: User

after defining the shapes that will be drawn by OpenGL, you certainly want to draw them. Drawing graphics using OpenGL ES 2.0 may require more code than you think, as the API provides a large number of graphical rendering pipeline control interfaces.
This chapter describes how to use the OpenGL ES 2.0 API to draw the shapes defined in the previous chapter
1. Initializing shapes
Before you do any of the drawing operations, you must initialize and load the shapes that you plan to draw. You should initialize them in the onsurfacecreated () method in render to improve memory and execution efficiency, unless the structure in which the shapes are located (the original coordinates) changes during execution.
public void onsurfacecreated (GL10 unused, EGLConfig config) {
...

Initialize a triangle
Mtriangle = new Triangle ();
Initialize a square
Msquare = new Square ();
}


2. Drawing shapes
Drawing a defined shape using OpenGL ES 2.0 requires a lot of code, because you have to give a lot of detail to the graphics rendering pipeline, specifically, you need to define the following details:
Vertex Shader-opengl ES Graphics code for rendering the vertices of a shape.
Vertex shader-OpenGL ES graphics code for rendering shape vertices
Fragment Shader-opengl ES code for rendering the face of a shape with colors or textures.
Slice shader-OpenGL ES graphic code that renders a shape's surface using color or texture
Program-an OpenGL ES object that contains the shaders you want to use for drawing one or more shapes.
Program -the OpenGL ES object that contains the shader used to draw the shape
you need at least one vertex shader to draw the shape, and a slice shader to color the shape. These shaders must be compiled and added to the OpenGL ES program. Here is an example of how to draw a graph by defining a shader:
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;" +
    "}";
The shader uses OpenGL Shading Language (GLSL) code, which must be compiled before it is used in the OpenGL ES environment, in order to compile the code, create a method in your renderer class:
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;
}


In order to draw graphics, you must compile the shader code, add them to the OpenGL ES programming object, link the program, perform these operations in the construction method of the drawing object, and do so only once.
Note: Compiling OpenGL shaders and linker consumption from CPU processing cycles and times is relatively expensive, so you should avoid executing more than once. If you do not know the content of the shader at run time, you should only compile it once when the shader is created, and then cache it for use.
Public class 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
}


At this point, you are ready to perform the actual drawing commands, and using OpenGL ES drawing requires specifying some parameters to tell the rendering pipeline what to draw and how to draw. Because drawing options can be differentiated by shape, it's a good idea to have your shape class contain drawing logic.
Create the Draw () method to draw, the following code sets the position and color values of the vertex shader and the slice shader, and then executes the drawing method.
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);
}


When these codes are present, the draw method only needs to be lowered in the renderer Ondrawframe () method when drawing the object. Here is the effect of the program execution:



This code example also has some problems. First of all, it won't impress your friends, second, the shape will change when the screen orientation changes, a bit squashed. The reason the shape is distorted is that the vertex of the object is not corrected by the scale of the area shown on the screen Glsurfaceview, and you can fix the problem by using the projection and camera view described in the next section.
Finally, the triangle is fixed and a bit dull. In the following lesson, you can learn to make this shape rotate so that the OpenGL ES graphics pipeline is more interesting.

Android OpenGL es Drawing Tutorial Three: Drawing graphics

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.