COMP371 COMPUTER GRAPHICS · • Color at a point on an object is decided by the properties of the...
Transcript of COMP371 COMPUTER GRAPHICS · • Color at a point on an object is decided by the properties of the...
Copyright © COMP371 Computer Graphics Instructors
COMP371 COMPUTER GRAPHICS
LECTURE 13LIGHTING AND SHADING
Copyright © COMP371 Computer Graphics Instructors
Lecture Overview▪Review of last class
▪Global and Local Illumination
▪Normal Vectors
▪Light Sources
▪Phong Illumination Model
2
Copyright © COMP371 Computer Graphics Instructors
Lighting
33
Without light … we don’t see our world!
Copyright © COMP371 Computer Graphics Instructors
Lighting and Shading
44
Without light … we don’t see our world!
Without shading, objects do not look three dimensional!
Copyright © COMP371 Computer Graphics Instructors5
Illumination• Illumination is the complete description of all the light striking a
particular point on a particular surface• Color at a point on an object is decided by the properties of the
light leaving that point• Knowing the illumination and the surface physics at a point on
a surface, we can determine the properties of the light leaving that point
• In order to generate realistic images we need to understand how light interacts with the surface of objects
Copyright © COMP371 Computer Graphics Instructors6
Interaction of light with a Solid
scatteringand absorption
refraction
incidentlight
specularreflection
diffusereflection
totalinternalreflection
Copyright © COMP371 Computer Graphics Instructors7
Interaction of Light• There are two illumination phenomena of importance
– interaction of light with the boundaries between materials– scattering and absorption of light as it passes through the
material• Boundaries between materials are surfaces which make up
the environment• Light striking a boundary is either reflected or transmitted.
For opaque materials light is absorbed on passing through the boundary
Copyright © COMP371 Computer Graphics Instructors8
Light Interaction in a Scene
8
To simulate and calculate the precise physics of light interacting with a surface is extremely complex
Most graphics applications (including ray tracing!) use an approximation … a lighting model or an illumination model
Copyright © COMP371 Computer Graphics Instructors9
Illumination models• A surface point could be illuminated by
– local illumination, light directly emitted by light sources– global illumination, light reflected from and transmitted
through its own and other surfaces• Illumination models express the factors which determine
the surface color at a given point on a surface• Illumination models compute the color at a point in terms
of both local and global illumination
Copyright © COMP371 Computer Graphics Instructors
Global Illumination
▪Ray tracing▪Radiosity▪Photon Mapping▪Follow light as it progresses
through the scene▪Accurate, but expensive (off-line)
10
Tobias R. Metoc
Copyright © COMP371 Computer Graphics Instructors
Raytracing example
Martin Moeck,Siemens Lighting
11
Copyright © COMP371 Computer Graphics Instructors
Radiosity ExampleRestaurant Interior. Guillermo Leal, Evolucion Visual
12
Copyright © COMP371 Computer Graphics Instructors13
Reflection Models• Illumination models used for graphics initially were often
approximate models• Main goal was to model the interaction of light with matter in
a way that appears realistic and is fast• Reflection Models are the simplest of illumination models• Reflection models assume
– local illumination only. No global illumination– light is only reflected from the surface. There is no
transmission through the object– there is no propagation media. Surfaces are in vacuum
Copyright © COMP371 Computer Graphics Instructors
Local Illumination
▪Approximate model▪Local interaction between light, surface, viewer▪Phong model (this lecture):▪fast, supported in OpenGL
▪GPU shaders
14
Copyright © COMP371 Computer Graphics Instructors
Local Illumination cont’d
▪Color determined onlybased on surface normal,relative camera position,relative light positionand reflection properties at the surface.
▪What effects does this ignore?
15
Copyright © COMP371 Computer Graphics Instructors
Normal Vectors
▪Must calculate and specify the normal vector▪Even in OpenGL!
▪Example: plane
16
Copyright © COMP371 Computer Graphics Instructors
Method 1: Normal of a plane
▪Method 1: given by f(p) = ax + by + cz + d = 0▪Let p0 be a known point on the plane▪Let p be an arbitrary point on the plane▪Recall: u • v = 0 if and only if u orthogonal to v▪n • (p – p0) = 0 ▪Consequently n0 = [a b c]T▪Normalize to n = n0/|n0|
17
Copyright © COMP371 Computer Graphics Instructors
Method 2: Normal of a plane
▪Method II: plane given by p0, p1, p2▪Points must not be collinear → cross product defined as 0▪Recall: u x v orthogonal to u and v▪n0 = (p1 – p0) x (p2 – p0)▪Order of cross product determines orientation▪Normalize to n = n0/|n0|
18
Copyright © COMP371 Computer Graphics Instructors
Reflected Vector
▪Perfect reflection: angle of incidence equals angle of reflection▪Also: I, n, and r lie in the same plane → ▪Assume |l| = |n| = 1, guarantee |r| = 1
l.n = cosθ = n.rr = al + bn
(1)n.r = al.n + b = l.n(2)1 = r.r = a2+2abl.n+b2
Solution: a = ‐1 and b = 2(l‐n) → r = 2(l.n)n‐l
19
Can write r as a linear combination of l and n
Copyright © COMP371 Computer Graphics Instructors
Light Sources and Material Properties
▪Appearance depends on▪Light sources, their locations and properties▪Material (surface) properties▪Viewer position
20
Copyright © COMP371 Computer Graphics Instructors
Types of Light Sources
▪Ambient light: no identifiable source or direction▪Point source: given only by point position▪Directional light: given only by direction▪Spotlight: point source + direction▪Cut-off angle defines a cone of light▪Attenuation function (brighter in center)
21
Copyright © COMP371 Computer Graphics Instructors
Point Source
▪Given by a point p0▪Light emitted equally in all directions▪Intensity decreases with square of distance
22
Copyright © COMP371 Computer Graphics Instructors
Limitations of Point Sources
▪Shading and shadows inaccurate▪Example: penumbra (partial “soft” shadow)▪Similar problems with highlights▪Compensate with attenuation▪Softens lighting▪Better with ray tracing▪Better with radiosity
23
q = distance |p – p0|a, b, c constants
Copyright © COMP371 Computer Graphics Instructors
Directional Light Source
▪Given by a direction vector▪Simplifies some calculations▪In OpenGL:▪Point source [x y z 1]T▪Directional source [x y z 0]T
24
Copyright © COMP371 Computer Graphics Instructors
Spotlight
▪Most complex light source in OpenGL▪Light still emanates from point▪Cut-off by cone determined by angle θ
25
Copyright © COMP371 Computer Graphics Instructors
Global Ambient Light
▪Independent of light source▪Lights entire scene▪Computationally inexpensive▪Not very interesting on its own. ▪A cheap hack to make the scene brighter.
26
Copyright © COMP371 Computer Graphics Instructors
Phong Illumination Model
▪Calculate color for arbitrary point on surface▪Compromise between realism and efficiency▪Local computation (no inter-object visibility calculations)▪Basic inputs are material properties and l, n, v:
27
l = unit vector to light sourcen = surface normalv = unit vector to viewerr = reflection of l at p(determined by l and n)
Copyright © COMP371 Computer Graphics Instructors
Phong Illumination Overview
1. Add contributions from each light source2. Clamp the final result to [0, 1]3. Calculate each color channel (R,G,B) separately4. Light source contributions decomposed into
▪Diffuse reflection▪Ambient reflection▪Specular reflection
(Based on ambient, diffuse, and specular lighting and material properties)
28
Copyright © COMP371 Computer Graphics Instructors
Diffuse Reflection
▪Diffuse reflector scatters light equally to all directions▪Called Lambertian surface▪Diffuse reflection coefficient kd, 0 ≤ kd ≤ 1▪Only the angle of incoming light is important▪Models non-shiny dull matt surfaces like chalk
29
Copyright © COMP371 Computer Graphics Instructors
Lambert’s Law
Intensity depends on angle of incoming light
30
Copyright © COMP371 Computer Graphics Instructors
Diffuse Light Intensity Depends on Angle of Incoming Light▪Recalll = unit vector to light; n = unit surface normal; θ = angle to normal
• cos θ = l • n• Id = kd Ld (l • n)• With attenuation:
q = distance to light source,Ld = diffuse component of light
31
With the diffuse reflection the part of the surface not illuminated by the light is dark.Solution (hack): Ambient lighting
Copyright © COMP371 Computer Graphics Instructors
Ambient Reflection
Ia = ka La▪Intensity of ambient light is uniform at every point▪Ambient reflection coefficient ka, 0 ≤ ka ≤ 1▪May be different for every surface and r,g,b▪Determines reflected fraction of ambient light▪La = ambient component of light source (can be set to different value for each light source)▪Note: Defining La, Ka in this fashion is not physically meaningful.
32
Copyright © COMP371 Computer Graphics Instructors
Effect of ambient and diffuse reflections
33
• By adding the ambient term to the diffuse reflection the illumination equation is
Copyright © COMP371 Computer Graphics Instructors
Specular Reflection
▪Specular reflection coefficient ks, 0 ≤ ks ≤ 1▪Shiny surfaces have high specular coefficient▪Used to model specular highlights
34
Specular highlightsSpecular reflection
Copyright © COMP371 Computer Graphics Instructors
Specular Reflection
▪Recallv = unit vector to camerar = unit reflected vectorφ = angle between v and r
▪cos φ = v • r▪Is = ks Ls (cos φ)α▪Ls is specular component of light▪α is shininess coefficient▪Can add distance term as well
35
Copyright © COMP371 Computer Graphics Instructors
Shininess Coefficient▪Is = ks Ls (cos φ)α▪ α is the shininess coefficient▪ maximum reflection at φ = 0▪ α controls the drop off away from R
36
low α high α
incident light reflected light
Copyright © COMP371 Computer Graphics Instructors
Effect of adding specular reflection
37
Diffuse Illumination Model Phong Illumination Model
Copyright © COMP371 Computer Graphics Instructors
Summary of Phong Model
▪Light components for each color:▪Ambient (La), diffuse (Ld), specular (Ls)▪Material coefficients for each color:▪Ambient (ka), diffuse (kd), specular (ks)▪Distance q for surface point from light sourcel = unit vector to lightn = surface normalr = l reflected about nv = vector to viewer
38
Copyright © COMP371 Computer Graphics Instructors
Material Property Coeffs - Examples
©: Advanced Graphics Programming Using OpenGL by Tom McReynolds and David Blythe
Copyright © COMP371 Computer Graphics Instructors
Material Property Coeffs - Examples
©: Advanced Graphics Programming Using OpenGL by Tom McReynolds and David Blythe
Copyright © COMP371 Computer Graphics Instructors
Material Property Coeffs - Examples
©: Advanced Graphics Programming Using OpenGL by Tom McReynolds and David Blythe
Copyright © COMP371 Computer Graphics Instructors
Material Property Coeffs - Examples
©: Advanced Graphics Programming Using OpenGL by Tom McReynolds and David Blythe
Copyright © COMP371 Computer Graphics Instructors
BRDF
▪Bidirectional Reflection Distribution Function▪Must measure for real materials▪Isotropic vs. anisotropic▪Mathematically complex▪Programmable pixel shading
43
Lighting properties of a human face werecaptured and face re-rendered;Institute for Creative Technologies
Copyright © COMP371 Computer Graphics Instructors44
Copyright © COMP371 Computer Graphics Instructors45
Shading• Shading is the process of determining the colors of all the
pixels covered by a surface using an illumination model• Simplest method is to
– determine surface visible at each pixel– compute normal of the surface– evaluate light intensity and color using an illumination
model• This is quite expensive. The shading methods could be
made efficient by customizing for specific surface representation
Copyright © COMP371 Computer Graphics Instructors46
Shading Models• Shading Models give a technique to determine the colors of
all the pixels covered by a surface using appropriate illumination model
• Triangle meshes are commonly used for representing complex surfaces
• The geometric information is available only at the vertices of a triangle
• Interpolative shading models could be used to increase the efficiency substantially
Copyright © COMP371 Computer Graphics Instructors 47
Constant (Flat) Shading• It is the simplest of the shading models• Also called as faceted shading or flat shading• One polygon receives only one intensity value• Illumination model is applied only once for each polygon• Makes the following assumptions
– light source is at infinity, so is constant across a polygon face
– viewer is at infinity, so is constant across the polygon face
– polygon represents the actual surface being modeled
Copyright © COMP371 Computer Graphics Instructors
Gouraud Shading Model
• “Fixed Pipeline” OpenGL implements Gouraud Shading• Phong Lighting is computed per-vertex• The result is then interpolated on the whole Triangle• Specular highlights are typically blurry, a high tessellation
is required for good lighting• It can be completely implemented in a Vertex Shader
48COMP 371 Week 6
Copyright © COMP371 Computer Graphics Instructors49
Gouraud Shading
• I1, I2 and I3 are computed using Phong Illumination.
• Intensity Ip at a point is calculated as:
I1
I3
I2
Ia IbIp
y1
y
y2
y3
xa xbx
Might miss specular highlights, if the highlight doesn’t fall at the vertex
Copyright © COMP371 Computer Graphics Instructors
Gouraud Shading
50COMP 371 Week 6
Low Poly Count High Poly Count
Copyright © COMP371 Computer Graphics Instructors
Phong Shading
• Is typically implemented per Fragment (pixel)• It provides “sharp” specular highlights even with low polygon count
since lighting is calculated for each pixel individually.• It is implemented on both the Vertex and Fragment Shader• The Vertex Shader Transforms the Lighting Data to View Space• The Fragment Shader calculates the Phong Shading Equation
(Ambient + Diffuse + Specular)
51COMP 371 Week 6
Copyright © COMP371 Computer Graphics Instructors 52
Constant Shading Gouraud Shading Phong Shading
Copyright © COMP371 Computer Graphics Instructors
Phong Model Implementation with GLSL
• Light Properties and Material Properties are implemented as Shader Uniform Variables
• Be careful from which Coordinate System the Light Positions are sent to the Shaders
• Typically, lighting is computed in View Space– Normals must be converted– Eye, Light vectors must be converted
53COMP 371 Week 6
Copyright © COMP371 Computer Graphics Instructors
Phong Shading in GLSL
Vertex Shader• Transform vertex positions
to Clip Space• Transform lighting data to
View Space
Fragment Shader• Calculate and add
ambient, diffuse and specular components
54
Copyright © COMP371 Computer Graphics Instructors
Phong Shading in GLSL• Vertex Shader• Uniforms
• Light position• Transformation Matrices
• Input– Vertex Data(pos, normal, color)
• Output– Normal (N)– Light Vector (L)– Eye Vector (V)– Vertex Color
• Fragment Shader• Uniforms
– Material Coefficients(ka, kd, ks, n)– Light Coefficients(Color, Attenuation const)
• Inputs in View Space– Normal (N)– Light Vector (L)– Eye Vector (V)
• Output– Fragment Color
55COMP 371 Week 6
Copyright © COMP371 Computer Graphics Instructors
Phong Shading in GLSL Multiple Light Sources
• Use arrays for light parameters in your Shaders to support multiple light sources.
56COMP 371 Week 6
Copyright © COMP371 Computer Graphics Instructors
Review
▪Global and Local Illumination▪Normal Vectors▪Light Sources▪Phong Illumination Model▪Gouraud and Phong Shading
57
Copyright © COMP371 Computer Graphics Instructors
Next Lecture
▪ Rasterization Techniques
58
Copyright © COMP371 Computer Graphics Instructors59
Copyright © COMP371 Computer Graphics Instructors60
Studying Support Slides
Copyright © COMP371 Computer Graphics Instructors61
Lighting in OpenGL
Steps of specifying lighting in OpenGL• Define normal vectors for each vertex of every object.• Create, position, and enable one or more light sources.• Select a lighting model.• Define material properties for the objects in the scene.
Copyright © COMP371 Computer Graphics Instructors
62
Lights in OpenGL
• glLightfv(Light #, Property, Array of Vals)– Light # = One of GL_LIGHTi– Property, one of
• GL_AMBIENT• GL_DIFFUSE• GL_SPECULAR• GL_POSITION = where is the light?
– Array of values = value for the property
Copyright © COMP371 Computer Graphics Instructors63
Specifying Materials in OpenGLGeneral form: glMaterialf(face, parameter, value);
glMaterialfv(face, parameter,*array);
face is GL_FRONT, GL_BACK, GL_FRONT_AND_BACKparameter is:GL_AMBIENT four values that specify the ambient RGBA reflectance of the material. (0.2,0.2,0.2,1.0)GL_DIFFUSE four values that specify the diffuse RGBA reflectance of the material. (0.8,0.8,0.8,1.0)GL_SPECULAR four values that specify the ambient RGBA reflectance of the material. (0.0,0.0,0.0,1.0)GL_SHININESS specifies the specular reflectance exponent of the material. 0.0
Copyright © COMP371 Computer Graphics Instructors64
Problems with Interpolated Shading
• Polygon silhouette : The silhouette edge of the mesh is always a polygon
• Solution :– finer subdivision for the
entire surface– finer subdivision only along
silhouette (view dependent)
Copyright © COMP371 Computer Graphics Instructors65
Problems with Interpolated Shading
• Perspective distortionSolution : – smaller polygons by
finer subdivision– interpolate in world space
• Orientation dependence Solution :– decomposing polygons into
triangles– rotation invariant interpolation in
object space
0 0 0 00
255 255255 255 255 255
128 128 128
A
B
C
DP
A
B
C
D
P
Copyright © COMP371 Computer Graphics Instructors66
Problems with Interpolated Shading
• Problems at shared vertices– avoid such cases
• Unrepresentative vertex normals– subdividing the polygons before calculating normals