Spite中初始化
TrianglesCommand _trianglesCommand; _trianglesCommand.init(_globalZOrder, _texture, getGLProgramState(), _blendFunc, _polyInfo.triangles, // 类型为Triangles transform, flags); 复制代码
struct Triangles { /**Vertex data pointer.*/ V3F_C4B_T2F* verts; // 注意这里是V3F /**Index data pointer.*/ unsigned short* indices; /**The number of vertices.*/ int vertCount; /**The number of indices.*/ int indexCount; }; 复制代码
填充顶点数据的逻辑
void Renderer::fillVerticesAndIndices(const TrianglesCommand* cmd) { memcpy(&_verts[_filledVertex], cmd->getVertices(), sizeof(V3F_C4B_T2F) * cmd->getVertexCount()); // 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 const unsigned short* indices = cmd->getIndices(); for(ssize_t i=0; i< cmd->getIndexCount(); ++i) { _indices[_filledIndex + i] = _filledVertex + indices[i]; } _filledVertex += cmd->getVertexCount(); _filledIndex += cmd->getIndexCount(); } 复制代码
setupVBOAndVAO函数规定了如何解析顶点数据, 数据结构类型就是V3F_C4B_T2F
void Renderer::setupVBOAndVAO() { //generate vbo and vao for trianglesCommand glGenVertexArrays(1, &_buffersVAO); GL::bindVAO(_buffersVAO); glGenBuffers(2, &_buffersVBO[0]); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); // vertices glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, vertices)); // colors glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, colors)); // tex coords glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*) offsetof( V3F_C4B_T2F, texCoords)); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * INDEX_VBO_SIZE, _indices, GL_STATIC_DRAW); // Must unbind the VAO before changing the element buffer. GL::bindVAO(0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); CHECK_GL_ERROR_DEBUG(); } 复制代码
顶点数据布局
Position | Color | TexCoord |
Vec3 | Color4B | Vec2 |
可以使用Vec3的Z进行纹理单元的设置