Glsl Study Notes-6.2 vertex shader

Source: Internet
Author: User
A simple vertex-by-vertex illumination model. The illumination effect consists of scattered light and reflected light.

The program flow converts the vertex and normal to the current eye coordinate system.
Vec3 ecposition = vec3 (gl_modelviewmatrix * gl_vertex );
Vec3 tnorm = normalize (gl_normalmatrix * gl_normal );

Obtain vertex-> light source vector and vertex-> observation point vector
Vec3 lightvec = normalize (lightposition-ecposition );
Vec3 viewvec = normalize (-ecposition );

Calculate the reflected light Vector Based on the incident light Vector
Vec3 reflectvec = reflect (-lightvec, tnorm );

Assuming that the diffuse light intensity is cosine distributed with the angle between the incident light and the normal line,
Then the diffuse reflection illumination intensity can be approximately
Float diffuse = max (dot (lightvec, tnorm), 0.0 );

Assuming that the intensity of the reflected light is cosine distributed with the angle between the reflected light and the observed direction,
Then, the light intensity of the mirror reflection can be approximately
Spec = max (dot (reflectvec, viewvec), 0.0 );
Then enhance the mirror reflection Aggregation
Spec = POW (SPEC 16.0 );

Finally, the light intensity of the vertex is obtained through the mixed scattered light and the light intensity of the mirror.
Lightintensity = diffusecontribution * diffuse + specularcontribution * spec;

Program list

Uniform vec3 lightposition;

Const float specularcontribution = 0.3;

Constfloat diffusecontribution = 1.0-specularcontribution;

Varying float lightintensity;

Varying vec2 mcposition;

Void main (void ){
Vec3 ecposition = vec3 (gl_modelviewmatrix * gl_vertex );

Vec3 tnorm = normalize (gl_normalmatrix * gl_normal );

Vec3 lightvec = normalize (lightposition-ecposition );

Vec3 reflectvec = reflect (-lightvec, tnorm );

Vec3 viewvec = normalize (-ecposition );

Float diffuse = max (dot (lightvec, tnorm), 0.0 );

Float spec = 0.0;

If (diffuse> 0.0 ){
Spec = max (dot (reflectvec, viewvec), 0.0 );
Spec = POW (SPEC 16.0 );
}

Lightintensity = diffusecontribution * diffuse + specularcontribution * spec;

Mcposition = gl_vertex.xy;

Gl_position = ftransform ();
}

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.