Shader three types of variables (Uniform,attribute and varying)

Source: Internet
Author: User

The uniform variable is declared in exactly the same way as the vertex and fragment, and it can be shared with vertex and fragment. (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.   The following are examples:   uniform mat4 Viewprojmatrix; Projection + view matrix uniform mat4 Viewmatrix; View matrix uniform vec3 lightposition; Light source position     2.attribute variable attribute variable is a variable that can be used only in vertex shader. (It cannot declare attribute variable in fragment shader, nor can it be used in fragment shader)   generally uses attribute variables 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, Each attribute variable is then assigned a value using the function Glvertexattribpointer ().   The following are examples:   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 VA The rying variable is used for data transfer between vertex and fragment shader. Generally vertex shader modifies the value of the varying variable, and then fragment shader uses the value of the varying variable. Therefore, the declaration of the varying variable between vertex and fragment shader must be consistent. Application cannot use this variable.   Below 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_ligh TMap, V_texcoord); Gl_fragcolor = Basecolor * (Lightcolor + 0.25); }

Shader three types of variables (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.