viewing/projection iv week 4, fri jan 29
DESCRIPTION
http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010. Viewing/Projection IV Week 4, Fri Jan 29. News. extra TA office hours in lab 005 Fri 2-4 (Garrett) Tamara's usual office hours in lab Fri 4-5 hand in H1 here/now or in box next to 005 lab by 5pm correction: problem 6 worth 54 not 60 marks. - PowerPoint PPT PresentationTRANSCRIPT
University of British ColumbiaCPSC 314 Computer Graphics
Jan-Apr 2010
Tamara Munzner
http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010
Viewing/Projection IV
Week 4, Fri Jan 29
2
News
• extra TA office hours in lab 005• Fri 2-4 (Garrett)
• Tamara's usual office hours in lab• Fri 4-5
• hand in H1 here/now or in box next to 005 labby 5pm• correction: problem 6 worth 54 not 60 marks
3
Review: 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
homogeneoushomogeneouscoordscoords
€
y'
d=
y
z→ y '=
y ⋅dz
€
x'=x ⋅d
z
€
z'= d
€
x
z /dy
z /d
d
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥
€
1 0 0 0
0 1 0 0
0 0 1 0
0 0 1/d 1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
€
x
y
z
z /d
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
4
Review: 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
5
Review: 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)
6
Review: 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)
€
′ x
′ y
′ z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
e 0 0 f
0 a 0 b
0 0 c d
0 0 0 1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
byay +⋅='
1'
1'
−=→==→=
ybotyytopy
bottop
bottopb
bottopa
−+
−=
−=
2
7
Review: 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
'
8
Demo
• Robins demo: projection• orthographic
• perspective
9
Projections II
10
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
11
Asymmetric Frusta
• our formulation allows asymmetry• why bother? binocular stereo
• view vector not perpendicular to view plane
Right EyeRight Eye
Left EyeLeft Eye
12
Simpler Formulation
• left, right, bottom, top, near, far• nonintuitive
• often overkill
• look through window center• symmetric frustum
• constraints• left = -right, bottom = -top
13
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
14
Perspective OpenGL
glMatrixMode(GL_PROJECTION);glLoadIdentity();
glFrustum(left,right,bot,top,near,far); orglPerspective(fovy,aspect,near,far);
15
Demo: Frustum vs. FOV
• Nate Robins tutorial (take 2): projection• frustum vs perspective
16
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
17
Perspective Warp
• warp perspective view volume to orthogonal view volume
• render all scenes with orthographic projection!• aka perspective normalization
Z
x
z= z=d
Z
x
z=0 z=d
18
Perspective Warp
• perspective viewing frustum transformed to cube
• orthographic rendering of warped objects in cube produces same image as perspective rendering of original frustum
19
Predistortion
20
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
21
Separate Warp From Homogenization
• warp requires only standard matrix multiply• distort such that orthographic projection of distorted
objects shows desired perspective 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
22
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
€
x
y
z
−z
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
€
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
23
Perspective Divide Example
alter walter w / w/ w
projectionprojectiontransformationtransformation
perspectiveperspectivedivisiondivision
• after homogenizing, once again w=1
€
T
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
⎛
⎝
⎜ ⎜ ⎜ ⎜
⎞
⎠
⎟ ⎟ ⎟ ⎟
=
1 0 0 0
0 1 0 0
0 0 1 0
0 0 −1 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
⋅
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
x
y
z
−z
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
−x /z
−y /z
−1
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
Perspective Normalization
• matrix formulation
• warp and homogenization both preserve relative depth (z coordinate)€
1 0 0 0
0 1 0 0
0 0d
d − a
−a ⋅dd − a
0 01
d0
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥
⋅
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
x
y(z − a) ⋅d
d − az
d
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥
€
x p
y p
zp
⎡
⎣
⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥=
x
z /dy
z /dd2
d − a1−
a
z
⎛
⎝ ⎜
⎞
⎠ ⎟
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥
25
Demo
• Brown applets: viewing techniques• parallel/orthographic cameras
• projection cameras
• http://www.cs.brown.edu/exploratories/freeSoftware/catalogs/viewing_techniques.html
26
Perspective To NDCS Derivation
x
z
NDCS
y
(-1,-1,-1)
(1,1,1)x=left
x=right
y=top
y=bottom z=-near z=-farx
VCS
y
z
27
Perspective Derivation
simple example earlier:simple example earlier:
complete: shear, scale, projection-normalizationcomplete: shear, scale, projection-normalization€
x '
y '
z'
w'
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
1 0 0 0
0 1 0 0
0 0 1 0
0 0 1/d 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
€
′ x
′ y
′ z
′ w
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
E 0 A 0
0 F B 0
0 0 C D
0 0 −1 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
28
Perspective Derivation
earlier:earlier:
complete: shear, scale, projection-normalizationcomplete: shear, scale, projection-normalization€
x '
y '
z'
w'
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
1 0 0 0
0 1 0 0
0 0 1 0
0 0 1/d 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
€
′ x
′ y
′ z
′ w
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
E 0 A 0
0 F B 0
0 0 C D
0 0 −1 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
29
Perspective Derivation
earlier:earlier:
complete: shear, scale, projection-normalizationcomplete: shear, scale, projection-normalization
€
x '
y '
z'
w'
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
1 0 0 0
0 1 0 0
0 0 1 0
0 0 1/d 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
€
′ x
′ y
′ z
′ w
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
E 0 A 0
0 F B 0
0 0 C D
0 0 −1 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
x
y
z
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
30
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
31
Perspective Derivation
• similarly for other 5 planes
• 6 planes, 6 unknowns
€
2n
r − l0
r + l
r − l0
02n
t − b
t + b
t − b0
0 0−( f + n)
f − n
−2 fn
f − n0 0 −1 0
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥
32
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
33
NDC to Device Transformation
• map from NDC to pixel coordinates on display• NDC range is x = -1...1, y = -1...1, z = -1...1
• typical display range: x = 0...500, y = 0...300• maximum is size of actual screen• z range max and default is (0, 1), use later for visibility
xxyy
viewportviewportNDCNDC
0 500
300
0
-1
1
1
-1
xx
yy
glViewport(0,0,w,h);glDepthRange(0,1); // depth = 1 by default
34
Origin Location
• yet more (possibly confusing) conventions• OpenGL origin: lower left
• most window systems origin: upper left
• then must reflect in y• when interpreting mouse position, have to flip your
y coordinates
xxyy
viewportviewportNDCNDC
0 500
300
0
-1
1
1
-1
xx
yy
35
N2D Transformation
• general formulation• reflect in y for upper vs. lower left origin
• scale by width, height, depth
• translate by width/2, height/2, depth/2• FCG includes additional translation for pixel centers at
(.5, .5) instead of (0,0)
xxyy
viewportviewportNDCNDC
0 500
300
0
-1
1
1
-1height
width
xx
yy
36
N2D Transformation
xxyy
viewportviewportNDCNDC
0 500
300
0
-1
1
1
-1height
width
xx
yy€
xD
yD
zD
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
1 0 0width
2−
1
2
0 1 0height
2−
1
2
0 0 1depth
20 0 0 1
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥
width
20 0 0
0height
20 0
0 0depth
20
0 0 0 1
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥
1 0 0 0
0 −1 0 0
0 0 1 0
0 0 0 1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
xN
yN
zN
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
width(xN +1) −1
2height(−yN +1) −1
2depth(zN +1)
21
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥
37
Device vs. Screen Coordinates
• viewport/window location wrt actual display not available within OpenGL• usually don’t care
• use relative information when handling mouse events, not absolute coordinates
• could get actual display height/width, window offsets from OS
• loose use of terms: device, display, window, screen...
xx
displaydisplayviewportviewport
01024
768
0
300
500
displayheight
display width
x offset
y offsetyy
viewportviewport
xx00 yy
38
Projective Rendering Pipeline
OCS - object coordinate system
WCS - world coordinate system
VCS - viewing coordinate system
CCS - clipping coordinate system
NDCS - normalized device coordinate system
DCS - device coordinate system
OCSOCS WCSWCS VCSVCS
CCSCCS
NDCSNDCS
DCSDCS
modelingmodelingtransformationtransformation
viewingviewingtransformationtransformation
projectionprojectiontransformationtransformation
viewportviewporttransformationtransformation
alter walter w
/ w/ w
object world viewing
device
normalizeddevice
clipping
perspectiveperspectivedivisiondivision
glVertex3f(x,y,z)glVertex3f(x,y,z)
glTranslatef(x,y,z)glTranslatef(x,y,z)glRotatef(a,x,y,z)glRotatef(a,x,y,z)........
gluLookAt(...)gluLookAt(...)
glFrustum(...)glFrustum(...)
glutInitWindowSize(w,h)glutInitWindowSize(w,h)glViewport(x,y,a,b)glViewport(x,y,a,b)
O2WO2W W2VW2V V2CV2C
N2DN2D
C2NC2N
39
Coordinate Systemsviewing
(4-space, W=1)
clipping
(4-space parallelepiped, with COP moved backwards to infinity
normalized device
(3-space parallelepiped)
device
(3-space parallelipiped)
projection matrix
divide by w
scale &
translate
framebuffer
40
Perspective Example
tracks in VCS: left x=-1, y=-1 right x=1, y=-1
view volume left = -1, right = 1 bot = -1, top = 1 near = 1, far = 4
z=-1
z=-4
x
zVCS
top view
-1-1 1
1
-1NDCS
(z not shown)
realmidpoint
0 xmax-10DCS
(z not shown)
ymax-1
x=-1 x=1
41
Perspective Example
view volume• left = -1, right = 1• bot = -1, top = 1• near = 1, far = 4
€
2n
r − l0
r + l
r − l0
02n
t − b
t + b
t − b0
0 0−( f + n)
f − n
−2 fn
f − n0 0 −1 0
⎡
⎣
⎢ ⎢ ⎢ ⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥ ⎥ ⎥ ⎥
€
1 0 0 0
0 1 0 0
0 0 −5 /3 −8 /3
0 0 −1 0
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
42
Perspective Example
/ w/ w
€
xNDCS = −1/zVCS
yNDCS =1/zVCS
zNDCS =5
3+
8
3zVCS
€
1
−1
−5zVCS /3− 8 /3
−zVCS
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
=
1
1
−5 /3 −8 /3
−1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
1
−1
zVCS
1
⎡
⎣
⎢ ⎢ ⎢ ⎢
⎤
⎦
⎥ ⎥ ⎥ ⎥
43
OpenGL Example
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45, 1.0, 0.1, 200.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0, 0.0, -5.0 );
glPushMatrix()
glTranslate( 4, 4, 0 );
glutSolidTeapot(1);
glPopMatrix();
glTranslate( 2, 2, 0);
glutSolidTeapot(1);OCS2OCS2
O2WO2W VCSVCSmodelingmodeling
transformationtransformationviewingviewing
transformationtransformation
projectionprojectiontransformationtransformation
object world viewingW2VW2V V2CV2CWCSWCS
• transformations that are applied to object first are specified last
OCS1OCS1
WCSWCS
VCSVCS
W2OW2O
W2OW2O
CCSCCSclipping
CCSCCS
OCSOCS
44
Reading for Next Time
• RB Chap Color
• FCG Sections 3.2-3.3
• FCG Chap 20 Color
• FCG Chap 21.2.2 Visual Perception (Color)