我想將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命令?