projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume...

29
Projection: Introduction 2D viewing simple: 1. Clip image to window 2. Map to view port 3D more complex Need to map 3D world to 2D window Process called projection View volume determines part of world to be mapped to 2D window World clipped against VV VV projected onto front plane of VV (projection plane, view plane) General process: 3D world -→ coords specify VV 3D world -→ coords transform into canonical VV 3D transformed -→ world coords clip against canonical VV clipped transformed -→ world coords project onto projection plane 2D transformed -→ world coords transform window to view port device -→ coords Projections defined in terms of 1. View reference point (VRP) 2. View plane normal (VPN) 3. View up vector (VUP) 4. Viewing reference coordinate system (VRC system) 5. Center of (viewing/projection) window (CW) 6. Projection reference point (PRP) 7. Center of projection (COP) 8. Direction of projection (DOP) 9. Type of projection 1

Transcript of projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume...

Page 1: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Introduction

• 2D viewing simple:

1. Clip image to window

2. Map to view port

• 3D more complex

– Need to map 3D world to 2D window

– Process called projection

• View volume determines part of world to be mapped to 2D window

– World clipped against VV

– VV projected onto front plane of VV (projection plane, view plane)

• General process:

3D world−→coords

specifyVV

3D world−→coords

transforminto

canonical VV

3D transformed−→

world coords

clipagainst

canonical VV

clipped transformed−→

world coords

project ontoprojection

plane

2D transformed−→

world coords

transformwindow toview port

device−→coords

• Projections defined in terms of

1. View reference point (VRP)

2. View plane normal (VPN)

3. View up vector (VUP)

4. Viewing reference coordinate system (VRC system)

5. Center of (viewing/projection) window (CW)

6. Projection reference point (PRP)

7. Center of projection (COP)

8. Direction of projection (DOP)

9. Type of projection

1

Page 2: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Specifying a Coordinate System for Viewing

• View plane

– Defined by VRP and VPN

– VP contains VRP and is normal to VPN

– Defined in world coords

– (Cf gluLookAt)

• VRC system

– Defines a coord system with which to specify projection window

– Origin is VRP

– n axis is VPN

– v axis is projection of VUP onto view plane

V UP × V PN 6= 0

– u axis orthogonal to n and v

– u, n, and v form right-handed coord system

– To generate:

1. n axis is VPN

2. u = V UP × V PN3. v = n× u

2

Page 3: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Specifying a Coordinate System for Viewing (2)

• Viewing window

– Defined in terms of umin, umax, vmin, vmax

– Window does not have to be symmetric wrt VRP

– In general, CW 6≡ V RP

3

Page 4: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Specifying a View Volume

• Specified wrt VRC system

• PRP defines DOP:

1. Defined in terms of VRC

2. DOP is vector from PRP to CW

• If projection is parallel, sides of VV parallel to DOP: infinite parallelpiped

4

Page 5: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Specifying a View Volume (2)

• If projection is perspective, COP is PRP and sides of VV extend from PRP toedges of viewing window: pyramid

• Near and far (hither and yon) clip planes specified as distances from the VPalong n axis

– Positive values are in direction of VPN

near > far (or VV will be empty)

• Parallel VV: finite parallelpiped

• Perspective VV: truncated pyramid (frustum)

5

Page 6: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Simple Planar Geometric Projections

• Only simple projection considered here

– More complex situations will be transformed into simple cases (discussedlater)

1. Perspective

(a) Case 1

• VP normal to z axis

• VP located at z = d

• COP at origin

• Point p(x, y, z)) projected onto point pp(xp, yp, zp)

• By similar triangles,xpd

=x

zypd

=y

z

xp =d ∗ xz

=x

z/d

yp =d ∗ yz

=y

z/d

• Undefined for z = 0

6

Page 7: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Simple Planar Geometric Projections (2)

• Transformation matrix:

Mper =

1 0 0 00 1 0 00 0 1 00 0 1/d 0

Mper ∗ p =[x y z z/d

]T– Homogenize by dividing by z/d, giving

pP =[

xz/d

yz/d d 1

]T(b) Case 2

• VP at z = 0, normal to z axis

• COP located at z = −d• By similar triangles,

xpd

=x

z + dypd

=y

z + d

xp =d ∗ xz + d

=x

z/d+ 1

yp =d ∗ yz + d

=y

z/d+ 1

7

Page 8: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Simple Planar Geometric Projections (3)

• Transformation matrix:

M ′per =

1 0 0 00 1 0 00 0 0 00 0 1/d 1

• No restrictions on z in this case

M ′per ∗ p =

[x y 0 z/d+ 1

]T• Homogenizing by dividing by z/d+ 1 gives

pP =[

xz/d+1

yz/d+1 0 1

]T2. Parallel

VP at z = 0, normal to z axisxp = x, yp = y, zp = 0

8

Page 9: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Simple Planar Geometric Projections (4)

• Transformation matrix:

Mpar =

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

Mpar ∗ p =[x y 0 1

]T

• As d→∞, M ′per →Mpar

9

Page 10: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: General Planar Geometric Projections

• VP located at (0, 0, zp)

• Q is distance from COP to (0, 0, zp)

•[dx dy dz

]Tis unit vector from (0, 0, zp) to COP

• Parametric equation of line from COP to P is

P (α) = COP + α(P − COP )

• Pp lines on this line

• COP =[

0 0 zp]T

+Q[dx dy dz

]T• Substituting above for COP in line parametric equation gives

Px(α) = Qdx + α(x−Qdx)Py(α) = Qdy + α(y −Qdy)Pz(α) = (zp +Qdz) + α(z − (zp +Qdz))

• Pp lies on line at point where Pz(α) = zp

• Solve for α by substituting zp for Pz(α) in parametric equation:

α =zp − (zp +Qdz)

z − (zp +Qdz)

• Solve for xp and yp using above value for α:

xp =x− z dxdz + zp

dxdz

zp−zQdz

+ 1

yp =y − z dydz + zp

dydz

zp−zQdz

+ 1

10

Page 11: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: General Planar Geometric Projections (2)

• Want to express zp in same form as xP and yp:

zp = zp

zp−zQdz

+ 1zp−zQdz

+ 1

=−z zp

Qdz+

z2p+zpQdzQdz

zp−zQdz

+ 1

• Corresponding matrix:

Mg =

1 0 −dxdz

zpdxdz

0 1 −dydz

zpdydz

0 0 − zpQdz

z2pQdz

+ zp0 0 − 1

Qdz

zpQdz

+ 1

• When multiply point by Mg, homogeneous coordinate is

zp − zQdz

+ 1

• Mg reduces to simple projection matrices:

zp Q[dx dy dz

]

Mpar 0 ∞[

0 0 −1]

Mper d d[

0 0 −1]

M ′per 0 d

[0 0 −1

]• Since DOP normal to VP, matrix represents a one-point projection

– Can use this matrix to specify location of COP for a specific projection:

∗ Vanishing point on z-axis at ∞:[

0 0 1 0]T

∗ Multiplying by Mg gives[Qdx = xp Qdy = yp zp 1

]∗ If know Q and projected coords of vanishing point, can determine dz

from√dx2 + dy2 + dz2 = 1

11

Page 12: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Canonical View Volumes - Intro

• Prior to projection, will clip primitives against VV

• In general,

– VP not normal to z axis

– DOP not parallel to z axis

– VV not symmetric about z axis

• While can do all necessary calculations with any VV, some are more easy towork with

• A canonical VV is one that is ”standard” and has very specific properties thatmake calculations easier

• For parallel projections, the canonical VV is often a cube and/or has sides thatintersect the axes at 0, ±1

– E.g., x = ±1, y = ±1, znear = 0, zfar = −1

• For perspective projections, the canonical VV usually has sides with slope = ±1

– E.g., xmin = −z, xmax = z, ymin = −z, ymax = z, znear = −zmin, zfar = 1

12

Page 13: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Canonical View Volumes - Intro (2)

• Most API’s transform general VV into canonical VV prior to clipping

– Advantage is makes clipping easier

– Disadvantage is that transform parts that may be clipped out

• Canonical VV defined in terms of normalized projection coordinates (NPC)

• After clipping and projection, map results to display

• Task is then to

1. Transform a general VV into a canonical VV

– These are called normalizing transforms

2. Clip

3. Project

• Following generates 3 normalizing transform matrices:

1. Npar (for parallel projections)

2. Nper

3. N ′per (for perspective projections)

• In each case, image is distorted so that the projected result is identical to imagethat would result if had projected using original VV

13

Page 14: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Npar

• Derivation considers most general case: oblique projection

• Steps:

14

Page 15: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Npar (2)

1. Translate VRP to origin

T (−vrpx,−vrpy,−vrpz)2. Rotate VRC so n→ z, v → y, u→ x,

– Most easily done creating special orthogonal matrix

– Let

rz =V PN

|V PN |– Let

rx =V UP × rz|V UP × rz|

– Let ry = rz × rx

– Then R() =

rx1 rx2 rx3 0ry1 ry2 ry3 0rz1 rz2 rz3 00 0 0 1

3. Shear to bring DOP into z

PRP =[prpx prpy prpz 1

]TCW =

[umax+umin

2vmax+vmin

2 0 1]T

DOP = CW − PRP =[dopx dopy dopz 0

]T– Want shear matrix that leaves z coords unchanged:

Hxy(hx, hy) =

1 0 hx 00 1 hy 00 0 1 00 0 0 1

– Need hx, hy so that DOP ′ = Hxy ∗DOP =

[0 0 dopz 0

]Thx = −dopx

dopz

hy = −dopydopz

15

Page 16: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Npar (3)

4. Translate and scale into canonical VV

– VV bounded by

(a) umin ≤ x ≤ umax

(b) vmin ≤ y ≤ vmax

(c) B ≤ z ≤ F

(a) Translate front center of VV to origin

Tpar = T (−umax+umin

2 ,−vmax+vmin

2 ,−F )

(b) Scale to 2× 2× 1

Spar = S( 2umax−umin

, 2vmax−vmin

, 1F−B )

• Normalizing transforms:

Npar = Spar ∗ Tpar ∗Hpar ∗R ∗ T (−V RP )

16

Page 17: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Nper

• Derivation considers most general case: DOP not normal to PP

• Steps:

17

Page 18: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: Nper (2)

1. Translate VRP to origin (same as parallel case)

T (−vrpx,−vrpy,−vrpz)2. Rotate VRC so n→ z, v → y, u→ x (same as parallel case)

R() =

rx1 rx2 rx3 0ry1 ry2 ry3 0rz1 rz2 rz3 00 0 0 1

3. Translate COP → origin

T (−prpx,−prpy,−prpz)– Brings VRC into world coordinate system

4. Shear to bring CW − PRP into z (same as parallel case)

Hxy(−dopxdopz

,−dopydopz

)

5. Scale into canonical VV

– Do as 2 steps:

(a) Scale in x and y to bring sides of VV into x, y = ±z

S1

−2 ∗ vrp′zumax − umin

,−2 ∗ vrp′zvmax − vmin

, 1

(b) Scale in x, y, z to bring back plane at z = vrp′z +B → z = −1

Ss

−1

vrp′z +B,−1

vrp′z +B,−1

vrp′z +B

Sper = S2 ∗ S1

= S(

2∗vrp′z(umax−umin)(vrp′z+B) ,

2∗vrp′z(vmax−vmin)(vrp′z+B) ,

−1vrp′z+B

)– Results are

z =vrp′z

vrp′z +B

zmin =vrp′z + F

vrp′z +B

zmax = −vrp′z +B

vrp′z +B= −1

• Normalizing transforms:

Nper = Sper ∗Hpar ∗ T (−PRP ) ∗R ∗ T (−V RP )

18

Page 19: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: N ′per

• Projection in parallel case is easy:

– Simply drop z coordinates and keep (x, y) component of vertices

• Clipping in parallel case is easier than in perspective:

– Easy to determine whether points lies within rectangular parallelpiped

∗ (xmin ≤ x ≤ xmax, etc)

– Point of intersection of line with plane defined by x = α also easy

• Can canonical perspective VV be transformed into canonical parallel VV sothat projected results are equivalent?

• Consider simple singular perspective transform matrix

Mper =

1 0 0 00 1 0 00 0 0 00 0 −1 0

Mper ∗[x y z 1

]T=[x y 0 −z

]T– This produces the normalized point

[−x/z −y/z 0 1

]T, which projects

onto the z = 0 plane

• Now consider

M =

1 0 0 00 1 0 00 0 α β0 0 −1 0

Mpar ∗M =

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

1 0 0 00 1 0 00 0 α β0 0 −1 0

=

1 0 0 00 1 0 00 0 0 00 0 −1 0

– Hence,

Mpar ∗M ∗[x y z 1

]T ≡Mper ∗[x y z 1

]T

19

Page 20: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: N ′per (2)

• 3D APIs generally use this approach for all projections

– Perspective frustums transformed into parallelpipeds

– Transformation affects all vertices within volume

– Parallel projection of warped objects equivalent to perspective projectionof originals

• The goal is to find α and β of matrix M that take the canonical perspectiveVV into the canonical parallel VV

– Projection and clipping will always be performed against a single canonicalVV

• M ∗[x y z 1

]T=[x y αz + β −z

]T= p′

• Homogenizing p′ gives[−x/z −y/z −(α + β/z) 1

]T

20

Page 21: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: N ′per (3)

• We want to find α and β so that

αzmin + β = 0, and−1α + β = −1

1. αzmin + β = 0β = −αzmin

2. −1α + β = −1−α− αzmin = −1α(1 + zmin = 1α = 1/(1 + zmin)β = −zmin/(1 + zmin)

• So,

M =

1 0 0 00 1 0 00 0 1

1+zmin

−zmin

1+zmin

0 0 −1 0

• N ′per = M ∗Nper = M ∗ Sper ∗Hpar ∗ T (−PRP ) ∗R ∗ T (−V RP )

21

Page 22: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Clipping: 3D Using Cohen-Sutherland

• Parallel case

– Use 6 bits:

bit condition indication

1 y > 1 above2 y < 1 below3 x > 1 right4 x < 1 left5 z < −1 behind6 z > 0 in front

– Line defined by P0(x0, y0, z0), P1(x1, y1, z1)

– Parametric representation:

x = x0 + t(x1 − x0),y = y0 + t(y1 − y0),z = z0 + t(z1 − z0)

– To find intersection with y = 1 plane:

t =1− y0y1 − y0

∗ If 0 ≤ t ≤ 1, intersection is on clip edge

∗ Substitute t in equations for Pi(x, y, z) of intersection

22

Page 23: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Clipping: 3D Using Cohen-Sutherland (2)

• Perspective case

– Outcode bits:

bit condition indication

1 y > −z above2 y < z below3 x > −z right4 x < z left5 z < −1 behind6 z > zmin in front

– Line defined by P0(x0, y0, z0), P1(x1, y1, z1)

– For sloping planes:

y0 + t(y1 − y0) = z0 + t(z1 − z0)

t =z0 − y0

(y1 − y0)− (z1 − z0)

∗ If 0 ≤ t ≤ 1, intersection is on clip edge

∗ Substitute t in equations for Pi(x, y, z) of intersection

x = x0 +(y1 − x0)(z0 − y0)

(y1 − y0)− (z1 − z0)

y = y0 +(y1 − x0)(z0 − y0)

(y1 − y0)− (z1 − z0)

23

Page 24: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Clipping: 3D Using Cyrus-Beck

• Clip against all 6 sides

• Will generate at most 6 values of t

• Parallel case:

– Simply use 2D equations as applied to front and back clip planes

• Perspective case

– Must be modified for canonical perspective view volume

∗ Normals are modified as in table below

∗ Use same equations as for 2D case, with adjusted normals

∗ Points on edges adjusted accordingly

Edge Normal PEiP0 − PEi

right [ 1 0 1 ]T [ x y −x ]T [ x0 − x y0 − y z0 + x ]T

left [ −1 0 1 ]T [ x y x ]T [ x0 − x y0 − y z0 − x ]T

bottom [ 0 −1 1 ]T [ x y y ]T [ x0 − x y0 − y z0 − y ]T

top [ 0 1 1 ]T [ x y −y ]T [ x0 − x y0 − y z0 + y ]T

back [ 0 0 −1 ]T [ x y zmin ]T [ x0 − x y0 − y z0 − zmin ]T

front [ 0 0 1 ]T [ x y −1 ]T [ x0 − x y0 − y z0 + 1 ]T

24

Page 25: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Clipping: 3D Using Cyrus-Beck (2)

Edge t =Ni•(P0−PEi

)

−Ni•D

right (x0−x)+(Z0+x)−(dx+dz) = x0+z0

−dx−dz

left −(x0−x)+(Z0−x)dx−dz = −x0+z0

dx−dz

bottom −(y0−y)+(Z0−y)dy−dz = −y0+z0

dy−dz

top (y0−y)+(Z0+y)−(dy+dz) = y0+z0

−dy−dz

back (z0−zmin)−dz = z0−zmin

−dz

front −(z0+1)dz = −z0−1

dz

25

Page 26: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: OpenGL VRC, NPC, Canonical VV

• VRC defined by gluLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)

– eye defines V RP

– Vector from at to eye defines V PN

– Vector from eye to up defines V UP

– So

n =eye− at|eye− at|

u =(up− eye)× n|(up− eye)× n|v = n× u

– VRC transformed into world axes to place camera at origin facing downnegative z axis

26

Page 27: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: OpenGL: VRC, NPC, Canonical VV (2)

• VV more restrictive than general VV specification

– General VVs specified by glFrustum and glOrtho with arguments left,right, top, bottom, front, back

∗ left/right correspond to umin/max

∗ top/bottom correspond to vmin/max

∗ front/back as expected

– PRP at eye (transformed to origin)

∗ Cannot be specified independently of VRP

– VP located at −F– VP is not always at origin

– DOP defined by n

∗ DOP always normal to VP in openGL parallel projection

27

Page 28: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: OpenGL: VRC, NPC, Canonical VV (3)

• Canonical parallel VV extends from x = ±1, y = ±1, z = ±1

• Canonical perspective VV

– Sides defined by x = ±z, y = ±z– Front plane (PP) at z = 1

– Back plane at z = −1

• Uses 2D viewport, not 3D viewport

28

Page 29: projection projection plane view planedjmoon/cgalgs/cgalgs...Projection: Specifying a View Volume (2) If projection is perspective, COP is PRP and sides of VV extend from PRP to edges

Projection: OpenGL: VRC, NPC, Canonical VV (4)

• Matrices

1. Modelview

– Catenates user transforms to those generating VRC

– Created from gluLookAt

– Default: V PN = z, V UP = y, COP at origin

2. Projection

– Created from glFrustum, glOrtho

29