1. Uniform variable
The uniform variable is the variable passed by the external application to the (vertex and fragment) shader. Therefore, it is assigned by the application through the gluniform ** () function. In the (vertex and fragment) shader program, the uniform variable is like a const in the C language. It cannot be modified by the shader program. (Shader can only be used and cannot be changed)
If the uniform variable is declared in the same way between vertex and fragment, it can be shared between vertex and fragment. (Equivalent to a global variable shared by vertex and fragment shader)
The uniform variable is generally used to represent information such as Transform Matrices, materials, illumination parameters, and colors.
The following is an example:
Uniform mat4 viewprojmatrix; // projection + view Matrix
Uniform mat4 viewmatrix; // view Matrix
Uniform vec3 lightposition; // Light Source Position
2. Attribute variable
Attribute variables are variables that can only be used in vertex shader. (It cannot declare attribute variables in fragment shader or be used in fragment shader)
Generally, attribute variables are used to represent vertex data, such as vertex coordinates, normal, texture coordinates, and vertex color.
In application, the glbindattriblocation () function is generally used to bind the location of each attribute variable, and then glvertexattribpointer () is used to assign values to each attribute variable.
The following is an example:
Uniform mat4 u_matviewprojection;
Attribute vec4 a_position;
Attribute vec2 a_texcoord0;
Varying vec2 v_texcoord;
Void main (void)
{
Gl_position = u_matviewprojection * a_position;
V_texcoord = a_texcoord0;
}
3. Varying variable
The varying variable is used for data transmission between vertex and fragment shader. Generally, vertex shader modifies the value of the varying variable, and fragment shader uses the value of this varying variable. Therefore, the varying variable Declaration must be consistent between vertex and fragment shader. Application cannot use this variable.
The following is an example:
// Vertex shader
Uniform mat4 u_matviewprojection;
Attribute vec4 a_position;
Attribute vec2 a_texcoord0;
Varying vec2 v_texcoord; // varying in vertex shader
Void main (void)
{
Gl_position = u_matviewprojection * a_position;
V_texcoord = a_texcoord0;
}
// Fragment shader
Precision mediump float;
Varying vec2 v_texcoord; // varying in fragment shader
Uniform sampler2d s_basemap;
Uniform sampler2d s_lightmap;
Void main ()
{
Vec4 basecolor;
Vec4 lightcolor;
Basecolor = texture2d (s_basemap, v_texcoord );
Lightcolor = texture2d (s_lightmap, v_texcoord );
Gl_fragcolor = basecolor * (lightcolor + 0.25 );
}
Three variable types of OpenGL ES (uniform, attribute, and varying)