Cocos2d-x project porting to WP8 Series 9: using custom shader

Source: Internet
Author: User

 

Sometimes want to get some special rendering effects such as grayscale, you have to use custom shader, some background knowledge about shader, Google, list two articles about shader in cocos2dx http://blog.csdn.net/while0/article/details/9666829 http://blog.sina.com.cn/s/blog_aa01f7030101mdom.html

 

Cocos2dx on the WP8 platform does not know whether OpenGL needs to be converted to d3d during rendering or other reasons. It cannot compile the link shader at runtime, cocos2dx is to compile the relevant shader into a specific machine code and store it in precompiledshaders. in the H file, the pre-generated sha1 code points to different shader and is directly obtained at runtime. For details about this part, refer to: http://www.ispinel.com/2014/07/03/12393 /. Now the work that needs to be done is, given the vertex shader and the chip element shader, how to generate the machine code, this Part references the http://cn.cocos2d-x.org/tutorial/show? Id = 1274. This document introduces the second type. Use winrtcompiler.exe to generate the shader machine code. The following describes how to use a custom grayscale image shader.

 

Vertex shader file -- myshader. Vert:

 1 uniform mat4 CC_PMatrix; 2 uniform mat4 CC_MVMatrix; 3 uniform mat4 CC_MVPMatrix; 4 uniform vec4 CC_Time; 5 uniform vec4 CC_SinTime; 6 uniform vec4 CC_CosTime; 7 uniform vec4 CC_Random01; 8 attribute vec4 a_position;                             9 attribute vec2 a_texCoord;                            10 attribute vec4 a_color;                                11                                                     12 #ifdef GL_ES                                        13 varying lowp vec4 v_fragmentColor;                    14 varying mediump vec2 v_texCoord;                    15 #else                                                16 varying vec4 v_fragmentColor;                        17 varying vec2 v_texCoord;                            18 #endif19 20 void main()                                            21 {                                                    22     gl_Position = CC_MVPMatrix * a_position;        23     v_fragmentColor = a_color;                        24     v_texCoord = a_texCoord;                        25 }


Slice-based shader file-myshader. Frag File

 1 #ifdef GL_ES                                 2 precision lowp float;                         3 #endif                                         4  5 varying vec4 v_fragmentColor;                 6 varying vec2 v_texCoord;                     7 uniform sampler2D CC_Texture0;                 8  9 void main()                                10 {                                            11 vec4 texColor = v_fragmentColor * texture2D(CC_Texture0, v_texCoord);            12 float gray = dot(texColor.rgb, vec3(0.299,0.587,0.114));13 gl_FragColor  = vec4(gray, gray, gray, texColor.a);14 }


Download winrtcompiler.exe

 

Run the command line to generate a machine code file through the top-point shaderfile, the original shaderfile, And the winrtcompiler.exe file. Copy the winrtcompiler.exe, vertex shader, and multipart shader files to the unified folder, run the CD command to enter the folder, and: winrtcompiler.exe-O = shader_wp8.h-P = WP8-A = gprogram-V = myshader. vert-F = myshader. frag, the generated machine code is in the shader_wp8.h file pointed to by the-O parameter.

 

Update the machine code to the precompiledshaders. h file, including num, length, program, and programkey. The programkey is the sha1 code obtained based on the myshader. Vert file and myshader. Frag file. Last question: How can I get this sha1 code?

Can refer to the http://www.cnblogs.com/yeshanghai/p/cocos2dx_shader.html 1 ~ , But you need to change it.

Change at: Put the above myshader. vert and myshader. the Frag file is transformed by adding double quotation marks, semicolons, and \ n \ to each line (except the last semicolon, this is because it needs to be dynamically loaded and compiled at runtime.

The modification in other steps is that the original text only contains the slice shader, and the related information of the vertex shader needs to be supplemented.

Finally, in ccprecompiledshaders. bool syntax in CPP: loadprogram (gluint program, const glchar * vshaderbytearray, const glchar * fshaderbytearray) STD: String id = computehash (vshaderbytearray, fshaderbytearray ); the breakpoint before the Code. This ID is the sha1 value calculated by the vertex shader file and the multipart shader file.

 

Cocos2d-x project porting to WP8 Series 9: using custom shader

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.