OpenGL ES provides two methods to draw a space ry:
Public abstract void gldrawarrays (INT mode, int first, int count) is drawn using vetexbuffer. The order of vertices is specified by the order in vertexbuffer.
Public abstract void gldrawelements (INT mode, int count, int type, buffer indices), you can redefine the vertex sequence, which is specified by indices buffer.
Mode indicates the vertices mode described above.
As mentioned above, vertices are generally defined using arrays and stored using buffer to improve drawing performance. For details, refer to the buffer usage in Android OpenGL ES development.
For example, we define three vertex coordinates and store them in floatbuffer:
Float [] vertexarray = new float [] {
-0.8f,-0.4f * 1.732f, 0.0f,
0.8f,-0.4f * 1.732f, 0.0f,
0.0f, 0.4f * 1.732f, 0.0f,
};
Bytebuffer vBB = bytebuffer. allocatedirect (vertexarray. length * 4 );
VBB. Order (byteorder. nativeorder ());
Floatbuffer vertex = vBB. asfloatbuffer ();
Vertex. Put (vertexarray );
Vertex. Position (0 );
With the vertex definition, you can open the corresponding switch of the OpenGL ES pipeline (pipeline) to pass the vertex parameters to the OpenGL library:
You can enable or disable the vertex switch as follows:
Gl. glableclientstate (gl10.gl _ vertex_array );
Gl. gldisableclientstate (gl10.gl _ vertex_array );
After the vertex switch is turned on, the method to pass the vertex coordinates to the OpenGL pipeline is glvertexpointer:
Public void glvertexpointer (INT size, int type, int stride, buffer pointer)
Size: The Coordinate Dimension of each vertex, which can be 2, 3, or 4.
Type: the data type of the vertex. It can be gl_byte, gl_short, gl_fixed, or gl_float. The default value is gl_float.
Stride: interval (number of bytes) between each adjacent vertex in the array. The default value is 0, indicating that there is no interval between vertex storage.
Pointer: An array that stores vertices.
The color values of common vertices are stored after the corresponding vertex. For example, RGB uses 4 bytes for representation. In this case, adjacent vertices are not stored consecutively. The stride value is 4.
In addition to defining coordinates, the corresponding vertex can also specify color, material, and normal (used for illumination.
The pipeline switches that can be controlled by glenableclientstate and gldisableclientstate can include gl_color_array (color), gl_normal_array (normal), gl_texture_coord_array (material), gl_vertex_array (vertex), and timeline.
The method for passing in the color, vertex, material, and normal is as follows:
Glcolorpointer (INT size, int type, int stride, buffer pointer)
Glvertexpointer (INT size, int type, int stride, buffer pointer)
Gltexcoordpointer (INT size, int type, int stride, buffer pointer)
Glnormalpointer (INT type, int stride, buffer pointer)
If you need to use triangles to construct complex graphs, you can use the gl_triangle_strip or gl_triangle_fan mode, or by defining the vertex sequence:
For example, a square is defined:
Corresponding vertex and buffer definition code:
Private short [] indices = {0, 1, 2, 0, 2, 3 };
// To gain some performance we also put this ones in a byte buffer.
// Short is 2 bytes, therefore we multiply the number if vertices with 2.
Bytebuffer Ibb = bytebuffer. allocatedirect (indices. length * 2 );
Ibb. Order (byteorder. nativeorder ());
Required buffer indexbuffer = Ibb. asw.buffer ();
Indexbuffer. Put (indices );
Indexbuffer. Position (0 );
It is important to define the order of triangle vertices when splicing a surface. It is important to define the order of the vertices of the surface, because the order of vertices defines the face orientation (forward or backward ), in order to obtain the high performance of the painting, the front and back of the face are generally not drawn, but only the front of the face is drawn ". Although the definition of "front" and "back" can be human-friendly, it generally defines a uniform vertex sequence (clockwise or counterclockwise) for all "Front ).
The following code sets the counter-clockwise method as the "front ":
Gl. glfrontface (gl10.gl _ CCW );
Enable ignore "later" Settings:
Gl. glable (gl10.gl _ cull_face );
Specify the code for "Ignore" as follows:
Gl. glcullface (gl10.gl _ Back );