Example 5 Directional Lighting Slide Set 8 34







- Slides: 7
Example 5: Directional Lighting Slide Set 8. 34
Overview • Directional light is effectively at infinite distance so that the light falls in the same direction on all surfaces • The sun shining on the earth is very close to directional light • Send new values to uniform variables • Object values • Scene values • Compute lighting in fragment shader • Computation uses positions and vectors in view coordinates • So, camera is at the origin, pointed along the positive z axis 2
Object Values • the_color • The material color, the basic color of the surface • shininess • How sharp the specular reflection is • specular_strength • Amount to scale the specular reflection value to make it stronger 3
Scene Values • Ambient light color • Used in example 2, ambient light • The light color • The basic color of the light source • Light direction • A vector pointing from the light source • Transformed to view coordinates 4
Computing Diffuse Contribution • Main factor: cosine of angle between vector from fragment to light and the normal • Dot product of unit vectors • If the value is negative, the surface is pointing away from the light, so set the factor to 0 • Multiply by the light color 5
Specular Contribution • Cosine between the normal and the half vector • The half vector is the average of two vectors: from the fragment to the light; from the fragment to the camera • Dot product with the normal • If the diffuse factor is 0, set the specular contribution to 0 and skip the rest • Otherwise, raise the specular factor to the power of the shininess • Affects the size of the shiny ‘spot’ • Multiply by the light color and the specular strength 6
Add It Up • Add the ambient, specular, and diffuse contributions • Use that, with alpha set to 1, as the fragment color 7