Cocos2d-x 2.x 和 3.x 的渲染流程区别以及Sha

2016-10-25  本文已影响302人  babybus_superdo

1.渲染流程的区别

2.3.x中Shader的使用。

    _customCommand.func = CC_CALLBACK_0(BBSpriteEmboos::onDraw, this, transform, flags);
    renderer->addCommand(&_customCommand);
bool BBSpriteEmboos::initWithTexture(CCTexture2D *pTexture, const CCRect& rect) {
    do{
        // CCLog("override initWithTexture!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        CC_BREAK_IF(!CCSprite::initWithTexture(pTexture, rect));
        
        GLchar * fragSource = (GLchar*) CCString::createWithContentsOfFile(
            CCFileUtils::sharedFileUtils()->fullPathForFilename("res/plg/effect/BBSpriteEmboos.fsh").c_str())->getCString();
        
        // 加载顶点着色器和片元着色器
        m_pShaderProgram = new  CCGLProgram();
        m_pShaderProgram ->initWithVertexShaderByteArray(ccPositionTextureA8Color_vert, fragSource);
        this->setShaderProgram(m_pShaderProgram);
        CHECK_GL_ERROR_DEBUG();
        
        // 启用顶点着色器的attribute变量,坐标、纹理坐标、颜色
        m_pShaderProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
        m_pShaderProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
        m_pShaderProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
        
        CHECK_GL_ERROR_DEBUG();
        
        // 自定义着色器链接
        m_pShaderProgram->link();
        
        CHECK_GL_ERROR_DEBUG();
        
        // 设置移动、缩放、旋转矩阵
        m_pShaderProgram->updateUniforms();
        CHECK_GL_ERROR_DEBUG();
        
        return true;
        
    }while(0);
    return false;
}
        // 获取GL程序
        auto glProgram = getGLProgram();
        glProgram->use();
        // 设置变换矩阵,旋转平移等
        glProgram->setUniformsForBuiltins(transform);
        
        // 开启顶点属性
        ccGLEnableVertexAttribs(kCCVertexAttribFlag_PosColorTex );
        ccBlendFunc blend = getBlendFunc();
        ccGLBlendFunc(blend.src, blend.dst);
    
        ccGLBindTexture2D( getTexture()->getName());
    
        //
        // Attributes
        //
        #define kQuadSize sizeof(_quad.bl)
        long offset = (long)&_quad;
    
        // vertex
        int diff = offsetof( ccV3F_C4B_T2F, vertices);
        glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, kQuadSize, (void*) (offset + diff));
    
        // texCoods
        diff = offsetof( ccV3F_C4B_T2F, texCoords);
        glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, kQuadSize, (void*)(offset + diff));
    
        // color
        diff = offsetof( ccV3F_C4B_T2F, colors);
        glVertexAttribPointer(kCCVertexAttrib_Color, 4, GL_UNSIGNED_BYTE, GL_TRUE, kQuadSize, (void*)(offset + diff));
        
        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
        
        CC_INCREMENT_GL_DRAWS(1);
上一篇 下一篇

猜你喜欢

热点阅读