Lighting and Shading - PowerPoint PPT Presentation

1 / 53
About This Presentation
Title:

Lighting and Shading

Description:

Build a simple lighting model---the Phong model--- that is used with real time ... to model a shiny surface, such as polished metal or a glossy car finish. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 54
Provided by: xiaoyu1
Category:
Tags: lighting | shading

less

Transcript and Presenter's Notes

Title: Lighting and Shading


1
Lighting and Shading
2
Objectives
  • Learn to add lighting and shading to objects so
    their images appear three-dimensional
  • Introduce the OpenGL approximation of
    light-material interactions
  • Build a simple lighting model---the Phong
    model--- that is used with real time graphics
    hardware
  • Introduce OpenGL shading models
  • Normal computation and transformation

3
Illumination Models
  • Illumination
  • The flux of light energy from light sources to
    objects in the scene via direct and indirect
    paths
  • Lighting
  • The process of computing the luminous
  • intensity reflected from a specified 3-D point
  • Shading
  • The process of assigning a colors to a pixel
  • Illumination Models
  • Simple approximations of light transport
  • Physical models of light transport

4
Lighting
  • Lighting is the process of determine the color of
    each point in the scene. It depends on
  • Light sources
  • Color, position, direction, shape
  • Surface properties
  • Normal
  • Material properties
  • Light is absorbed, transmitted, or reflected at a
    point on surface. The reflected light determines
    the color of that point.
  • A surface appears red under white light because
    the red component of the light is reflected and
    the rest is absorbed

5
Two Components of Lighting
  • Light Sources (Emitters)
  • Emission Spectrum (color)
  • Geometry (position and direction)
  • Directional Attenuation
  • Surface Properties (Reflectors)
  • Reflectance Spectrum (color)
  • Geometry (position, orientation, and
    micro-structure)
  • Absorption
  • Approximations
  • Accurate simulation of lighting is too complex
  • Human visual system can be tricked by reasonable
    approximations

6
Indirect Lighting (Scattering)
  • Light strikes A
  • Some scattered
  • Some absorbed
  • Some of scattered light strikes B
  • Some scattered
  • Some absorbed
  • Some of this scattered
  • light strikes A and so on

7
Rendering Equation
  • The infinite scattering and absorption of light
    can be described by the rendering equation
  • 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

8
OpenGL Lighting
  • Real-world lighting should consider light
    reflected or refracted from other objects in the
    scene global illumination
  • OpenGL is a Local illumination model
  • Only depends relationship to the light source.
  • Dont consider light reflected or refracted from
    other objects.
  • Dont check if an object is obstructed from light
    source, therefore dont model shadow (can be
    faked)
  • Very efficient for computation
  • Not a physical model but shades polygons nicely

Local illumination
9
Light Sources
  • Broken into three color components (Lr, Lg, Lb)
    for the intensity of red, green and blue light
    respectively.
  • General light sources are difficult to work with
    because we must integrate light coming from all
    points on the source

10
OpenGL Light Sources
  • Ambient light
  • Same amount of light everywhere in scene
  • Can model contribution of many sources and
    reflecting surfaces
  • Directional Light Sources
  • All light rays have the same direction and no
    point of origin.
  • A good approximation for light sources that are
    infinitely far away, e.g. sun light
  • Point Light Sources
  • Light rays are from a point in 3D space. A good
    approximation for local light sources.
  • Have different light direction for different
    points in the scene

11
Ambient Light Source
  • An object is lighted by the ambient light even
    if it is not visible to any light source.
  • Ambient light
  • no spatial or directional characteristics.
  • The amount of ambient light incident on each
    object is a constant for all surfaces in the
    scene.
  • A ambient light can have a color.
  • The amount of ambient light that is reflected by
    an object is independent of the object's position
    or orientation.
  • Surface properties are used to determine how
    much ambient light is reflected.

12
Directional Light Sources
  • All of the rays from a directional light source
    have a common direction, and no point of origin.
  • It is as if the light source was infinitely far
    away from the surface that it is illuminating.
  • Sunlight is an example of a directional light
    source.
  • The direction from a surface to a light source
    is important for computing the light reflected
    from the surface.
  • A directional light source has a constant
    direction for every surface. A directional light
    source can be colored.

13
Point Light Sources
  • The point light source emits rays in radial
    directions from its source. A point light source
    is a fair approximation to a local light source
    such as a light bulb.
  • The direction of the light to each point P on a
    surface changes when a point light source is
    used. Thus, a normalized vector to the light
    emitter must be computed for each point that is
    illuminated.

14
Other Light Sources
  • Spotlights (supported by OpenGL)
  • Restricting the shape of light emitted by a point
    light to a cone.
  • Requires a color, a point, a direction, and a
    cutoff angle to define the cone.
  • Area Light Sources (not supported)
  • Light source occupies a 2-D area (usually a
    polygon or disk)
  • Generates soft shadows
  • Extended Light Sources (not supported)
  • Spherical Light Source
  • Generates soft shadows

15
OpenGL Specifications
  • Available light models in OpenGL
  • Ambient lights
  • Point lights
  • Directional lights
  • Spot lights
  • Attenuation
  • Physical attenuation
  • OpenGL attenuation
  • Default a 1, b0, c0

16
OpenGL Light Functions
  • Turn on the power (for all the lights). Lighting
    is by default off.
  • glEnable(GL_LIGHTING)
  • glDisable(GL_LIGHTING)
  • Flip each lights switch
  • glEnable(GL_LIGHTn) (n 0,1,2,)
  • Point Light Source
  • GLfloat position x, y, z, 1
  • glLightfv(GL_LIGHT0, GL_POSITION, position)
  • Directional Light Source
  • GLfloat position x, y, z, 0
  • glLightfv(GL_LIGHT0, GL_POSITION, position)
  • (x, y, z) defines the light direction

17
More OpenGL Light Functions
  • Spot Light Source
  • GLfloat direction 0.0, -1.0, 0.0
  • glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,
    direction)
  • glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 15.0f)
  • glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 10.0f)
  • How spot light focused in the center. Higher the
    exponent, more concentrated the spot light
  • OpenGL attenuation 1/(a bd cd2)
  • Default a 1, b0, c0
  • glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION,
    2.0f)
  • glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0f)
  • glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0)

18
Light Surface Interaction
  • The smoother a surface, the more reflected light
    is concentrated in the direction a perfect mirror
    would reflected the light
  • A very rough surface scatters light in all
    directions
  • The reflection depends light direction, surface
    normal and properties.

rough surface
smooth surface
19
Ideal Diffuse Reflection
  • Ideal Diffuse Reflection an incoming ray of
    light is equally likely to be reflected in any
    direction over the hemisphere.
  • An ideal diffuse surface is, at the microscopic
    level a very rough surface. Chalk is a good
    approximation to an ideal diffuse surface.

20
Lambert's Cosine Law
  • Ideal diffuse reflectors reflect light according
    to Lambert's cosine law, (there are sometimes
    called Lambertian reflectors).
  • Lambert's law states that the reflected energy
    from a small surface area in a particular
    direction is proportional to cosine of the angle
    between that direction of light and the surface
    normal.
  • The amount energy that is reflected in any one
    direction is constant in this model. In other
    words the reflected intensity is independent of
    the viewing direction. The intensity does however
    depend on the light source's orientation relative
    to the surface.

21
Computing Diffuse Reflection
  • Angle of incidence The angle between the
    surface normal and the incoming light ray
  • Lambert's law states that the reflected energy
    from a small surface area in a particular
    direction is proportional to cosine of the angle
    of incidence.

22
Diffuse Lighting Examples
  • We need only consider angles from 0 to 90
    degrees. Greater angles.
  • If the angle is greater than 90 degrees (where
    the dot product is negative), the surface faces
    away from the light source and is blocked, so
    the reflected energy is 0.
  • Below are several examples of a spherical
    diffuse reflector with a varying lighting angles.

23
Specular Reflection
  • A specular reflector is necessary to model a
    shiny surface, such as polished metal or a glossy
    car finish.
  • We see a highlight, or bright spot on those
    surfaces.
  • Where this bright spot appears on the surface
    depends on where the surface is seen from. This
    type of reflectance is view dependent.
  • At the microscopic level a specular reflecting
    surface is very smooth.
  • Specular reflection is merely the mirror
    reflection of the light source in a surface. An
    ideal mirror is a purely specular reflector.

24
Specular Reflection
  • Specular light reflects in a particular direction
    the mirror reflection direction
  • The reflected light decrease rapidly when the
    direction moves away from the mirror reflection
    direction
  • Specular reflection make surface look shiny or
    create highlights.

25
Ideal Reflection Direction
  • The light vector l and the ideal reflection
    direction r are in the same plane as the normal n
  • l and r have the same angle from n
  • Angle of incidence angle of relection

r 2 (l n ) n - l
26
Non-ideal Reflectors
  • Snell's law, however, applies only to ideal
    mirror reflectors.
  • Real materials tend to deviate significantly from
    ideal reflectors.
  • At this point we will introduce an empirical
    model that is consistent with our experience, at
    least to a crude approximation.
  • In general, we expect most of the reflected
    light to travel in the direction of the ideal
    ray.
  • However, we might expect some of the light to be
    reflected just slightly off from the ideal
    reflected ray.
  • As we move farther and farther, in the angular
    sense, from the reflected ray we expect to see
    less light reflected.

27
Phong Model for Specular Reflection
  • Phong proposed using a term that dropped off as
    the angle between the viewer and the ideal
    reflection increased. This model has no physical
    basis, yet it is one of the most commonly used
    illumination models in computer graphics.

Ir ks I cosaf
shininess factor
reflected intensity
incoming intensity
reflection coef
28
Effect of the shininess coefficient
  • The diagram below shows the how the reflectance
    drops off in a Phong illumination model. For a
    large value of the nshiny coefficient, the
    reflectance decreases rapidly with increasing
    viewing angle.

29
Phong Examples
  • The following spheres illustrate specular
    reflections as the direction of the light source
    and the coefficient of shininess is varied.

30
Computing Phong Illumination
  • The V vector is the unit viewing vector and the R
    vector is the mirror reflectance direction

R 2 (L N ) N - L
31
Blinn Torrance Variation
  • Jim Blinn introduced another approach for
    computing Phong-like illumination based on the
    work of Ken Torrance. His illumination function
    uses the following equation
  • In this equation the angle of specular
    dispersion is computed by how far the surface's
    normal is from a vector bisecting the incoming
    light direction and the viewing direction.
  • On your own you should consider
  • how this approach and the previous
  • one differ. OpenGL implements this
  • model.

32
Putting it all together
  • Phong Illumination Model

33
Multiple Lights
  • Same formula for each color component
  • Sum over all lights Ii
  • reflectance coefficients Kd, Ks, and Ka scalars
    between 0 and 1 may or may not vary with color
  • nshiny scalar integer 1 for diffuse surface,
    100 for metallic shiny surfaces
  • Truncate if the sum is greater than 1.0

34
OpenGL Lights
  • Setting up a simple light source
  • GLfloat ambientColor4 0.5, 0.5, 0.5, 1.0
  • GLfloat diffuseColor4 1.0, 0.0, 0.0, 1.0
  • GLfloat specularColor 1.0, 1.0, 1.0, 1.0
  • GLfloat position4 2.0, 4.0, 5.0, 1.0
  • // set up light 0 properties
  • glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor)
  • glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor)
  • glLightfv(GL_LIGHT0, GL_SPECULAR,
    specularColor)
  • glLightfv(GL_LIGHT0, GL_POSITION, position)
  • glEnable(GL_LIGHTING) // enable lighting
  • glEnable(GL_LIGHT0) // enable light 0

35
Material Properties
  • Lighting is also affected by the color and
    material properties of surfaces (dull, shiny,
    etc)
  • How much incident light is reflected by the
    surface (ambient/diffuse/specular reflecetion
    coefficients)
  • glMaterialfv(face, property, value)
  • Face material property for which face (e.g.
    GL_FRONT, GL_BACK, GL_FRONT_AND_BACK)
  • Property what material property you want to set
    (e.g. GL_AMBIENT, GL_DIFFUSE, GL_SPECULAR,
    GL_SHININESS, GL_EMISSION, etc)
  • Value the reflection coefficients you can assign
    to the property
  • Nonzero GL_EMISSION makes an object appear to be
    giving off light of that color

36
Effects of Specular Coefficients
37
Some Typical Materials
More at http//www.sgi.com/software/opengl/advance
d98/notes/node119.html
38
Transform Light Positions
  • The position and direction of a light source in
    OpenGL is just treated as the position of a
    geometric primitive.
  • Subject to OpenGL matrix transformation as a
    primitive
  • To keep the light moving with the view point, we
    set light position before whatever viewing and/or
    modeling transformation
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • glLightf(GL_LIGHT0, GL_POSITION, position)
  • gluLookAt()
  • Apply Modeling transformations
  • Draw object

39
Stationary Light in the scene
  • To keep the light stationary in the scene, we
    apply the same transformations to the light and
    object
  • glMatrixMode(GL_MODELVIEW)
  • glLoadIdentity()
  • gluLookAt()
  • Apply Modeling transformations
  • glLightf(GL_LIGHT0, GL_POSITION, position)
  • Draw object

40
Moving Lights
  • Move the light independently
  • Perform the desired modeling transformation and
    reset the light position.
  • gluLookAt ()
  • glPushMatrix()
  • glRotated(spin, 1.0, 0.0, 0.0)
  • glLightfv(GL_LIGHT0, GL_POSITION,
    light_position)
  • glPopMatrix()
  • draw object
  • Example

41
Where do we compute lighting?
  • Lighting can be a costly process involving the
    computation of and normalizing of vectors to
    multiple light sources and the viewer.
  • In standard OpenGL, for models defined by
    collections of polygonal facets or triangles
    lighting is computed per vertex so that each
    vertex gets a color.
  • Therefore we need normals for the vertices.
  • In OpenGL the normal vector is part of the state
  • Set by glNormal()
  • glNormal3f(x, y, z)
  • glNormal3fv(p)
  • Usually we want to set the normal to have unit
    length so cosine calculations are correct
  • glEnable(GL_NORMALIZE) allows for
    auto-normalization at a performance penalty

42
Polygonal Shading
  • Shading calculations are done for each vertex
  • Vertex colors are determined
  • We still need to determine colors for pixels
    inside a polygon.
  • If we use glShadeModel(GL_FLAT) This technique
    is called constant or flat shading. It is often
    used on polygonal primitives.
  • the color at the first vertex will determine the
    color of the whole polygon

43
Gouraud (smooth) Shading
  • In smooth shading, vertex colors are
    interpolated across the polygon
  • glShadeModel(GL_SMOOTH)
  • The interpolation is well defined only for
    triangles, equivalent to a barycentric
    combination
  • Must smoother looking than flat shading. Notice
    that facet artifacts are still visible.

44
Phong Shading
  • A even smoother shading is called Phone shading
    (not to be confused with the Phong illumination
    model). However, evidence of the polygonal model
    can usually be seen along silhouettes.
  • In Phong shading the surface normal is linearly
    interpolated across polygonal facets, and the
    lighting model is computed at every pixel.
  • A Phong shader assumes the same input as a
    Gouraud shader, which means that it expects a
    normal for every vertex.
  • The illumination model is applied at every pixel
    on the surface being rendered, and is also called
    per-pixel lighting.

45
Gouraud and Phong Shading
46
Global OpenGL Lighting Models
  • We can use glLightModelif(pname, param) to
    change some global setting for lighting
  • pname GL_LIGHT_MODEL_AMBIENT
  • Global ambient light, default (0.2, 0.2, 0.2, 1)
  • pname GL_LIGHT_MODEL_LOCAL_VIEWER
  • Specify if computing lighting using true view
    direction or a fixed direction (0, 0, 1). Default
    False. Set to True for better highlighting but
    slower performance
  • pname GL_LIGHT_MODEL_TWO_SIDE
  • Specify if you want to illuminate the back side
    of polygons. Default Fasle. Note normal is
    reversed for back side

47
Vertex Normals
  • If vertex normals are not provided they can often
    be approximated by averaging the normals of the
    facets which share the vertex.
  • This only works if the polygons reasonably
    approximate the underlying surface

n (n1n2n3n4)/ n1n2n3n4
48
Normal for Triangle
n
p2
plane n (p - p0 ) 0
n (p2 - p0 ) (p1 - p0 )
p
  • p1

p0
normalize n ? n/ n
Note that right-hand rule determines outward face
49
Normal to Sphere
  • Implicit function f(x,y.z)0
  • Normal given by gradient
  • Unit Sphere f(p)pp-1 0
  • n ?f/?x, ?f/?y, ?f/?zTp

50
Parametric Form
  • For sphere
  • Tangent plane determined by vectors
  • Normal given by cross product

xx(u,v)cos u sin v yy(u,v)cos u cos v z
z(u,v)sin u
?p/?u ?x/?u, ?y/?u, ?z/?uT ?p/?v ?x/?v,
?y/?v, ?z/?vT
n ?p/?u ?p/?v
51
Transforming Surface Normals
  • Surface normals are the most important geometric
    surface characteristic used in computing
    illumination models. They are used in computing
    both the diffuse and specular components of
    reflection.
  • However, the vertices of a model do not
    transform in the same way that surface normals
    do. A naive implementer might consider
    transforming normals by treating them as points
    offset a unit length from the surface. But this
    approach will not work. Consider the following
    two dimensional example.

52
Normals Represent Tangent paces
  • The fundamental problem with transforming
    normals is what a normal really is.
  • Strictly speaking, a normal is not really a
    vector. Instead normals represent the tangent
    plane of the surface at a point.
  • A tangent plane can be represented by either two
    basis vectors, or the normal perpendicular to the
    plane.
  • So we want the normal remains perpendicular to
    the plane after the transformations

53
Transforming Normal Vectors
  • Normal vectors are not transformed the same way
    points are
  • Plane equation should still be true with
    transformed points!
  • Transform normal vectors with the inverse
    transpose of the transformation matrix
  • For what matrices M, would K M ?

54
Summary OpenGL Specifications
  • Each light has an ambient, diffuse, and specular
    component.
  • Each light can be a point light, directional
    light, or spot light.
  • Directional light is a point light positioned at
    infinity
  • There ambient, diffuse and specular reflections.
  • Diffuse and specular reflections depend on the
    surface normal
  • Specular reflection also depends on viewing
    direction
  • Shading Models flat and smooth
  • glShadeModel()
  • GL_FLAT, GL_SMOOTH
  • Smooth model uses Gouraud shading
Write a Comment
User Comments (0)
About PowerShow.com