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