viewing 3 - home | computer science at ubc

43
Tamara Munzner http://www.ugrad.cs.ubc.ca/~cs314/Vjan2016 Viewing 3 University of British Columbia CPSC 314 Computer Graphics Jan-Apr 2016

Upload: others

Post on 26-May-2022

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Viewing 3 - Home | Computer Science at UBC

Tamara Munzner

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

Viewing 3

University of British Columbia CPSC 314 Computer Graphics

Jan-Apr 2016

Page 2: Viewing 3 - Home | Computer Science at UBC

2

View Volumes •  specifies field-of-view, used for clipping •  restricts domain of z stored for visibility test

z

perspective view volume orthographic view volume

x=left

x=right

y=top

y=bottom z=-near z=-far x VCS x

z

VCS

y y

x=left y=top

x=right

z=-far z=-near y=bottom

Page 3: Viewing 3 - Home | Computer Science at UBC

3

Canonical View Volumes •  standardized viewing volume representation

perspective orthographic orthogonal parallel

x or y

-z

1

-1

-1 front plane

back plane

x or y

-z front plane

back plane

x or y = +/- z

Page 4: Viewing 3 - Home | Computer Science at UBC

4

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 5: Viewing 3 - Home | Computer Science at UBC

5

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 6: Viewing 3 - Home | Computer Science at UBC

6

Normalized Device Coordinates

left/right x =+/- 1, top/bottom y =+/- 1, near/far z =+/- 1

-z

x

Frustum

z=-n z=-f

right

left z

x

x= -1 z=1

x=1

Camera coordinates NDC

z= -1

Page 7: Viewing 3 - Home | Computer Science at UBC

7

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

y x=left

y=top

x=right

z=-far z=-near y=bottom

x

z

NDCS

y

(-1,-1,-1)

(1,1,1)

Page 8: Viewing 3 - Home | Computer Science at UBC

8

Understanding Z near, far always positive in GL calls THREE.OrthographicCamera(left,right,bot,top,near,far); mat4.frustum(left,right,bot,top,near,far, projectionMatrix);

orthographic view volume

x

z

VCS

y x=left

y=top

x=right

z=-far z=-near y=bottom

perspective view volume

x=left

x=right

y=top

y=bottom z=-near z=-far x VCS

y

Page 9: Viewing 3 - Home | Computer Science at UBC

9

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 10: Viewing 3 - Home | Computer Science at UBC

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)

Page 11: Viewing 3 - Home | Computer Science at UBC

11

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)

byay +⋅='1'

1'

−=→=

=→=

yboty

ytopy

Page 12: Viewing 3 - Home | Computer Science at UBC

12

Orthographic Derivation

•  scale, translate, reflect for new coord sys

byay +⋅='1'

1'

−=→=

=→=

yboty

ytopy

bottop

bottopb

bottop

topbottopb

bottop

topb

btopbottop

−−=

⋅−−=

⋅−=

+−

=

2)(

21

21

bbota

btopa

+⋅=−

+⋅=

1

1

bottopa

topbota

topabota

botatopa

botabtopab

−=

+−=

⋅−−⋅−=−−

⋅−−=⋅−

⋅−−=⋅−=

2

)(2

)()1(1

11

1,1

Page 13: Viewing 3 - Home | Computer Science at UBC

13

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

byay +⋅='1'

1'

−=→=

=→=

yboty

ytopy

bottop

bottopb

bottopa

+−=

−=

2

same idea for right/left, far/near

Page 14: Viewing 3 - Home | Computer Science at UBC

14

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 15: Viewing 3 - Home | Computer Science at UBC

15

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 16: Viewing 3 - Home | Computer Science at UBC

16

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 17: Viewing 3 - Home | Computer Science at UBC

17

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 18: Viewing 3 - Home | Computer Science at UBC

18

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 O2W VCS

CCS

NDCS

DCS

modeling transformation

viewing transformation

projection transformation

viewport transformation

perspective divide

object world viewing

device

normalized device

clipping

W2V V2C

N2D

C2N

WCS

Page 19: Viewing 3 - Home | Computer Science at UBC

19

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 20: Viewing 3 - Home | Computer Science at UBC

20

Perspective Warp •  perspective viewing frustum transformed to

cube •  orthographic rendering of cube produces same

image as perspective rendering of original frustum

Page 21: Viewing 3 - Home | Computer Science at UBC

21

Predistortion

Page 22: Viewing 3 - Home | Computer Science at UBC

22

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 O2W VCS

CCS

NDCS

DCS

modeling transformation

viewing transformation

projection transformation

viewport transformation

perspective divide

object world viewing

device

normalized device

clipping

W2V V2C

N2D

C2N

WCS

Page 23: Viewing 3 - Home | Computer Science at UBC

23

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 NDCS

alter w / w

VCS projection

transformation

viewing normalized device

clipping

perspective division

V2C C2N

Page 24: Viewing 3 - Home | Computer Science at UBC

24

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

1

z

y

x

− z

z

y

x

Page 25: Viewing 3 - Home | Computer Science at UBC

25

Perspective Divide Example

=

=

1

1

/

/

10100

0100

0010

0001

1

zy

zx

z

z

y

x

z

y

x

z

y

x

T

alter w / w

projection transformation

perspective division

•  after homogenizing, once again w=1

Page 26: Viewing 3 - Home | Computer Science at UBC

Perspective Normalization •  matrix formulation

•  warp and homogenization both preserve relative depth (z coordinate)

⋅−=

⋅−

d

z

d

dz

y

x

z

y

x

d

d

d

d

d

α

α

α

α

α

)(

101

00

00

0010

0001

=

zd

ddz

ydz

x

z

y

x

p

p

p

α

α1

/

/

2

Page 27: Viewing 3 - Home | Computer Science at UBC

27

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

Page 28: Viewing 3 - Home | Computer Science at UBC

28

Perspective Derivation

simple example earlier:

complete: 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

Page 29: Viewing 3 - Home | Computer Science at UBC

29

Perspective Derivation

earlier:

complete: 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

Page 30: Viewing 3 - Home | Computer Science at UBC

30

Perspective Derivation

earlier:

complete: 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

Page 31: Viewing 3 - Home | Computer Science at UBC

31

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

Page 32: Viewing 3 - Home | Computer Science at UBC

32

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

Page 33: Viewing 3 - Home | Computer Science at UBC

33

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 O2W VCS

CCS

NDCS

DCS

modeling transformation

viewing transformation

projection transformation

viewport transformation

perspective divide

object world viewing

device

normalized device

clipping

W2V V2C

N2D

C2N

WCS

Page 34: Viewing 3 - Home | Computer Science at UBC

34

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 y

viewport NDC

0 500

300

0

-1 1

1

-1

x

y

gl.viewport(0,0,w,h); gl.depthRange(0,1); // depth = 1 by default

Page 35: Viewing 3 - Home | Computer Science at UBC

35

Origin Location •  yet more (possibly confusing) conventions

•  GL 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

y

viewport NDC

0 500

300

0

-1 1

1

-1

x

y

Page 36: Viewing 3 - Home | Computer Science at UBC

36

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 y

viewport NDC

0 500

300

0

-1 -1

1

1 height

width

x

y

Page 37: Viewing 3 - Home | Computer Science at UBC

37

N2D Transformation

x y

viewport NDC

0 500

300

0

-1 -1

1

1 height

width

x

y

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(− y

N+1) −1

2depth(z

N+1)

21

reminder: NDC z range is -1 to 1

Display z range is 0 to 1. gl.depthRange(n,f) can constrain further, but depth = 1 is both max and default

Page 38: Viewing 3 - Home | Computer Science at UBC

38

Device vs. Screen Coordinates •  viewport/window location wrt actual display not available

within GL •  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

display viewport

0 1024

768

0

300

500

display height

display width

x offset

y offset y

viewport

x 0 0 y

Page 39: Viewing 3 - Home | Computer Science at UBC

39

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 WCS VCS

CCS

NDCS

DCS

modeling transformation

viewing transformation

projection transformation

viewport transformation

alter w

/ w

object world viewing

device

normalized device

clipping

perspective division

glVertex3f(x,y,z)

glTranslatef(x,y,z) glRotatef(a,x,y,z) ....

gluLookAt(...)

glFrustum(...)

glutInitWindowSize(w,h) glViewport(x,y,a,b)

O2W W2V V2C

N2D

C2N

Page 40: Viewing 3 - Home | Computer Science at UBC

40

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

framebuffer

Page 41: Viewing 3 - Home | Computer Science at UBC

41

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

-1 NDCS

(z not shown)

real midpoint

0 xmax-1 0 DCS

(z not shown)

ymax-1

x=-1 x=1

Page 42: Viewing 3 - Home | Computer Science at UBC

42

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

Page 43: Viewing 3 - Home | Computer Science at UBC

43

Perspective Example

/ 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