1.uniform variables
The uniform variable is a variable that the external application program passes to (vertex and fragment) shader. So it was application through
The function gluniform** () function is assigned a value. Inside the shader program (vertex and fragment), the uniform variable is like the C language.
Constant (const), it cannot be modified by the shader program. (shader can only be used, cannot be changed)
If the uniform variable is declared exactly the same way between vertex and fragment, it can be used in both vertex and fragment shares.
(equivalent to a global variable shared by vertex and fragment shader)
Uniform variables are generally used to represent: transformation matrices, materials, lighting parameters and color information.
Here is an example:
// projection + view matrix uniform mat4 Viewmatrix; // View matrix uniform vec3 lightposition; // Light source Position
2.attribute variables
The attribute variable is a variable that can be used only in vertex shader. (it cannot declare the attribute variable in fragment shader,
Can not be used in fragment shader)
The attribute variable is generally used to represent some vertex data, such as vertex coordinates, normals, texture coordinates, vertex colors, and so on.
In application, the function glbindattriblocation () is used to bind the position of each attribute variable, and then the function
Glvertexattribpointer () assigns a value to each attribute variable.
Here is an example:
uniform mat4 u_matviewprojection;attribute vec4 a_position;attribute vec2 a_texcoord0;varying vec2 V_texcoord; void Main (void) { = u_matviewprojection * a_position; = a_texcoord0;}
3.varying variables
The varying variable is used for data transfer between vertex and fragment shader. General vertex shader Modify the value of the varying variable,
Then fragment shader uses the value of the varying variable. So the varying variable between vertex and fragment shader the sound
The state must be consistent. Application cannot use this variable.
Here is an example:
//Vertex Shaderuniform mat4 u_matviewprojection;attribute vec4 a_position;attribute vec2 a_texcoord0;varying vec2 V_texcoord;//Varying in vertex shadervoidMainvoid) {gl_position= U_matviewprojection *a_position; V_texcoord=a_texcoord0;}//Fragment ShaderPrecision Mediumpfloat; varying vec2 v_texcoord;//Varying in fragment shaderuniform sampler2d s_basemap;uniform sampler2d s_lightmap;voidMain () {VEC4 basecolor; VEC4 Lightcolor; Basecolor=texture2d (S_basemap, V_texcoord); Lightcolor=texture2d (S_lightmap, V_texcoord); Gl_fragcolor= Basecolor * (Lightcolor +0.25);}
"Reprint" OpenGL ES three types of modified uniform attribute varying