61
University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2010 Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314/ Vjan2010 Viewing II Week 4, Mon Jan 25

Viewing II Week 4, Mon Jan 25

  • Upload
    lars

  • View
    21

  • Download
    0

Embed Size (px)

DESCRIPTION

http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010. Viewing II Week 4, Mon Jan 25. 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. Reading for This and Next 2 Lectures. - PowerPoint PPT Presentation

Citation preview

Page 1: Viewing II Week 4, Mon Jan 25

University of British ColumbiaCPSC 314 Computer Graphics

Jan-Apr 2010

Tamara Munzner

http://www.ugrad.cs.ubc.ca/~cs314/Vjan2010

Viewing II

Week 4, Mon Jan 25

Page 2: Viewing II Week 4, Mon Jan 25

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 II Week 4, Mon Jan 25

3

Reading for This and Next 2 Lectures

• FCG Chapter 7 Viewing

• FCG Section 6.3.1 Windowing Transforms

• RB rest of Chap Viewing

• RB rest of App Homogeneous Coords

Page 4: Viewing II Week 4, Mon Jan 25

4

Review: Display Lists

• precompile/cache block of OpenGL code for reuse• usually more efficient than immediate mode

• exact optimizations depend on driver

• good for multiple instances of same object• but cannot change contents, not parametrizable

• good for static objects redrawn often• display lists persist across multiple frames• interactive graphics: objects redrawn every frame from new

viewpoint from moving camera

• can be nested hierarchically• snowman example: 3x performance improvement, 36K polys

Page 5: Viewing II Week 4, Mon Jan 25

5

Review: Computing Normals

• normal• direction specifying orientation of polygon

• w=0 means direction with homogeneous coords• vs. w=1 for points/vectors of object vertices

• used for lighting• must be normalized to unit length

• can compute if not supplied with object

1P

N

2P

3P)()( 1312 PPPPN −×−=

N

Page 6: Viewing II Week 4, Mon Jan 25

6

Review: Transforming Normals

• cannot transform normals using same matrix as points• nonuniform scaling would cause to be not

perpendicular to desired plane!

MPP ='PN QNN ='

given M,given M,what should Q be?what should Q be?

( )T1MQ −= inverse transpose of the modelling transformation

Page 7: Viewing II Week 4, Mon Jan 25

7

Review: Rendering Pipeline

GeometryDatabaseGeometryDatabase

Model/ViewTransform.Model/ViewTransform. LightingLighting Perspective

Transform.PerspectiveTransform. ClippingClipping

ScanConversion

ScanConversion

DepthTest

DepthTest

TexturingTexturing BlendingBlendingFrame-buffer

Frame-buffer

Page 8: Viewing II Week 4, Mon Jan 25

8

Review: 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 9: Viewing II Week 4, Mon Jan 25

9

Review: Viewing Transformation

OCSOCS WCSWCS VCSVCSmodelingmodeling

transformationtransformationviewingviewing

transformationtransformation

OpenGL ModelView matrix

object world viewing

y

x

VCS

Peye

z

y xWCS

y

zOCS

imageplane

MMmodmod MMcamcam

Page 10: Viewing II Week 4, Mon Jan 25

10

Review: Basic Viewing

• starting spot - OpenGL• camera at world origin

• probably inside an object

• y axis is up• looking down negative z axis

• why? RHS with x horizontal, y vertical, z out of screen

• translate backward so scene is visible• move distance d = focal length

• where is camera in P1 template code?• 5 units back, looking down -z axis

Page 11: Viewing II Week 4, Mon Jan 25

11

Convenient Camera Motion

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

• demo: Robins transformation, projection

Page 12: Viewing II Week 4, Mon Jan 25

12

OpenGL Viewing Transformation

gluLookAt(ex,ey,ez,lx,ly,lz,ux,uy,uz)

• postmultiplies current matrix, so to be safe:

glMatrixMode(GL_MODELVIEW);glLoadIdentity();gluLookAt(ex,ey,ez,lx,ly,lz,ux,uy,uz)// now ok to do model transformations

• demo: Nate Robins tutorial projection

Page 13: Viewing II Week 4, Mon Jan 25

13

Convenient Camera Motion

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

Peye

Pref

upview

eye

lookaty

z

xWCS

Page 14: Viewing II Week 4, Mon Jan 25

14

From World to View Coordinates: W2V

• 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 15: Viewing II Week 4, Mon Jan 25

15

Deriving W2V Transformation

• translate eye to origin

T =

1 0 0 ex

0 1 0 ey

0 0 1 ez

0 0 0 1

⎢ ⎢ ⎢ ⎢

⎥ ⎥ ⎥ ⎥

y

z

xWCS

v

u

VCS

Peyew

Pref

upview

eye

lookat

Page 16: Viewing II Week 4, Mon Jan 25

16

Deriving W2V Transformation

• rotate view vector (lookat – eye) to w axis• w: normalized opposite of view/gaze vector g

w = −ˆ g = −g

g

y

z

xWCS

v

u

VCS

Peyew

Pref

upview

eye

lookat

Page 17: Viewing II Week 4, Mon Jan 25

17

Deriving W2V Transformation

• rotate around w to bring up into vw-plane• u should be perpendicular to vw-plane, thus

perpendicular to w and up vector t• v should be perpendicular to u and w

u =t × w

t × w

v = w × uy

z

xWCS

v

u

VCS

Peyew

Pref

upview

eye

lookat

Page 18: Viewing II Week 4, Mon Jan 25

18

Deriving W2V Transformation

• rotate from WCS xyz into uvw coordinate system with matrix that has columns u, v, w

• reminder: rotate from uvw to xyz coord sys with matrix M that has columns u,v,w

u =t × w

t × w

v = w × u

w = −ˆ g = −g

g

R =

ux vx wx 0

uy vy wy 0

uz vz wz 0

0 0 0 1

⎢ ⎢ ⎢ ⎢

⎥ ⎥ ⎥ ⎥

MW2V=TR

T =

1 0 0 ex

0 1 0 ey

0 0 1 ez

0 0 0 1

⎢ ⎢ ⎢ ⎢

⎥ ⎥ ⎥ ⎥

Page 19: Viewing II Week 4, Mon Jan 25

19

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

• inverse is transpose for orthonormal matrices• inverse is negative for translations€

T−1 =

1 0 0 −ex

0 1 0 −ey

0 0 1 −ez

0 0 0 1

⎢ ⎢ ⎢ ⎢

⎥ ⎥ ⎥ ⎥

R−1 =

ux uy uz 0

vx vy vz 0

wx wy wz 0

0 0 0 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

⎢ ⎢ ⎢ ⎢

⎥ ⎥ ⎥ ⎥

Page 20: Viewing II Week 4, Mon Jan 25

20

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 21: Viewing II Week 4, Mon Jan 25

21

Moving the Camera or the World?

• two equivalent operations• move camera one way vs. move world other way

• example• initial OpenGL camera: at origin, looking along -z axis

• create a unit square parallel to camera at z = -10

• translate in z by 3 possible in two ways• camera moves to z = -3

• Note OpenGL models viewing in left-hand coordinates

• camera stays put, but world moves to -7

• resulting image same either way• possible difference: are lights specified in world or view

coordinates?

• third operation: scaling the world• smaller vs farther away

Page 22: Viewing II Week 4, Mon Jan 25

22

World vs. Camera Coordinates Example

WW

a = (1,1)a = (1,1)WW

aa

b = (1,1)b = (1,1)C1 C1 = (5,3)= (5,3)WW

c = (1,1)c = (1,1)C2C2= = (1,3)(1,3)C1C1 = (5,5)= (5,5)WW

C1C1

bb

C2C2

cc

Page 23: Viewing II Week 4, Mon Jan 25

23

Projections I

Page 24: Viewing II Week 4, Mon Jan 25

24

Pinhole Camera

• ingredients• box, film, hole punch

• result• picture www.kodak.com

www.pinhole.org

www.debevec.org/Pinhole

Page 25: Viewing II Week 4, Mon Jan 25

25

Pinhole Camera

• theoretical perfect pinhole• light shining through tiny hole into dark space

yields upside-down picture

film plane

perfectpinhole

one rayof projection

Page 26: Viewing II Week 4, Mon Jan 25

26

Pinhole Camera

• non-zero sized hole• blur: rays hit multiple points on film plane

film plane

actualpinhole

multiple raysof projection

Page 27: Viewing II Week 4, Mon Jan 25

27

Real Cameras• pinhole camera has small aperture (lens

opening)

• minimize blur

• problem: hard to get enough light to expose the film

• solution: lens

• permits larger apertures

• permits changing distance to film plane without actually moving it

• cost: limited depth of field where image is in focus

apertureaperture

lenslensdepthdepth

ofoffieldfield

http://en.wikipedia.org/wiki/Image:DOF-ShallowDepthofField.jpg

Page 28: Viewing II Week 4, Mon Jan 25

28

Graphics Cameras

• real pinhole camera: image inverted

imageimageplaneplane

eyeeye pointpoint

computer graphics camera: convenient equivalent

imageimageplaneplane

eyeeye pointpoint

center ofcenter ofprojectionprojection

Page 29: Viewing II Week 4, Mon Jan 25

29

General Projection

• image plane need not be perpendicular to view plane

imageimageplaneplane

eyeeye pointpoint

imageimageplaneplane

eyeeye pointpoint

Page 30: Viewing II Week 4, Mon Jan 25

30

Perspective Projection

• our camera must model perspective

Page 31: Viewing II Week 4, Mon Jan 25

31

Perspective Projection

• our camera must model perspective

Page 32: Viewing II Week 4, Mon Jan 25

32

Projective Transformations

• planar geometric projections• planar: onto a plane

• geometric: using straight lines

• projections: 3D -> 2D

• aka projective mappings

• counterexamples?

Page 33: Viewing II Week 4, Mon Jan 25

33

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 34: Viewing II Week 4, Mon Jan 25

34

Perspective Projection

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

• onto an image plane

xxzz xxzz

yy

xx

-z-z

Page 35: Viewing II Week 4, Mon Jan 25

35

Perspective Projection

how tall shouldthis bunny be?

projectionplane

center of projection(eye point)

Page 36: Viewing II Week 4, Mon Jan 25

36

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 37: Viewing II Week 4, Mon Jan 25

37

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 38: Viewing II Week 4, Mon Jan 25

38

Simple Perspective Projection Matrix

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

d

dz

y

dz

x

/

/

Page 39: Viewing II Week 4, Mon Jan 25

39

Simple Perspective Projection Matrix

⎥⎥⎥⎥⎥⎥⎥

⎢⎢⎢⎢⎢⎢⎢

d

dz

y

dz

x

/

/ is homogenized version of

where w = z/d ⎥⎥⎥⎥

⎢⎢⎢⎢

dz

z

y

x

/

Page 40: Viewing II Week 4, Mon Jan 25

40

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 41: Viewing II Week 4, Mon Jan 25

41

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 42: Viewing II Week 4, Mon Jan 25

42

Moving COP to Infinity

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

Page 43: Viewing II Week 4, Mon Jan 25

43

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 44: Viewing II Week 4, Mon Jan 25

44

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 45: Viewing II Week 4, Mon Jan 25

45

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 46: Viewing II Week 4, Mon Jan 25

46

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 47: Viewing II Week 4, Mon Jan 25

47

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 48: Viewing II Week 4, Mon Jan 25

48

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 49: Viewing II Week 4, Mon Jan 25

49

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 50: Viewing II Week 4, Mon Jan 25

50

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 51: Viewing II Week 4, Mon Jan 25

51

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 52: Viewing II Week 4, Mon Jan 25

52

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 53: Viewing II Week 4, Mon Jan 25

53

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 54: Viewing II Week 4, Mon Jan 25

54

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 55: Viewing II Week 4, Mon Jan 25

55

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 56: Viewing II Week 4, Mon Jan 25

56

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 57: Viewing II Week 4, Mon Jan 25

57

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 58: Viewing II Week 4, Mon Jan 25

58

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 59: Viewing II Week 4, Mon Jan 25

59

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 60: Viewing II Week 4, Mon Jan 25

60

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 61: Viewing II Week 4, Mon Jan 25

61

Orthographic OpenGL

glMatrixMode(GL_PROJECTION);glMatrixMode(GL_PROJECTION);glLoadIdentity();glLoadIdentity();glOrtho(left,right,bot,top,near,far);glOrtho(left,right,bot,top,near,far);