CSPC 352: Computer Graphics - PowerPoint PPT Presentation

1 / 44
About This Presentation
Title:

CSPC 352: Computer Graphics

Description:

If polygons are too large, highlights get distorted and dimmed (notice the funny shape) ... Compute a normal for each vertex as average of adjacent faces ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 45
Provided by: HarryPl6
Category:

less

Transcript and Presenter's Notes

Title: CSPC 352: Computer Graphics


1
CSPC 352 Computer Graphics
Chapter 6 Lighting and Shading
2
Overview
  • Local and global illumination
  • Phong reflectance model (local illumination)
  • Flat, Gouraud, and Phong
  • Shading on a polygonal mesh
  • Surface subdivisions
  • Shading in OpenGL

3
Perspective
  • Lighting and shading are accomplished by modeling
    the world and simulating the laws of physics
  • Short story Stanislaw Lem, The Cyberiad The
    constructor Trurl creates a tiny simulation of a
    kingdom in a box to make a deposed, exiled despot
    happy. Trurls friend thinks that is terrible
  • There are those who say that we exist in the mind
    of God. What do you think of that idea?
  • Pascal, Pensées The arithmetical machine
    produces effects which come closer to thought
    than anything which animals can do but it can do
    nothing which might lead us to say that it
    possesses free will, as the animals have.

4
(No Transcript)
5
Need for shading
  • Was it hard to make the 3D flower (first program)
    look 3D?
  • Shading that is appropriate for the lighting is
    the primary cue to 3D appearance
  • What are some other cues?

6
Illumination models
  • General approach
  • model the world
  • simulate physics
  • Global illumination models (ray tracing,
    radiosity) determine shading by bouncing light
    around an entire environment (too slow for
    interactive graphics)
  • Local illumination models consider only the
    surface, light direction, and viewing direction

7
(No Transcript)
8
Local illumination
  • To make lighting fast enough, we will initially
    restrict our attention to
  • Light source, one surface, and viewer (ignore
    inter-object reflections, shadows)
  • Ambient, diffuse, and specular reflection (ignore
    transparency, refraction, reflection, )

9
(No Transcript)
10
Light sources
  • In general, a light source is a rather
    complicated thing. It can emit different amounts
    of light for each
  • Location (x, y, z)
  • Direction (?, f)
  • Wavelength (l)
  • Illumination functionI(x, y, z, ?, f, l)
  • Examples ambient, point, area, spot,distant,

11
Colored lights
  • Intensity of emitted light can also be a function
    of wavelength
  • We usually model as I Ir, Ig, Ib components
  • Some experiments have been done with a whole
    spectrum of color values, giving more realistic
    results in some cases

12
Ambient light
  • Intensity doesnt vary with x, y, z, ?, f
  • I Iar, Iag, Iab

13
Point lights
  • Point lights have a location (so farther objects
    receive less light) but are not directional
  • I(p0) Ir(p0), Ib(p0), Ig(p0)
  • How would you compute the illumination at point
    p?
  • Illumination proportional to inverse square of
    distance
  • I(p, p0) (1/d2) Ir(p0), Ib(p0), Ig(p0)

14
Limitations of point lights
  • Usually result in artificiallyhigh-contrast
    images
  • Can generate umbra (full shadow) but
    notpenumbra (partial shadow)
  • Area lights generate softershadows, but are
    usuallyused only in raytracing or radiosity

15
Distant (directional) lights
  • Light point lights, but
  • Without attenuation based on the distance
  • Without difference in direction (parallel rays)
  • Location of light source becomes x, y, z, 0
    noattenuation
  • More efficient to computethan point sources

16
Spotlights
  • Directional, i.e. light is emitted in a narrow
    range of angles, q
  • More realistic spotlights wouldmodel a gradual
    fall-off of light
  • E.g. cose f (s l)e if s is direction
    ofsource, l direction to source, both unit
    vectors

17
Illumination and shading
  • How do these light sources affect brightness of a
    surface point?
  • Most commonly used model for interactive
    graphics Phong Illumination Model
  • Involves terms
  • Ambient
  • Diffuse
  • Specular
  • It is a (simplified) model of the physics of
    reflection

18
Vectors used by Phong model
  • The directions used by the phong model
  • n surface outward normal
  • v direction to viewer
  • l direction to light source
  • r reflection direction
  • Since these are directions, theyare unit
    vectors.

19
Ambient term of Phong model
  • An object has an ambient reflectivity
    coefficient, ka
  • A light source gives off a certain amount of
    ambient light, La
  • Total ambient illumination Ia ka La
  • (For colored light, we repeat this computation
    for R, G, and B ambient light values and
    reflectivity coefficients)

20
(No Transcript)
21
Diffuse term
  • A perfectly diffuse reflector is so rough that it
    scatters light equally in all directions
  • But note that when thelight comes in at an
    angle,the same energy is spreadout over larger
    area
  • Such surfaces are calledLambertian surfaces
    (obeying Lamberts Law)

22
Diffuse shading
  • At noon, illum. is 1
  • As the angle q (u infigure) decreases,
    illumination goes to zero
  • Illumination is proportional to cos(q)
    (Lamberts law)
  • cos(q) l n
  • Id kd l n Ld

23
(No Transcript)
24
Specular Term
  • Specular term adds highlights in the reflection
    direction
  • Note that the smoother and shinier the object,
    the tigher and brighter thehighlight
  • Highlight power falls as viewer v moves away from
    reflection dir, r. (cos f v r)
  • Modeled as cosa f, a is shininess coefficient
    (1..200)
  • Is ks Ls (r v)a

25
(No Transcript)
26
Phong illumination model
  • Phong illumination model
  • I Ambient Diffuse Specular
  • Ia Id Is
  • ka La kd Ld l n ks Ls (r v)a
  • May add light attenuation term
  • 1/(abdcd2) ( ka La kd l n Ld) ks Ls (r
    v)a
  • Parameters needed
  • Light La, Ld, Ls for each light
  • Surface ka, kd, ks, a
  • Repeat for each color component, light source
  • How hard to calculate?

27
Polygon shading
  • How do you use the Phong Illumination Model to
    render an object with shading?
  • Consider a polygonal sphere approximation
  • How do you find the normals to the faces?
  • Shade a face with a constant color?
  • glShadeModel(GL_FLAT)
  • Called flat shading or Constant shading
  • How much computation would this require
  • Per pixel?
  • Per vertex?

28
(No Transcript)
29
Flat shading drawbacks
  • The human visual system enhances edges
  • We see stripes (known as MachBands) along edges
  • Much like aconvolution!
  • How to avoid?

30
Gouraud shading
  • Gouraud shading
  • Define vertex normals as averageof surrounding
    faces
  • Compute lighting equation at each vertex
  • Interpolate colors across polygon
  • glShadeModel(GL_SMOOTH)
  • Computation required
  • Per pixel?
  • Per vertex?
  • Very fast! Especially with reasonably large
    polygons and hardware color interpolation

31
(No Transcript)
32
Gouraud drawbacks
  • Drawbacks of Gouraudshading?
  • Polygon edges are still visible
  • Brightness is modelled asa linear function, but
    thatsnot really accurate
  • Real highlights are smalland bright and drop off
    sharply
  • If polygons are too large, highlights get
    distorted and dimmed (notice the funny shape)
  • How to avoid these artifacts?

33
Phong shading
  • To eliminate artifacts, interpolate normals
  • Results better shading, much nicer highlights
  • Computation required per pixel?
  • This is still too expensive to do in hardware, in
    general

34
(No Transcript)
35
Shading summary
  • Dont confuse Phong Illumination Model and Phong
    Shading
  • Gouraud shading compute illumination model at
    each vertex. Interpolate colors. (Often done in
    hardware)
  • Phong shading interpolate vertex normals.
    Compute illumination model at each vertex

36
Specifying lights in OpenGL
  • OpenGL supports those four light types
  • Point, directional lights
  • GLfloat light0_pos 1.0, 2.0, 3.0, 1.0
  • GLfloat light0_pos 1.0, 2.0, 3.0, 0.0
  • Diffuse, Ambient, Specular coefficients
  • GLfloat diffuse0 1, 0, 0, 1
  • GLfloat ambient0 1, 0, 0, 1
  • GLfloat spedular0 1, 1, 1, 1
  • glEnable(GL_LIGHTING)

37
(No Transcript)
38
Enabling lights
  • Can enable at least 8 lights
  • glEnable(GL_LIGHT0)
  • glLightfv(GL_LIGHT0, GL_POSITION, light0_pos)
  • glLightfv(GL_LIGHT0, GL_AMBIENT, ambient0)
  • glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse0)
  • glLightfv(GL_LIGHT0, GL_SPECULA, specular0)
  • Spotlights set more light parameters as above
  • GL_SPOT_DIRECTION
  • GL_SPOT_EXPONENT
  • GL_SPOT_CUTOFF

39
Specifying Materials
  • Material properties are part of the drawing
    state, specified by glMaterialfv
  • GLfloat ambient 0.2, 0.2, 0.2, 1.0
  • GLfloat diffuse 1.0, 0.8, 0.0, 1.0
  • GLfloat specular 1.0, 1.0, 1.0, 1.0
  • glMaterialfv(GL_FRONT, GL_AMBIENT, ambient)
  • glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse)
  • glMaterialfv(GL_FRONT, GL_SPECULAR, specular)
  • glMaterialf(GL_FRONT, GL_SHININESS, 100.0)
  • GLfloat emission0.0, 0.3, 0.3, 1.0
  • glMaterialfv(GL_FRONT, GL_EMISSION, emission)
  • Use GL_FRONT_AND_BACK for two-sided faces

40
(No Transcript)
41
OpenGL Gouraud Shading
  • OpenGL needs to know vertex normals as well as
    locations
  • glNormal3fv(n)
  • glVertex3fv(p)
  • How to compute vertex normals?
  • Cross product for face normals
  • Average normals of surrounding faces
  • How to find neighboring faces?

42
Virtual Trackball shading
  • Flat shading
  • Compute a normal for each face
  • Gouraud shading
  • Compute a normal for each vertex as average of
    adjacent faces
  • Defect you may not want to smooth-shade across
    every edge how should it really be done?
  • What would you have to do to handle material
    properties, surface colors, etc?

43
Surface subdivision
  • In real modelers
  • one can usually define smooth curved surfaces
  • Eg spheres, quadrics, NURBS, smoothed polygons
  • Modeler renders with smoothness setting
  • Recursively split polygons into smaller pieces,
    with new vertices on the smooth surface
  • Splitting a triangle can be done by
  • Bisecting angles
  • Computing the centrum
  • Bisecting sides
  • Of course, smoother surfaces take longer to draw

44
Chapter summary
  • Phong illumination model has ambient, diffuse,
    and specular terms
  • It can be used for Flat, Gouraud, and Phong
    shading
  • OpenGL supports eight ambient, point, distant,
    and spot lights
  • You must specify light and material properties
    with many OpenGL function calls
  • Curved surfaces are modeled with polygon
    subdivisions
Write a Comment
User Comments (0)
About PowerShow.com