Cocos2dx shader implements grayscale image switching back in android background, leading to image offset Problems

Source: Internet
Author: User

Projects often encounter the need to process an image as gray. in order to save resources, the art will not make a set of the same gray-scale images, and the images will usually be grayed out through code processing. There are also many ways on the Internet to use shader to process images in gray mode. These methods have indeed met the need to gray out images. However, when the android platform switches back from the background, the shader is released, the image location is disordered. The key is to re-load the shader when switching back from the android background. Let's take a look at the native shader Processing Method of cocos2dx. We found CCShaderCache from the cocos2dx library. cpp. It is found that this class has a reloaddefashadshaders method. This method is to reload the shader, but it is not called on the ios platform. On the android platform, jni/main. cpp is called. Let's take a look at main. cpp: Copy code 1 void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit (JNIEnv * env, jobject thiz, jint w, jint h) 2 {3 if (! CCDirector: sharedDirector ()-> getOpenGLView () 4 {5 CCEGLView * view = CCEGLView: Export dopenglview (); 6 view-> setFrameSize (w, h ); 7 8 AppDelegate * pAppDelegate = new AppDelegate (); 9 CCApplication: sharedApplication ()-> run (); 10} 11 else12 {13 ccGLInvalidateStateCache (); 14 CCShaderCache :: sharedShaderCache ()-> reloaddefashadshaders (); 15 ccDrawInit (); 16 CCTextureCache: reloadAllTextures (); 17 CCNotificat IonCenter: sharedNotificationCenter ()-> postNotification (success, NULL); 18 ccctor ctor: sharedDirector ()-> setgldefavaluvalues (); 19} 20} copy the code CCShaderCache: sharedShaderCache () -> reloaddefashadshaders (); this line of code is to reload the shader. Okay, the problem is found. As long as we re-init the shader in this place, it will be OK. I paste a feasible processing method on the Internet to the following for your reference: 1. Write a shader program with an image grayed out and put it in the shader folder under the cocos2dx engine. Copy code 1 // ccShader_PositionTextureGray_frag.h 2 "\ n \ 3 # ifdef GL_ES \ n \ 4 precision mediump float; \ n \ 5 # endif \ n \ 6 \ n \ 7 uniform sampler2D u_texture; \ n \ 8 varying vec2 v_texCoord; \ n \ 9 varying vec4 v_fragmentColor; \ n \ 10 \ n \ 11 void main (void) \ n \ 12 {\ n \ 13 // Convert to greyscale using NTSC weightings \ n \ 14 vec4 col = texture2D (u_texture, v_texCoord ); \ n \ 15 float gray = dot (col. rgb, vec3 (0.29 9, 0.587, 0.114); \ n \ 16 gl_FragColor = vec4 (gray, col. a); \ n \ 17} \ n \ 18 "; copy Code 2, ccShaders. add 1 extern CC_DLL const GLchar * ccPositionTextureGray_frag; 3. ccShaders. add 1 const GLchar * ccPositionTextureGray_frag = 2 # include "ccShader_PositionTextureGray_frag.h" 4. CCGLProgram to cpp. add 1 # define kCCShader_PositionTextureGray "ShaderPositionTextureGray" 5. CCShaderCache. add Enumeration type copy code 1 in cpp Um {2 blocks, 3 blocks, 4 kCCShaderType_PositionColor, 5 kCCShaderType_PositionTexture, 6 blocks, 7 blocks, 8 blocks, 9 blocks, 10 kCCShaderType_ControlSwitch, 11 12 kCCShaderType_MAX, 13 14 blocks Ray, 15}; copy the code CCShaderCache: loaddefashadshaders () to add 1 // Position Texture Gray shader2 p = new CCGLProgram (); 3 loaddefashshader (p, kCCShaderType_PositionTextureGray ); 4 5 m_pPrograms-> setObject (p, kCCShader_PositionTextureGray); 6 p-> release (); CCShaderCache: reloaddefashadshaders () add 1 // 2 // Position Texture Gray shader3 // 4 p = programForKey (kCCShader_PositionTextureGray); 5 p-> reset (); 6 loadDefau LtShader (p, strong); copy code 1 case when: 2 p-> commit (ccPositionTextureColor_vert, ccPositionTextureGray_frag) added to CCShaderCache: loaddefashshader (CCGLProgram * p, int type ); 3 4 p-> addattrib_position (kCCAttributeNamePosition, kCCVertexAttrib_Position); 5 p-> addAttribute (kCCAttributeNameColor, kCCVertexAttrib_Color); 6 p-> addAttribute (KCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords); 7 8 break; copy Code 6. Create a grayscale conversion call class (to extend other color conversions) and copy code 1 // ColorUtils. h 2 # ifndef _ COLOR_UTILS_H _ 3 # define _ COLOR_UTILS_H _ 4 5 # include "cocos2d. h "6 7 USING_NS_CC; 8 9 class ColorUtils10 {11 public: 12 ColorUtils (); 13 ~ ColorUtils (); 14 15 static void AddColorGray (CCSprite * spr); 16 static void RemoveColorGray (CCSprite * spr); 17 18 private: 19}; 20 # endif21 22/ColorUtils. cpp23 # include "ColorUtils. h "24 25 ColorUtils: ColorUtils () 26 {27 28} 29 30 ColorUtils ::~ ColorUtils () 31 {32 33} 34 35 void ColorUtils: AddColorGray (CCSprite * spr) 36 {37 spr-> setShaderProgram (CCShaderCache: sharedShaderCache () -> programForKey (kCCShader_PositionTextureGray); 38} 39 40 void ColorUtils: RemoveColorGray (CCSprite * spr) 41 {42 spr-> setShaderProgram (CCShaderCache: sharedShaderCache () -> programForKey (kCCShader_PositionTextureColor); 43}

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.