COMPUTER GRAPHICS גרפיקה ממוחשבת CS

Download Report

Transcript COMPUTER GRAPHICS גרפיקה ממוחשבת CS

University of British Columbia
CPSC 314 Computer Graphics
Jan-Apr 2010
Tamara Munzner
Viewing/Projection IV
Week 4, Fri Jan 29
http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010
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
2
Review: Basic Perspective Projection
similar triangles
y
y' y
yd
  y'
z
z
P(x,y,z) d
P(x’,y’,z’)
z’=d
z

 x 
 
z / d  homogeneous x 
 
coords
 y 
y 

z / d 
 z 
 
 
 
z / d
 d 
xd
x'
z
1


0
0

0
z' d
0

1 0 0
0 1 0

0 1/d 1
0
0
3
Review: View Volumes
• specifies field-of-view, used for clipping
• restricts domain of z stored for visibility test
orthographic view volume
perspective view volume
y=top
y=top
x=left
x=left
y
y
z
x=right
z
VCS
x
y=bottom z=-near
x=right
VCS
z=-far
x
y=bottom
z=-far
z=-near
4
Review: Understanding Z
• z axis flip changes coord system handedness
• RHS before projection (eye/view coords)
• LHS after projection (clip, norm device coords)
VCS
NDCS
y=top
y
x=left
y
(1,1,1)
z
z
(-1,-1,-1)
x=right
x
y=bottom
x
z=-far
z=-near
5
Review: Orthographic Derivation
• scale, translate, reflect for new coord sys
VCS
NDCS
y=top
x=left
y
y
z
z
x=right
x
y=bottom
x  e
  
y  0
z 0
  
1  0
(1,1,1)
(-1,-1,-1)
x
z=-far
z=-near
0 0
a 0
0 c
0 0
f x
 
b y
d z 
 
1 1
y'  a  y  b
y  top  y '  1
y  bot  y '  1
2
a
top  bot
top  bot
b
top  bot
6
Review: Orthographic Derivation
• scale, translate, reflect for new coord sys
2

 right  left


0

P'  

0



0

0
2
top  bot
0
0
right  left 
0

right  left 

top  bot 
0


top  bot  P
2
far  near


far  near
far  near

0
1

7
Demo
• Robins demo: projection
• orthographic
• perspective
8
Projections II
9
Asymmetric Frusta
• our formulation allows asymmetry
• why bother?
x
x
right
right
Frustum
left
z=-n
Frustum
-z
left
-z
z=-f
10
Asymmetric Frusta
• our formulation allows asymmetry
• why bother? binocular stereo
• view vector not perpendicular to view plane
Left Eye
Right Eye
11
Simpler Formulation
• left, right, bottom, top, near, far
• nonintuitive
• often overkill
• look through window center
• symmetric frustum
• constraints
• left = -right, bottom = -top
12
Field-of-View Formulation
• FOV in one direction + aspect ratio (w/h)
• determines FOV in other direction
• also set near, far (reasonably intuitive)
x
w

Frustum
z=-n
fovx/2
-z
z=-f
h
fovy/2
13
Perspective OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(left,right,bot,top,near,far);
or
glPerspective(fovy,aspect,near,far);
14
Demo: Frustum vs. FOV
• Nate Robins tutorial (take 2): projection
• frustum vs perspective
15
Projective Rendering Pipeline
object
OCS
world
O2W
WCS
modeling
transformation
W2V
viewing
VCS
viewing
transformation
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
V2C
projection
transformation
C2N
clipping
CCS
perspective
divide
normalized
N2D
device
NDCS
viewport
transformation
device
DCS
16
Perspective Warp
• warp perspective view volume to orthogonal
view volume
• render all scenes with orthographic projection!
• aka perspective normalization
x
x
z= z=d
z=0
Z
z=d
Z
17
Perspective Warp
• perspective viewing frustum transformed to
cube
• orthographic rendering of warped objects in
cube produces same image as perspective
rendering of original frustum
18
Predistortion
19
Projective Rendering Pipeline
object
OCS
world
O2W
WCS
modeling
transformation
W2V
viewing
VCS
viewing
transformation
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
V2C
projection
transformation
C2N
clipping
CCS
perspective
divide
normalized
N2D
device
NDCS
viewport
transformation
device
DCS
20
Separate Warp From Homogenization
viewing
VCS
V2C
clipping
CCS
projection
transformation
alter w
C2N
perspective
division
/w
normalized
device
NDCS
• 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
21
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
x
 
y
z 
 
1
x 
 
y 
z 
 
z
-z


22
Perspective Divide Example
x 1
  
y 0


T

z  0
  
1 0
0 0
1 0
0 1
0 1
0 x x  x /z
     

0 y y  y /z



0 z  z   1 
     

0 1 z  1 
• after homogenizing, once again w=1
projection
transformation
alter w
perspective
division
/w
23
Perspective Normalization
• matrix formulation
1

0
0

0

0
0
1
0
d
da
1
d
0
0
 x

0 

 x  y
0    

a  d y (z  a)  d 


d  a  z   d  a 
   z


1
0



 d



x


x p   z /d

  
y

y

 p   z /d



z p   d 2  a 
1 

d  a  z 
• warp and homogenization both preserve relative depth (z
coordinate)

Demo
• Brown applets: viewing techniques
• parallel/orthographic cameras
• projection cameras
• http://www.cs.brown.edu/exploratories/freeSoftware/catalogs
/viewing_techniques.html
25
Perspective To NDCS Derivation
VCS
NDCS
y=top
x=left
y
y
(1,1,1)
z
(-1,-1,-1)
z
x
y=bottom z=-near
x
z=-far
x=right
26
Perspective Derivation
simple example earlier:
x' 1
  
y' 0
z'  0
  
w' 0
0x
 
1 0 0y
0 1 0z 
 
0 1/d 01
0
0
complete: shear, scale, projection-normalization

x  E
  
y  0
z 0
  
w 0
0 x
 
F B 0 y
0 C Dz 
 
0 1 0 1
0
A
27
Perspective Derivation
x' 1
  
y' 0
z'  0
  
w' 0
earlier:
0x
 
1 0 0y
0 1 0z 
 
0 1/d 01
0
0
complete: shear, scale, projection-normalization

x  E
  
y  0
z 0
  
w 0
0 x
 
F B 0 y
0 C Dz 
 
0 1 0 1
0
A
28
Perspective Derivation
earlier:
x' 1
  
y' 0
z'  0
  
w' 0
0x
 
1 0 0y
0 1 0z 
 
0 1/d 01
0
0
complete: shear, scale, projection-normalization

x  E
  
y  0
z 0
  
w 0
0 x
 
F B 0 y
0 C Dz 
 
0 1 0 1
0
A
29
Perspective Derivation
x' E
  
y' 0
z'  0
  
w' 0
0 x
 
F B 0 y
0 C Dz 
 
0 1 0 1
0
A
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

y' Fy  Bz
Fy  Bz
Fy  Bz
y' Fy  Bz,

,
1
, 1
,
w'
w'
w'
z

y
z
y
top
1 F
 B , 1 F
 B, 1  F
 B,
z
z
z
(near)
top
1 F
B
near
30
Perspective Derivation
• similarly for other 5 planes
• 6 planes, 6 unknowns
 2n
r  l

 0

 0


 0
0
2n
t b
0
0
rl
rl
tb
t b
( f  n)
f n
1

0 

0 

2 fn 
f  n 
0 

31
Projective Rendering Pipeline
object
OCS
world
O2W
WCS
modeling
transformation
W2V
viewing
VCS
viewing
transformation
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
V2C
projection
transformation
C2N
clipping
CCS
perspective
divide
normalized
N2D
device
NDCS
viewport
transformation
device
DCS
32
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
glViewport(0,0,w,h);
glDepthRange(0,1); // depth = 1 by default
0 x
0 y
y
500
-1
viewport
x
1
NDC
-1
1
300
33
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
0 x
0 y
y
500
-1
viewport
x
1
NDC
-1
1
300
34
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)
0 x
0 y
y
500
height
-1
viewport
x
1
NDC
-1
1
300
width
35
N2D Transformation

x D  1
  
y D  0
zD  
  0
1  

0
0 0
1 0
0 1
0 0
width 1 width
 
2
2
2


height 1
  0
2
2 
depth 
0
2


1

 0

 width(x N  1) 1 
0

1 0 0 0x N  
2

  height(y N  1) 1
height
0
1
0
0
0
0

y N  
2
2



depth 0 0 1 0zN   depth(zN  1) 
0
0 0 0 0 11 
  
2

2



0
0
1
1



0
0
0 x
0 y
y
500
height
-1
viewport
x
1
NDC
-1
1
300
width
36
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...
0 x
1024
0 y
0 x
500
y offset
0 y
display
x offset
viewport
viewport
height
display
300
768
display width
37
Projective Rendering Pipeline
glVertex3f(x,y,z)
object
OCS
O2W
world
WCS
modeling
transformation
W2V
viewing
alter w
VCS V2C
glFrustum(...)
viewing
transformation
projection
transformation
clipping
CCS
glTranslatef(x,y,z)
gluLookAt(...)
/w
C2N
glRotatef(a,x,y,z)
perspective
....
division normalized
OCS - object coordinate system
glutInitWindowSize(w,h)
device
N2D
WCS - world coordinate system glViewport(x,y,a,b)
NDCS
viewport
VCS - viewing coordinate system
transformation
CCS - clipping coordinate system
device
NDCS - normalized device coordinate system
DCS - device coordinate system
DCS
38
Coordinate Systems
viewing
(4-space, W=1)
projection
matrix
clipping
(4-space parallelepiped,
with COP moved
backwards to infinity
divide by w
normalized device
(3-space
parallelepiped)
scale &
translate
device
(3-space
parallelipiped)
framebuffer
39
Perspective Example
view volume
left = -1, right = 1
bot = -1, top = 1
near = 1, far = 4
tracks in VCS:
left x=-1, y=-1
right x=1, y=-1
x=-1
x=1
1
ymax-1
z=-4
real
midpoint
z=-1
x
z
VCS
top view
-1
-1 -1
1
NDCS
(z not shown)
0 0
xmax-1
DCS
(z not shown)
40
Perspective Example
view volume
• left = -1, right = 1
• bot = -1, top = 1
• near = 1, far = 4
 2n
r  l

 0

 0


 0
0
2n
t b
0
0
rl
rl
tb
t b
( f  n)
f n
1

0 

0 

2 fn 
f  n 
0 

1

0

0

0
0 

1
0
0 
0 5 /3 8 /3

0 1
0 
0
0
41
Perspective Example

 1
 1 
1

 
 
1

  1
1 
5zVCS /3 8 /3 
5 /3 8 /3zVCS 

 
 
zVCS
1

 
 1 
/w
x NDCS  1/zVCS
y NDCS  1/zVCS
5
8
zNDCS  
3 3zVCS
42
OpenGL Example
object
OCS O2W
world
WCS
modeling
transformation
CCS
VCS
WCS
OCS1
OCS2
W2V
viewing
VCS
viewing
transformation
V2C
clipping
CCS
projection
transformation
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45, 1.0, 0.1, 200.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0, 0.0, -5.0 ); • transformations that
glPushMatrix()
are applied to object
glTranslate( 4, 4, 0 ); W2O
first are specified
glutSolidTeapot(1);
last
glPopMatrix();
glTranslate( 2, 2, 0); W2O
43
glutSolidTeapot(1);
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)
44