I have 10 - 15 sprites and i’m trying to shade them instead of using rendertexture cause i thought 10-15 for shading is a small number here is my code showing how i Implement and deimplement shader.
I dont thik deimplementation has to do anything with this problem cause it (lag or jerk) occurs as soon as shader is implemented on
10-15 sprites in a loop
like this
for(j=0; j<=TOTAL_SPRITES; j++){
shadeSprite(sprite_array[j]);
}
Implementation
void TouchScene::shadeSprite(cocos2d::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->setGLProgram(p);
}
}
deimplementation (or removing shader)
void TouchScene::removeShader(cocos2d::Sprite* sprite){
sprite->setGLProgramState(
GLProgramState::getOrCreateWithGLProgramName(
GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR_NO_MVP
)
);
}
gray.fsh
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
void main()
{
vec4 v_orColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);
float gray = dot(v_orColor.rgb, vec3(0.299, 0.587, 0.114));
gl_FragColor = vec4(gray, gray, gray, v_orColor.a);
}
gray.vsh
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
void main()
{
//gl_Position = CC_MVPMatrix * a_position;
gl_Position = CC_PMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}