Title: Andries van Dam September 18, 2003 3D Viewing III
1Andries van Dam September 18,
2003 3D Viewing III
CIS 736 Computer Graphics Review of Basics 5 of
5 Viewing III Friday, 06 February 2004 Reading
Transformations Adapted with Permission W. H.
Hsu http//www.kddresearch.org
2Andries van Dam September 18,
2003 3D Viewing III 1/42
Viewing in Three Dimensions
Mathematics of Projections
- Review from last time
- specifying a 3D viewsetting 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 its 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 users
specified arbitrary position. Well break the
process into three easier steps - get parameters for view specification (covered in
last lecture) - transform from specified view volume into
canonical view volume - using canonical view, clip and project scene to
make 2D picture
3Andries van Dam September 18,
2003 3D Viewing III 2/42
Stage One Specifying a View
Volume
- 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
4Andries van Dam September 18,
2003 3D Viewing III 3/42
Examples of a View Volume (1/2)
- 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 For ease of specification , up vector need
not to be perpendicular to Look vector, but they
cannot be collinear
Height angle Aspect ratio
5Andries van Dam September 18,
2003 3D Viewing III 4/42
Examples of a View Volume (2/2)
- Orthographic Parallel Projection Truncated View
Volume Cuboid - Orthographic parallel projection has no view
angle parameter
6Andries van Dam September 18,
2003 3D Viewing III 5/42
Specifying Arbitrary 3D Views
- Placement of view volume (visible part of world)
specified by cameras 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
7Andries van Dam September 18,
2003 3D Viewing III 6/42
Arbitrary View Volume Too
Complex
- 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
- there is a view specification from which it is
easy to take a picture. Well call it the
canonical view from the origin, looking down the
negative z-axis
think of the scene as lying behind a window and
were looking through the window
- 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
8Andries van Dam September 18,
2003 3D Viewing III 7/42
Normalizing to the Canonical
View Volume
- 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
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
9Andries van Dam September 18,
2003 3D Viewing III 8/42
Viewing Transformation
Normalizing Transformation
- 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 well 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)
10Andries van Dam September 18,
2003 3D Viewing III 9/42
Building Viewing Transformation
from View Specification
- 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 (i.e. the cross-section
of the view volume) to make a square
cross-section will happen at a later stage, as
will clipping - 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
11Andries van Dam September 18,
2003 3D Viewing III 10/42
Rotation (1/5)
- 3 x 3 rotation matrices
- We learned about 3 x 3 matrices that rotate the
world (were 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! - Lets call the three x-axis, y-axis, and
z-axis-aligned unit vectors e1, e2, e3 - Writing out
12Andries van Dam September 18,
2003 3D Viewing III 11/42
Rotation (2/5)
- Lets 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
13Andries van Dam September 18,
2003 3D Viewing III 12/42
Rotation (3/5)
- 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, theyre
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)
14Andries van Dam September 18,
2003 3D Viewing III 13/42
Rotation (4/5)
- Therefore (for rotation matrices)
- We can write this matrix of vivj 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!
15Andries van Dam September 18,
2003 3D Viewing III 14/42
Rotation (5/5)
- 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,
16Andries van Dam September 18,
2003 3D Viewing III 15/42
Building the Orientation Matrix
- Now we know that a rotation matrix is a matrix
that will turn the perpendicular unit vectors e1,
e2, and e3 into 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 - Lets call the vectors e1, e2, and e3 the
world-space axes x, y, and z, and lets call the
vectors v1, v2, and v3 the 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-1MT 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 n
using Position, and the Look vector and Up
vector, then applying MT to get the x, y, z unit
vectors e1, e2, e3
17Andries van Dam September 18,
2003 3D Viewing III 16/42
Finding u, v, and n from Position, Look, and Up
(1/6)
- 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
u from n and Up, then find v from n and u
18Andries van Dam September 18,
2003 3D Viewing III 17/42
Finding u, v, and n (2/6)
- 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 -
Look
19Andries van Dam September 18,
2003 3D Viewing III 18/42
Finding u, v, and n (3/6)
- Finding u
- Problem the orientation of the u-axis is
constrained by the v and n-axes . . . But we
dont 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 Up is at an arbitrary
angle to n). Well 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-gtLinear Algebra-gtCross Product
20Andries van Dam September 18,
2003 3D Viewing III 19/42
Finding u, v, and n (4/6)
- Finding u, contd.
- 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 the length Up x n is zero, which
means we would divide by 0 in the above equation.
Bad stuff, dont let it happen! - try it out on the applet
- As a reminder, the cross product of two vectors a
and b is
21Andries van Dam September 18,
2003 3D Viewing III 20/42
Finding u, v, and n (5/6)
- 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 u are unit length and mutually perpendicular,
then their cross product is also unit length (see
the applet!).
22Andries van Dam September 18,
2003 3D Viewing III 21/42
Finding u, v, and n (6/6)
- 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 dont 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
23Andries van Dam September 18,
2003 3D Viewing III 22/42
Transforming to the Canonical
View
- 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
- Normalize the perspective view specification to a
unit frustum at the origin looking down the z
axis then transform the perspective view volume
into a parallel (cuboid) view volume, simplifying
both clipping and projection
- Note its a cuboid, not a cube (makes
transformation arithmetic and clipping easy)
(1, 1, 0)
Back clip plane transforms to z -1
Front clip plane transforms to z 0
24Andries van Dam September 18,
2003 3D Viewing III 23/42
Steps for Normalizing View
Volume
- 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
25Andries van Dam September 18,
2003 3D Viewing III 24/42
Perspective (1/4)
- Get the eye to the origin
- We want a matrix that will take
- (Posx, Posy, Posz) to (0, 0, 0)
- Solution its 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)
26Andries van Dam September 18,
2003 3D Viewing III 25/42
The Current Situation
- Position now at origin
- But were hardly done! Still need to
- align orientation with x,y,z world coordinate
system - normalize proportions of the view volume
y
Look
z
x
27Andries van Dam September 18,
2003 3D Viewing III 26/42
Perspective (2/4)
- 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 -
- Weve 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
current situation
Look
28Andries van Dam September 18,
2003 3D Viewing III 27/42
Perspective (3/4)
- Weve 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 x and y axes - Well do this by scaling in x and y
29Andries van Dam September 18,
2003 3D Viewing III 28/42
Perspective (4/4)
- 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
30Andries van Dam September 18,
2003 3D Viewing III 29/42
The xy scaling matrix
- The scale matrix we need looks like this
- So our current composite transformation looks
like this -
31Andries van Dam September 18,
2003 3D Viewing III 30/42
One more scaling matrix
- 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 its still far (distance to far clipping
plane) - If we scale in z only, proportions of volume will
change instead well scale uniformly
32Andries van Dam September 18,
2003 3D Viewing III 31/42
The Current Situation
- Far plane at z 1.
- Near clip plane now at z k (note k gt 0)
-
y
z
z -1
x
33Andries van Dam September 18,
2003 3D Viewing III 32/42
The Results
- 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
34Andries van Dam September 18,
2003 3D Viewing III 33/42
The Perspective Transformation
(1/5)
- Weve put the perspective view volume into
canonical position, orientation and size - Lets 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
-
-
Look
Position
35Andries van Dam September 18,
2003 3D Viewing III 34/42
The Perspective Transformation
(2/5)
- 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 xyz
- scaling changes this to , so
36Andries van Dam September 18,
2003 3D Viewing III 35/42
The Perspective Transformation
(3/5)
- 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 matrix
- that does this is just
- (Remember that
- 0lt k lt1 )
Flip the z-axis and unhinge
Note not 1!
37Andries van Dam September 18,
2003 3D Viewing III 36/42
The Perspective Transformation
(4/5)
- 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
Try values of d 0, 1-k, ½(1-k), -1, 1
38The Perspective Transformation
(5/5)
- 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 beyond the 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 (kd) becomes negative, the sign of
x and y will be flipped text would be
upside-down and backwards - you wont 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
Andries van Dam September 18,
2003 3D Viewing III 37/42
39Andries van Dam September 18,
2003 3D Viewing III 38/42
The End Result
- 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
40Andries van Dam September 18,
2003 3D Viewing III 39/42
Clipping
- 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, weve 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
41Andries van Dam September 18,
2003 3D Viewing III 40/42
Clipping (cont.)
- 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
(x1, y1, z1 )
t1
t0
(x0, y0, z0 )
y
(1, 1)
(x1, y1 )
x1
x
(x0, y0 )
(-1, -1)
42Andries van Dam September 18,
2003 3D Viewing III 41/42
Projection
- 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 Managers window,
then we need to scale down and translate to
produce window coordinates - Note because its 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 -
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(x1) will
address this problem since the desired x will be
less than 1.
43Andries van Dam September 18,
2003 3D Viewing III 42/42
Summary
- 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 -
- Thats all there is to it, folks!