lundi 3 avril 2017

Override cocos2d::Sprite class and pass additional texture for custom shader

I want to pass N number of texture to a custom shader. As I surfed thru net, the best option I've got is to override the Sprite::draw() function and add a CustomCommand object to the draw pool.

CPP file:

void SpriteSub::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) {

    m_cmdCustom.init(_globalZOrder, transform, flags);
    m_cmdCustom.func = CC_CALLBACK_0(SpriteSub::onDraw, this, transform, flags);
    renderer->addCommand(&m_cmdCustom);

    Sprite::draw(renderer, transform, flags);

}

void SpriteSub::onDraw(const Mat4 &transform, uint32_t /*flags*/) {

    this->getGLProgram()->use();

    auto wwProgram = m_shader->getProgram();

    auto texBall0 = Director::getInstance()->getTextureCache()->addImage("ball.png")->getName();
    GLuint locTexIcon0 = glGetUniformLocation(wwProgram, "texBall0");
    glUniform1i(locTexIcon0, 0);
    //glActiveTexture(GL_TEXTURE0 + 0); glBindTexture(GL_TEXTURE_2D, texBall0);
    //glActiveTexture(GL_TEXTURE0 + 0);
    GL::bindTexture2D(texBall0);

}

frament shader:

    #ifdef GL_ES
    precision lowp float;
    #endif

    varying vec4 v_fragmentColor;
    varying vec2 v_texCoord;

    uniform sampler2D texBall0;
    uniform sampler2D texBall1;
    //uniform sampler2D texBall2;
    //uniform sampler2D texBall3;
    //uniform sampler2D texBall4;
    //uniform sampler2D texBall5;

    void main()
    {

        float t = 0.7f;

        //gl_FragColor = texture2D(CC_Texture0, v_texCoord);

        //this is just a sample code
        gl_FragColor = texture2D(texBall0, v_texCoord) * t;
        gl_FragColor += texture2D(texBall1, v_texCoord) * (t-1);

        //some code that uses the other textures...

    }   

The shader above is just a sample and not the actual code I'm using. For now, I just need to successfully pass multiple textures in a single shader to process their pixels.

Currently, I can't display the passed texture. What should be done to achieve this? Does Sprite::draw() in SpriteSub::draw() cancel-out the gl commands from onDraw()?

Aucun commentaire:

Enregistrer un commentaire