University of British ColumbiaCPSC 314 Computer Graphics
Jan-Apr 2010
Tamara Munzner
http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010
Viewing III
Week 4, Wed Jan 27
2
News
• extra TA office hours in lab 005• Tue 2-5 (Kai)
• Wed 2-5 (Garrett)
• Thu 1-3 (Garrett), Thu 3-5 (Kai)
• Fri 2-4 (Garrett)
• Tamara's usual office hours in lab• Fri 4-5
3
Review: Convenient Camera Motion
• rotate/translate/scale versus• eye point, gaze/lookat direction, up vector
Peye
Pref
upview
eye
lookaty
z
xWCS
4
Review: World to View Coordinates
• translate eye to origin
• rotate view vector (lookat – eye) to w axis
• rotate around w to bring up into vw-plane
y
z
xWCS
v
u
VCS
Peyew
Pref
upview
eye
lookat
5
Review: 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 −ex
0 1 0 −ey
0 0 1 −ez
0 0 0 1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
ux uy uz −ex
vx vy vz −ey
wx wy wz −ez
0 0 0 1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
6
Review: Graphics Cameras
• real pinhole camera: image inverted
imageimageplaneplane
eyeeye pointpoint
computer graphics camera: convenient equivalent
imageimageplaneplane
eyeeye pointpoint
center ofcenter ofprojectionprojection
7
Review: Projective Transformations
• planar geometric projections• planar: onto a plane
• geometric: using straight lines
• projections: 3D -> 2D
• aka projective mappings
• counterexamples?
8
Projective Transformations
• properties• lines mapped to lines and triangles to triangles• parallel lines do NOT remain parallel
• e.g. rails vanishing at infinity
• affine combinations are NOT preserved• e.g. center of a line does not map to center of
projected line (perspective foreshortening)
9
Perspective Projection
• project all geometry • through common center of projection (eye point)
• onto an image plane
xxzz xxzz
yy
xx
-z-z
10
Perspective Projection
how tall shouldthis bunny be?
projectionplane
center of projection(eye point)
11
Basic Perspective Projection
similar trianglessimilar triangles
zz
P(x,y,z)P(x,y,z)
P(x’,y’,z’)P(x’,y’,z’)
z’=dz’=d
yy
• nonuniform foreshortening• not affine
butbut€
y'
d=
y
z→ y '=
y ⋅dz
€
x'
d=
x
z→ x '=
x ⋅dz
€
z'= d
12
Perspective Projection
• desired result for a point [x, y, z, 1]T projected onto the view plane:
• what could a matrix look like to do this?
dzdz
y
z
dyy
dz
x
z
dxx
z
y
d
y
z
x
d
x
==⋅
==⋅
=
==
',','
',
'
13
Simple Perspective Projection Matrix
⎥⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢⎢
⎣
⎡
d
dz
y
dz
x
/
/
14
Simple Perspective Projection Matrix
⎥⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢⎢
⎣
⎡
d
dz
y
dz
x
/
/ is homogenized version of
where w = z/d ⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
dz
z
y
x
/
15
Simple Perspective Projection Matrix
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
=
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
10100
0100
0010
0001
/
z
y
x
ddz
z
y
x⎥⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢⎢
⎣
⎡
d
dz
y
dz
x
/
/ is homogenized version of
where w = z/d ⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
dz
z
y
x
/
16
Perspective Projection
• expressible with 4x4 homogeneous matrix• use previously untouched bottom row
• perspective projection is irreversible• many 3D points can be mapped to same
(x, y, d) on the projection plane
• no way to retrieve the unique z values
17
Moving COP to Infinity
• as COP moves away, lines approach parallel• when COP at infinity, orthographic view
18
Orthographic Camera Projection
• camera’s back plane parallel to lens
• infinite focal length
• no perspective convergence
• just throw away z values
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡=
⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
0
y
x
z
y
x
p
p
p
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
=
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
11000
0000
0010
0001
1
z
y
x
z
y
x
p
p
p
19
Perspective to Orthographic
• transformation of space• center of projection moves to infinity
• view volume transformed • from frustum (truncated pyramid) to
parallelepiped (box)
-z-z
xx
-z-z
xx
FrustumFrustum ParallelepipedParallelepiped
20
View Volumes
• specifies field-of-view, used for clipping
• restricts domain of z stored for visibility test
z
perspective view volumeperspective view volume orthographic view volumeorthographic view volume
x=left
x=right
y=top
y=bottom z=-near z=-farxVCS x
z
VCS
yy
x=lefty=top
x=right
z=-far
z=-neary=bottom
21
Canonical View Volumes
• standardized viewing volume representation
perspective orthographic orthogonal parallel
x or y
-z
1
-1
-1frontplane
backplane
x or y
-zfrontplane
backplane
x or y = +/- z
22
Why Canonical View Volumes?
• permits standardization• clipping
• easier to determine if an arbitrary point is enclosed in volume with canonical view volume vs. clipping to six arbitrary planes
• rendering• projection and rasterization algorithms can be
reused
23
Normalized Device Coordinates
• convention• viewing frustum mapped to specific
parallelepiped• Normalized Device Coordinates (NDC)• same as clipping coords
• only objects inside the parallelepiped get rendered
• which parallelepiped? • depends on rendering system
24
Normalized Device Coordinates
left/right x =+/- 1, top/bottom y =+/- 1, near/far z =+/- 1
-z-z
xx
FrustumFrustum
z=-nz=-n z=-fz=-f
rightright
leftleft zz
xx
x= -1x= -1z=1z=1
x=1x=1
Camera coordinatesCamera coordinates NDCNDC
z= -1z= -1
25
Understanding Z
• z axis flip changes coord system handedness• RHS before projection (eye/view coords)
• LHS after projection (clip, norm device coords)
x
z
VCS
yx=left
y=top
x=right
z=-far
z=-neary=bottom
x
z
NDCS
y
(-1,-1,-1)
(1,1,1)
26
Understanding Z
near, far always positive in OpenGL calls
glOrtho(left,right,bot,top,near,far);glOrtho(left,right,bot,top,near,far); glFrustum(left,right,bot,top,near,far);glFrustum(left,right,bot,top,near,far); glPerspective(fovy,aspect,near,far);glPerspective(fovy,aspect,near,far);
orthographic view volumeorthographic view volume
x
z
VCS
yx=left
y=top
x=right
z=-far
z=-neary=bottom
perspective view volumeperspective view volume
x=left
x=right
y=top
y=bottom z=-near z=-farxVCS
y
27
Understanding Z
• why near and far plane?• near plane:
• avoid singularity (division by zero, or very small numbers)
• far plane:• store depth in fixed-point representation
(integer), thus have to have fixed range of values (0…1)
• avoid/reduce numerical precision artifacts for distant objects
28
Orthographic Derivation
• scale, translate, reflect for new coord sys
x
z
VCS
yx=left
y=top
x=right
z=-far
z=-neary=bottom
x
z
NDCS
y
(-1,-1,-1)
(1,1,1)
29
Orthographic Derivation
• scale, translate, reflect for new coord sys
x
z
VCS
yx=left
y=top
x=right
z=-far
z=-neary=bottom
x
z
NDCS
y
(-1,-1,-1)
(1,1,1)
byay +⋅='1'
1'
−=→==→=
ybotyytopy
30
Orthographic Derivation
• scale, translate, reflect for new coord sys
byay +⋅='1'
1'
−=→==→=
ybotyytopy
bottop
bottopb
bottop
topbottopb
bottop
topb
btopbottop
−−−
=
−⋅−−
=
−⋅
−=
+−
=
2)(
21
21
bbota
btopa
+⋅=−+⋅=
11
bottopa
topbota
topabota
botatopa
botabtopab
−=
+−=⋅−−⋅−=−−
⋅−−=⋅−⋅−−=⋅−=
2)(2
)()1(111
1,1
31
Orthographic Derivation
• scale, translate, reflect for new coord sys
x
z
VCS
yx=left
y=top
x=right
z=-far
z=-neary=bottom
byay +⋅='1'
1'
−=→==→=
ybotyytopy
bottop
bottopb
bottopa
−+
−=
−=
2
same idea for right/left, far/near same idea for right/left, far/near
32
Orthographic Derivation
• scale, translate, reflect for new coord sys
P
nearfar
nearfar
nearfar
bottop
bottop
bottop
leftright
leftright
leftright
P
⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢
⎣
⎡
−+
−−−
−+
−−
−+
−−
=
1000
200
02
0
002
'
33
Orthographic Derivation
• scale, translate, reflect for new coord sys
P
nearfar
nearfar
nearfar
bottop
bottop
bottop
leftright
leftright
leftright
P
⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢
⎣
⎡
−+
−−−
−+
−−
−+
−−
=
1000
200
02
0
002
'
34
Orthographic Derivation
• scale, translate, reflect for new coord sys
P
nearfar
nearfar
nearfar
bottop
bottop
bottop
leftright
leftright
leftright
P
⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢
⎣
⎡
−+
−−−
−+
−−
−+
−−
=
1000
200
02
0
002
'
35
Orthographic Derivation
• scale, translate, reflect for new coord sys
P
nearfar
nearfar
nearfar
bottop
bottop
bottop
leftright
leftright
leftright
P
⎥⎥⎥⎥⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢⎢⎢⎢⎢
⎣
⎡
−+
−−−
−+
−−
−+
−−
=
1000
200
02
0
002
'
36
Orthographic OpenGL
glMatrixMode(GL_PROJECTION);glMatrixMode(GL_PROJECTION);glLoadIdentity();glLoadIdentity();glOrtho(left,right,bot,top,near,far);glOrtho(left,right,bot,top,near,far);
37
Demo
• Brown applets: viewing techniques• parallel/orthographic cameras
• projection cameras
• http://www.cs.brown.edu/exploratories/freeSoftware/catalogs/viewing_techniques.html
38
Projections II
39
Asymmetric Frusta
• our formulation allows asymmetry• why bother?
-z-z
xx
FrustumFrustumrightright
leftleft-z-z
xx
FrustumFrustum
z=-nz=-n z=-fz=-f
rightright
leftleft
40
Asymmetric Frusta
• our formulation allows asymmetry• why bother? binocular stereo
• view vector not perpendicular to view plane
Right EyeRight Eye
Left EyeLeft Eye
41
Simpler Formulation
• left, right, bottom, top, near, far• nonintuitive
• often overkill
• look through window center• symmetric frustum
• constraints• left = -right, bottom = -top
42
Field-of-View Formulation
• FOV in one direction + aspect ratio (w/h)• determines FOV in other direction
• also set near, far (reasonably intuitive)
-z-z
xx
FrustumFrustum
z=-nz=-n z=-fz=-f
fovx/2fovx/2
fovy/2fovy/2hh
ww
43
Perspective OpenGL
glMatrixMode(GL_PROJECTION);glLoadIdentity();
glFrustum(left,right,bot,top,near,far); orglPerspective(fovy,aspect,near,far);
44
Demo: Frustum vs. FOV
• Nate Robins tutorial (take 2):
• http://www.xmission.com/~nate/tutors.html
45
Projective Rendering Pipeline
OCS - object/model coordinate system
WCS - world coordinate system
VCS - viewing/camera/eye coordinate system
CCS - clipping coordinate system
NDCS - normalized device coordinate system
DCS - device/display/screen coordinate system
OCSOCS O2WO2W VCSVCS
CCSCCS
NDCSNDCS
DCSDCS
modelingmodelingtransformationtransformation
viewingviewingtransformationtransformation
projectionprojectiontransformationtransformation
viewportviewporttransformationtransformation
perspectiveperspectivedividedivide
object world viewing
device
normalizeddevice
clipping
W2VW2V V2CV2C
N2DN2D
C2NC2N
WCSWCS
46
Projection Warp
• warp perspective view volume to orthogonal view volume
• render all scenes with orthographic projection!• aka perspective warp
Z
x
z= z=d
Z
x
z=0 z=d
47
Perspective Warp
• perspective viewing frustum transformed to cube
• orthographic rendering of cube produces same image as perspective rendering of original frustum
48
Predistortion
49
Projective Rendering Pipeline
OCS - object/model coordinate system
WCS - world coordinate system
VCS - viewing/camera/eye coordinate system
CCS - clipping coordinate system
NDCS - normalized device coordinate system
DCS - device/display/screen coordinate system
OCSOCS O2WO2W VCSVCS
CCSCCS
NDCSNDCS
DCSDCS
modelingmodelingtransformationtransformation
viewingviewingtransformationtransformation
projectionprojectiontransformationtransformation
viewportviewporttransformationtransformation
perspectiveperspectivedividedivide
object world viewing
device
normalizeddevice
clipping
W2VW2V V2CV2C
N2DN2D
C2NC2N
WCSWCS
50
Separate Warp From Homogenization
• warp requires only standard matrix multiply• distort such that orthographic projection of distorted
objects is desired persp projection• w is changed
• clip after warp, before divide• division by w: homogenization
CCSCCSNDCSNDCS
alter walter w / w/ w
VCSVCSprojectionprojection
transformationtransformation
viewing normalizeddevice
clipping
perspectiveperspectivedivisiondivision
V2CV2C C2NC2N
51
Perspective Divide Example
• specific example• assume image plane at z = -1
• a point [x,y,z,1]T projects to [-x/z,-y/z,-z/z,1]T
[x,y,z,-z]T
-z-z
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
1
z
y
x
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
−z
z
y
x
52
Perspective Divide Example
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
−−−
≡
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
−
=
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
⋅
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
−
=
⎟⎟⎟⎟⎟
⎠
⎞
⎜⎜⎜⎜⎜
⎝
⎛
⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢
⎣
⎡
1
1
/
/
10100
0100
0010
0001
1
zy
zx
z
z
y
x
z
y
x
z
y
x
T
alter walter w / w/ w
projectionprojectiontransformationtransformation
perspectiveperspectivedivisiondivision
• after homogenizing, once again w=1
Perspective Normalization
• matrix formulation
• warp and homogenization both preserve relative depth (z coordinate)
⎥⎥⎥⎥⎥⎥
⎦
⎤
⎢⎢⎢⎢⎢⎢
⎣
⎡
⎟⎠
⎞⎜⎝
⎛ −−
=⎥⎥⎥
⎦
⎤
⎢⎢⎢
⎣
⎡
zd
ddz
ydz
x
z
y
x
p
p
p
α
α1
/
/
2
54
Demo
• Brown applets: viewing techniques• parallel/orthographic cameras
• projection cameras
• http://www.cs.brown.edu/exploratories/freeSoftware/catalogs/viewing_techniques.html