Cocos2D-X source code analysis from cocos2D-X learning OpenGL (3) ---- batch_command

Source: Internet
Author: User

Personal Original, welcome to reprint, reprint please indicate the original address http://blog.csdn.net/bill_man

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.

Else if (commandtype = rendercommand: Type: batch_command) {// flush (); Auto cmd = static_cast <batchcommand *> (command ); // call command cmd-> execute ()}

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.

Void batchcommand: Execute () {// set the rendering material // shader _ shader-> Use (); // obtain the MV and P first, then multiply the MV and P to get the MVP, that is, Model View projection matrix _ shader-> setuniformsforbuiltins (_ mV); // select the Texture unit, create a named texture GL: bindtexture2d (_ textureid) bound to the target texture; // mixed GL: blendfunc (_ blendtype. SRC, _ blendtype. DST); // Draw _ textureatlas-> drawquads ();}

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

Void glprogram: equals (const kmmat4 & matrixmv) {// use the variables in the flag to determine whether related matrices are used. These variable variables are set to kmmat4 matrixp in updateuniforms; kmglgetmatrix (km_gl_projection, & matrixp); If (_ flags. usesp) setuniformlocationwithmatrix4fv (_ uniforms [uniform_p_matrix], matrixp. mat, 1); If (_ flags. usesmv) setuniformlocationwithmatrix4fv (_ uniforms [uniform_mv_matrix], matrixmv. mat, 1); If (_ flags. usesmvp) {kmmat4 matrixmvp; kmmat4multiply (& matrixmvp, & matrixp, & matrixmv); setuniformlocationwithmatrix4fv (_ uniforms [uniform_mvp_matrix], matrixmvp. mat, 1);} If (_ flags. usestime) {Director * Director = Director: getinstance (); // the actual time per frame is not used here, which is too efficient. Float time = Director-> gettotalframes () * Director-> getanimationinterval (); setuniformlocationwith4f (_ uniforms [glprogram: uniform_time], time/10.0, time, time * 2, time * 4 ); setuniformlocationwith4f (_ uniforms [glprogram: uniform_sin_time], time/8.0, time/4.0, time/2.0, sinf (time); setuniformlocationwith4f (_ uniforms [glprogram: uniform_cos_time], time/8.0, time/4.0, time/2.0, cosf (time);} If (_ flags. usesrandom) Evaluate (_ uniforms [glprogram: uniform_random01], ccrandom_0_1 (), ccrandom_0_1 (), ccrandom_0_1 (), ccrandom_0_1 ());}

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.

Void bindtexture2dn (gluint textureunit, gluint textureid) {# If cc_enable_gl_state_cache // cache, The ccassert (textureunit <kmaxactivetexture, "textureunit is too big"); If (s_currentboundtexture [textureunit]! = Textureid) {response [textureunit] = textureid; activetexture (Response + textureunit); glbindtexture (response, textureid);} # else glactivetexture (Response + textureunit); glbindtexture, textureid); # endif}

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.

_batchCommand.init(                       _globalZOrder,                       _shaderProgram,                       _blendFunc,                       _textureAtlas,                       transform);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


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.