Transferred from: Http://www.tuicool.com/articles/U3URRrI
Projects often encounter the need to process an image into gray, in order to save resources, generally do not allow the art to do a set of the same grayscale, usually through the code processing to make the picture gray. There are many ways to use shader to deal with the image of Gray, these methods do also achieve the need to make the picture Gray, but the Android platform from the background switch back, shader was released, resulting in the image location confusion. The key is to reload the shader when switching back from the Android backend. Let's take a look at the shader treatment of the native COCOS2DX, We found CCShaderCache.cpp from the COCOS2DX library and found that this class had a Reloaddefaultshaders method, which was to reload shader, but it was not called on the iOS platform, and on Android platform, jni/ Main.cpp is called, let's take a look at main.cpp:
voidJava_org_cocos2dx_lib_cocos2dxrenderer_nativeinit (jnienv*env, Jobject thiz, Jint W, Jint h) {if(! Ccdirector::shareddirector ()Getopenglview ()) {Cceglview*view =Cceglview::sharedopenglview ();
View-setframesize (W, h); Appdelegate*pappdelegate =Newappdelegate (); Ccapplication::sharedapplication ()-run (); }Else{Ccglinvalidatestatecache (); Ccshadercache::sharedshadercache () - reloaddefaultshaders (); Ccdrawinit (); Cctexturecache::reloadalltextures (); Ccnotificationcenter::sharednotificationcenter ()-postnotification (Event_come_to_foreground, NULL); Ccdirector::shareddirector ()-setgldefaultvalues (); }}
Ccshadercache::sharedshadercache ()->reloaddefaultshaders (); This line of code is to reload shader. OK, the problem has been found, as long as we re-init in this place shader is OK. I will be online a workable way to paste into the following, for your reference: 1, write a gray image of the shader program, and put into the COCOS2DX engine under the shader folder.
//ccshader_positiontexturegray_frag.h"\n\#ifdef gl_es \nprecision mediumpfloat; \ n#endif\n\\nuniform sampler2d u_texture; \nvarying VEC2 V_texcoord; \nvarying VEC4 V_fragmentcolor; \ nvoidMainvoid) \n{\ n//Convert to greyscale using NTSC weightings \n\Vec4 col =texture2d (U_texture, V_texcoord); \ nfloatGrey = Dot (Col.rgb, VEC3 (0.299,0.587,0.114)); \ngl_fragcolor=VEC4 (grey, grey, grey, COL.A); \ n}";
2. Add in CcShaders.h
extern Const Glchar * CCPOSITIONTEXTUREGRAY_FRAG;
3. Add in CcShaders.cpp
Const Glchar * Ccpositiontexturegray_frag ="ccshader_positiontexturegray_frag.h"
4. Add in CCGLProgram.h
#define kccshader_positiontexturegray "Shaderpositiontexturegray"
5. Adding enumeration types in CCShaderCache.cpp
enum { Kccshadertype_positiontexturecolor, kccshadertype_positiontexturecoloralphatest, Kccshadertype_positioncolor, kccshadertype_positiontexture, Kccshadertype_positiontexture_ucolor, Kccshadertype_positiontexturea8color, Kccshadertype_position_ucolor, kccshadertype_ Positionlengthtexurecolor, Kccshadertype_controlswitch, Kccshadertype_max, kccshadertype_ Positiontexturegray,};
Add in Ccshadercache::loaddefaultshaders ()
// Position Texture Gray shader New Ccglprogram (); Loaddefaultshader (P, Kccshadertype_positiontexturegray); M_pprograms-setobject (p, Kccshader_positiontexturegray);p->release ();
Add in Ccshadercache::reloaddefaultshaders ()
// // Position Texture Gray shader //p = programforkey (kccshader_positiontexturegray);p-Reset (); Loaddefaultshader (P, Kccshadertype_positiontexturegray);
Ccshadercache::loaddefaultshader (ccglprogram *p, int type) added
Case kccshadertype_positiontexturegray: P--Initwithvertexshaderbytearray (Ccpositiontexturecolor _vert, Ccpositiontexturegray_frag); P--AddAttribute (Kccattributenameposition, kccvertexattrib_position); P--AddAttribute (Kccattributenamecolor, kccvertexattrib_color); P--AddAttribute (Kccattributenametexcoord, kccvertexattrib_texcoords) ; break;
6. Create a new grayscale conversion call class (to extend other color transformations)
//ColorUtils.h#ifndef __color_utils_h__#define__color_utils_h__#include"cocos2d.h"using_ns_cc;classcolorutils{ Public: Colorutils (); ~colorutils (); Static voidAddcolorgray (Ccsprite *SPR); Static voidRemovecolorgray (Ccsprite *SPR); Private:};#endif//ColorUtils.cpp#include"ColorUtils.h"colorutils::colorutils () {} colorutils::~colorutils () {}voidColorutils::addcolorgray (Ccsprite *SPR) {SPR->setshaderprogram (Ccshadercache::sharedshadercache ()Programforkey (Kccshader_positiontexturegray));} voidColorutils::removecolorgray (Ccsprite *SPR) {SPR->setshaderprogram (Ccshadercache::sharedshadercache ()Programforkey (Kccshader_positiontexturecolor));}
COCOS2DX shader Implementation Gray-level image android background switch back cause image offset problem