Computergrafik

GLSL Phong Shader

GLSL Simple Shader | | GLSL Tone Shader

Phong Diffuse Shading mit GLSL:

Vertex Shader:

varying vec3 normal;
varying vec3 vertex_to_light_vector;

void main()
{
   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

   normal = gl_NormalMatrix * gl_Normal;
   vertex_to_light_vector = vec3(gl_LightSource[0].position –
                                 gl_ModelViewMatrix * gl_Vertex);
}
PhongShader

Fragment Shader:

varying vec3 normal;
varying vec3 vertex_to_light_vector;

void main()
{
   const vec4 ambientColor = vec4(0.1, 0.0, 0.0, 1.0);
   const vec4 diffuseColor = vec4(1.0, 0.0, 0.0, 1.0);

   vec3 normalized_normal = normalize(normal); // renormalization

   float diffuseTerm = clamp(dot(normal, vertex_to_light_vector), 0.0, 1.0);

   gl_FragColor = ambientColor + diffuseColor * diffuseTerm;
}

Darstellung rechts zusätzlich mit spekularem Highlight.

GLSL Simple Shader | | GLSL Tone Shader

Options: