cocos2d-x,Shader的用法:精灵变灰

前端之家收集整理的这篇文章主要介绍了cocos2d-x,Shader的用法:精灵变灰前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

GL Shader脚本代码@H_404_5@ 
gray.vsh@H_404_5@ 
<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;
}@H_404_5@ 


@H_404_5@ 
</pre><pre code_snippet_id="688699" snippet_file_name="blog_20150609_3_5918082" name="code" class="cpp">gray.fsh@H_404_5@ 
#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;
}@H_404_5@ 

以上两文件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;
    }

}@H_404_5@

猜你在找的Cocos2d-x相关文章