cocos2dx在wp上使用自定义shader

实践cocos2dx 2.x版本wp上增加自定义shader

根据cocos2dx 的官方文档http://www.cocos2d-x.org/wiki/How_to_update_wp8_shader

(吐个槽:不知道为什么cocos2dx团队做事总是做一半,实际上直接使用angle 是不能用的,需要修改)

1,首先从github上下载一个angle工程https://github.com/google/angle,

2.打开src\winrtcompiler\winrtcompiler_vs2013.sln 工程文件

3,修改main.cpp

enum
{
	VERTEX_ATTRIB_POSITION,
	VERTEX_ATTRIB_COLOR,
	VERTEX_ATTRIB_TEX_COORD,
};
void bindPredefinedVertexAttribs(GLuint program)
{
	static const struct {
		const char *attributeName;
		int location;
	} attribute_locations[] =
	{
		{ "a_position", VERTEX_ATTRIB_POSITION },
		{ "a_color", VERTEX_ATTRIB_COLOR },
		{ "a_texCoord", VERTEX_ATTRIB_TEX_COORD },
	};

	const int size = sizeof(attribute_locations) / sizeof(attribute_locations[0]);

	for (int i = 0; i<size; i++) {
		glBindAttribLocation(program, attribute_locations[i].location, attribute_locations[i].attributeName);
	}
}

上述vertex属性定义可以在cocos源代码中找到。name和id,也是未来保持跟cocos一致

1 [Platform::MTAThread]
2 int __cdecl main(int argc, char* argv[])

之前即可,因为要在main中调用

4.在main函数中的glLinkProgram(program);之前调用bindPredefinedVertexAttribs(program);来实现绑定预定义的vertex 属性

另外发现在cocos加载编译过的shader还需要知道二进制的长度,就是需要修改main.cpp,增加导出时候的信息,修改后main中部分代码如下:

if(outputToHeaderFile)
{
FILE *fp = fopen(outputFile.c_str(), "w");
fprintf(fp, "unsigned char %s[] = {\n", variableName);
fprintf(fp, "%3i, ", binary[0]);
for(int i = 1; i < linkStatus - 1; ++i)
{
if(i % 8 == 0)
fprintf(fp, "\n");
fprintf(fp, "%3i, ", binary[i]);
}
if((linkStatus - 1) % 8 == 0)
fprintf(fp, "\n");
fprintf(fp, "%3i\n};\n", binary[linkStatus - 1]);
fprintf(fp, "unsigned int %s_len = %d;\n", variableName, linkStatus);
fclose(fp);
}

5.编译exe

注意:在编译shader是需要使用cocos增加了shader头部的代码,

例如:fragment shader

 1 precision mediump float;
 2
 3 //CC INCLUDES END
 4
 5
 6 /*
 7  * cocos2d for iPhone: http://www.cocos2d-iphone.org
 8  *
 9  * Copyright (c) 2011 Ricardo Quesada
10  * Copyright (c) 2012 Zynga Inc.
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in
20  * all copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28  * THE SOFTWARE.
29  */
30
31
32
33
34
35
36
37 varying vec4 v_fragmentColor;
38
39 varying vec2 v_texCoord;
40
41 uniform sampler2D CC_Texture0;
42
43
44
45 void main()
46
47 {
48
49     vec4 color = texture2D(CC_Texture0, v_texCoord);
50
51     float grey = color.r * 0.299 + color.g * 0.587 + color.b * 0.114;
52
53     gl_FragColor = vec4(grey*1.13, grey*1.13, grey*1.13,color.a);
54
55
56 }                                            

vertex shader

 1 precision highp float;
 2
 3 uniform mat4 CC_PMatrix;
 4
 5 uniform mat4 CC_MVMatrix;
 6
 7 uniform mat4 CC_MVPMatrix;
 8
 9 uniform vec4 CC_Time;
10
11 uniform vec4 CC_SinTime;
12
13 uniform vec4 CC_CosTime;
14
15 uniform vec4 CC_Random01;
16
17 //CC INCLUDES END
18
19
20 /*
21  * cocos2d for iPhone: http://www.cocos2d-iphone.org
22  *
23  * Copyright (c) 2011 Ricardo Quesada
24  * Copyright (c) 2012 Zynga Inc.
25  *
26  * Permission is hereby granted, free of charge, to any person obtaining a copy
27  * of this software and associated documentation files (the "Software"), to deal
28  * in the Software without restriction, including without limitation the rights
29  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
30  * copies of the Software, and to permit persons to whom the Software is
31  * furnished to do so, subject to the following conditions:
32  *
33  * The above copyright notice and this permission notice shall be included in
34  * all copies or substantial portions of the Software.
35  *
36  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
37  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
38  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
39  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
40  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
41  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
42  * THE SOFTWARE.
43  */
44
45
46
47 attribute vec4 a_position;
48
49 attribute vec2 a_texCoord;
50
51 attribute vec4 a_color;
52
53
54
55 #ifdef GL_ES
56
57 varying lowp vec4 v_fragmentColor;
58
59 varying mediump vec2 v_texCoord;
60
61 #else
62
63 varying vec4 v_fragmentColor;
64
65 varying vec2 v_texCoord;
66
67 #endif
68
69
70
71 void main()
72
73 {
74
75     gl_Position = CC_MVPMatrix * a_position;
76
77     v_fragmentColor = a_color;
78
79     v_texCoord = a_texCoord;
80
81 }                                                    

vertex shader

命令行:winrtcompiler.exe -p=wp8 -o=shader_gray_wp.h -a=g_xc_gray_Program -v=vert_src.h -f=frag_src.h

可以查看编译出来文件:shader_gray_wp.h

unsigned char g_xc_gray_Program[] = {
166, 147,   0,   0, 142,   9,   2,   1,
  0, 128,   0,   0,  82, 139,   0,   0,
 10,   0,   0,   0,  97,  95, 112, 111,
115, 105, 116, 105, 111, 110,   1,   0,
  0,   0,  82, 139,   0,   0,   7,   0,
  0,   0,  97,  95,  99, 111, 108, 111,
114,   0,   0,   0,   0,  80, 139,   0,
  0,  10,   0,   0,   0,  97,  95, 116,
101, 120,  67, 111, 111, 114, 100,   2,
  0,   0,   0,   0,   0,   0,   0,   0,
  0,   0,   0, 255, 255, 255, 255,   0,
  0,   0,   0,   0,   0,   0,   0, 255,
255, 255, 255,   0,   0,   0,   0,   0,
  0,   0,   0, 255, 255, 255, 255,   0,
....
};
unsigned int g_xc_gray_Program_len = 2954;

这样的shader才能编译后给cocos2dx使用,

使用:

我的做法是修改加载shader的代码,来加载编译出来的二进制

CCGLProgram增加bool initWithVertexShaderByteArray(const GLchar* vShaderByteArray, GLint buffsize); 可以使用导出的文件的两个变量来加载一个shader

 1 bool CCGLProgram::initWithPrecompiledProgramByteArray(const GLchar* vShaderByteArray, GLint buffsize)
 2 {
 3     bool haveProgram = false;
 4
 5     m_uProgram = glCreateProgram();
 6     CHECK_GL_ERROR_DEBUG();
 7
 8     m_uVertShader = m_uFragShader = 0;
 9
10     haveProgram = CCPrecompiledShaders::sharedPrecompiledShaders()->loadProgram(m_uProgram, (const GLvoid*)vShaderByteArray, buffsize);
11
12     CHECK_GL_ERROR_DEBUG();
13     m_pHashForUniforms = NULL;
14
15     CHECK_GL_ERROR_DEBUG();
16
17     return haveProgram;
18 }

initWithPrecompiledProgramByteArray

在ccshaderCache.cpp中loadDefaultShaders()中增加

 1 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
 2     CCGLProgram* pProgram = new CCGLProgram();
 3     #include "shader_xc_wp.h"
 4     pProgram->initWithVertexShaderByteArray((const GLchar*)g_xc_gray_Program, g_xc_gray_Program_len);
 5
 6     pProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
 7     pProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
 8     pProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
 9     pProgram->link();
10     pProgram->updateUniforms();
11
12
13     m_pPrograms->setObject(pProgram, "greysprite");
14
15     pProgram->release();
16 #endif

add shader

在reloadDefaultShaders中增加

 1 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
 2     CCGLProgram* pProgram = programForKey("greysprite");
 3     pProgram->reset();
 4     #include "shader_xc_wp.h"
 5     pProgram->initWithVertexShaderByteArray((const GLchar*)g_xc_gray_Program, g_xc_gray_Program_len);
 6
 7     pProgram->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
 8     pProgram->addAttribute(kCCAttributeNameColor, kCCVertexAttrib_Color);
 9     pProgram->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
10     pProgram->link();
11     pProgram->updateUniforms();
12     CHECK_GL_ERROR_DEBUG();
13 #endif

reload shader

这样就可以使用了,当然要把.h拷贝到shader目录或者能包含到的目录中

happy coding!

时间: 2024-10-07 01:43:56

cocos2dx在wp上使用自定义shader的相关文章

Cocos2d-x项目移植到WP8系列之九:使用自定义shader

有时候想得到一些例如灰度图等特殊的渲染效果,就得用到自定义shader,关于shader的一些背景知识,自行谷歌,列出两篇cocos2dx里介绍shader的相关文章 http://blog.csdn.net/while0/article/details/9666829     http://blog.sina.com.cn/s/blog_aa01f7030101mdom.html cocos2dx在wp8平台不知道是不是在渲染时OpenGL要转成D3D的原因还是其他原因,不能在运行时编译链接s

【转】cocos2d-x游戏开发(十四)用shader使图片背景透明

转自:http://blog.csdn.net/dawn_moon/article/details/8631783 好吧,终于抽时间写这篇文章了. 手头上有很多人物行走图,技能特效图等,但这些图都有个纯黑色背景,怎么样将内容显示出来,让背景透明呢?前段时间搞了一下,感谢群里的童鞋们,提供了思路和方法. 这里用shader处理了像素,使黑色背景透明,直接上代码 ShaderSprite.h 1 #ifndef __TestShader__ShaderSprite__ 2 #define __Tes

升级IOS8游戏上传自定义头像功能失效的问题

为了支持arm64,之前已经折腾了很久,昨晚打包准备提交苹果审核时,测试那边的同事反馈说游戏上传自定义头像功能不可用了. 游戏上传自定义功能的简介:卡牌游戏最初是<比武招亲>中有一个充VIP之后就可使用了上传自定义功能的特权,我们的游戏就"复制"了该功能.   具体实现就是点击游戏内换自定义头像的按钮后,调用不同平台相应的方法,获取用户选择的图片数据,然后将图片裁剪再传给后台保存至特定的目录下.   测试设备是ipad air2,系统版本IOS 8.0.1,点击游戏内的按钮

WordPress 后台上传自定义网站Logo

需求: 众所周知一般网站的logo都是固定的所以我在做网站时也是使用的静态logo文件,但最近用wp给一个客户做的网站时,因为网站现在的logo可能会需要重新设计,所以客户提出了需要在后台可以自己修改网站logo,接收需求后就在网络上找如何解决,但找了一圈都没有找到想要的效果(都是如何修改wp的登录logo),还好找到两篇相关的文章,最后根据这两篇文章自己Codeing最终实现了功能代码: 1.在function中添加以下代码 <?php /**在function中添加以下代码 * WordPr

wp上使用AssetsManager

不知道什么原因cocos2dx v2.2.5版本在AssetsManager.h和AssetsManager.cpp中增加 #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT) && (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) 这个宏直接关闭了AssetsManager,具体原因不得而知了, 不过鄙人发现通过修改是可以使用的,因为已经有了CCPThreadWinRT.h可以实现线程, 修改之,首先去掉那个无理由的宏定

wp上一款应用的出生与死亡

起因 因为自己买了个wp手机,所以对于微软的这个wp系统还是非常喜欢,无奈软件质量不高,过年前便买了个wp的开发者帐号,不是很贵,还想着为wp的生态系统做点贡献.无奈工作繁忙,一直没有机会去做.但是自己总在想着至少做一个简单点的,让我熟悉下wp的开发流程.正好,这段时间是世界杯,大概搜索了下,已有的应用,惨不忍睹,用户体验极其不佳,超级难用,于是便有了我准备做世界杯应用的念头. 准备 既然准备开始做,于是大概规划了下,需要做的功能. 主要功能: 按照三种方式查看信息 国家 时间 小组 小组积分榜

WPF技术触屏上的应用系列(二): 嵌入百度地图、API调用及结合本地数据库在地图上进行自定义标点的实现

原文:WPF技术触屏上的应用系列(二): 嵌入百度地图.API调用及结合本地数据库在地图上进行自定义标点的实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7操作系统,54寸大屏电脑电视一体机.要求有很炫的展示效果,要有一定的视觉冲击力,可触控操作.当然满足客户的要求也可以有其它途径.但鉴于咱是搞 .NET技术的,首先其冲想到的微软WPF方面,之前对WPF的了解与学习也只是停留在比较浅的层面,没有进一步深入学习与应用.所以在项目接来以后,也就赶鸭子上架了,经过

unity中使用自定义shader进行光照贴图烘培无法出现透明度的坑爹问题

最近开发中在对场景进行光照贴图烘焙时发现一个坑爹问题,在使用自定义shader的时候,shader命名中必须包含Transparent路径,否则烘焙的时候不对alpha通道进行计算,烘焙出来都是狗皮膏药 比如一个shader叫 Shader "xx/UnlitAlphaCutout" 要改为 Shader "xx/Transparent/UnlitAlphaCutout" 才能烘焙出正常的效果,不知道Unity做了什么黑科技,居然在烘焙的时候判断了Transpare

Microsoft Azure 上的自定义数据和 Cloud-Init

http://blog.csdn.net/azurechina/article/details/27106071 自定义数据是什么? 客户经常询问如何才能在配置Microsoft Azure 虚拟机时插入脚本或其他元数据.在其他云中,这个概念通常称为用户数据.MicrosoftAzure 中也有一项类似的功能,我们称之为自定义数据. 自定义数据随其他配置信息(例如新的主机名.用户名.密码.证书和密钥等)一起发送到 VM.这类数据经过base64 编码后传递给 Azure API.在 Window