Use shader in cocos2dx (3.X)
A shader's Basic Concept 1 What is a shader
The shader is a painter that is used to render 3D images.
With the shader, you can write your own graphics card rendering algorithm to make the screen more beautiful and realistic.
2. shader Classification
There are two types of shader: vertex shader (3D graphics are composed of triangles, and vertex shader is used to calculate vertex positions and prepare for post-pixel rendering ),
The other is pixel shader, which is a series of algorithms that calculate illumination and color in pixels.
3. shader Language
Several different graphic APIs have their own shader languages:
In DirectX, vertex shader is called vertex shader, and pixel shader is called pixel shader;
In OpenGL, vertex shader is also called vertex shader, but pixel shader is called fragment shader.
In addition, nVidia, the graphics card chip manufacturer, also released the CG graphics card programming language and also supported shader.
Two shader program 1 Language glsl
Glsl (OpenGL Shading Language) is a Language used for coloring and programming in OpenGL.
2. vertex shader
// Vert. vsh
attribute vec4 a_position;attribute vec4 a_color;varying vec4 v_fragmentColor;void main(){ gl_Position = CC_MVPMatrix * a_position; v_fragmentColor = a_color;}
(1) Each Shader program has a main function;
(2) There are two types of variables:
Attribute is transmitted from the outside;
Varying variables are used to transmit data between vertex shader and fragment shader;
Each vertex has an attribute and a varying attribute. The fixed-point shader acts on each fixed point, and the number of vertices will be executed several times.
3. segment coloring er
// Frag. HCG
varying vec4 v_fragmentColor;void main(){ gl_FragColor = v_fragmentColor;}
(1) main function;
(2) varying variables are used to transmit data between vertex shader and fragment shader;
(3) The gl_FragColor system has built-in variables to define the color of the pixel that is finally drawn on the screen;
Three Programs call to create a cocos project, put the above two files under the Resource/shaders folder, modify the Code as follows:
//. H
# Ifndef _ HELLOWORLD_SCENE_H __# define _ HELLOWORLD_SCENE_H __# include cocos2d. hclass HelloWorld: public cocos2d: Layer {public: // there's no 'id' in cpp, so we recommend returning the class instance pointer static cocos2d :: scene * createScene (); // Here's a difference. method 'init 'in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone virtual bool init (); // implement the static create () method manually CREATE_FUNC (HelloWorld ); // Add the following code: virtual void draw (cocos2d: Renderer * renderer, const cocos2d: Mat4 & transform, uint32_t flags) override; void onDraw (const cocos2d: Mat4 & transform, uint32_t flags); private: cocos2d: CustomCommand _ customCommand;}; # endif // _ HELLOWORLD_SCENE_H __
//. Cpp
# Include HelloWorldScene. hUSING_NS_CC; Scene * HelloWorld: createScene () {// 'Scene 'is an autorelease object auto scene = scene: create (); // 'player' is an autorelease object auto layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on init you need to initialize your instancebool HelloWorld: init () {///////////////////////////// /// 1. super init first if (! Layer: init () {return false;} // init shader auto glProgram = GLProgram: createWithFilenames (shaders/vert. vsh, shaders/frag. SH); auto state = GLProgramState: getOrCreateWithGLProgram (glProgram); setGLProgramState (state); return true;} void HelloWorld: draw (Renderer * renderer, const Mat4 & transform, uint32_t flags) {_ customCommand. init (_ globalZOrder, transform, flags); _ customCommand. func = CC_CALLBACK_0 (HelloWorld: onDraw, this, transform, flags); renderer-> addCommand (& _ customCommand);} void HelloWorld: onDraw (const Mat4 & transform, uint32_t flags) {auto glProgram = getGLProgram (); glProgram-> use (); glProgram-> setUniformsForBuiltins (); // draw the triangle auto size = Director: getInstance () -> getWinSize (); // coordinates of the three vertices float vertercies [] = {0, 0, size. width, 0, size. width/2, size. height}; // RGBA float color [] = {0, 1, 0, 1, 1, 0, 0, 1, 1}; // activate vertex attribute GL: enableVertexAttribs with the name position and color (GL: VERTEX_ATTRIB_FLAG_POSITION | GL: VERTEX_ATTRIB_FLAG_COLOR ); // specify the Data Source Catalog for position and color respectively (GLProgram: VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, vertercies); Catalog (GLProgram: VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, 0, color); // draw the triangle, the so-called draw call refers to this function call glDrawArrays (GL_TRIANGLES, 0, 3); // notify the renderer of the cocos2d-x, call the OpenGL command CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES (1, 3) when appropriate; // if an error occurs, you can use this function to obtain the error message CHECK_GL_ERROR_DEBUG ();}
The effect is as follows: