COCOS2DX 3.x Ccpositiontexturecolor_vert and Ccpositiontexturecolor_nomvp_vert

Source: Internet
Author: User

In Cocos2dx 2.x, if we want to replace the sprite shader, write:

Myprogram->initwithbytearrays (Ccpositiontexturecolor_vert, Myfragsource);

However, in 3.x, the above practice causes the displayed sprite coordinates to be incorrect.

So look at 3.x code, found in 3.x, the default shader of the sprite is no longer kccshader_positiontexturecolor (i.e.

Glprogram::shader_name_position_texture_color), but changed to Glprogram::shader_name_position_texture_color_no_. MVP (see Sprite::initwithtexture (...) function implementation).

Therefore, you should use:

Myprogram->initwithbytearrays (Ccpositiontexturecolor_nomvp_vert, Myfragsource);

The display is normal.

Compare the code for Ccpositiontexturecolor_vert and Ccpositiontexturecolor_nomvp_vert:

Const char* Ccpositiontexturecolor_vert = stringify (

Attribute Vec4 a_position;

Attribute VEC2 A_texcoord;

Attribute Vec4 A_color;

\n#ifdef gl_es\n

Varying lowp vec4 v_fragmentcolor;

Varying mediump vec2 V_texcoord;

\n#else\n

Varying VEC4 v_fragmentcolor;

Varying VEC2 V_texcoord;

\n#endif\n

void Main ()

{

gl_position = Cc_mvpmatrix * a_position;

V_fragmentcolor = A_color;

V_texcoord = A_texcoord;

}

);

Const char* Ccpositiontexturecolor_nomvp_vert = stringify (

Attribute Vec4 a_position;

Attribute VEC2 A_texcoord;

Attribute Vec4 A_color;

\n#ifdef gl_es\n

Varying lowp vec4 v_fragmentcolor;

Varying mediump vec2 V_texcoord;

\n#else\n

Varying VEC4 v_fragmentcolor;

Varying VEC2 V_texcoord;

\n#endif\n

void Main ()

{

gl_position = Cc_pmatrix * a_position;

V_fragmentcolor = A_color;

V_texcoord = A_texcoord;

}

);

The only difference is that vertex coordinates a_position multiply by the matrix, Ccpositiontexturecolor_vert is Cc_mvpmatrix,ccpositiontexturecolor_nomvp_ In the vert, the Cc_pmatrix is multiplied.

So I understood:

In the immediate mode of 2.x, the vertex coordinates of the incoming shader are local coordinates, so you need to multiply the MVP matrix in shader to find the screen coordinates.

In the 3.x command mode, the vertex coordinates of the incoming shader have been pre-converted to world coordinates (that is, the MV matrix has been multiplied), so only the P-matrix is in the shader.

This leads to two questions:

Question 1, why is the sprite pre-converted to world coordinates in command mode?

Because if all the vertices are converted to the same space (such as world space), then their transformation matrix M is the same (can be used as a uniform variable), so these vertices can be submitted one time, thus reducing the number of draw call. This is where the command mechanism is intended. However, it is important to note that not all nodes in 3.x use the NOMVP form, because not all render objects such as sprites are rarely suitable for flattening batch--suppose there are several mesh with many different vertex numbers in the MV Matrix, If you also flatten a one-time commit, like a sprite, although the number of draw call is down, the overhead of the CPU's vertex transformation is significantly increased, and the result is likely to be less than a few draw call, but it is more efficient to transfer the vertex transform to the GPU (shader). So when you replace a node with a shader, be careful to see if its default shader is MVP or NOMVP.

Question 2,sprite where is the code pre-translated into world coordinates?

if (Rendercommand::type::quad_command = = CommandType in void renderer::visitrenderqueue (const renderqueue& queue) ) Branch, there is a sentence fillquads (cmd). Fillquads (...) is implemented as follows:

void Renderer::fillquads (const quadcommand *cmd)

{

memcpy (_quadverts + _numberquads * 4, cmd->getquads (), sizeof (V3F_C4B_T2F_QUAD) * Cmd->getquadcount ());

Const mat4& Modelview = Cmd->getmodelview ();

for (ssize_t i=0; i< cmd->getquadcount () * 4; ++i)

{

V3F_C4B_T2F *q = &_quadverts[i + _numberquads * 4];

VEC3 *VEC1 = (vec3*) &q->vertices;

Modelview.transformpoint (VEC1);

}

_numberquads + = Cmd->getquadcount ();

}

The For loop is multiplied by the MV matrix for each coordinate in the quads vertex array.

COCOS2DX 3.x Ccpositiontexturecolor_vert and Ccpositiontexturecolor_nomvp_vert

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.