How to use “Opengl Shader” in cocos2d-x3.0

前端之家收集整理的这篇文章主要介绍了How to use “Opengl Shader” in cocos2d-x3.0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

http://discuss.cocos2d-x.org/t/how-to-use-opengl-shader-in-cocos2d-x3-0/12430

First step:
Add 2 shader files to projects Resource

Second step

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    ize visibleSize = Director::getInstance()->getVisibleSize();
    auto sprite = Sprite::create("HelloWorld.png");
    sprite->setAnchorPoint(Point(0.5,0.5));
    sprite->setPosition(Point(visibleSize.width / 3,visibleSize.height / 3));
    this->addChild(sprite);
    graySprite(sprite);
    return true;
}

void HelloWorld::graySprite(Sprite * sprite)
{
    if(sprite)
    {
        GLProgram * p = new GLProgram();
        p->initWithFilenames("gray.vsh","gray.fsh");
        p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION,GLProgram::VERTEX_ATTRIB_POSITION);
        p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR,GLProgram::VERTEX_ATTRIB_COLOR);
        p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD,GLProgram::VERTEX_ATTRIB_TEX_COORDS);
        p->link();
        p->updateUniforms();
        sprite->setShaderProgram(p);
    }
}


gray.vsh.zip(0.3 KB)
gray.fsh.zip(330 Bytes)

猜你在找的Cocos2d-x相关文章