在cocos2d-x中有两个类CCGLProgram和CCShaderCache.CCGLProgram类来处理着色器相干操作,对当前绘图程序进行了封装.CCShaderCache通过CCGLProgram来完成对shaders的缓存和管理.这两个类的功能在项目中常常用到,比照实现图像由正常色彩变成灰色(好处是减少资源图),还有在刀塔传奇中用令人物冰封,石化等效果都是用shader.
这里对这两个类的功能不作深入讨论,由于本人也是刚刚接触,这里主要是介绍在Quick-cocos2dx-lua -v2.2.6版本中如何配置和使用这个功能。
由于Quick-cocos2dx-lua -v2.2.6版本中没有在tolua中导出CCGLProgram,所以lua中不能使用。
接下来就是生成可使用CCGLProgram的framework和player
第1:找到CCGLProgram.h和CCGLProgram.cpp,:
在CCGLProgram.h中声明以下两个函数:
static CCGLProgram* createWithByteArrays(const char* vShaderByteArray, const char* fShaderByteArray);
static CCGLProgram* createWithFilenames(const char* vShaderFilename, const char* fShaderFilename);
在CCGLProgram.cpp实现这两个方法(这两个方法可以参考v3.5):
CCGLProgram* CCGLProgram::createWithByteArrays(const char* vShaderByteArray, const char* fShaderByteArray)
{
CCGLProgram* ret = new CCGLProgram();
if(ret && ret->initWithVertexShaderByteArray(vShaderByteArray, fShaderByteArray)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
CCGLProgram* CCGLProgram::createWithFilenames(const char* vShaderFilename, const char* fShaderFilename)
{
auto ret = new CCGLProgram();
if(ret && ret->initWithVertexShaderFilename(vShaderFilename, fShaderFilename)) {
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
注掉:CCGLProgram构造方法中两行:
// there is no need to delete the shaders. They should have been already deleted.
//CCAssert(m_uVertShader == 0, "Vertex Shaders should have been already deleted");
//CCAssert(m_uFragShader == 0, "Fragment Shaders should have been already deleted");
第2:修改framework
进入libluabindingcocos2dxdraw_nodes,新建文件CCGLProgram.tolua,内容以下:
class CCGLProgram : public CCObject
{
static CCGLProgram* createWithByteArrays(const char* vShaderByteArray, const char* fShaderByteArray);
static CCGLProgram* createWithFilenames(const char* vShaderFilename, const char* fShaderFilename);
void addAttribute(const char* attributeName, GLuint index);
bool link();
void use();
void updateUniforms();
}
注:这里是导出你所需要的公共方法,供在lua中调用。
修改:libluabindingcocos2dxCocos2d.tolua,
增加 $pfile "cocos2dx/draw_nodes/CCGLProgram.tolua"
接下履行tolua指令,由quick cocos framework提供:
履行:libluabindinguild.bat,和incompile_luabinding.bat
履行完命令后检查tolua绑定:
libcocos2d-xscriptingluacocos2dx_supportLuaCocos2d.cpp是不是生成CCGLProgram相干绑定函数
生成:framework_precompiled.zip
履行bat:binmake_framework_package.bat会重生生成framework_precompiled.zip
第3:生新编译生成player.exe
由于修改了cpp(LuaCocos2d.cpp,CCGLProgram.cpp),需要重新编译player.
用vs2013直接打开playerproj.win32player.vcxproj编译就能够了。
第4:简单利用shader:
local pProgram = CCGLProgram:createWithFilenames("res/shader/IceShader.vsh","res/shader/IceShader.fsh")
self.sp = display.newSprite("house.png"):addTo(self)
self.sp:setPosition(300, 100)
pProgram:addAttribute("a_position", 0) --对应vs里面的顶点坐标
pProgram:addAttribute("a_color", 1) --对应vs里面的顶点色彩
pProgram:addAttribute("a_texCoord", 2)--对应vs里面的顶点纹理坐标
pProgram:link() -- 由于绑定了属性,所以需要link1下,否则vs没法辨认属性
pProgram:updateUniforms() -- 绑定了纹理贴图
self.sp:setShaderProgram(pProgram)
注:
IceShader.fsh内容:
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
uniform sampler2D u_texture;
void main()
{
vec4 color1 = texture2D(u_texture, v_texCoord) * v_fragmentColor;
float brightness = (color1.r + color1.g + color1.b) * (1. / 3.);
float gray = (1.5)*brightness;
color1 = vec4(gray, gray, gray, color1.a)*vec4(0.8,1.2,1.5,1);
gl_FragColor =color1;
}
IceShader.vsh文件内容:
attribute vec4 a_position;
attribute vec2 a_texCoord;
attribute vec4 a_color;
#ifdef GL_ES
varying lowp vec4 v_fragmentColor;
varying mediump vec2 v_texCoord;
#else
varying vec4 v_fragmentColor;
varying vec2 v_texCoord;
#endif
void main()
{
gl_Position = CC_MVPMatrix * a_position;
v_fragmentColor = a_color;
v_texCoord = a_texCoord;
}
程序运行结果: