hi!
with cocos2d-x 2.0.3 this code works perfectly:
CCGLProgram *pBWShaderProgram = new CCGLProgram();
pBWShaderProgram->autorelease();
pBWShaderProgram->initWithVertexShaderFilename("BlackAndWhite.vsh", "BlackAndWhite.fsh");
pBWShaderProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
pBWShaderProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
pBWShaderProgram->link();
pBWShaderProgram->updateUniforms();
CCShaderCache::sharedShaderCache()->addProgram(pBWShaderProgram, "kBWShaderProgram");
CCSprite *mySprite = CCSprite::create("meat.png");
mySprite->setPosition(ccp(getContentSize().width/2.0f, getContentSize().height/2.0f));
addChild(mySprite);
mySprite->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey("kBWShaderProgram"));
mySprite->getShaderProgram()->use();
but it doesn’t with cocos2d-x v2.1beta3-x-2.1.0
and mySprite instead to be in black and white colors just invisible.
why?
here are my vertex and fragment shaders accordingly:
BlackAndWhite.vsh
attribute vec4 a_position;
attribute vec2 a_texCoord;
uniform mat4 u_MVPMatrix;
#ifdef GL_ES
varying mediump vec2 v_texCoord;
#else
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = u_MVPMatrix * a_position;
v_texCoord = a_texCoord;
}
BlackAndWhite.fsh
#ifdef GL_ES
precision mediump float;
#endif
varying vec2 v_texCoord;
uniform sampler2D u_texture;
void main()
{
vec4 normalColor = texture2D(u_texture, v_texCoord);
float gray = 0.299*normalColor.r + 0.587*normalColor.g + 0.114*normalColor.b;
gl_FragColor = vec4(gray, gray, gray, normalColor.a);
}
how to make works this code with cocos2d-x v2.1beta3-x-2.1.0 ?
i would appreciate any suggestions