您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx shader实现灰度图android后台切换回来导致图像偏移的问题

2014-10-29 19:48 295 查看
转自:http://www.tuicool.com/articles/U3URRrI

项目中经常会遇到将一张图像处理成灰色的需求,为了节省资源,一般不会让美术再做一套同样的灰度图,通常会通过代码处理让图片变灰。网上也有很多用shader处理图片变灰的方法,这些方法确实也实现了让图片变灰的需求,但是android平台从后台切换回来的时候,shader被释放,导致图片位置错乱。关键在于从android后台切换回来的时候需要重新加载shader。我们看一下cocos2dx原生的shader处理方式,我们从cocos2dx库中找到CCShaderCache.cpp,发现这个类有个reloadDefaultShaders方法,这个方法是重新加载shader,但它在ios平台没被调用,而在android平台,jni/main.cpp中被调用了,我们看一下main.cpp:

void Java_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 = new AppDelegate();
    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();
这一行代码就是重新加载shader。好了,问题找到了,只要我们在这个地方重新init一下shader就ok了。我将网上一种可行的处理方式粘贴到下面,给大家参考:

1、写好一个图像变灰的shader程序,并放入到cocos2dx引擎下面的shader文件夹中。

//ccShader_PositionTextureGray_frag.h
"                                           \n\
#ifdef GL_ES                                \n\
precision mediump float;                    \n\
#endif                                      \n\
\n\
uniform sampler2D u_texture;                \n\
varying vec2 v_texCoord;                    \n\
varying vec4 v_fragmentColor;               \n\
\n\
void main(void)                             \n\
{                                           \n\
// Convert to greyscale using NTSC weightings               \n\
vec4 col = texture2D(u_texture, v_texCoord);                \n\
float grey = dot(col.rgb, vec3(0.299, 0.587, 0.114));       \n\
gl_FragColor = vec4(grey, grey, grey, col.a);               \n\
}                                           \n\
";


2、 ccShaders.h中添加

extern CC_DLL const GLchar * ccPositionTextureGray_frag;


3、ccShaders.cpp中添加

const GLchar * ccPositionTextureGray_frag =
#include "ccShader_PositionTextureGray_frag.h"


4、CCGLProgram.h中添加

#define kCCShader_PositionTextureGray  "ShaderPositionTextureGray"


5、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,
};


CCShaderCache::loadDefaultShaders()中添加

// Position Texture Gray shader
p = new CCGLProgram();
loadDefaultShader(p, kCCShaderType_PositionTextureGray);

m_pPrograms->setObject(p, kCCShader_PositionTextureGray);
p->release();


CCShaderCache::reloadDefaultShaders()中添加

//
// Position Texture Gray shader
//
p = programForKey(kCCShader_PositionTextureGray);
p->reset();
loadDefaultShader(p, kCCShaderType_PositionTextureGray);


CCShaderCache::loadDefaultShader(CCGLProgram *p, int type)中添加

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、新建一个灰度转换调用类(以便扩展其他的颜色转换)

//ColorUtils.h
#ifndef __COLOR_UTILS_H__
#define __COLOR_UTILS_H__

#include "cocos2d.h"

USING_NS_CC;

class ColorUtils
{
public:
ColorUtils();
~ColorUtils();

static void AddColorGray(CCSprite * spr);
static void RemoveColorGray(CCSprite * spr);

private:
};
#endif

//ColorUtils.cpp
#include "ColorUtils.h"

ColorUtils::ColorUtils()
{

}

ColorUtils::~ColorUtils()
{

}

void ColorUtils::AddColorGray(CCSprite * spr)
{
spr->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureGray));
}

void ColorUtils::RemoveColorGray(CCSprite * spr)
{
spr->setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: