Cocos2dx3. X shader make the picture gray

Source: Internet
Author: User

1 OpenGL ESColoring Device

Opengl es .fsh and .vsh two files. These two files are compiled and linked to produce an executable program with the gpu Interactive. attribute vertex attribute varying Span style= "Font-family:times New Roman" >vertex shader fragment shader

. Vsh is the vertex shader, which, withvertex calculations, can understand the position of the control vertex, in which we usually pass in the position of the current vertex, and the coordinates of the texture. Create a new gray.vsh file.

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;}

Gray.fsh is a fragment of shader. In this case I can recalculate each pixel point.

Varying VEC4 v_fragmentcolor;varying vec2 v_texcoord;void Main () {VEC4 V_orcolor = V_fragmentcolor * TEXTURE2D (cc_ TEXTURE0, V_texcoord); float Gray = dot (V_orcolor.rgb, VEC3 (0.299, 0.587, 0.114)); Gl_fragcolor = Vec4 (Gray, Gray, Gray, V_o RCOLOR.A);}

Two files are placed under the Resource folder.


2make a picture gray
void Helloworld::spraddgray (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->setshaderprogram (P);}}

3To restore the picture
void Helloworld::sprremovegray (Ccsprite * Sprite) {if (sprite!=null) {std::string str = "Shaderpositiontexturecolor_ NOMVP "; Ccglprogram * Pprogram = Ccshadercache::sharedshadercache ()->programforkey (str), Sprite->setshaderprogram ( Pprogram); Check_gl_error_debug ();} }

4Run effect

The program responds to the button code as follows, forming a gray and color graph alternating display.

void Helloworld::onbtncolorchange (ref* myref) {static int idx = 0;if (idx% 2 = = 0) {Spraddgray (M_SPR);} Else{sprremovegray (M_SPR);} idx++;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Cocos2dx3. X shader make the picture gray

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.