Introduction to Computer Graphics**3.** **Shading**I-Chen Lin, Assistant ProfessorNational Chiao Tung Univ, TaiwanTextbook: E.Angel, Interactive Computer Graphics, 5 th Ed., Addison WesleyRef: Hearn and Baker, Computer Graphics, 3rd Ed., Prentice Hall

Illumination and **Shading**• Is it a ball or a plate?• What color should I set for each pixel?

Why Do We Need **Shading**?• Suppose we color a sphere model. We getsomething like• But we want

**Shading**• Why does the image of a real sphere look like• Light-material interactions cause each point to have adifferent color or shade• Need to consider– Light sources– Material properties– Location of the viewer– Surface orientation

Illumination and **Shading**• Factors that affect the “color” of apixel.– Light sources• Emittance spectrum (color)• Geometry (position and direction)• Directional attenuation– Objects’ surface properties• Reflectance spectrum (color)• Geometry (position, orientation, andmicro-structure)• Absorption

Scattering• Light strikes A– Some scattered– Some absorbed• Some of scattered light strikesB– Some scattered– Some absorbed• Some of this scattered lightstrikes A and so on

Global Effectsshadowtranslucent surfacemultiple reflection

Rendering Equation• The infinite scattering and absorption of light can bedescribed by the rendering equation– [outgoing]-[incoming] = [emitted]-[absorbed]– [outgoing] =[emitted]+[reflected](+[transmitted])– Cannot be solved in general– Ray tracing is a special case for perfectly reflecting surfaces• Rendering equation is global and includes– Shadows– Multiple scattering from object to object

Local vs Global Illumination• Correct shading requires a global calculation– Incompatible with a pipeline model which shades each polygonindependently (local rendering)• However, in computer graphics, especially real timegraphics, we are happy if things “look right”– Exist many techniques for approximating global effects

Local Illumination• Adequate for real-time graphics.• No inter-reflection, no refraction, no realisticshadow ….

Light Sources• General light sources are difficult to simulated– because we must integrate light coming from all pointson the source.

Simple Light Sources• Point source– Model with position and color– Distant source = infinite distance away (parallel)• Spotlight– Restrict light from ideal point source• Ambient light– Same amount of light everywhere in scene– Can model contribution of many sources and reflectingsurfaces

Surface Types• The smoother a surface, the more reflected light isconcentrated in the direction• A very rough surface scatters light in all directionssmooth surfacerough surface

Phong Reflection Model• A simple model that can be computed rapidly• Has three components– Ambient– Diffuse– Specular• Uses four vectors– To source l– To viewer v– Normal n– Perfect reflector rr = 2 (l · n ) n - l

Ambient Light• The result of multiple interactions between (large)light sources and the objects in the environment.• I ambient = K a ·I a

Diffuse Reflection• Light scattered equally in all directions• Reflected intensities vary with the direction of thelight.• Lambertian Surface– Perfect diffuse reflector• I diffuse = K d ·I d (n ·l)

Specular Surfaces• Most surfaces are neither ideal diffusers nor perfectlyspecular (ideal reflectors)• Incoming light being reflected in directionsconcentrated close to the direction of a perfectreflectionspecularhighlight

Modeling Specular Relections• Phong proposedI r ~ k s I cos a Φreflected shininess coefintensity incoming intensityabsorption coeff

The Shininess Coefficient• Values of a between 100 and 200 correspond tometals• Values between 5 and 10 give surface that look likeplastic

Distance Terms• Inversely proportional to the square of the distancebetween them• Add a factor of the form 1/(ad + bd +cd 2 ) to thediffuse and specular terms• The constant and linear terms soften the effect of thepoint source

Coefficients• 9 coefficients for each point light source– I dr , I dg , I db , I sr , I sg , I sb , I ar , I ag , I ab• Material properties– Nine absorbtion coefficients• k dr , k dg , k db , k sr , k sg , k sb , k ar , k ag , k ab– Shininess coefficient a

Adding up the Components• A primitive virtual world with lighting can be shadedby combining the three light components .• I = I ambient + I diffuse + I specular= k a I a + k d I d (l ·n) + k s I s (v ·r ) adiffusediffuse+speculardiffuse+ambient

Modified Phong Model• Problem: In the specular component of Phongmodel, it requires the calculation of a new reflectionvector and view vector for each vertexr = 2 (l · n ) n - l• Blinn suggested an approximation using the halfwayvector that is more efficient

Using the Halfway Angle• Replace (v ·r ) a by (n ·h ) b• b is chosen to match shineness• Note that halway angle is half of angle between rand v if vectors are coplanarHalfway vector :h = ( l + v )/ | l + v |nhlφ Θ -φθ θ Φrv f2 f

Using the Halfway Angle• Resulting model is known as the modified Phong orBlinn lighting model– Specified in OpenGL standard and most real-timeapplications

ExampleTeapots withdifferent parameters.

Computation of Vectors• l and v : specified by the application• r: computed from l and n• Determine n– OpenGL leaves determination of normal to application• Exception for GLU quadrics and Bezier surfaces

Plane Normals• Equation of plane: ax+by+cz+d = 0• Normal can be obtained byn = (p 2 -p 0 ) × (p 1 -p 0 )

Normal to Sphere• Implicit function f(x,y.z)=0• Normal given by gradient• Sphere– n = [∂f/∂x, ∂f/∂y, ∂f/∂z] T

Parametric Form• For spherex = x(u,v) = cos u sin vy = y(u,v) = cos u cos vz = z(u,v) = sin u• Tangent plane determined by vectors∂p/∂u = [∂x/∂u, ∂y/∂u, ∂z/∂u] T∂p/∂v = [∂x/∂v, ∂y/∂v, ∂z/∂v] T• Normal given by cross productn = ∂p/∂u × ∂p/∂v

Polygonal **Shading**• Practical implementation to fill color within apolygon.– Flat shading– Gouraud shading (smooth shading)– Phong shading

Flat **Shading**• Flat or constant shading.• Assume l, n, v are constant for a polygon.– **Shading** calculation: once for each polygon.

Gouraud **Shading**• Find average normal at each vertex• Apply Phong lighting model at each vertex• Interpolate vertex shades across each polygon

Gouraud **Shading**Iay11y2I1y y I y y s221sIby11y4I1y y I y y s441sIsxb1xaIax x I x x bsbsa

Phong **Shading**• Find vertex normals• Interpolate vertex normals across edges• Find shades along edges• Interpolate edge shades across polygons

Problems with Interpolated **Shading**• Polygonal silhouette• Perspective distortion• Orientation dependence• Problems at shared vertices• Unrepresentative vertex normals