Overview
Cocos2d-x underlying implementation relies on OpenGL, This is a graphics library, can operate the graphics card, therefore, in the processing of more complex games, the consumption of a large game, the processing efficiency will be very high. The canvas tool provided by Android cannot provide this efficiency because it does not directly operate the video card. Of course, some of the drawing tools provided by android can be used for simple game development, such as playing chess, greedy snakes, and even watching.
Opengl1.0 is a fixed pipeline with built-in functions to set all rendering statuses: Lights, vertices, colors, and cameras.
Opengl2.0 is intended for programmable pipelines, and you need to manually set the shader ). Shader includes vertexshader and textureshader, which can process vertex and texture values. In this way, the coordinates and colors of objects can be modified before the video card is to be displayed, so that many special effects can be easily implemented: such as ripple. You can use the shader to process the lighting and shadow details, but 1.0 cannot process these details.
Cocos2d-x uses OpenGL technology, probably as described below, because it is a 2D graphics engine, so, less use of 3D OpenGL API, therefore, this article roughly covers the content of OpenGL involved in cocos2d-x.
Cocos2d-x also uses the DirectX library, in fact and OpenGL rendering process, the principle is the same, but the API naming method is different. Therefore, the design principles can be referenced from OpenGL.
Procedure
1. Prepare a view, such as mysurfaceview in Android.
2. Create OpenGL Context
3. Create a render Buffer
Is an OpenGL object used to store rendered images. The size of the screen area is used as a constraint.
4. Create a frame buffer
Is an OpenGL object that stores frames. It contains the render buffer. depth buffer, stencel buffer, and accumulation buffer.
Depth buffer: When 3D rendering is affected by the Z axis, there will be occlusion. You need to save the depth information for depth comparison to ensure that the occlusion relationship is correct.
The rendering process requires attention to one of the features of the rendering process: first pass in the pointer and then pass in the value. Therefore, we can see multiple operations on the vertex or a variable. The pointer I am referring to may be glxxxhandle, glxxxlocation, and glxxxpointer. Then, set the previously prepared values: glbufferdata and gluniform4fv. The meanings of these variables are shown below. Compile the shader program, which includes vertex. SH and frag. Sh. Directly executed by the graphics card GPU, rather than by the CPU. The GPU graphics processing capability is higher than the CPU, so openg2.0 is much more efficient than 1.0. The shader program can be saved in any document format or even in strings, because we only need to get the program content, regardless of the format. In the vertex coloring tool, 1. Four variables are used to store vertex positions. This is because W is a variable more than X, Y, and Z in matrix calculation, which is "convenient for matrix calculation ". 2. varing is used to declare the color. The function of this keyword is to achieve "gradient", that is, although we only specify the color of four vertices, however, the area between the vertices is Gradient based on the color difference between the vertices. Varing lowp vec4 destinationcolor; // represents the vector for low-precision computation. Changing lowp to the highp keyword indicates high-precision computation. Generally, low-precision computation can be performed on mobile phones.
The two paintors are compiled at runtime and are compiled through the OpenGL library. Because OpenGL is cross-platform and suitable for runtime compilation, the compiled code can be compatible with different platforms.
OpenGL system function glcreateshaderglshadersource // obtain the shader source code glcompileshader // compile shaderglgetshaderiv/glgetshaderinfolog // check whether the compilation is successful, get the error message glcreateprogram // create programglattachshader // bind two shader to programgllinkprogram // link the two shader into a completed programglgetprogramiv, glgetprograminfolog // check whether the link is successful. gluseprogram // Let OpenGL execute programglgetattriblocation // get the input variable in the shader. Later, the passed value will be calculated in the shader to calculate the glenablevertexattribarray // enable the data, glge Tattriblocation data will not be passed into the program vertex data (preparation phase) glgenbuffers // create the vertex buffer object glbindbuffer // bind the buffer, tell OpenGL that this refers to gl_array_buffer Array Buffer glbufferdata // bind data to the buffer rendering graph (coloring phase) glvertexattribpointer // provide the input parameter value to Shader (the above is only to prepare vertex data, you have not entered the coloring tool yet.) gldrawelement // draws a graph. vertex shader is called for each vertex, and frag shader is called for each "pixel" to complete rendering. Projection Matrix uniform mat4 projection; // mat4 is the matrix variable of four variables. The uniform keyword affects the position variables of all vertices. Gl_position = projection * position; glgetuniformlocation; // gets the projection pointer in vertex shader (the following function will pass in the value for this pointer) gluniformmatrix4fv // input the value model view matrix of the variable to the shader. unifrom mat4 modelview; gl_position = projection * modelview * position; each object has a model view matrix, used to control the rotation, translation, and scaling of a model. Texture data attribute vec2 texcoordin; varying vec2 numeric; Numeric = texcoordin; varying lowp vec2 numeric; unirom sampler2d texture; // read an image gl_fragcolor = desitinationcolor * texture2d (texture, numeric ); // call the system function texture2d to display the texuture image (two-dimensional array) based on the texture coordinate texcoordout ). // Destinationcolor is the original color. It can be used for multiplication without multiplication. It is only a hybrid operation. Glgentexutures; // creates a texture (pointer) glbindtexture; // After the texture is bound, the following code uses the texture until the glbindtexturegltexparameteri binding is called again; // sets the texture filtering (processing method when the texture is smaller or larger than the object, such as min MAP) glteximage2d; // sets the texture data (read a two-digit array, that is, the image data) glactivetexture; // activates the texture data (drawing) glbindtexture; // binds the texture glunirom1i again; // transmits the texture "data" to the frag shader so far, the image can be successfully pasted to the object. The OpenGL text display usually draws the text to the image through the drawstring under each platform, and then attaches the image texture to the square. Android calls canvas, Windows calls GDI, and cocos2d calls XX 2D API.