Skip to content

Instantly share code, notes, and snippets.

@butihuzi
Created September 23, 2013 09:43
Show Gist options
  • Save butihuzi/6668405 to your computer and use it in GitHub Desktop.
Save butihuzi/6668405 to your computer and use it in GitHub Desktop.
cocos2d-x render to texture with OpenGL CCGLProgram. cocos2d-x 中给 ccSprite 添加灰白滤镜(类似黑白照片效果)。
void CommUtil::disableSprite(CCSprite* sp)
{
const 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);
pProgram->release();
sp->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
sp->getShaderProgram()->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
sp->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
sp->getShaderProgram()->link();
sp->getShaderProgram()->updateUniforms();
}
@liukun
Copy link

liukun commented Oct 10, 2013

注释下,取消的代码在 https://gist.github.com/butihuzi/6667106

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment