Three variable types of OpenGL ES (uniform, attribute, and varying)

Source: Internet
Author: User
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)

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.