viewing in three dimensions: mathematics of...

44
I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S Andries van Dam September 20, 2001 3D Viewing III 1/44 Review from last time: specifying a 3D view—setting up the synthetic camera the shape of the 3D view volume which is the clipping region The mathematics of planar geometric projections how do we get from the view specification to the 2D picture? Lecture roadmap deriving 2D picture from 3D view parameters is a hard problem our method relies on the fact that it’s easier to make a picture from the canonical view volume (3D parallel projection cuboid) at the canonical view position (camera at the origin, looking down the negative z-axis) than it is from the user’s specified arbitrary position. We’ll break the process into three easier steps: 1. get parameters for view specification (covered in last lecture) 2. transform from specified view volume into canonical view volume 3. using canonical view, clip and project scene to make 2D picture Viewing in Three Dimensions: Mathematics of Projections

Upload: others

Post on 17-Mar-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 1/44

• Review from last time:– specifying a 3D view—setting up the synthetic camera– the shape of the 3D view volume which is the clipping

region

• The mathematics of planar geometric projections– how do we get from the view specification to the 2D

picture?

• Lecture roadmap– deriving 2D picture from 3D view parameters is a hard

problem– our method relies on the fact that it’s easier to make a

picture from the canonical view volume (3D parallel projection cuboid) at the canonical view position (camera at the origin, looking down the negative z-axis) than it is from the user’s specified arbitrary position. We’ll break the process into three easier steps:

1. get parameters for view specification (covered in last lecture)

2. transform from specified view volume into canonical view volume

3. using canonical view, clip and project scene to make 2D picture

Viewing in Three Dimensions: Mathematics of Projections

Page 2: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 2/44

• Reduce degrees of freedom; five steps to specifying view volume

– position the camera (and therefore its view/film plane)– point it at what you want to see, with the camera in the

orientation you want– define the field of view (for a perspective view volume,

aspect ratio of film and angle of view: somewhere between wide angle, normal, and zoom; for a parallel view volume, width and height)

– choose perspective or parallel projection – determine the focal distance

Stage One: Specifying a ViewVolume

Page 3: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 3/44

• Perspective Projection: Truncated Pyramid – Frustum

• Look vector is the center line of the pyramid, the vector that lines up with “the barrel of the lens”

• Note: Up vector need not to be perpendicular to Look vector, but they cannot be collinear

Examples of a View Volume (1/2)

Position

Near distance

Far distance

Heightangle

Width angle =

Look vector

Up vector

Height angle •Aspect ratio

x

y

z

Page 4: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 4/44

• Orthographic Parallel Projection: Truncated View Volume – Cuboid

• Orthographic parallel projection has no view angle parameter

Examples of a View Volume (2/2)

Height

Width

Look vectorNear

distance

Position

Fardistance

Up vector

x

y

z

Page 5: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 5/44

• Placement of view volume (visible part of world) specified by camera’s position and orientation

– Position (a point)– Look and Up vectors

• Shape of view volume specified by– horizontal and vertical view angles– front and back clipping planes

• Perspective projection: projectors intersect at Position• Parallel projection: projectors parallel to Look vector,

but never intersect (or intersect at infinity)• Coordinate Systems

– world coordinates – standard right-handed xyz 3-space– viewing reference coordinates – camera-space right

handed coordinate system (u, v, n); origin at Position and axes rotated by orientation; used for transforming arbitrary view into canonical view

Specifying Arbitrary 3D Views

Up

Look

Page 6: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 6/44

• We have now specified an arbitrary view using our viewing parameters

• Problem: map arbitrary view specification to 2D picture of scene. This is hard, both for clipping and for projection

• Solution: reduce to a simpler problem and solve

• Note: Look vector along negative, not positive, z-axis is arbitrary but makes math easier

Arbitrary View Volume Too Complex

Canonical View at Origin(u, v, n maps to x, y, z)

– there is a view specification from which it is easy to take a picture. We’ll call it the canonical view: from the origin, looking down the negative z-axis

– parallel projection– sits at origin: Position = (0, 0, 0)– looks along negative z-axis: Look vector = (0, 0, –1)– oriented upright: Up vector = (0, 1, 0)– film plane extending from –1 to 1 in x and y

– think of the scene as lying behind the screen as if screen were a house’s window:

Page 7: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 7/44

• Our goal is to transform our arbitrary view and the world to the canonical view volume, maintaining the relationship between view volume and world, then take picture

– for parallel view volume, transformation is affine: made up of translations, rotations, and scales

– in the case of a perspective view volume, it also contains a non-affine† perspective transformation that frustum into a parallel view volume, a cuboid

– the composite transformation that will transform the arbitrary view volume to the canonical view volume, named the normalizing transformation, is still a 4x4 homogeneous coordinate matrix that typically has an inverse

– easy to clip against this canonical view volume; clipping planes are axis-aligned!

– projection using the canonical view volume is even easier: just omit the z-coordinate

– for oblique parallel projection, a shearing transform is part of the composite transform, to “de-oblique” the view volume

Normalizing to the Canonical View Volume

† Affine transformations preserve parallelism but not lengths and angles. The perspective transformation is a type of non-affine transformation known as a projective transformation, which does not preserve parallelism

Page 8: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 8/44

• Problem of taking a picture has now been reduced to problem of finding correct normalizing transformation

• It is a bit tricky to find the rotation component of the normalizing transformation. But it is easier to find the inverse of this rotational component (trust us)

• So we’ll digress for a moment and focus our attention on the inverse of the normalizing transformation, which is called the viewing transformation. The viewing transformation turns the canonical view into the arbitrary view, or (x, y, z) to (u, v, n)

Viewing Transformation Normalizing Transformation

Page 9: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 9/44

• We know the view specification: Position, Look vector, and Up vector

• We need to derive an affine transformation from these parameters that will translate and rotate the canonical view into our arbitrary view

– the scaling of the film to make a square cross-section and then clipping will happen at a later stage

• Translation is easy to find: we want to translate the origin to the point Position; therefore, the translation matrix is

• Rotation is harder: how do we generate a rotation matrix from the viewing specifications that will turn x, y, z, into u, v, n?

– a digression on rotation will help answer this

Building Viewing Transformation from View Specification

=

1000100010001

)(z

y

x

PosPosPos

PositionT

Page 10: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 10/44

3 x 3 rotation matrices• We learned about 3 x 3 matrices that “rotate” the

world (we’re leaving out the homogeneous coordinate for simplicity)

• When they do, the three unit vectors that used to point along the x, y, and z axes are moved to new positions

• Because it is a rigid-body rotation– the new vectors are still unit vectors– the new vectors are still perpendicular to each other– the new vectors still satisfy the “right hand rule”

• Any matrix transformation that has these three properties is a rotation about some axis by some amount!

• Let’s call the three x-axis, y-axis, and z-axis-aligned unit vectors e1, e2, e3

• Writing out:

Rotation (1/5)

=

001

1e

=

010

2e

=

100

3e

Page 11: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 11/44

• Let’s call our rotation matrix M and suppose that it has columns v1, v2, and v3:

• When we multiply M by e1, what do we get?

• Similarly for e2 and e3:

Rotation (2/5)

[ ]321 vvvM =

[ ] 23212

010

vvvvMe =

=

[ ] 33213

100

vvvvMe =

=

[ ] 13211

001

vvvvMe =

=

Page 12: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 12/44

• Thus, for any matrix M, we know that Me1 is the first column of M

• If M is a rotation matrix, we know that Me1 (i.e., where e1 got rotated to) must be a unit-length vector (because rotations preserve length)

• Since Me1 = v1, the first column of any rotation matrix M must be a unit vector

• Also, the vectors e1 and e2 are perpendicular…• So if M is a rotation matrix, the vectors Me1 and Me2

are perpendicular… (if you start with perpendicular vectors and rotate them, they’re still perpendicular)

• But these are the first and second columns of M … Ditto for the other two pairs

• As we noted in the slide on rotation matrices, for a rotation matrix with columns vi

– columns must be unit vectors: ||vi|| = 1– columns are perpendicular: vi • vj = 0 (i ≠ j)

Rotation (3/5)

Page 13: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 13/44

• Therefore (for rotation matrices)

• We can write this matrix of vi•vj dot products as

where MT is a matrix whose rows are v1, v2, and v3

• Also, for matrices in general, M-1M = I, (actually, M-1

exists only for “well-behaved” matrices)• Therefore, for rotation matrices only we have just

shown that M-1 is simply MT

• MT is trivial to compute, M-1 takes considerable work: big win!

Rotation (4/5)

=

⋅⋅⋅⋅⋅⋅⋅⋅⋅

100010001

332313

322212

312111

vvvvvvvvvvvvvvvvvv

IMM T =

Page 14: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 14/44

Summary• If M is a rotation matrix, then its columns are

pairwise perpendicular and have unit length• Inversely, if the columns of a matrix are pairwise

perpendicular and have unit length and satisfy the right-hand rule, then the matrix is a rotation

• For such a matrix,

Rotation (5/5)

=

100010001

MM T

Page 15: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 15/44

• Now we know that a rotation matrix is a matrix that will turn the perpendicular unit vectors e1, e2, and e3into the new perpendicular unit vectors v1, v2, and v3

• Additionally, we know that the columns of this matrix will be the vectors v1, v2, and v3

• Let’s call the vectors e1, e2, and e3 the world-space axes x, y, and z, and let’s call the vectors v1, v2, and v3the camera-space unit vector axes u, v, and n

• We want to build a rotation matrix that will normalize (u, v, n) into (x, y, z). We just found a matrix, M, that will turn (x, y, z) into (u, v, n). Conversely, M-1=MT

turns (u, v, n) into (x, y, z). The rows of MT will just be u, v, and n

• Reduces the problem of finding the correct rotation matrix into finding the correct perpendicular unit vectors u, v, and n

• Restatement of rotation problem: find u, v, and nusing Position, and the Look vector and Up vector, then applying MT to get the x, y, z unit vectors e1, e2, e3

Building the Orientation Matrix

Page 16: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 16/44

• We know that the (u, v, n) axes we want will have the properties that

– our arbitrary Look Vector will lie along the negative n-axis

– a projection of the Up Vector into the plane perpendicular to the n-axis will lie along the v-axis

– The u-axis will be mutually perpendicular to the v and n-axes, and will form a right-handed coordinate system

• Plan of attack: first find n from Look, then find ufrom n and Up, then find v from n and u

Finding u, v, and n from Position, Look, and Up (1/6)

Page 17: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 17/44

Finding n• Finding n is easy. Look vector in canonical volume

lies on –z. Since z maps to n, n is a normalized vector pointing in the opposite direction from our arbitrary Look vector

Finding u, v, and n (2/6)

LookLook

n−

=

Look

Page 18: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 18/44

Finding u• Problem: the orientation of the u-axis is constrained

by the v and n-axes . . . But we don’t have a v-axis yet. How can we proceed?

• Solution: we will use the Up vector instead of the v-axis. The only problem with this is that (u, Up, n) will not necessarily form an orthogonal basis, i.e., mutually perpendicular and unit vectors (because Upis at an arbitrary angle to n). We’ll correct this later.

• The constraints on u are:– 1. must be perpendicular to plane spanned by n-axis

and Up vector– 2. (u, Up, n) must form a right-handed (though not

mutually perpendicular) coordinate system

• To satisfy (1), we can use cross-product, but which one should we use?

– n X Up and Up X n are both perpendicular to the plane, but in different directions . . .

• Answer: cross-products are right-handed, so use Up X n to satisfy (2) as well

– Use the cross-product applet to convince yourself that “order matters” in cross-product: Applets->Linear Algebra->Cross Product

Finding u, v, and n (3/6)

Page 19: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 19/44

Finding u, cont’d.• So, the formula is:

• Note: n is unit length, but Up vector might not be unit length or perpendicular to n, so we had to normalize u

• Problem: what happens if Up and n are parallel?• Answer: bad stuff! Don’t let it happen!

– try it out on the applet– if you’re mathematically inclined, convince yourself of

the “bad stuff” analytically

• As a reminder, the cross product of two vectors a and b is:

Finding u, v, and n (4/6)

nUpnUp

u××

=

−−−

1221

3113

2332

babababababa

ba

Page 20: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 20/44

Finding v• From previous slide, we currently have (u, Up, n) but

this coordinate system is not mutually perpendicular• Finding v is just as easy as finding u; v must be

perpendicular to u and n, and (u, v, n) must be a right-handed coordinate system

• Again using right hand rule:

• Do not need to normalize v . . . why? Since n and uare unit length and mutually perpendicular, then their cross product is also unit length (see the applet!).

Finding u, v, and n (5/6)

unv ×=

Page 21: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 21/44

To summarize

• The viewing transformation is now fully specified– knowing u, v, and n, we can rotate the canonical

view into the user-specified orientation– we already know how to translate the view

• Important: Note that we don’t actually apply the forward viewing transformation. Instead, the inverse viewing transformation, namely the normalizing transformation, will be used to map the arbitrary view into the canonical view

Finding u, v, and n (6/6)

LookLook

n−

=

unv ×=

nUpnUp

u××

=

Page 22: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 22/44

The Viewing Problem for Parallel Projection• Given a parallel view specification and vertices of a

bunch of objects, we use the normalizing transformation, i.e., the inverse viewing transformation, to normalize the view volume to a cuboid at the origin, then clip, and then project by ignoring z

The Viewing Problem for Perspective Projection• For a perspective view specification, we’ll transform

to a standard perspective view specification and then do one last clever step: we’ll transform the perspective view volume into a parallel (cuboid) view volume, simplifying both clipping and projection

Transforming to the Canonical View

Page 23: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 23/44

• Note: it’s a cuboid, not a cube (makes transformation arithmetic and clipping easy)

The Canonical View Volume

Back clip planetransforms to z = -1

Front clip planetransforms to z = 0

Look(1, 1, 0)

Page 24: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 24/44

The Parallel Case• Decomposes into multiple steps• Each step defined by a matrix transformation• The product of these matrices defines the whole

transformation in one, more “complex,” composite matrix. The steps are:

– move the eye/camera to the origin– transform the view so that (u, v, n) is aligned with

(x, y, z)– adjust the scales so that the view volume fits between

–1 and 1 in x and y, the back clip plane lies at z = –1, the front plane at z = 0

The Perspective Case• Same as parallel, but add one more step:

– distort pyramid to cuboid to achieve perspective distortion and aligns the front clip plane with z = 0

Steps for Normalizing ViewVolume

Page 25: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 25/44

Get the eye to the origin• We want a matrix that will take

(Posx, Posy, Posz) to (0, 0, 0)• Solution: it’s just the inverse of the translation we

found for producing the viewing transform translation(tx, ty, tz) = (–Posx, –Posy, –Posz)

• We will take the matrix:

and we will multiply all vertices explicitly (and the camera implicitly) to preserve relationship between camera and scene, i.e., for all vertices p

• This will move Position to (0, 0, 0)

Going Through the Steps (1/4)

−−−

=−

1000100010001

)(z

y

x

PosPosPos

PositionT

pPositionTp )(' −=

Page 26: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 26/44

• Position now at origin

But we’re hardly done! Still need to:• align orientation with x,y,z world coordinate system• normalize proportions of the view volume

The Current Situation

Look

x

z

y

Page 27: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 27/44

Rotate the view and align with the world coordinate system• We found out that the view transformation matrix M with

columns u, v, and n would rotate the x, y, z axes into the u, v, and n axes

• We now apply inverse (transpose) of that rotation, MT, to scene. That is, a matrix with rows u, v, and n will rotate the axes u, v, and n into the axes x, y, and z

• Now every vertex in the scene (and the camera implicitly) get multiplied by the composite matrix

Going Through the Steps (2/4)

)( PositionTM T −

Page 28: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 28/44

• We’ve translated and rotated, so that the Position is at the origin, and the (u, v, n) coordinates and the (x, y, z) coordinates are aligned

• The scene looks like this:

The Current Situation

Look

Page 29: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 29/44

• We’ve gotten things more or less to the right place, but the proportions of the view volume need to be normalized. Last affine transformation: scaling

• Adjust so that the corners of far clipping plane eventually lie at (+1, +1, –1)

• One mathematical operation works for both parallel and perspective view volumes

• Imagine vectors emanating from origin passing through corners of far clipping plane. For perspective view volume, these are edges of volume. For parallel, these lie inside view volume

• First step: force vectors into 45-degree angles with xand y axes

• We’ll do this by scaling in x and y

Going Through the Steps (3/4)

Page 30: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 30/44

Scaling Clipping Planes• Looking down from above, we see this:

• Want to scale in x to make angle 90 degrees• Need to scale in x by

• Similarly in y

Going Through the Steps (4/4)

=

2

cot

2tan

1 w

w

θθ

1 0

2,,tan wθ

2Wtan θ

Page 31: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 31/44

• The scale matrix we need looks like this:

• So our current composite transformation looks like this:

The xy scaling matrix

=

10000100

002

cot0

0002

cot

h

w

xySθ

θ

)( PositionTMS Txy −

Page 32: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 32/44

• Relative proportions of view volume planes now correct, but back clipping plane probably lying at some z ≠ –1

• We need to shrink the back plane to be at z = –1• The z distance from the eye to that point has not

changed: it’s still far (distance to far clipping plane)• If we scale in z only, proportions of volume will

change; instead we’ll scale uniformly:

One more scaling matrix

=

1000

01

00

001

0

0001

1

far

far

far

farSxyz

Page 33: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 33/44

• Far plane at z = –1.

• Near clip plane now at z = –k (note k > 0)

The Current Situation

x

y

z

z = -1

Page 34: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 34/44

• Our near-final composite normalizing transformation for the canonical perspective view volume is

• Exactly the same tricks suffice to get the parallel view volume to the canonical one

The Results

)(1

PositionTMSfar

S Txyxyz −

Page 35: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 35/44

• We’ve put the perspective view volume into canonical position, orientation and size

• Let’s look at a particular point on the original near clipping plane lying on the Look vector:

It gets moved to a location

on the negative z-axis, say

The Perspective Transformation(1/5)

LooknearPositionp )(+=

pPositionTMSfar

Sp Txyxyz )(

1−

=′

( )kp −=′ 00

Position

Look

Page 36: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 36/44

• What is the value of k? Trace through the steps. p first gets moved to just (near)Look

• This point is the rotated to (near)(–e3)

• The xy scaling has no effect, and the xyzscaling changes this to , so

– Note: this is the same value of k as in slide 33 because the ratio of near and far has been preserved and on that slide, far is –1, so near/far is simply near

The Perspective Transformation(2/5)

3efar

near

farnear

k =

Look

Look

Position

Position

p

p

Look

Page 37: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 37/44

• We now transform the points in the standard perspective view volume between –k and –1 into the standard parallel view volume

• The “z-buffer,” used for visible surface calculation, needs the z values to be [0 1], not [–1 0]. So the perspective transformation must also transform the scene to the range z = 0 to z = 1

• The matrixthat does this is just

• (Remember that 0< k <1 …)

The Perspective Transformation(3/5)

−−−

=

010011

100

00100001

kk

kD

Page 38: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 38/44

• Take a typical point and rewrite it as

• where ; p is parametrized by distance along frustum

– when d = 0, p is on near clip plane; when d = (1-k), p is on far clip plane

– depending on x, y, and z, p may or may not actually fall within frustum

• Apply D (from previous slide) to p to get new point p’

• Note: must divide through by !!! – this causes x and y to be “perspectivized”, with points closest

to near clip plane being scaled up most– understanding homogenous coordinates is essential for

correct viewing

The Perspective Transformation(4/5)

=

1zyx

p

−−1

dkyx

kd −≤≤ 10

+−−++

=

+−−

=

−−=

111

1))dk)(k/((d

)dk/(y)dk/(x

dk)k/(d

yx

dkyx

D'p

1≠w

• Try values of d: 0, 1-k, ½(1-k), -1, 1

Page 39: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

The Perspective Transformation(5/5)

Andries van Dam September 20, 2001 3D Viewing III 39/44

• Again consider

• What happens to x and y when d gets very large?

– note: when we let d increase beyond (1-k), p is now beyondthe frustum (such lines will be clipped)

• This result provides perspective foreshortening: parallel lines converge to a vanishing point

• What happens when d is negative?– here p is now in front of the near clip plane, possibly

behind the eye point (COP)– result: when (k+d) becomes negative, the sign of x and y

will be flipped: text would be upside-down and backwards– you won’t see these points because they are clipped

• What happens after perspective transformation?• Answer: parallel projection is applied to determine

location of points onto the film plane– projection is easy: drop z!– however, we still need to keep the z ordering intact for

visible surface determination

+−−++

=

11 ))dk)(k/((d

)dk/(y)dk/(x

'p

∞→→→ dyx as 0 and 0

Page 40: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 40/44

• Final transformation:

• Note that once the viewing parameters (Position, Up vector, Look vector, Height angle, Aspect ratio, Near, and Far) are known, the matrices

can all be computed and multiplied together to get a single 4x4 matrix that is applied to all points of all objects to get them from “world space” to the standard parallel view volume. This is a huge win for homogeneous coordinates

The End Result

pPositionTMSfar

DSp Txyxyz )(

1−

=′

)(,,,1

,, PositionTMSfar

SD Txyxyz −

Page 41: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 41/44

• We said that taking the picture from the canonical view would be easy: final steps are clipping and projecting onto film plane

• We need to clip scene against sides of view volume• However, we’ve normalized our view volume into an

axis-aligned cuboid that extends from –1 to 1 in x and y and from 0 to 1 in z

• Note: This is the flipped (in z) version of the canonical view volume

• Clipping is easy! Test x and y components of vertices against +/-1. Test z components against 0 and 1

Clipping

Back clip planetransforms to the xy plane here

Front clip planetransforms to here(1, -1, 1)

(-1, -1, 1)

(-1, 1, 0)

(1, 1, 1)

(-1, -1, 0)

(1, 1, 0)

(1, -1, 0)

(-1, 1, 1)

x

y

z

Page 42: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 42/44

• Vertices falling within these values are saved, and vertices falling outside get clipped; edges get clipped by knowing x,y or z value at an intersection plane. Substitute x, y, or z = 1 in the corresponding parametric line equations to solve for t

• In 2D:

Clipping (cont.)

( ) 101 xtxtx ⋅+⋅−=

( ) 101 ytyty ⋅+⋅−=

( ) 101 ztztz ⋅+⋅−=

10 ≤≤ t

(x0, y0, z0 )

(x1, y1, z1 )

t=0

t=1

(1, 1)

(-1, -1)

x

y

(x0, y0 )

(x1, y1 )

x=1

( )

( )

01

0

010

100

10

1

1

1

11

xxx

t

xxtx

txtxx

txxt

−−

=

−=−+−=−

+−=

Page 43: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 43/44

• We can make a picture by the simple strategy of taking each point and “ignoring z” to project it onto the xy-plane

• A point (x,y,z) where

turns into the point (x´, y´) in screen space (assuming viewport is the entire screen) with

by- ignoring z--

• If viewport is inside a Window Manager’s window, then we need to scale down and translate to produce “window coordinates”

• Note: because it’s a parallel projection we could have projected onto the front plane, the back plane, or any intermediate plane … the final pixmap would have been the same

Projection

1024,0 <′′≤ yx

)1(512)1(512 1

+→′+→′

yyxx

1. Note that these functions are not exactly correct since if x or y is ever 1, then we will get x’ or y’ to be 1024 which is out of our range, so we need to make sure that we handle these cases gracefully. In most cases, making sure that we get the floor of 512(x+1) will address this problem since the desired x will be less than 1.

10 ,1,1 ≤≤≤≤− zyx

Page 44: Viewing in Three Dimensions: Mathematics of Projectionsborovsky/lessons/PG4mmg2/sources/Viewing3.pdf• Placement of view volume (visible part of world) specified by camera’s position

I N T R O D U C T I O N T O C O M P U T E R G R A P H I C S

Andries van Dam September 20, 2001 3D Viewing III 44/44

• In summary, the entire problem can be reduced to a composite matrix multiplication of vertices, clipping, and a final matrix multiplication to produce screen coordinates. The final composite matrix (CTM) is the composite of all modeling (instance) transformations (CMTM) accumulated during scene graph traversal from root to leaf, composited with the final composite normalizing transformation N applied to the root/world coordinate system:

• That’s all there is to it, folks!

Summary

CMTMNCTM ⋅=