Hi, I’d like to have
->a sprite
->apply a shader
->render the shader into a texture
->switch back to display the normal sprite
->use the texture shaded for pixel collision
outdated pixel perfect collision detection complete code
porting to cocos2dx v3.5
–if I set the shader program back to the default shader being used by the game the texture is like the default without the shader alpplied.
–if I don’t switch back the shader is applied to the sprite but the rendered texture is empty
–I tried _rt->end(); Director::getInstance()->getRenderer()->render(); doesn’t work
–seem that I should make an onDraw method, but I can’t figure out how to switch back correctly…
dynamic-texture-gradinet-not-rendering
synchronous-glreadpixel-in-cocos2d-x-v3
custom-mesh-rendering-with-texture-in
how-to-use-fragment-shader-with-cocos2d-x-3-1
this topic try to display the same image in multiple position, seem very similar, as I try to have the same sprite with different shader, one on the screen, one on a texture…
sprite-and-multiple-visits-inside-render-texture
this is the glProgram:
glProgram = new CCGLProgram();
glProgram->retain();
glProgram->initWithVertexShaderFilename("SolidVertexShader.vsh", "SolidColorShader.fsh");
glProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
glProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
glProgram->link();
glProgram->updateUniforms();
glProgram->use();
uniformColorRed = glGetUniformLocation(glProgram->getProgram(), "u_color_red");
uniformColorBlue = glGetUniformLocation(glProgram->getProgram(), "u_color_blue");
and this is the test:
Texture2D *CollisionDetection::spriteTest(cocos2d::CCSprite* spr1, CCRenderTexture* _rt) {
CCRect r1 = spr1->boundingBox();
// Setting the custom shader to be used
spr1->setShaderProgram(glProgram);
spr1->getShaderProgram()->use();
// Clearing the Secondary Draw buffer of all previous values
_rt->beginWithClear( 255, 255, 0, 255);
// The below two values are being used in the custom shaders to set the value of RED and BLUE colors to be used
glUniform1i(uniformColorRed, 255);
glUniform1i(uniformColorBlue, 0);
// The blend function is important we don't want the pixel value of the RED color being over-written by the BLUE color.
// We want both the colors at a single pixel and hence get a PINK color (so that we have both the RED and BLUE pixels)
spr1->setBlendFunc((ccBlendFunc){GL_SRC_ALPHA, GL_ONE});
// The visit() function draws the sprite in the _rt draw buffer its a Cocos2d-x function
spr1->visit();
// Setting the shader program back to the default shader being used by our game
// spr1->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
// Setting the default blender function being used by the game
// spr1->setBlendFunc((ccBlendFunc){CC_BLEND_SRC, CC_BLEND_DST});
_rt->end();
// Director::getInstance()->getRenderer()->render();
spr1->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP));//kCCShader_PositionTextureColor));
spr1->setBlendFunc((ccBlendFunc){CC_BLEND_SRC, CC_BLEND_DST});
return _rt->getSprite()->getTexture();
}
how can I render a shader to a texture then switch the texture to normal before is rendered on the screen?
thanks



