/** Command used to render one or more Triangles, which is similar to QuadCommand. Every TrianglesCommand will have generate material ID by give textureID, glProgramState, Blend function if the material id is the same, these TrianglesCommands could be batched to save draw call. */ classCC_DLLTrianglesCommand :public RenderCommand { public: /**The structure of Triangles. */ structTriangles { /**Vertex data pointer.*/ V3F_C4B_T2F* verts; /**Index data pointer.*/ unsigned short* indices; /**The number of vertices.*/ int vertCount; /**The number of indices.*/ int indexCount; }; /**Constructor.*/ TrianglesCommand(); /**Destructor.*/ ~TrianglesCommand(); /** Initializes the command. @param globalOrder GlobalZOrder of the command. @param textureID The openGL handle of the used texture. @param glProgramState The specified glProgram and its uniform. @param blendType Blend function for the command. @param triangles Rendered triangles for the command. @param mv ModelView matrix for the command. @param flags to indicate that the command is using 3D rendering or not. */ voidinit(float globalOrder, GLuint textureID, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles,const Mat4& mv, uint32_t flags); voidinit(float globalOrder, Texture2D* textureID, GLProgramState* glProgramState, BlendFunc blendType, const Triangles& triangles, const Mat4& mv, uint32_t flags); /**Apply the texture, shaders, programs, blend functions to GPU pipeline.*/ voiduseMaterial()const; /**Get the material id of command.*/ inlineuint32_tgetMaterialID()const{ return _materialID; } /**Get the openGL texture handle.*/ inline GLuint getTextureID()const{ return _textureID; } /**Get a const reference of triangles.*/ inlineconst Triangles& getTriangles()const{ return _triangles; } /**Get the vertex count in the triangles.*/ inlinessize_tgetVertexCount()const{ return _triangles.vertCount; } /**Get the index count of the triangles.*/ inlinessize_tgetIndexCount()const{ return _triangles.indexCount; } /**Get the vertex data pointer.*/ inlineconst V3F_C4B_T2F* getVertices()const{ return _triangles.verts; } /**Get the index data pointer.*/ inlineconstunsigned short* getIndices()const{ return _triangles.indices; } /**Get the glprogramstate.*/ inline GLProgramState* getGLProgramState()const{ return _glProgramState; } /**Get the blend function.*/ inline BlendFunc getBlendType()const{ return _blendType; } /**Get the model view matrix.*/ inlineconst Mat4& getModelView()const{ return _mv; } protected: /**Generate the material ID by textureID, glProgramState, and blend function.*/ voidgenerateMaterialID(); /**Generated material id.*/ uint32_t _materialID; /**OpenGL handle for texture.*/ GLuint _textureID; /**GLprogramstate for the command. encapsulate shaders and uniforms.*/ GLProgramState* _glProgramState; /**The GLProgram used by GLProgramState*/ GLProgram* _glProgram; /**Blend function when rendering the triangles.*/ BlendFunc _blendType; /**Rendered triangles.*/ Triangles _triangles; /**Model view matrix when rendering the triangles.*/ Mat4 _mv;
/**The structure of Triangles. */ structTriangles { /**Vertex data pointer.*/ V3F_C4B_T2F* verts; /**Index data pointer.*/ unsigned short* indices; /**The number of vertices.*/ int vertCount; /**The number of indices.*/ int indexCount; };
/** @struct V3F_C4B_T2F * A Vec2 with a vertex point, a tex coord point and a color 4B. */ structCC_DLLV3F_C4B_T2F { /// vertices (3F) Vec3 vertices; // 12 bytes
/// colors (4B) Color4B colors; // 4 bytes
// tex coords (2F) Tex2F texCoords; // 8 bytes };
其中纹理坐标实际上是2个浮点型数值:
1 2 3 4 5 6 7 8 9 10 11 12
/** @struct Tex2F * A TEXCOORD composed of 2 floats: u, y * @since v3.0 */ structCC_DLLTex2F { Tex2F(float _u, float _v): u(_u), v(_v) {}
auto cmd = static_cast<TrianglesCommand*>(command); // flush own queue when buffer is full if(_filledVertex + cmd->getVertexCount() > VBO_SIZE || _filledIndex + cmd->getIndexCount() > INDEX_VBO_SIZE) { CCASSERT(cmd->getVertexCount()>= 0 && cmd->getVertexCount() < VBO_SIZE, "VBO for vertex is not big enough, please break the data down or use customized render command"); CCASSERT(cmd->getIndexCount()>= 0 && cmd->getIndexCount() < INDEX_VBO_SIZE, "VBO for index is not big enough, please break the data down or use customized render command"); drawBatchedTriangles(); } // queue it _queuedTriangleCommands.push_back(cmd); _filledIndex += cmd->getIndexCount(); _filledVertex += cmd->getVertexCount(); } elseif (RenderCommand::Type::MESH_COMMAND == commandType) { flush2D(); // ...略去无关代码 } elseif(RenderCommand::Type::GROUP_COMMAND == commandType) { flush(); // ...略去无关代码 } // ...略去无关代码 else { CCLOGERROR("Unknown commands in renderQueue"); } }
int batchesTotal = 0; int prevMaterialID = -1; bool firstCommand = true;
for(auto it = std::begin(_queuedTriangleCommands); it != std::end(_queuedTriangleCommands); ++it) { constauto& cmd = *it; auto currentMaterialID = cmd->getMaterialID(); constbool batchable = !cmd->isSkipBatching();
fillVerticesAndIndices(cmd);
// in the same batch ? if (batchable && (prevMaterialID == currentMaterialID || firstCommand)) { CC_ASSERT(firstCommand || _triBatchesToDraw[batchesTotal].cmd->getMaterialID() == cmd->getMaterialID() && "argh... error in logic"); _triBatchesToDraw[batchesTotal].indicesToDraw += cmd->getIndexCount(); _triBatchesToDraw[batchesTotal].cmd = cmd; } else { // is this the first one? if (!firstCommand) { batchesTotal++; _triBatchesToDraw[batchesTotal].offset = _triBatchesToDraw[batchesTotal-1].offset + _triBatchesToDraw[batchesTotal-1].indicesToDraw; }
// fill vertex, and convert them to world coordinates const Mat4& modelView = cmd->getModelView(); for(ssize_t i=0; i < cmd->getVertexCount(); ++i) { modelView.transformPoint(&(_verts[i + _filledVertex].vertices)); }
// fill index constunsigned short* indices = cmd->getIndices(); for(ssize_t i=0; i< cmd->getIndexCount(); ++i) { _indices[_filledIndex + i] = _filledVertex + indices[i]; }