3D Viewing Pipeline - Drexel CCIdavid/Classes/CS430/Lectures/... · 2019. 11. 19. · 13...

42
1 CS 430 Computer Graphics 3D Viewing Pipeline Week 7, Lecture 13 David Breen, William Regli and Maxim Peysakhov Department of Computer Science Drexel University

Transcript of 3D Viewing Pipeline - Drexel CCIdavid/Classes/CS430/Lectures/... · 2019. 11. 19. · 13...

1

CS 430Computer Graphics

3D Viewing PipelineWeek 7, Lecture 13

David Breen, William Regli and Maxim PeysakhovDepartment of Computer Science

Drexel University

2

Overview• Projection Mathematics • Canonical View Volume• Parallel Projection Pipeline• Perspective Projection Pipeline

Lecture Credits: Most pictures are from Foley/VanDam; Additional and extensive thanks also goes to those credited on individual slides

1994 Foley/VanDam/Finer/Huges/Phillips ICGPics/Math courtesy of Dave Mount @ UMD-CP

3

Projection Mathematics

• What is the set of transformations needed to map 3D lines/planes onto a 2D screen positioned in 3D?

• Basic procedure– 4D homogeneous coordinates to– 3D homogeneous coordinates for– every primitive in– the 3D view volume

4

Projection Mathematics

We present 4x4 matrices to be used for implementing projections

• Perspective Projections– How much to scale objects as a function of

distance?• Parallel Projections

– (simplified case) Just chop out the z coordinate

5

The Perspective Projection

Determining scale• Consider

– Point P– Projected onto

projection plane as point Pp

• Idea: compute ratios via similar triangles

1994 Foley/VanDam/Finer/Huges/Phillips ICG

6

The Perspective Projection

• In the x direction ratio is

1994 Foley/VanDam/Finer/Huges/Phillips ICG

zd

=xxp

xp =xz /d

7

The Perspective Projection

• In the y direction ratio is

1994 Foley/VanDam/Finer/Huges/Phillips ICG

zd

=yyp

yp =yz /d

8

The Perspective Projection

• Homogenous perspective projection matrix

1994 Foley/VanDam/Finer/Huges/Phillips ICG

!!!!!

"

#

$$$$$

%

&

=

0100010000100001

per

d

M Assumes VPN is z axis.

9

The Perspective Projection

• Homogenous perspective projection

1994 Foley/VanDam/Finer/Huges/Phillips ICG

xyzz /d

"

#

$ $ $ $

%

&

' ' ' '

=

1 0 0 00 1 0 00 0 1 00 0 1

d 0

"

#

$ $ $ $

%

&

' ' ' '

xyz1

"

#

$ $ $ $

%

&

' ' ' '

10

The Perspective Projection

• Homogenous perspective projection to 3D

1994 Foley/VanDam/Finer/Huges/Phillips ICG

xyzz /d

"

#

$ $ $ $

%

&

' ' ' '

=

1 0 0 00 1 0 00 0 1 00 0 1

d 0

"

#

$ $ $ $

%

&

' ' ' '

xyz1

"

#

$ $ $ $

%

&

' ' ' '

xz /dyz /dd

"

#

$ $ $ $

%

&

' ' ' '

11

The Orthographic Projection

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Mort =

1 0 0 00 1 0 00 0 0 00 0 0 1

"

#

$ $ $ $

%

&

' ' ' '

Assumes DOP is z axis.

Just makesz component 0

12

Implementing Projections(Foley et al.)

… a sequence of matrix operations and a clipping procedure…

1994 Foley/VanDam/Finer/Huges/Phillips ICG

13

Implementing Projections(Foley et al.)

1. Extend 3D coordinates to homogenous coords2. Apply normalizing transformation, Npar or Nper3. Divide by W to map back down to 3D4. Clip in 3D against canonical view volume

• parallel or perspective view volume5. Extend 3D coordinates back to homogenous6. Perform parallel projection using Mort or

Perform perspective projection Mper

7. Divide by W to map from homogenous to 2D coordinates (division effects perspective projection)

8. Translate and scale (in 2D) to device coordinates

14

Canonical View Volume: Parallel Projection

• Defined by 6 planes:– x = 1– x = -1– y = 1– y = -1– z = 0– z = -1

• Easy to clip against

1994 Foley/VanDam/Finer/Huges/Phillips ICG

15

Canonical View Volume: Perspective Projection

1994 Foley/VanDam/Finer/Huges/Phillips ICG

• Defined by 6 planes:– x = z– x = -z– y = z– y = -z– z = zmin

– z = -1

• Easy to clip against

16

Parallel Projection Pipeline

1. Translate VRP to the origin

2. Rotate so VPN becomes z, VUP becomes y and ubecomes x

3. Shear to make direction of the projection become parallel to z

4. Translate and scale into a canonical view volume

Transforming an arbitrary view volume into the canonical one

17

1. Translate VRP to the origin

• Simple translation T(-VRP)

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Top Projection Off-Axis Projection Top Projection Off-Axis Projection

T =

1 0 0 −vrpx0 1 0 −vrpy0 0 1 −vrpz0 0 0 1

#

$

% % % %

&

'

( ( ( (

18

• VPN rotated to z• VUP rotated to y••

2. Rotate

1994 Foley/VanDam/Finer/Huges/Phillips ICG

!!!!

"

#

$$$$

%

&

=

1000000

321

321

321

zzz

yyy

xxx

rrrrrrrrr

R

||VPNVPN

zR =

|| z

zRVUPRVUP

xR ××=

xzy RRR ×=

[ ],...,, 321 xxxx rrrR =

VRP, VPN & VUP inWorld Coordinates (x,y,z)

2. Rotate

19

20

3. Shear

• Computing direction of projection DOP

1994 Foley/VanDam/Finer/Huges/Phillips ICG

PRP =prpuprpvprpn

!

"

####

$

%

&&&&

,CW =

umax+umin2

vmax+vmin2

0

!

"

####

$

%

&&&&

,DOP =

umax+umin2 − prpu

vmax+vmin2 − prpv−prpn

!

"

####

$

%

&&&&

,SHpar =

1 0 shx 00 1 shy 0

0 0 1 00 0 0 1

!

"

#####

$

%

&&&&&

PRP in VRCCoordinates (u,v,n)

21

3. Shear (Cont.)

1994 Foley/VanDam/Finer/Huges/Phillips ICG

SHpar =

1 0 shx 00 1 shy 00 0 1 00 0 0 1

"

#

$ $ $ $

%

&

' ' ' '

• For Shxy z’=z, x’=x+z•shx, y’=y+z•shy

x 'y 'z'1

"

#

$ $ $ $

%

&

' ' ' '

=

1 0 shx 00 1 shy 00 0 1 00 0 0 1

"

#

$ $ $ $

%

&

' ' ' '

xyz1

"

#

$ $ $ $

%

&

' ' ' '

22

3. Shear (Cont)• For Shxy z’=z, x’=x+z•shx, y’=y+z•shy

• Find Shxy such that:DOP’ = [0,0,DOPz,0]T = Shxy• DOP

0 = DOPx + DOPz • shx , 0 = DOPy + DOPz • shy

1994 Foley/VanDam/Finer/Huges/Phillips ICG

shx = − DOPxDOPz

, shy = −DOPyDOPz

Old Foley et al. is wrong!

24

• Given

• We can compute the SHpar matrix

3. Shear (Finally)

shx = − DOPxDOPz

,

shy = −DOPyDOPz

DOP =

umax+umin2 − prpu

vmax+vmin2 − prpv−prpn

"

#

$$$$

%

&

''''

,SHpar =

1 0 shx 00 1 shy 0

0 0 1 00 0 0 1

"

#

$$$$$

%

&

'''''

1 0 1/2(umax + umin ) − prpuprpn

0

0 1 1/2(vmax + vmin ) − prpvprpn

0

0 0 1 00 0 0 1

#

$

% % % % % %

&

'

( ( ( ( ( (

3. Shear

25

26

4. Translate and Scale • Translate the center of the volume to the origin• Scaling to 2x2x1• Needed for 3D clipping

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Top Projection Off-Axis Projection Top Projection Off-Axis Projection

Tpar =

1 0 0 −(umax + umin ) /20 1 0 −(vmax + vmin ) /20 0 1 −F0 0 0 1

#

$

% % % %

&

'

( ( ( (

Spar =

2 /(umax − umin ) 0 0 00 2 /(vmax − vmin ) 0 00 0 1/(F − B) 00 0 0 1

#

$

% % % %

&

'

( ( ( (

Foley et al. is wrong!

27

Parallel Projection View Volume Transformation

Npar = (Spar • (Tpar • (SHpar • (R • T(-VRP)))))

• Apply to all model vertices

• P’ = Npar P

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Parallel Projection Summary

28

29

Perspective Projection Pipeline

1. Translate VRP to the origin2. Rotate so VPN becomes z,

VUP becomes y and ubecomes x

3. Translate COP to origin4. Shear so volume centerline

becomes z axis5. Scale into a canonical view

volume for clipping

Transforming an arbitrary view volume into the canonical one

30

1. Translate VRP to the origin

• Simple translation T(-VRP)

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Top Projection Off-Axis Projection Top Projection Off-Axis Projection

T =

1 0 0 −vrpx0 1 0 −vrpy0 0 1 −vrpz0 0 0 1

#

$

% % % %

&

'

( ( ( (

VRP, VPN & VUP inWorld Coordinates (x,y,z)

31

• VPN rotated to z• VUP rotated to y••

2. Rotate

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Top Projection Off-Axis Projection Top Projection Off-Axis Projection

!!!!

"

#

$$$$

%

&

=

1000000

321

321

321

zzz

yyy

xxx

rrrrrrrrr

R

||VPNVPN

zR =

|| z

zRVUPRVUP

xR ××=

xzy RRR ×=

[ ],...,, 321 xxxx rrrR =

321994 Foley/VanDam/Finer/Huges/Phillips ICG

3. Translate

Top Projection Off-Axis Projection Top Projection Off-Axis Projection

• Simple translation T(-PRP)

T =

1 0 0 −prpu0 1 0 −prpv0 0 1 −prpn0 0 0 1

#

$

% % % %

&

'

( ( ( ( PRP in VRC

Coordinates (u,v,n)

33

• Goal is to transform the center line to the z axis

• Same as parallel projection

• Shear matrix is the same!

1994 Foley/VanDam/Finer/Huges/Phillips ICG

4. Shear

Top Projection Off-Axis Projection Top Projection Off-Axis Projection

SHpar =

1 0 1/2(umax + umin ) − prpuprpn

0

0 1 1/2(vmax + vmin ) − prpvprpn

0

0 0 1 00 0 0 1

#

$

% % % % % %

&

'

( ( ( ( ( (

34

• We can define VRP after transformation as– VRP’= SHpar • T(-PRP) • [0,0,0,1]T

• vrp’z = -prpn since shear does not affect z coordinates• First, we scale differentially in x and y to set plane slopes

to 1 and -1

• Second, we scale uniformly by

1994 Foley/VanDam/Finer/Huges/Phillips ICG

5. Scaling

Top Projection Off-Axis Projection Top Projection Off-Axis Projection

−2vrpz 'umax −umin

, −2vrpz 'vmax −vmin

, 1[ ]−1

vrpz '+B=

1prpn −B

35

5. Scaling (Cont)

1994 Foley/VanDam/Finer/Huges/Phillips ICG

• We can combine the transformations

Sper =

2vrpz '(umax − umin )(vrpz '+B)

0 0 0

0 2vrpz '(vmax − vmin )(vrpz '+B)

0 0

0 0 −1vrpz '+B

0

0 0 0 1

#

$

% % % % % % %

&

'

( ( ( ( ( ( (

VPN

VRP

(a) Before scaling

(1, —1)

Frontclippingplane: =

min

(b) After scaling

1

—1

= —

=

—1

Backclippingplane:

= —1

Viewplane at

proj

= max

2

—min

= — max

2

—min

+ = +

−2vrpz 'umax −umin

, −2vrpz 'vmax −vmin

, 1[ ]X,Y scaling

36

5. Scaling (Cont)

1994 Foley/VanDam/Finer/Huges/Phillips ICG

Sper =

2(prpn )(umax −umin )(prpn −B)

0 0 0

0 2prpn(vmax − vmin )(prpn −B)

0 0

0 0 1prpn −B

0

0 0 0 1

"

#

$$$$$$$$$

%

&

'''''''''

VPN

VRP

(a) Before scaling

(1, —1)

Frontclippingplane: =

min

(b) After scaling

1

—1

= —

=

—1

Backclippingplane:

= —1

Viewplane at

proj

= max

2

—min

= — max

2

—min

+ = +

2 prpnumax−umin

, 2 prpnvmax−vmin

, 1"# $%

X,Y scaling

• We can combine the transformations

Important Projection Info• Back clipping plane is z = -1

• Front clipping plane is

• Projection plane is

37

zmin = −vrpz '+Fvrpz '+B

=prpn − FB − prpn

d = zproj =−vrpz 'vrpz '+B

=prpn

B − prpn

391994 Foley/VanDam/Finer/Huges/Phillips ICG

Perspective Projection Pipeline

Nper = (Sper • (SHpar • (T(-PRP) • (R • T(-VRP)))))

• Apply to all model vertices

• P’ = Nper P

Perspective Projection Summary

40

41

Summary of 3D Transforms

• We know how to take any projection and convert it into a canonical View Volume

• 3D edges can be clipped against it and projected onto screen

1994 Foley/VanDam/Finer/Huges/Phillips ICG

42

Implementing Projections Without 3D Clipping

1. Extend 3D coordinates to homogeneous coordinates

2. Apply normalizing transformation, Npar or Nper3. Perform trivial reject test with view volume4. Perform parallel projection or

perform perspective projection5. Clip against 2D “world” (view plane) window6. Translate and scale (in 2D) to device

coordinates (i.e. into viewport)7. Draw/Fill polygons

43

Transformed Window

• Parallel Projection♦ (-1,-1) → (1,1)

• Perspective Projection♦ (-|zproj|, -|zproj|)→ (|zproj|, |zproj|)

• Use these for world window parameters in viewport mapping and 2D clipping

44

Programming assignment 4

• Read SMF file• Implement parallel projection• Implement perspective projection• Draw projected and clipped polygon

edges