cocos2dx GLProgram

98 阅读1分钟

glBindAttribLocation是手动给attribute分配一个location,方便后续直接使用

void glBindAttribLocation(GLuint program, GLuint index, const GLchar* name);

如果program里面没有对应name的attribute,并不会报错,仅仅会将index和attribute name对应起来。

void GLProgram::bindPredefinedVertexAttribs()
{
    static const struct {
        const char *attributeName;
        int location;
    } attribute_locations[] =
    {
        {GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION},
        {GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR},
        {GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORD},
        {GLProgram::ATTRIBUTE_NAME_TEX_COORD1, GLProgram::VERTEX_ATTRIB_TEX_COORD1},
        {GLProgram::ATTRIBUTE_NAME_TEX_COORD2, GLProgram::VERTEX_ATTRIB_TEX_COORD2},
        {GLProgram::ATTRIBUTE_NAME_TEX_COORD3, GLProgram::VERTEX_ATTRIB_TEX_COORD3},
        {GLProgram::ATTRIBUTE_NAME_NORMAL, GLProgram::VERTEX_ATTRIB_NORMAL},
    };

    const int size = sizeof(attribute_locations) / sizeof(attribute_locations[0]);

    for(int i=0; i<size;i++) {
        glBindAttribLocation(_program, attribute_locations[i].location, attribute_locations[i].attributeName);
    }
}

GLProgramState

更像是对GLProgram的一个状态,GLProgram有点像类的声明,GLProgramState更像是类的示例,state里面可以给具体的uniform、attribute等赋值,形成一个状态。

glGetProgramiv 函数可以用来查询 shader program 对象的一些信息,其中 GL_ACTIVE_ATTRIBUTES 枚举值可以用来获取当前 shader program 中定义的 attribute 变量数量。