lighting/shading i week 6, fri feb 12
TRANSCRIPT
University of British ColumbiaCPSC 314 Computer Graphics
Jan-Apr 2010
Tamara Munzner
http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010
Lighting/Shading I
Week 6, Fri Feb 12
2
Correction: W2V vs. V2W
• MW2V=TR
• we derived position of camera in world• invert for world with respect to camera
• MV2W=(MW2V)-1=R-1T-1
!
T =
1 0 0 ex
0 1 0 ey
0 0 1 ez
0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
!
R =
ux vx wx 0
uy vy wy 0
uz vz wz 0
0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
!
Mview2world
=
ux uy uz 0
vx vy vz 0
wx wy wz 0
0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
1 0 0 (ex0 1 0 (ey0 0 1 (ez0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
=
ux uy uz (e •u
vx vy vz (e • v
wx wy wz (e •w
0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
slide 38 week3.day3 (Fri Jan 22)
3
Correction: W2V vs. V2W
• MV2W=(MW2V)-1=R-1T-1
!
Mview2world
=
ux uy uz 0
vx vy vz 0
wx wy wz 0
0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
1 0 0 (ex0 1 0 (ey0 0 1 (ez0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
=
ux uy uz (e •u
vx vy vz (e • v
wx wy wz (e •w
0 0 0 1
"
#
$ $ $ $
%
&
' ' ' '
!
MV2W
=
ux uy uz "ex # ux + "ey # uy + "ez # uzvx vy vz "ex #vx + "ey #vy + "ez #vzwx wy wz "ex #wx + "ey #wy + "ez #wz
0 0 0 1
$
%
& & & &
'
(
) ) ) )
4
Correction: Perspective Derivation
!
x '
y '
z'
w'
"
#
$ $ $ $
%
&
' ' ' '
=
E 0 A 0
0 F B 0
0 0 C D
0 0 (1 0
"
#
$ $ $ $
%
&
' ' ' '
x
y
z
1
"
#
$ $ $ $
%
&
' ' ' '
!
y'= Fy + Bz, y'
w'=Fy + Bz
w', 1=
Fy + Bz
w', 1=
Fy + Bz
"z,
1= Fy
"z+ B
z
"z, 1= F
y
"z" B, 1= F
top
"("near)" B,
!
x'= Ex + Az
y'= Fy + Bz
z'= Cz + D
w'= "z
!
x = left " # x / # w = $1
x = right " # x / # w =1
y = top " # y / # w =1
y = bottom " # y / # w = $1
z = $near " # z / # w =1
z = $ far " # z / # w = $1
!
1= Ftop
near" B
z axis flip! slide 30 week4.day3 (Fri Jan 29)
5
News
• P2 due date extended to Tue Mar 2 5pm• V2W correction affects Q1 and thus cascades
to Q4-Q7• perspective correction affects Q8
• TA office hours in lab for P2/H2 questions Fri2-4 (Garrett)
PPT Fix: Basic Line Drawing
• assume• , slope
• one octant, other cases symmetric• how can we do this more quickly?
• goals• integer coordinates• thinnest line with no gaps
00
01
01 )()(
)(yxx
xx
yyy
bmxy
+!!
!=
+=
!
Line ( x0, y0, x1, y1)
begin
float dx, dy, x, y, slope ;
dx" x1 # x0;
dy" y1 # y0;
slope"dydx
;
y" y0
for x from x0 to x1 do
begin
PlotPixel ( x, Round (y) ) ;
y" y + slope ;
end ;
end ;!
0 <dydx
<1
!
x0
< x1
7
Clarification/Correction II: Midpoint• we're moving horizontally along x direction (first octant)
• only two choices: draw at current y value, or move up vertically toy+1?
• check if midpoint between two possible pixel centers above or below line• candidates
• top pixel: (x+1,y+1)• bottom pixel: (x+1, y)
• midpoint: (x+1, y+.5)• check if midpoint above or below line
• below: pick top pixel• above: pick bottom pixel
• other octants: different tests• octant II: y loop, check x left/right above: bottom pixel
below: top pixel
8
Review: Triangulating Polygons• simple convex polygons
• trivial to break into triangles• pick one vertex, draw lines to all others not
immediately adjacent• OpenGL supports automatically
• glBegin(GL_POLYGON) ... glEnd()
• concave or non-simple polygons• more effort to break into triangles• simple approach may not work• OpenGL can support at extra cost
• gluNewTess(), gluTessCallback(), ...
10
PPT Fix: Flood Fill• draw edges• run:
• drawbacks?
!
FloodFill(Polygon P, int x, int y, Color C)
if not (OnBoundary(x,y,P) or Colored(x,y,C))
begin
PlotPixel(x,y,C);
FloodFill(P,x +1,y,C);
FloodFill(P,x,y +1,C);
FloodFill(P,x,y "1,C);
FloodFill(P,x "1,y,C);
end ;
11
Review: Scanline Algorithms
• scanline: a line of pixels in an image• set pixels inside polygon boundary along
horizontal lines one pixel apart vertically• parity test: draw pixel if edgecount is odd• optimization: only loop over axis-aligned
bounding box of xmin/xmax, ymin/ymax
1
2
3
4
5=0P
12
Review: Bilinear Interpolation
• interpolate quantity along L and R edges,as a function of y
• then interpolate quantity as a function of x
yy
P(x,y)P(x,y)
PP11
PP22
PP33
PPLL PPRR
13
1P
3P
2P
P
Review: Barycentric Coordinates• non-orthogonal coordinate system based on
triangle itself• origin: P1, basis vectors: (P2-P1) and (P3-P1)
γ=1
γ=0
β=1
β=0
α=1
α=0
P = P1 + β(P2-P1)+γ(P3-P1)P = (1-β−γ)P1 + βP2+γP3
P = αP1 + βP2+γP3
α + β+ γ = 10 <= α, β, γ <= 1 ((α,β,γα,β,γ) =) =
(0,1,0)(0,1,0)
((α,β,γα,β,γ) =) =(1,0,0)(1,0,0)
((α,β,γα,β,γ) =) =(0,0,1)(0,0,1)
14
Using Barycentric Coordinates• weighted combination of vertices• smooth mixing• speedup
• compute once per triangle
• demohttp://www.cut-the-knot.org/Curriculum/Geometry/Barycentric.shtml
1P
3P
2P
P
((α,β,γα,β,γ) =) =(1,0,0)(1,0,0)
((α,β,γα,β,γ) =) =(0,1,0)(0,1,0)
((α,β,γα,β,γ) =) =(0,0,1)(0,0,1) 5.0=!
1=!
0=!
321PPPP !+!+!= "#$
1,,0
1
!!
=++
"#$
"#$
““convex combination of pointsconvex combination of points””
for points inside triangle
15
Computing Barycentric Coordinates• 2D triangle area• half of parallelogram area
• from cross product
A = ΑP1 +ΑP2 +ΑP3
α = ΑP1 /Aβ = ΑP2 /Aγ = ΑP3 /A
3PA
1P
3P
2P
P
((α,β,γα,β,γ) =) =(1,0,0)(1,0,0)
((α,β,γα,β,γ) =) =(0,1,0)(0,1,0)
((α,β,γα,β,γ) =) =(0,0,1)(0,0,1) 2
PA
1PA
16
PP22
PP33
PP11
PPLL PPRRPPdd22 : d : d
11
3
21
12
21
2
3
21
12
21
1
23
21
12
)1(
)(
Pdd
dP
dd
d
Pdd
dP
dd
d
PPdd
dPP
L
++
+=
=+
++
!=
!+
+=
Deriving Barycentric From Bilinear
• from bilinear interpolation of point P onscanline
17
Deriving Barycentric From Bilineaer
• similarly
bb 11 :
b
:
b22
PP22
PP33
PP11
PPLL PPRRPPdd22 : d : d
11
1
21
12
21
2
1
21
12
21
1
21
21
12
)1(
)(
Pbb
bP
bb
b
Pbb
bP
bb
b
PPbb
bPP
R
++
+=
=+
++
!=
!+
+=
18
bb 11 :
b
:
b22
PP22
PP33
PP11
PPLL PPRRPPdd22 : d : d
11
3
21
1
2
21
2P
dd
dP
dd
dPL
++
+=
1
21
1
2
21
2P
bb
bP
bb
bPR
++
+=cc11: c: c22
Deriving Barycentric From Bilinear
• combining
• gives
!
P =c2
c1
+ c2
d2
d1
+ d2
P2
+d1
d1
+ d2
P3
"
# $
%
& ' +
c1
c1
+ c2
b2
b1
+ b2
P2
+b1
b1
+ b2
P1
"
# $
%
& '
!
P =c2
c1+ c
2
" PL
+c1
c1+ c
2
" PR
19
Deriving Barycentric From Bilinear
• thus P = αP1 + βP2 + γP3 with
• can verify barycentric properties
1,,0,1 !!=++ "#$"#$!
" =c1
c1+ c
2
b1
b1+ b
2
# =c2
c1+ c
2
d2
d1+ d
2
+c1
c1+ c
2
b2
b1+ b
2
$ =c2
c1+ c
2
d1
d1+ d
2
21
Rendering Pipeline
GeometryDatabase
Model/ViewTransform. Lighting Perspective
Transform. Clipping
ScanConversion
DepthTestTexturing Blending
Frame-buffer
22
Projective Rendering Pipeline
OCS - object/model coordinate system
WCS - world coordinate system
VCS - viewing/camera/eye coordinatesystem
CCS - clipping coordinate system
NDCS - normalized device coordinatesystem
DCS - device/display/screen coordinatesystem
OCSOCS O2WO2W VCSVCS
CCSCCS
NDCSNDCS
DCSDCS
modelingmodelingtransformationtransformation
viewingviewingtransformationtransformation
projectionprojectiontransformationtransformation
viewportviewporttransformationtransformation
perspectiveperspectivedividedivide
object world viewing
device
normalizeddevice
clipping
W2VW2V V2CV2C
N2DN2D
C2NC2N
WCSWCS
23
Goal• simulate interaction of light and objects• fast: fake it!
• approximate the look, ignore real physics• get the physics (more) right
• BRDFs: Bidirectional Reflection Distribution Functions• local model: interaction of each object with light• global model: interaction of objects with each other
24
Photorealistic Illumination
[[electricimageelectricimage.com].com]
•transport of energy from light sources to surfaces & points•global includes direct and indirect illumination – more later
Henrik Wann Henrik Wann JensenJensen
25
Illumination in the Pipeline• local illumination
• only models light arriving directly from lightsource
• no interreflections or shadows• can be added through tricks, multiple
rendering passes• light sources
• simple shapes• materials
• simple, non-physical reflection models
26
Light Sources• types of light sources• glLightfv(GL_LIGHT0,GL_POSITION,light[])
• directional/parallel lights• real-life example: sun• infinitely far source: homogeneous coord w=0
• point lights• same intensity in all directions
• spot lights• limited set of directions:
• point+direction+cutoff angle
!!!!
"
#
$$$$
%
&
0
z
y
x
!!!!
"
#
$$$$
%
&
1
z
y
x
27
Light Sources
• area lights• light sources with a finite area• more realistic model of many light sources• not available with projective rendering pipeline
(i.e., not available with OpenGL)
28
Light Sources
• ambient lights• no identifiable source or direction• hack for replacing true global illumination
• (diffuse interreflection: light bouncing off fromother objects)
30
Ambient Light Sources
• scene lit only with an ambient light source
Light PositionNot Important
Viewer PositionNot Important
Surface AngleNot Important
31
Directional Light Sources
• scene lit with directional and ambient light
Light PositionNot Important
Viewer PositionNot Important
Surface AngleImportant
32
Point Light Sources
• scene lit with ambient and point light source
Light PositionImportant
Viewer PositionImportant
Surface AngleImportant
33
Light Sources• geometry: positions and directions• standard: world coordinate system
• effect: lights fixed wrt world geometry• demo:
http://www.xmission.com/~nate/tutors.html• alternative: camera coordinate system
• effect: lights attached to camera (car headlights)• points and directions undergo normal
model/view transformation• illumination calculations: camera coords
34
Types of Reflection• specular (a.k.a. mirror or regular) reflection causes
light to propagate without scattering.
• diffuse reflection sends light in all directions withequal energy.
• mixed reflection is a weightedcombination of specular and diffuse.
36
Types of Reflection
• retro-reflection occurs when incident energyreflects in directions close to the incidentdirection, for a wide range of incidentdirections.
• gloss is the property of a material surfacethat involves mixed reflection and isresponsible for the mirror like appearance ofrough surfaces.
37
Reflectance Distribution Model
• most surfaces exhibit complex reflectances• vary with incident and reflected directions.• model with combination
+ + =
specular + glossy + diffuse = reflectance distribution
38
Surface Roughness
• at a microscopic scale, allreal surfaces are rough
• cast shadows onthemselves
• “mask” reflected light:shadow shadow
Masked Light
39
Surface Roughness
• notice another effect of roughness:• each “microfacet” is treated as a perfect mirror.• incident light reflected in different directions by
different facets.• end result is mixed reflectance.
• smoother surfaces are more specular or glossy.• random distribution of facet normals results in diffuse
reflectance.
40
Physics of Diffuse Reflection
• ideal diffuse reflection• very rough surface at the microscopic level
• real-world example: chalk• microscopic variations mean incoming ray of
light equally likely to be reflected in anydirection over the hemisphere
• what does the reflected intensity depend on?
41
Lambert’s Cosine Law
• ideal diffuse surface reflectionthe energy reflected by a small portion of a surface from a light sourcein a given direction is proportional to the cosine of the angle betweenthat direction and the surface normal
• reflected intensity• independent of viewing direction• depends on surface orientation wrt light
• often called Lambertian surfaces
42
Lambert’s Law
intuitively: cross-sectional area ofthe “beam” intersecting an elementof surface area is smaller for greaterangles with the normal.
43
Computing Diffuse Reflection• depends on angle of incidence: angle between surfacenormal and incoming light• Idiffuse = kd Ilight cos θ
• in practice use vector arithmetic• Idiffuse = kd Ilight (n • l)
• always normalize vectors used in lighting!!!• n, l should be unit vectors
• scalar (B/W intensity) or 3-tuple or 4-tuple (color)• kd: diffuse coefficient, surface color• Ilight: incoming light intensity• Idiffuse: outgoing light intensity (for diffuse reflection)
nl
θ
44
Diffuse Lighting Examples
• Lambertian sphere from several lightingangles:
• need only consider angles from 0° to 90°• why?• demo: Brown exploratory on reflection• http://www.cs.brown.edu/exploratories/freeSoftware/repository/edu/brown/cs/ex
ploratories/applets/reflection2D/reflection_2d_java_browser.html