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 ();
}