54
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2010 Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314/ Vjan2010 Viewing III Week 4, Wed Jan 27

Viewing III Week 4, Wed Jan 27

  • Upload
    gilead

  • View
    43

  • Download
    0

Embed Size (px)

DESCRIPTION

http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010. Viewing III Week 4, Wed Jan 27. 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. Review: Convenient Camera Motion. - PowerPoint PPT Presentation

Citation preview

Page 1: Viewing III Week 4, Wed Jan 27

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

Page 2: 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

Page 3: Viewing III Week 4, Wed Jan 27

3

Review: Convenient Camera Motion

• rotate/translate/scale versus• eye point, gaze/lookat direction, up vector

Peye

Pref

upview

eye

lookaty

z

xWCS

Page 4: Viewing III Week 4, Wed Jan 27

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

Page 5: Viewing III Week 4, Wed Jan 27

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

⎢ ⎢ ⎢ ⎢

⎥ ⎥ ⎥ ⎥

Page 6: Viewing III Week 4, Wed Jan 27

6

Review: Graphics Cameras

• real pinhole camera: image inverted

imageimageplaneplane

eyeeye pointpoint

computer graphics camera: convenient equivalent

imageimageplaneplane

eyeeye pointpoint

center ofcenter ofprojectionprojection

Page 7: Viewing III Week 4, Wed Jan 27

7

Review: Projective Transformations

• planar geometric projections• planar: onto a plane

• geometric: using straight lines

• projections: 3D -> 2D

• aka projective mappings

• counterexamples?

Page 8: Viewing III Week 4, Wed Jan 27

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)

Page 9: Viewing III Week 4, Wed Jan 27

9

Perspective Projection

• project all geometry • through common center of projection (eye point)

• onto an image plane

xxzz xxzz

yy

xx

-z-z

Page 10: Viewing III Week 4, Wed Jan 27

10

Perspective Projection

how tall shouldthis bunny be?

projectionplane

center of projection(eye point)

Page 11: Viewing III Week 4, Wed Jan 27

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

Page 12: Viewing III Week 4, Wed Jan 27

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

==⋅

==⋅

=

==

',','

',

'

Page 13: Viewing III Week 4, Wed Jan 27

13

Simple Perspective Projection Matrix

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

d

dz

y

dz

x

/

/

Page 14: Viewing III Week 4, Wed Jan 27

14

Simple Perspective Projection Matrix

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

d

dz

y

dz

x

/

/ is homogenized version of

where w = z/d ⎥⎥⎥⎥

⎢⎢⎢⎢

dz

z

y

x

/

Page 15: Viewing III Week 4, Wed Jan 27

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

/

Page 16: Viewing III Week 4, Wed Jan 27

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

Page 17: Viewing III Week 4, Wed Jan 27

17

Moving COP to Infinity

• as COP moves away, lines approach parallel• when COP at infinity, orthographic view

Page 18: Viewing III Week 4, Wed Jan 27

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

Page 19: Viewing III Week 4, Wed Jan 27

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

Page 20: Viewing III Week 4, Wed Jan 27

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

Page 21: Viewing III Week 4, Wed Jan 27

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

Page 22: Viewing III Week 4, Wed Jan 27

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

Page 23: Viewing III Week 4, Wed Jan 27

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

Page 24: Viewing III Week 4, Wed Jan 27

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

Page 25: Viewing III Week 4, Wed Jan 27

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)

Page 26: Viewing III Week 4, Wed Jan 27

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

Page 27: Viewing III Week 4, Wed Jan 27

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

Page 28: Viewing III Week 4, Wed Jan 27

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)

Page 29: Viewing III Week 4, Wed Jan 27

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

Page 30: Viewing III Week 4, Wed Jan 27

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

Page 31: Viewing III Week 4, Wed Jan 27

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

Page 32: Viewing III Week 4, Wed Jan 27

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

'

Page 33: Viewing III Week 4, Wed Jan 27

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

'

Page 34: Viewing III Week 4, Wed Jan 27

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

'

Page 35: Viewing III Week 4, Wed Jan 27

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

'

Page 36: Viewing III Week 4, Wed Jan 27

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);

Page 37: Viewing III Week 4, Wed Jan 27

37

Demo

• Brown applets: viewing techniques• parallel/orthographic cameras

• projection cameras

• http://www.cs.brown.edu/exploratories/freeSoftware/catalogs/viewing_techniques.html

Page 38: Viewing III Week 4, Wed Jan 27

38

Projections II

Page 39: Viewing III Week 4, Wed Jan 27

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

Page 40: Viewing III Week 4, Wed Jan 27

40

Asymmetric Frusta

• our formulation allows asymmetry• why bother? binocular stereo

• view vector not perpendicular to view plane

Right EyeRight Eye

Left EyeLeft Eye

Page 41: Viewing III Week 4, Wed Jan 27

41

Simpler Formulation

• left, right, bottom, top, near, far• nonintuitive

• often overkill

• look through window center• symmetric frustum

• constraints• left = -right, bottom = -top

Page 42: Viewing III Week 4, Wed Jan 27

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

Page 43: Viewing III Week 4, Wed Jan 27

43

Perspective OpenGL

glMatrixMode(GL_PROJECTION);glLoadIdentity();

glFrustum(left,right,bot,top,near,far); orglPerspective(fovy,aspect,near,far);

Page 44: Viewing III Week 4, Wed Jan 27

44

Demo: Frustum vs. FOV

• Nate Robins tutorial (take 2):

• http://www.xmission.com/~nate/tutors.html

Page 45: Viewing III Week 4, Wed Jan 27

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

Page 46: Viewing III Week 4, Wed Jan 27

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

Page 47: Viewing III Week 4, Wed Jan 27

47

Perspective Warp

• perspective viewing frustum transformed to cube

• orthographic rendering of cube produces same image as perspective rendering of original frustum

Page 48: Viewing III Week 4, Wed Jan 27

48

Predistortion

Page 49: Viewing III Week 4, Wed Jan 27

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

Page 50: Viewing III Week 4, Wed Jan 27

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

Page 51: Viewing III Week 4, Wed Jan 27

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

Page 52: Viewing III Week 4, Wed Jan 27

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

Page 53: Viewing III Week 4, Wed Jan 27

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

Page 54: Viewing III Week 4, Wed Jan 27

54

Demo

• Brown applets: viewing techniques• parallel/orthographic cameras

• projection cameras

• http://www.cs.brown.edu/exploratories/freeSoftware/catalogs/viewing_techniques.html