#11: Lighting
CSE167: Computer GraphicsInstructor: Ronen Barzel
UCSD, Winter 2006
2
Outline for today
Overview of lighting Notes on color Local Illumination Light Sources Shading Advanced Lighting
3
Where we are so far…
We know how to rasterize: Given a 3D triangle (or a bunch of triangles) Given a 3D camera… …we know which pixels represent the triangles
But what color should those pixels be?
4
Lighting
(non-teapot images by Henrik Wann Jensen)
5
Lighting To create a photorealistic image:
Simulate the interaction of light with the objects in the scene
Simulate the interaction of light with the eye or camera I.e., simulation of physics and optics
• Advanced rendering course (CSE168)• Many aspects “solved” in principle but still an area of active research
• Very slow to compute fully• Use global illumination techniques: examine the whole scene at once
• Known as physically-based rendering
For interactive computer graphics: Use a simplified model for speed Empirical/perceptual -- approximate interesting observed
phenomena Use local illumination techniques:
• only direct effect of lights on surfaces
6
Basic Components of Lighting
Light sources AKA emitters Color and intensity Geometric attributes: Position, Direction, Shape Spatial attenuation Advanced properties: Spectrum, Polarization, …
Surfaces Geometric attributes: Position, Orientation Material properties: reflectance
• color• shininess, glossiness, …• texture• Advanced: translucency, microstructure, sub-surface scattering, …
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
7
Lighting vs. Shading
Lighting: compute the result of light illuminating surfaces
Shading: assign colors to pixels
For photorealistic rendering: in principle, shading==lighting: perform lighting at every pixel
In practice: may take shortcuts may include non-lighting effects
• fog• illustration• cartoon shading
8
Vertex Lighting
Each vertex goes through lighting process Lighting computation determines final color at the vertex Based on initial “unlit” vertex color Based on lights in the scene Based on material properties of the surface Based on surface normal
Interpolate colors using Gouraud shading
(Same lighting computation for per-pixel lighting)
rn
9
Outline for today
Overview of lighting Notes on color Local Illumination Light Sources Shading Advanced Lighting
10
What is light? What is color?
Light is electromagnetic energy a continuous range of wavelengths varying intensity at each wavelength
Color is a property of the visual system Not an inherent property of light Human eyes have Red, Green, Blue receptors (cones)
• Each receptor responds to a range of wavelengths• Gives rise to “primary colors”:
• all colors expressed as combination of red, green, blue cone stimulation
• Lots of perceptual, psychophysical effects:• adaptation, inhibition, illusion
Physically correct computation requires computing interactions at all wavelengths
Perceptually correct computation Requires taking into account psychophysics
Pretty good approximation: Separate light into red, green, blue components Process each component independently
11
Color illusion
The squares marked A and B are the same shade of gray
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
12
Color Illusion -- proof
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
13
Color Spaces
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
14
Material Colors
Inherent “material color” which is the color that the object reflects Material reflects different wavelengths of light different amounts
In RGB, have a reflectivity amount for each of red, green, blue
An object can’t reflect more light than it receives Maximum: reflect 100% of light in all wavelengths--bright white
Reasonable: reflect 95% of light, material color =(0.95, 0.95, 0.95)
Material colors range from 0.0 to 1.0 in RGB
15
Light Color
No limit to total light intensity reflecting from surface Can make individual light source brighter Can add more lights
Represent a light source using intensity in RGB Range from 0.0 up There is no upper limit to the intensity of light In other words, a bright white light might have color (10,10,10)
Units? physically-based rendering: photon power flux density in practice: arbitrary units (“my light goes up to 11”)
16
Color & Intensity
Distinction between material color and light color: Material colors represent the proportion of light reflected
Light colors represent the actual intensity of a beam of light
We never perceive the inherent material color All we see is the light reflected off of a material
Shine a red light…• on a white or red surface: the object appears red• on a grey surface: the object appears dark red• on a blue surface: the object appears black
17
Exposure and Display
What do we mean by “white”? Human eyes (and digital cameras) adjust exposure settings
automatically In a moderatly lit room, intensity 0.5 might appear as white In bright sunlight, intensity 100 might appear as (same) white
The monitor has an upper limit to the brightness it can display RGB units: 0=no light at the pixel, 1=full intensity at the pixel exact color light that emerges depends on monitor properties
• brightness, contrast, white point, color balance, … Final result of lighting calculation shouldn’t be more than 1.0 Advanced techniques: exposure control, AKA “tone mapping”
In practice: Assume intensity (1,1,1) is white clamp all final color values to 0.0-1.0 range before storing in
pixel
18
Outline for today
Overview of lighting Notes on color Local Illumination Light Sources Shading Advanced Lighting
19
Local Illumination
AKA Local Lighting Models Light on a point on the surface (vertex) Assume we have an incident ray of light
Light coming from a known direction With a given RGB color (intensity)
We will build up empirical material properties Fancy name: Bidirectional Reflectance Distribution Function “BRDF”
20
Reflectivity
White sheet of paper might reflect 95% of incident light
A mirror might reflect 95% of incedent light
Yet, these two things look completely different: They reflect light in different directions The paper is a diffuse reflector The mirror is a specular reflector
21
“Standard” Lighting Model
Consists of three terms linearly combined: Diffuse component for the amount
of incoming light reflected equally in all directions
Specular component for the amount of light reflected in a mirror-like fashion
Ambient term to approximate light arriving via other surfaces
This is very simple approximation particularly good for plastic particularly good for metal That’s why CG images tend to
look like plastic and metal
22
Diffuse Reflection
An ideal diffuse reflector receives light from some direction and bounces it uniformly in all directions very rough at microscopic level
Diffuse materials have a dull or matte appearance example: chalk
23
n
Diffuse Reflection
Assume a beam of parallel rays shining on the surface
Consider area of the surface covered by the beam varies based on the angle between the beam and the normal
The larger this area, the less incident light per area
The incident light per unit area is proportional to the cosine of the angle between the normal and the light rays
Object darkens as normal turns away from light This is known as Lambert’s cosine law Diffuse surfaces AKA Lambertian surfaces
24
Lambert’s Cosine Law
25
Given:rn=surface unit normalrL =unit vector pointing towards the light
θi =angle of incidence, between rL and
rn
kd =material diffuse reflectance coefficient (0.0 to 1.0)cl =color (intensity) of the light source
Diffuse color:
cd =kdcl cos(θi ) =kdcl (rL ⋅
rn)
Notes: Depends on light and normal directions Doesn’t depend on eye position
• diffuse reflection is same in every direction Don’t want to illuminate from rear
• use
Diffuse Reflection
kdcl max(rL⋅
rn,0)
cl
kd
rL
rn
θi
26
Diffuse Lighting Examples
A Lambertian sphere at several different lighting angles:
Diffuse lighting provides visual cues indicates 3D depth indicates surface curvature
27
Multiple Lights
Can have many light sources in a scene Light (generally) behaves additively Add up the contribution of each light
c =kd cl i
(rL i ⋅ ∑ r
n)
28
In the real world, light gets bounced all around the environment Resulting light illuminates surface from every direction. Global illumination techniques attempt to compute this.
Complex. Simple approximation (hack): Ambient light
Assume net effect is a constant color shining from every direction
Add to the net color, attenuated by reflectance coefficient
Effect of ambient light: Keeps unlit areas from going completely black Makes things look flatter
• with ambient and no diffuse: object has solid color, is completely 2D
ka or ca usually small (.1 or less)
Ambient Light
c =kaca + kdcli
rL i ⋅
rn( )∑
29
Specular Reflection
Shiny surfaces exhibit specular reflection Polished metal Glossy car finish Plastics
A light shining on a specular surface causes a bright spot: known as a specular highlight essentially, a rough reflection of the light source
Highlight location depends viewer position relative to surface & lights
30
Specular Reflection
An ideal specular reflector = mirror perfectly smooth surface bounces an incoming light ray in a single direction
angle of incidence equals the angle of reflection
31
p
€
θr =θ i€
θr
€
θi
Law of Reflection
Angle of reflectance = angle of incidence
rR +
rL =2cosθ rn=2
rL ⋅
rn( )
rn
rR =2
rL ⋅
rn( )
rn−
rL
rL
rR
rn
rL
32
Specular (Glossy) Reflection
Many materials not quite perfect mirrors Glossy materials look shiny and will show specular highlights
In CG, this is sometimes referred to as glossy reflection
Many formulations for this First: most basic and famous: Phong lighting model (1973)
Then: most popular: Blinn lighting model (1977)
33
The surface roughness will vary from material to material Smooth surfaces have sharp highlights Rougher surfaces have larger, more blurry highlights
Assume surface composed of microfacets with random orientation Smooth surfaces: microfacet normals very close to surface
normal Rough surfaces: microfacet normals are spread around more
• on average, microfacet normals close to surface normal
Polished:
Smooth:
Rough:
Very rough:
Shiny materials
34
Empirical observation
In general, we expect most reflected light to travel in direction of exact reflection
But because of microscopic surface variations, some light may be reflected in a direction slightly off the ideal reflected ray
So: Most reflected light in direction of ideal reflection Brightest when eye vector (view vector) is aligned with reflection
Intensity decreases as eye vector angle from reflection increases
Use dot product of eye vector with reflection vector
35
parameters: specular reflectance coefficient, ks
Phong Exponent p controls the apparent size of the specularity• Higher p, smaller highlight
Phong Lighting Model
ks
rL
rn
θi
cs =kscl
rR ⋅
re( )
p
θr
cl rR
re
36
Phong Lighting Model Examples
p=1 p=2 p=4
p=8 p=16 p=32
p=64 p=128 p=256
37
Blinn Lighting Model
• Define the unit-length "halfway" vector rh,
halfway between rL and
re :
rh =
rL +
re
rL +
re
• rh represents the normal of a microfacet we would
need to have oriented so as to reflect the light to the eye
ks
rL
rncl
re
rh
38
Blinn Lighting Model
• Microfacets roughly line up with surface
• The farther rh is from
rn, the less we expect microfact to align
• So use cos ine of angle between them :
c = ksclrh ⋅
rn( )
s
• Very s imilar in effect to Phong model
• Optimization due to Schick (1994):
replace t s with t
s − st + t
• Fas ter than exponent. Result is different but acceptable
ks
rL
rncl
re
rh
39
Complete Blinn Lighting Model
Add to ambient and diffuse Add specular contribution for each light
It appears in a few slightly different forms and in a wide variety of notations…
c =kaca + cli
kd
rL i ⋅
rn( ) + ks
rhi ⋅
rn( )
s
( )∑
40
Note on color
Do this in parallel for R,G,B Coefficients ka, kd, ks can be different for each of R,G,B This defines the material ambient color, diffuse color, and specular color.
Other (expensive) terms in expression are shared for each of R,G,B
Generally, use ambient color = diffuse color For metals, specular color = diffuse color
highlight is color of the material For plastics, specular color = white
highlight is the color of the light
c =kaca + cli
kd
rL i ⋅
rn( ) + ks
rhi ⋅
rn( )
s
( )∑
41
Note on normals and spaces
Lighting depends on angles between normals, vectors Must be in space that preserves angles
World Space or Camera Space Not normalized view space: perspective doesn’t preserve
angles Conveniently, we can put world-space normals as per-
vertex data Doesn’t get transformed by projection.
But remember, when taking normals from object to world space: if world transform has nonuniform scale, normals must use
inverse-transpose if world transform has uniform scale, normals must be
renormalized if world transform has no scales, normals transform like
vectors
42
General Lighting Models
General form: Bidirectional Reflectance Distribution Function BRDF,
B()
There are many lighting models Phong Blinn Cook-Torrance
• includes “roughness”, other parameters Gooch
• non-photorealistic, for illustrations Ward
• includes anisotropy etc…
c = cli
B(rL ,
re)∑
43
Cook-Torrance
44
Gooch
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
45
Anisotropy
Material reflects differently in different directions
E.g., brushed metal; Ward’s model
isotropic anisotropic
46
Outline for today
Overview of lighting Notes on color Local Illumination Light Sources Shading Advanced Lighting
47
Light Sources
In general, light sources can have complex properties Geometric area over which light is produced Anisotropy in direction Variation in color
Some very simple light sources models are standard
48
Light Sources
Two aspects of light sources are important for a local shading model: Where is the light coming from (the L vector)? How much light is coming (the I values)?
Various light source types give different answers to the above questions: Directional: Light from a specific direction Point light source: Light from a specific point Spotlight: Light from a specific point with intensity that depends on the direction
49
Directional Light
When light is coming from a distant source light rays are parallel light ray direction is the same everywhere in the scene
as if the source were infinitely far away good approximation to sunlight
Specified by a unit length direction vector, and a colorcsrc
rL
rn
rd
rL=−
rd
cl =csrc
cl
50
cl
v
pcsrc
Point Lights
For closer light sources, such as light bulbs
Model as a point that radiates light in all directions equally Light vector varies across the surface Intensity from a point light source drops off proportionally to the inverse square of the distance from the light
rL
rn
rL=
p−vp−v
cl =csrc
p−v 2 rn
rLcl
v
51
Attenuation
Sometimes, it is desirable to modify the inverse square falloff behavior of point lights A common (although not physically accurate) model for the distance attenuation is:
cl =csrc
kc + kl p−v + kq p−v 2
52
Like point source, but intensity depends on direction: Position: the location of the source Spot direction: the center axis of the light Requires falloff parameters:
• how broad the beam is (cone angle)• how light tapers off at edges of he beam (cosine exponent)
Spotlights
csrc
rL
rn
rd
cl
rL=
p−vp−v
cl =0 if −
rL ⋅
rd < cos(θmax)
csrc −rL ⋅
rd( )
fotherwise
⎧⎨⎪
⎩⎪
v
p
53
Spotlights
54
Outline for today
Overview of lighting Notes on color Local Illumination Light Sources Shading Advanced Lighting
55
Shading
Various techniques: Flat Gouraud Phong Cartoon Procedural
56
Flat shading
Compute shading at a representative point and apply to whole polygon OpenGL uses one of the vertices
Advantages: Fast - one shading computation per polygon, fill entire polygon with same color
Disadvantages: Inaccurate Faceted
57
Gouraud Shading
Light each vertex with its own location and normal Result is a color at each vertex
Interpolate the colors across each triangle
Default mode for most hardware Advantages:
Fast: incremental calculations when rasterizing Much smoother - use one normal per shared vertex to get continuity between faces
Disadvantages: Don’t get smooth specular highlights C1-discontinuities in colorcause Mach bands: perceptualillusions of edges
58
Mach bands
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
59
Phong Shading Want to recompute lighting at every pixel
Need normal vector at each pixel Interpolate vertex normals
Interpolate the normals while scan-converting Typically, bilinearly interpolate x,y,z; then renormalize Also interpolate or transform to get position in
world/camera space Known as Phong Shading or Phong Interpolation
Not to be confused with Phong Lighting Model (though both are often used at the same time)
Advantages: More accurate Better images
Disadvantages: Slow Still not completely accurate
Modern GPUs can perform Phong shading via pixel shaders
60
Gouraud vs. Phong shading
Gouraud misses specular hilights
solution: tessellate more finely: use smaller triangles that would help with the polygonal artifacts on the silhouettes too
Gouraud Phong
61
Linear interpolation in screen space doesn’t align with linear interpolation in world space
Solutions: Do hyperbolic interpolation (see Buss) Break up into smaller triangles
Z – into the scene
Imageplane
Note: Perspective Distortion
62
Cartoon Shading
General approach: Compute diffuse or Phong lighting Remap continuous intensityto two (or a few) levels
Some trickery to draw dark edges
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
63
Procedural Shading
Software or hardware renderer calls an arbitrary routine at every pixel (or sub-pixel) Extreme of “do whatever you want” Can typically implement Phong or any other lighting model Used for patterns or other special effects Used for toon shading
Routine typically written in a special-purpose language RenderMan Shading Language (Pixar) Cg (Nvidia) HLSL (DirectX)
Routine: can get surface normal, position, material properties,
light locations, … computes a color value sometimes can even change the surface position
(displacement)
64
Procedural shading
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.QuickTime™ and a
TIFF (Uncompressed) decompressorare needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.
65
Procedural Shading
surface turbulence (float Kd=.8, Ka=.2){ float a,scale,sum; point M; M = transform( "marble", P ); scale = 1; sum = 0; a = sqrt(area(M)); while( a < scale ) { sum += scale * float noise(M/scale); scale *= 0.5; } Oi = sum; Ci = Cs * Oi * (Ka + Kd * I.N * I.N /(I.I * N.N));}
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
xform to object space
fractal
diffuse lighting (variant)
66
Programmable GPUs
Two kinds of routines: vertex shaders: routine run for each vertex pixel shaders: routine run for each pixel
Limits on size of program amount of memory available types of memory and texture access (will talk about texture later)
etc. Technology still improving, capabilities keep expanding
67
Outline for today
Overview of lighting Notes on color Local Illumination Light Sources Shading Advanced Lighting
68
Lighting Review
Local Illumination Lighting Models Ambient
• Normals don’t matter Lambert
• Angle between surface normal and light Phong
• Surface normal, light, and viewpoint
What they capture: Direct illumination from light sources Diffuse and Specular reflections (Very) Approximate effects of global lighting
69
What they don’t do
Shadows Accurate reflection models Translucency, refraction Caustics (light focusing) Global Illumination Area light sources/emitters Atmospheric effects (fog, clouds) Motion blur Depth of field Lens imperfections Exposure & dynamic range …
70
Advanced Lighting
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
QuickTime™ and aTIFF (Uncompressed) decompressor
are needed to see this picture.
71
Done
Next class: more with lighting… Upcoming: Guest Speakers!
Check course web page for info