Viewing/Projections III Week 4, Wed Jan 31 - - PowerPoint PPT Presentation
Viewing/Projections III Week 4, Wed Jan 31 - - PowerPoint PPT Presentation
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2007 Tamara Munzner Viewing/Projections III Week 4, Wed Jan 31 http://www.ugrad.cs.ubc.ca/~cs314/Vjan2007 News extra TA coverage in lab to answer questions Wed 2-3:30
2
News
- extra TA coverage in lab to answer questions
- Wed 2-3:30
- Thu 12:30-2
- my office hours reminder (in lab also)
- Wed (today) 11-12
- Fri 11-12
3
Reading for Today
- FCG Chapter 7 Viewing
- FCG Section 6.3.1 Windowing Transforms
- RB rest of Chap Viewing
- RB rest of App Homogeneous Coords
4
Reading for Next Time
- RB Chap Color
- FCG Sections 3.2-3.3
- FCG Chap 20 Color
- FCG Chap 21 Visual Perception
5
Review: Graphics Cameras
- real pinhole camera: image inverted
image image plane plane eye eye point point
ν computer graphics camera: convenient equivalent
image image plane plane eye eye point point center of center of projection projection
6
Review: Basic Perspective Projection
similar triangles similar triangles
→ = z y d y'
z d y y ⋅ = '
z z P(x,y,z) P(x,y,z) P( P(x x’ ’,y ,y’ ’, ,z z’ ’) ) z z’ ’=d =d y y
z d x x ⋅ = ' d z = '
1 1 1 1 d
d d z y d z x / /
d z z y x /
homogeneous homogeneous coords coords
7
Review: Orthographic Cameras
- center of projection at infinity
- no perspective convergence
- just throw away z values
= 1 1 1 1 1 z y x z y x
p p p
8
Review: Transforming View Volumes
x z
NDCS
y
(-1,-1,-1) (1,1,1)
- rthographic view volume
- rthographic view volume
x z VCS y x=left y=top x=right z=-far z=-near y=bottom perspective view volume perspective view volume x=left x=right y=top y=bottom z=-near z=-far x VCS y
9
Orthographic Derivation
- scale, translate, reflect for new coord sys
x z
VCS
y x=left y=top x=right z=-far z=-near y=bottom x z
NDCS
y
(-1,-1,-1) (1,1,1)
10
Orthographic Derivation
- scale, translate, reflect for new coord sys
x z
VCS
y x=left y=top x=right z=-far z=-near y=bottom x z
NDCS
y
(-1,-1,-1) (1,1,1)
b y a y + ⋅ = ' 1 ' 1 ' − = → = = → = y bot y y top y
11
Orthographic Derivation
- scale, translate, reflect for new coord sys
b y a y + ⋅ = ' 1 ' 1 ' − = → = = → = y bot y y top y
bot top bot top b bot top top bot top b bot top top b b top bot top − − − = − ⋅ − − = − ⋅ − = + − = 2 ) ( 2 1 2 1
b bot a b top a + ⋅ = − + ⋅ = 1 1
bot top a top bot a top a bot a bot a top a bot a b top a b − = + − = ⋅ − − ⋅ − = − − ⋅ − − = ⋅ − ⋅ − − = ⋅ − = 2 ) ( 2 ) ( ) 1 ( 1 1 1 1 , 1
12
Orthographic Derivation
- scale, translate, reflect for new coord sys
x z
VCS
y x=left y=top x=right z=-far z=-near y=bottom
b y a y + ⋅ = ' 1 ' 1 ' − = → = = → = y bot y y top y bot top bot top b bot top a − + − = − = 2
same idea for right/left, far/near same idea for right/left, far/near
13
Orthographic Derivation
- scale, translate, reflect for new coord sys
P near far near far near far bot top bot top bot top left right left right left right P − + − − − − + − − − + − − = 1 2 2 2 '
14
Orthographic Derivation
- scale, translate, reflect for new coord sys
P near far near far near far bot top bot top bot top left right left right left right P − + − − − − + − − − + − − = 1 2 2 2 '
15
Orthographic Derivation
- scale, translate, reflect for new coord sys
P near far near far near far bot top bot top bot top left right left right left right P − + − − − − + − − − + − − = 1 2 2 2 '
16
Orthographic Derivation
- scale, translate, reflect for new coord sys
P near far near far near far bot top bot top bot top left right left right left right P − + − − − − + − − − + − − = 1 2 2 2 '
17
Orthographic OpenGL
glMatrixMode glMatrixMode(GL_PROJECTION); (GL_PROJECTION); glLoadIdentity glLoadIdentity(); (); glOrtho glOrtho(left,right, (left,right,bot bot,top,near,far); ,top,near,far);
18
Demo
- Brown applets: viewing techniques
- parallel/orthographic cameras
- projection cameras
- http://www.cs.brown.edu/exploratories/freeSoftware/catalogs
/viewing_techniques.html
19
Projections II
20
Asymmetric Frusta
- our formulation allows asymmetry
- why bother?
- z
- z
x x Frustum Frustum
right right left left
- z
- z
x x Frustum Frustum z=-n z=-n z=-f z=-f
right right left left
21
Asymmetric Frusta
- our formulation allows asymmetry
- why bother? binocular stereo
- view vector not perpendicular to view plane
Right Eye Right Eye Left Eye Left Eye
22
Simpler Formulation
- left, right, bottom, top, near, far
- nonintuitive
- often overkill
- look through window center
- symmetric frustum
- constraints
- left = -right, bottom = -top
23
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
x x Frustum Frustum z=-n z=-n z=-f z=-f α α
fovx fovx/2 /2 fovy fovy/2 /2
h h w w
24
Perspective OpenGL
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(left,right,bot,top,near,far);
- r
glPerspective(fovy,aspect,near,far);
25
Demo: Frustum vs. FOV
- Nate Robins tutorial (take 2):
- http://www.xmission.com/~nate/tutors.html
26
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
OCS OCS O2W O2W VCS VCS CCS CCS NDCS NDCS DCS DCS
modeling modeling transformation transformation viewing viewing transformation transformation projection projection transformation transformation viewport viewport transformation transformation perspective perspective divide divide
- bject
world viewing device normalized device clipping W2V W2V V2C V2C N2D N2D C2N C2N WCS WCS
27
Projection Normalization
- 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
28
Perspective Normalization
- perspective viewing frustum transformed to
cube
- orthographic rendering of cube produces same
image as perspective rendering of original frustum
29
Predistortion
30
Demos
- Tuebingen applets from Frank Hanisch
- http://www.gris.uni-tuebingen.de/projects/grdev/doc/html/etc/
AppletIndex.html#Transformationen
31
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
OCS OCS O2W O2W VCS VCS CCS CCS NDCS NDCS DCS DCS
modeling modeling transformation transformation viewing viewing transformation transformation projection projection transformation transformation viewport viewport transformation transformation perspective perspective divide divide
- bject
world viewing device normalized device clipping W2V W2V V2C V2C N2D N2D C2N C2N WCS WCS
32
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
CCS CCS NDCS NDCS
alter w alter w / w / w
VCS VCS
projection projection transformation transformation
viewing normalized device clipping
perspective perspective division division
V2C V2C C2N C2N
33
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
34
Perspective Divide Example
− − − ≡ − = ⋅ − = 1 1 / / 1 1 1 1 1 1 z y z x z z y x z y x z y x T
alter w alter w / w / w projection projection transformation transformation perspective perspective division division
- after homogenizing, once again w=1
Perspective Normalization
- matrix formulation
- warp and homogenization both preserve
relative depth (z coordinate)
− ⋅ − = − ⋅ − − d z d d z y x z y x d d d d d α α α α α ) ( 1 1 1 1 − − = z d d d z y d z x z y x
p p p
α α 1 / /
2
36
Demo
- Brown applets: viewing techniques
- parallel/orthographic cameras
- projection cameras
- http://www.cs.brown.edu/exploratories/freeSoftware/catalogs
/viewing_techniques.html
37
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
OCS OCS O2W O2W VCS VCS CCS CCS NDCS NDCS DCS DCS
modeling modeling transformation transformation viewing viewing transformation transformation projection projection transformation transformation viewport viewport transformation transformation perspective perspective divide divide
- bject
world viewing device normalized device clipping W2V W2V V2C V2C N2D N2D C2N C2N WCS WCS
38
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
x x y y viewport viewport NDC NDC 500 300
- 1
1 1
- 1
x x y y
glViewport(0,0,w,h); glDepthRange(0,1); // depth = 1 by default
39
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
x x y y viewport viewport NDC NDC 500 300
- 1
1 1
- 1
x x y y
40
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) x x y y viewport viewport NDC NDC 500 300
- 1
1 1
- 1
height width x x y y
41
N2D Transformation
+ − + − − + = − − − = 1 2 ) 1 ( 2 1 ) 1 ( 2 1 ) 1 ( 1 1 1 1 1 1 2 2 2 1 2 1 2 1 2 1 2 1 2 1 1
N N N N N N D D D
z depth y height x width z y x depth height width depth height width z y x
x x y y viewport viewport NDC NDC 500 300
- 1
1 1
- 1
height width x x y y
42
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...
x x display display viewport viewport 1024 768 300 500 display height display width x offset y offset y y viewport viewport x x y y
43
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 z VCS top view
- 1
- 1
1 1
- 1
NDCS (z not shown) real midpoint xmax-1 DCS (z not shown) ymax-1 x=-1 x=1
44
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
OCS OCS WCS WCS VCS VCS CCS CCS NDCS NDCS DCS DCS
modeling modeling transformation transformation viewing viewing transformation transformation projection projection transformation transformation viewport viewport transformation transformation alter w alter w / w / w
- bject
world viewing device normalized device clipping
perspective perspective division division glVertex3f(x,y,z) glVertex3f(x,y,z) glTranslatef glTranslatef(x,y,z) (x,y,z) glRotatef glRotatef( (th th,x,y,z) ,x,y,z) .... .... gluLookAt gluLookAt(...) (...) glFrustum glFrustum(...) (...) glutInitWindowSize glutInitWindowSize(w,h) (w,h) glViewport glViewport(x,y,a,b) (x,y,a,b)
O2W O2W W2V W2V V2C V2C N2D N2D C2N C2N
45
Coordinate Systems
viewing (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 projection matrix
46
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=-far x
VCS
y z
47
Perspective Derivation
simple example earlier: simple example earlier: complete: shear, scale, projection-normalization complete: shear, scale, projection-normalization
− = 1 1 ' ' ' ' z y x D C B F A E w z y x = 1 / 1 1 1 1 ' ' ' ' z y x d w z y x
48
Perspective Derivation
earlier: earlier: complete: shear, scale, projection-normalization complete: shear, scale, projection-normalization
− = 1 1 ' ' ' ' z y x D C B F A E w z y x = 1 / 1 1 1 1 ' ' ' ' z y x d w z y x
49
Perspective Derivation
earlier: earlier: complete: shear, scale, projection-normalization complete: shear, scale, projection-normalization
− = 1 1 ' ' ' ' z y x D C B F A E w z y x = 1 / 1 1 1 1 ' ' ' ' z y x d w z y x
50
Perspective Derivation
− = 1 1 ' ' ' ' z y x D C B F A E w z y x
z w D Cz z Bz Fy y Az Ex x − = + = + = + = ' ' ' '
, ) ( 1 , 1 , 1 , 1 , ' 1 , ' ' ' , ' B near top F B z y F z z B z y F z Bz Fy w Bz Fy w Bz Fy w y Bz Fy y − − − = − − = − + − = − + = + = + = + = 1 ' / ' 1 ' / ' 1 ' / ' 1 ' / ' 1 ' / ' 1 ' / ' − = → − = = → − = − = → = = → = − = → = = → = w z far z w z near z w y bottom y w y top y w x right x w x left x B near top F − = 1
51
Perspective Derivation
- similarly for other 5 planes
- 6 planes, 6 unknowns
− − − − + − − + − − + − 1 2 ) ( 2 2 n f fn n f n f b t b t b t n l r l r l r n
52
Perspective Example
− − − − + − − + − − + − 1 2 ) ( 2 2 n f fn n f n f b t b t b t n l r l r l r n
view volume
- left = -1, right = 1
- bot = -1, top = 1
- near = 1, far = 4
− − − 1 3 / 8 3 / 5 1 1
53
Perspective Example
− − − − = − − − − 1 1 1 1 3 / 8 3 / 5 1 1 3 / 8 3 / 5 1 1
VCS VCS VCS
z z z
VCS NDCS VCS NDCS VCS NDCS
z z z y z x 3 8 3 5 / 1 / 1 + = = − =
/ w / w
54
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);
OCS2 OCS2 O2W O2W VCS VCS
modeling modeling transformation transformation viewing viewing transformation transformation projection projection transformation transformation
- bject
world viewing W2V W2V V2C V2C WCS WCS
- transformations that