I am not very busy recently. I plan to start learning OpenGL this week. There are already many books and materials on OpenGL on the Internet. The reason for writing is to develop a good learning habit and give a summary of your learning process.
Basic Concepts
OpenGL defines a cross-programming language and cross-platform programming interface specification. It is a high performance 3D graphics standard. OpenGL is a professional graphical program interface and is a powerful underlying graphics library with convenient calling. OpenGL ES is a lightweight 2D/3D graphics library designed for embedded and mobile devices. Is a subset of OpenGL.
The minimum unit of a 3D image is point or vertex ). They represent a point in a 3D space and are used to build more complex objects. A polygon is composed of vertices, and an object is composed of multiple polygon. Although OpenGL usually supports polygon, OpenGL ES only supports the triagon, so even if we want to draw a square, we need to split it into two triangles.
Let's talk about the subcoordinate system.
By default, the Axis origin is centered on the screen. X on the left side of the origin is a negative value, y on the right side is a positive value, y on the top of the origin is a negative value, Z on the vertical screen is a positive value, and a negative value in the direction.
Coordinates:
After learning about the coordinate axis, let's see how to represent a point in the coordinate system. A group of floating point numbers are usually used to represent a point. For example, four vertices of a square can be expressed:
Float vertices [] = {-1.0f, 1.0f, 0.0f, // upper left-1.0f,-1.0f, 0.0f, // 1.0f at lower left,-1.0f, 0.0f, // 1.0f at lower right, 1.0f, 0.0f, // top right };
To improve performance, you usually need to store floating point groups in a byte buffer. Therefore, the following operations are available:
Bytebuffer vBB = bytebuffer. allocatedirect (vertices. length * 4); // apply for memory vBB. order (byteorder. nativeorder (); // you can specify byteorder. nativeorder () is to obtain the local byte order floatbuffer vertexbuffer = vBB. asfloatbuffer (); // converts it to a float-type vertexbuffer. put (vertices); // Add vertexbuffer data. position (0); // you can specify the starting position of the buffer.
Byteorder. nativeorder () is used to obtain the byte sequence of the local machine. OpenGL ES has functions used to operate graphics rendering pipelines. By default, these functions are disabled. To enable and disable these functions, you can use glenableclientstate and gldisableclientstate.
// Specify to enable the fixed-point array GL. glableclientstate (gl10.gl _ vertex_array); // specifies the type and byte buffer of the enabled array. The type is gl_float GL. glvertexpointer (3, gl10.gl _ float, 0, vertexbuffer); // disable the vertex array GL when no longer needed. gldisableclientstate (gl10.gl _ vertex_array );
Edge
An edge is a line connecting two points and an edge of a polygon.
Polygon
A polygon is a single closed ring composed of edges. The polygon in OpenGL ES must be a convex polygon, that is, any two points inside the polygon. If the line segments connecting the two points are in the changeable interior, the polygon is a convex polygon. When drawing a polygon, you must specify the rendering direction, which can be clockwise or counterclockwise. The direction determines the orientation of the polygon, that is, the front and back. Avoiding rendering of blocked parts can effectively improve program performance. The glfrontface function defines the direction of the rendered vertex.
/Set the direction of CCW to "Front", CCW to counterclockwise, and glfrontface (gl_ccw) counterclockwise; // set the direction of CW to "Front", CW to clockwise, and clockwise to glfrontface (gl_cw );
Rendering
With the above concepts, we need to do the most important work-rendering now. Rendering is to convert the elements specified by the object coordinates into images in the frame buffer. Images are closely related to vertex coordinates. This relationship is given in the drawing mode. Commonly used draw modes include gl_points, gl_line_strip, gl_line_loop, gl_lines, gl_triangles, gl_triangle_strip, and gl_triangle_fan. The following sections describe:
Gl_points: processes each vertex as a vertex. vertex N defines vertex N and draws n vertices in total.
Gl_lines: each vertex is used as an independent line segment. A total of N line segments are defined between the 2n-1 and 2n vertices, and n/2 lines are drawn ., If n is an odd number, the last vertex is ignored.
Gl_line_strip: Draw a group of lines from the first vertex to the last vertex, which are connected in turn. The N and n + 1 vertices define the line N, a total of N-1 line segments.
Gl_triangles: each three vertices are used as an independent triangle. Vertex 3N-2, 3n-1, and 3n define the nth triangle and draw n/3 triangles in total.
Gl_triangle_strip: Draw a group of connected triangles. The Nth triangle is defined for odd vertex N, vertex N, N + 1, and n + 2. For even N, vertex N + 1, N and n + 2 define the nth triangle and draw a total of N-2 triangles.
Gl_triangle_fan: draws a group of connected triangles. A triangle is determined by the first vertex and Its given vertex. Vertex 1, n + 1 and N + 2 define the nth triangle and draw a total of N-2 triangles.
Draw function:
Void gldrawarrays (INT mode, int first, int count) void gldrawelements (INT mode, int count, int type, buffer indices) gldrawarrays creates a sequence of geometric elements, use the array elements starting from first to ending with first + count-1 in each array. mode is the drawing mode. Gldrawelements defines a sequence of elements using the Count element. The type is the data type in the indices array, and the mode is the drawing mode. The indices array stores the index value of the vertex.
Drawing steps:
1. Define the vertex and store the conversion in the byte buffer;
2. We use vertex arrays to draw graphs, and opengles disables this switch by default, so we need to enable it. Gl. glableclientstate (gl10.Gl_vertex_array);
3. Set the painting color. Set red as follows
Gl. glableclientstate (gl10.Gl_vertex_array);
4. Because we use vertex arrays, We must notify the OpenGL vertex array where it is located. Functions are required:
GL. glvertexpointer (3, // dimension of the coordinates of each vertex. Here it is 3xyz gl10.gl _ fixed, // The type of the coordinate value of the vertex is gl_fixed 0, // The Offset Value of the data in the array mvertexbuffer // The vertex coordinate data array );
5. start plotting
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3);
OK! First, we will introduce the Drawing Process of OpenGL through an example in the next article.