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

Source: Internet
Author: User

Cocos2D-X source code analysis from cocos2D-X learning OpenGL (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.

Else if (commandType = RenderCommand: Type: BATCH_COMMAND) {// flush (); auto cmd = static_cast
(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 exist // cache, you do not need to repeatedly call CCASSERT (textureUnit <kMaxActiveTexture, textureUnit is too big) for the same textureId ); 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

Related Article

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: 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.