2017-04-03 88 views
0

我想將N數量的紋理傳遞給自定義着色器。當我通過網絡衝浪時,我得到的最佳選擇是重寫Sprite :: draw()函數並將一個CustomCommand對象添加到繪圖池中。將額外的N個紋理傳遞給cocos2d中的自定義着色器

CPP文件:

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着色器:

#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... 

    } 

以上着色器只是一個樣本,而不是我使用的實際代碼。目前,我只需要在單個着色器中成功傳遞多個紋理來處理它們的像素。

目前,我無法顯示傳遞的紋理。應該做些什麼來實現這一目標? SpriteSub :: draw()中的Sprite :: draw()是否取消了onDraw()中的gl命令?

回答

0

'解決了!

您甚至不需要重寫繪圖或添加CustomCommand來呈現隊列。

步驟:

1),然後使用您的自定義着色器創建的對象GLProgram創建GLProgramState。使用GLProgramState::setUniformTexture("uniformVarName", tex2D)找到統一變量並傳遞紋理名稱。

3.)撥打sprite->setGLProgramState(programState)而不是sprite->setGLProgram(program);

CCP文件:

m_shader = GLProgram::createWithByteArrays(ccPositionTextureColor_noMVP_vert, shader_frag); 
m_shader->retain(); 

auto shaderState = GLProgramState::getOrCreateWithGLProgram(m_shader); 

auto textureCache = Director::getInstance()->getTextureCache(); 
auto img0 = textureCache->addImage("icon_tex0.png")->getName(); 
auto img1 = textureCache->addImage("icon_tex1.png")->getName(); 
auto img2 = textureCache->addImage("icon_tex2.png")->getName(); 
auto img3 = textureCache->addImage("icon_tex3.png")->getName(); 
auto img4 = textureCache->addImage("icon_tex4.png")->getName(); 
auto img5 = textureCache->addImage("icon_tex5.png")->getName(); 

shaderState->setUniformTexture("texIcon0", img0); 
shaderState->setUniformTexture("texIcon1", img1); 
shaderState->setUniformTexture("texIcon2", img2); 
shaderState->setUniformTexture("texIcon3", img3); 
shaderState->setUniformTexture("texIcon4", img4); 
shaderState->setUniformTexture("texIcon5", img5); 

//this->setGLProgram(m_shader); 
this->setGLProgramState(shaderState); 

片段着色器:

#ifdef GL_ES 
    precision lowp float; 
    #endif 

    varying vec4 v_fragmentColor; 
    varying vec2 v_texCoord; 

    uniform sampler2D texIcon0; 
    uniform sampler2D texIcon1; 
    uniform sampler2D texIcon2; 
    uniform sampler2D texIcon3; 
    uniform sampler2D texIcon4; 
    uniform sampler2D texIcon5; 

    void main() 
    { 

     int numTex = 6; 
     float fWeight = 1.0f/float(length); 

     //gl_FragColor = texture2D(CC_Texture0, v_texCoord); 
     gl_FragColor += texture2D(texIcon0, v_texCoord) * fWeight; 
     gl_FragColor += texture2D(texIcon1, v_texCoord) * fWeight; 
     gl_FragColor += texture2D(texIcon2, v_texCoord) * fWeight; 
     gl_FragColor += texture2D(texIcon3, v_texCoord) * fWeight; 
     gl_FragColor += texture2D(texIcon4, v_texCoord) * fWeight; 
     gl_FragColor += texture2D(texIcon5, v_texCoord) * fWeight; 


    } 

來源:

相關問題