GL Shader脚本代码
gray.vsh
<pre name="code" class="cpp">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_PMatrix * a_position; v_fragmentColor = a_color; v_texCoord = a_texCoord; }
</pre><pre code_snippet_id="688699" snippet_file_name="blog_20150609_3_5918082" name="code" class="cpp">gray.fsh
#ifdef GL_ES precision mediump float; #endif varying vec4 v_fragmentColor; varying vec2 v_texCoord; void main(void) { vec4 c = texture2D(CC_Texture0,v_texCoord); gl_FragColor.xyz = vec3(0.2126*c.r + 0.7152*c.g + 0.0722*c.b); gl_FragColor.w = c.w; }
以上两文件copy到工程
使用
void CMovingObject::SetGraySprite(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); //恢复 //<span style="font-family: Arial,Helvetica,sans-serif;">sprite->setGLProgram(GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));</span> //解决坐标错乱问题:gray.vsh 中的CC_MVPMatrix 改为 CC_PMatrix 即可 //解决报错问题:fsh中注释掉这句话//uniform sampler2D CC_Texture0; } }原文链接:https://www.f2er.com/cocos2dx/342707.html