void Utils::addGray(CCSprite* sp)
{
do
{
GLchar* pszFragSource =
"#ifdef GL_ES \n \
precision mediump float; \n \
#endif \n \
uniform sampler2D u_texture; \n \
varying vec2 v_texCoord; \n \
varying vec4 v_fragmentColor; \n \
void main(void) \n \
{ \n \
// Convert to greyscale using NTSC weightings \n \
vec4 col = texture2D(u_texture, v_texCoord); \n \
float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
gl_FragColor = vec4(grey, grey, grey, col.a); \n \
}";
CCGLProgram* pProgram = new CCGLProgram();
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
sp->setShaderProgram(pProgram);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->link();
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->updateUniforms();
CHECK_GL_ERROR_DEBUG();
} while (0);
}
void Utils::removeGray(CCSprite* sp)
{
do
{
CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor);
sp->setShaderProgram(pProgram);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->link();
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->updateUniforms();
CHECK_GL_ERROR_DEBUG();
} while (0);
{
do
{
GLchar* pszFragSource =
"#ifdef GL_ES \n \
precision mediump float; \n \
#endif \n \
uniform sampler2D u_texture; \n \
varying vec2 v_texCoord; \n \
varying vec4 v_fragmentColor; \n \
void main(void) \n \
{ \n \
// Convert to greyscale using NTSC weightings \n \
vec4 col = texture2D(u_texture, v_texCoord); \n \
float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114)); \n \
gl_FragColor = vec4(grey, grey, grey, col.a); \n \
}";
CCGLProgram* pProgram = new CCGLProgram();
pProgram->initWithVertexShaderByteArray(ccPositionTextureColor_vert, pszFragSource);
sp->setShaderProgram(pProgram);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->link();
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->updateUniforms();
CHECK_GL_ERROR_DEBUG();
} while (0);
}
void Utils::removeGray(CCSprite* sp)
{
do
{
CCGLProgram* pProgram = CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor);
sp->setShaderProgram(pProgram);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->link();
CHECK_GL_ERROR_DEBUG();
sp->getShaderProgram()->updateUniforms();
CHECK_GL_ERROR_DEBUG();
} while (0);
}
http://blog.csdn.net/xingchen1106/article/details/45477351
http://www.cocoachina.com/bbs/read.php?tid-271978-keyword-%BB%D2%BB%AF.html