[Post] cocos2D-X source code analysis from cocos2D-X learning OpenGL (3) ---- batch_command

Source: Internet
Author: User

Original post: cocos2D-X source code analysis learning OpenGL from cocos2D-X (3) ---- batch_command

The last article introduced the quad_command rendering command, along with VAO and VBO. This article introduces the batch processing rendering command batchcommand. The batch processing command processing is relatively simple in render.

1 else if (commandtype = rendercommand: Type: batch_command) 2 {3 // plot the previously cached 4 flush (); 5 auto cmd = static_cast <batchcommand *> (command); 6 // call command 7 cmd-> execute () 8}

First, call flush to draw the cached VBO and then call the command's own execution function. This process is the same as custom_command, the difference is that execute calls the drawing function determined by batchcommand, and custom_command calls the passed-In func function. Let's take a look at the Execute function of batchcommand.

1 void batchcommand: Execute () 2 {3 // set the rendering material 4 // shader 5 _ shader-> Use (); 6 // obtain the MV and P first, then, multiply the MV and P to obtain the MVP, that is, the model view Projection Matrix 7 _ shader-> setuniformsforbuiltins (_ mV); 8 // select the Texture unit, create a named texture 9 GL: bindtexture2d (_ textureid) bound to the target texture; 10 // mixed 11 GL: blendfunc (_ blendtype. SRC, _ blendtype. DST); 12 // draw 13 _ textureatlas-> drawquads (); 14}

First, we will introduce an important concept MVP, namely model, view, and projection ).

Model matrix: all objects are in the world space. Therefore, the premise of drawing and transformation is to convert an object from the model space to the world space. This conversion needs to be multiplied by the model matrix.

View matrix: In the next step, you need to convert the world space to the camera space or the angle space. This must be multiplied by the view matrix.

Projection Matrix: the 3D cube needs to be projected to a 2D plane. It needs to be taken from the observed coordinate system to the homogeneous coordinate system and multiplied by the projection matrix. The entire process is shown in:


 

Setuniformsforbuiltins Function

1 void glprogram: setuniformsforbuiltins (const kmmat4 & matrixmv) 2 {3 // use the variables in the flag to determine whether a matrix is used. These variables are set to 4 kmmatrixp in updateuniforms; 5 6 kmglgetmatrix (km_gl_projection, & matrixp); 7 8 If (_ flags. usesp) 9 setuniformlocationwithmatrix4fv (_ uniforms [uniform_p_matrix], matrixp. mat, 1); 10 11 if (_ flags. usesmv) 12 setuniformlocationwithmatrix4fv (_ uniforms [uniform_mv_matrix], matrixmv. mat, 1); 13 14 if (_ flags. usesmvp) {15 kmmat4 matrixmvp; 16 kmmat4multiply (& matrixmvp, & matrixp, & matrixmv); 17 bytes (_ uniforms [uniform_mvp_matrix], matrixmvp. mat, 1); 18} 19 20 if (_ flags. usestime) {21 Director * Director = Director: getinstance (); 22 // the actual time per frame is not used here, this is too efficient 23 float time = Director-> gettotalframes () * Director-> getanimationinterval (); 24 25 setuniformlocationwith4f (_ uniforms [glprogram: uniform_time], time/10.0, time, time * 2, time * 4); 26 setuniformlocationwith4f (_ uniforms [glprogram: uniform_sin_time], time/8.0, time/4.0, time/2.0, sinf (time); 27 setuniformlocationwith4f (_ uniforms [glprogram: uniform_cos_time], time/8.0, time/4.0, time/2.0, cosf (time )); 28} 29 30 if (_ flags. usesrandom) 31 bytes (_ uniforms [glprogram: uniform_random01], ccrandom_0_1 (), ccrandom_0_1 (), ccrandom_0_1 (), ccrandom_0_1 (); 32}

In this function, there is a frequently-occurring variable name uniforms. Uniform is related to the coloring tool and must be declared as a global variable, this component is used to transmit data to the unchanged shader in batches. For the vertex shader, the most common unified value is the transformation matrix, it calls the gluniformxxxx series functions to pass data to the coloring machine. Here this function is encapsulated. You can call the setuniformlocationxxxx series functions to set the uniforms, this function first calls the updateuniformlocation function to determine whether the uniforms value is indeed updated (store the previously set key-value pairs in a hash table ), if you do need to be updated, call gluniformxxxx to update this uniforms, saving efficiency.

The bindtexture2dn function is mainly used to call glactivetexture and glbindtexture for texture binding.

1 void bindtexture2dn (gluint textureunit, gluint textureid) 2 {3 # If finished 4 // cache, you do not need to repeatedly call 5 ccassert (textureunit <kmaxactivetexture for the same textureid, "textureunit is too big"); 6 if (s_currentboundtexture [textureunit]! = Textureid) 7 {8 s_currentboundtexture [textureunit] = textureid; 9 activetexture (gl_texture0 + textureunit); 10 glbindtexture (gl_texture_2d, texreid ); 11} 12 # else13 14 glactivetexture (gl_texture0 + textureunit); 15 glbindtexture (gl_texture_2d, textureid); 16 # endif17}

Glactivetexture: select a texture unit. The texture function of the line plane applies to the texture unit. The parameter is the symbol constant gl_texturei, and the value range of I is 0 ~ N-1, n is the maximum number of texture units supported by OpenGL implementation, here, because the batch processing only has one texture, so textureunit is always 0, because the cache is used, will only be called once

Glbindtexture creates a named texture bound to the target texture. For example, bind a texture to a shape.

The Final Draw function called is similar to the one described in the previous article. In addition, we will introduce the shader and hybrid problems separately in the following sections.

In the engine, the batchcommand command contains two parts: the particle batchnode and the spritebatchnode, which are easy to use.

1 _batchCommand.init(2                        _globalZOrder,3                        _shaderProgram,4                        _blendFunc,5                        _textureAtlas,6                        transform);7 renderer->addCommand(&_batchCommand);

In Versions later than 3.0, the auto-batch function is added, and the efficiency-saving functions of the participant batchnode and spritebatchnode are not so obvious. However, in versions earlier than 3.0, placing the spritebatchnode on the parent node and the particle system on the particle batchnode can batch process the same Sprite. It is helpful to call the rendering function only once for the same texture, or to improve efficiency.

In case of any errors, please note

Next introduction to graphic rendering

[Post] cocos2D-X source code analysis from cocos2D-X learning OpenGL (3) ---- batch_command

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.