Lighting and Shading

Document Sample
Lighting and Shading Powered By Docstoc
Computer Graphics

Lighting and Shading

    Chi-Cheng Lin, Winona State University

 Introduction
 Geometric Model in Lighting
 Colored Surfaces and Lights
 Shading and Graphics Pipeline
 Flat Shading and Smooth Shading


   Lighting
    Process of computing the luminous
     intensity reflected from a specific 3-D point
   Shading
    Process of assigning colors to pixels
   Shading model dictates how light is
    scattered or reflected from a surface
    We will begin with achromatic light then
     colored lights

   Two types of light sources
    Point light source
    Ambient light
   Light interacts surfaces in different ways
    Absorbed by surface
    Reflected by surface
    Transmitted into the interior
   What absorbs all of the incident
    achromatic light?
   Types of reflection of incident light
    Diffuse scattering
       Some of the incident light penetrates the
        surface slightly and is re-radiated uniformly in all
       Scattering light interacts strongly with surface
         color is affected by nature of surface material
    Specular reflections
       Incident light does not penetrate the surface
       Reflected directly from the surface
       More mirror like and highly directional
       Highlight, shiny, plastic like                         5

   Total light reflected from the surface in
    a certain direction is the sum of
    Diffuse components
    Specular components
   We calculate the size of each
    component that reaches the eye for
    each point of interest on surfaces

        Geometric Model in Lighting

   Principle vectors to find amount of light
    reaching the eye from a point P
    m: normal vector of surface at P
    v: from P to the eye
    s: from P to light source
    Angles between vectors are s           v
     calculated in the world
 Is:   intensity of light source
                Diffuse Component
 Id:   intensity of the diffused component
    Scattering is uniform in all directions
    Independent of v
        unless m  v  0, where Id = 0 (why?)
   Lambert’s law: brightness is proportional
    to the area subtended (= fraction cos())
      0: brightness varies slightly with angle
      90°: brightness falls rapidly to 0

                                                   8
             Diffuse Component

            s ˆ
    cos() = ˆ  m
 Id    I s d max((ˆ  m),0),
                     s ˆ
    where d  diffuse reflection coefficien t
 How do we calculate/obtain the value
  for d ? By experiments.

            Specular Reflection
 Isp:intensity of specular reflection
 Phong model is used to approximate
   Amount of light reflected is greatest in the
    direction of perfect mirror reflection, r
   Amount of light reflected diminished
    rapidly at the nearby angles
   Beam pattern               m         r

                               P                   10
                Specular Reflection
                          ˆ ˆ
  Remember r  s  2(s  m)m ?
 Amount of light reflected falls off as 
  increases and is approximately cosf(),
  where f is the Phong exponent
 I sp    I s s (ˆ  v)f ,
                   r ˆ
     where s  specular reflection coefficien t
                               m           r

                           s              eye

   Problem: expensive to compute as r
    has to be found and normalized                 11
              Specular Reflection
   Solution (proposed by Jim Blinn)
    Calculate h = s + v
    Let  be the angle between h and m
    Use  to calculate the falloff of specular
     intensity as  has the same property as 
      , but can be compensated by different
     value of f        m h          =0 m =h
                     s               s    v


                      ˆ ˆ
    I sp  I s s max((h  m)f ,0)                12
                  Ambient Light

   A uniform background glow in the
    Source is not situated at any particular place
    Light spreads uniformly in all directions
 Ia:intensity of light source
 Iaa is added to the light reaching the eye
    a: ambient reflection coefficient
    a is often the same as d

        Combining Light Contributions
   I = Ia a + Id d  lambert + Isp s  phongf
    lambert = max(( ˆ  m),0)
                     s ˆ
                   ˆ ˆ
    phong = max((h  m),0)
   Implications for different points P on a facet
    Ambient is not changed for different P
    m is the same for all point on the facet
    If the light is far far away, s will change slightly
     as P changes  diffuse will change slightly on
     different P
    If the light or/and eye is/are close, s and h will
     change a lot as P changes  specular changes
     significantly over the facet                         14
          Colored Surfaces and Lights

   Colored surface
    Ir = Iar ar + Idr dr  lambert + Ispr sr  phongf
    Ig = Iag ag + Idg dg  lambert + Ispg sg  phongf
    Ib = Iab ab + Idb db  lambert + Ispb sb  phongf
 lambert and phong terms do not depends on
  color component
 We have to define 9 reflection coefficients
    Ambient and diffuse reflection are based on the
     color of surface
         Colored Surfaces and Lights
   Colored light (Isr, Isg, Isb)
    If the color of a surface is (r, g, b), then it is
     reasonable to set
     (ar, ag, ab) = (dr, dg, db) = (rK, gK, bK),
     where K is the fraction of light reflected
    The diffusion of the surface =
      (Isrdr, Isgdg, Isbdb) = (IsrrK, IsggK, IsbbK)
   Example: white light
    Isr = Isg = Isb = I, (r, g, b) = (0.3, 0.45, 0.25)
     then diffusion = (0.3IK, 0.45IK, 0.25IK) 
     the surface is seen as its color                     16
       Colored Surfaces and Lights

   Color of specular light
    Often the same as that of light source
   Example: sunlight
    Highlight on plastic caused by sunlight is
    Set (sr, sg, sb) = (s, s, s)
       s = 0.5  slightly shiny
       s = 0.9  highly shiny
   Different coefficients are selected for
    specific materials. (Fig 8.17)                17
         Shading and Graphics Pipeline

   Vertices are sent down the pipeline
    along with their associated normals
   All shading calculations are done on
           v 1 , m1
v2, m2            VM       projection
                                        clip   viewport
                             matrix             matrix

      v 0 , m0          shading is
                       applied here

    Shading and Graphics Pipeline

 Lights are objects and the positions of
  light sources are also transformed by
  the modelview matrix
 After all quantities are expressed in
  camera coordinates, colors are attached
  to vertices using the formula
 If an object is clipped, normals of newly
  generated vertices are calculated by
    Flat Shading and Smooth Shading

   Polygonal face in 3D space
    Individual face
    Underlying surface approximated
   Shading methods
    Flat shading
    Smooth shading
       Gouraud shading
       Phong shading

Comparison of Shading Methods

                 Flat Shading

   Entire face is drawn with the same color
   Lateral inhibition
    When there is a discontinuity across an
     object, the eye manufactures a Mach band
     at the discontinuity and a vivid edge is seen
   Specular highlights are rendered poorly
    Either no highlight at all
    Or highlight on the entire face

               Smooth Shading

 Smooth shading computes colors at more
  points on each face to de-emphasize
  edges between adjacent faces
 Use linear interpolation
 Gouraud shading
    Interpolate vertex colors
   Phong shading
    Interpolate vertex normals
    Interpolate normal for each pixel
               Gouraud Shading

 Used by OpenGL                          color3
 Example
    colora: by interpolating
            color3 and color4 colora             colorb
    colorb: by interpolating color
            color1 and color2             color1

    Colors of pixels on the horizontal line
     segment is obtained by interpolating
     colora and colorb
   Does not picture highlights well                       25
               Phong Shading
 Compute normal at each pixel by
  interpolating the normals at the vertices
 Apply the shading model to to every
  point to find the color
 Example                              m3
    ma: by interpolating m3 and m4
    mb: by interpolating m1 and m2
    Normals of pixels on the        ma        mb
     horizontal line segment is      m4
     obtained by interpolating ma and mb
    Colors of the pixels are then computed      26
              Phong Shading
 Very smooth appearance
 Highlights are approximated better
 Principle drawback
    Heavy computation  slow speed
   Not supported by OpenGL
    Can be approximated using texture

                                       Don’t be
   Phong shading  Phong model                    27

Shared By: