In computer animation today, a human animator still defines the keyframes, but the data in between is interpolated by the computer.. The keyframes are values that the animated property ha
Trang 1This page intentionally left blank
Trang 2ANIMATION
Animation is what ultimately breathes life into 3D graphics While still images can be nice as such, most applications involve objects moving and interacting with each other and the user, or scenes in some other way changing over time This chapter introduces basic, commonly used animation concepts that we will encounter when we discuss the M3G animation functionality later in the book
4.1 KEYFRAME ANIMATION
Keyframe animation is perhaps the most common way of describing predefined motions
in computer graphics The term originates from cartoons, where the senior animator would first draw the most important “key” frames describing the main poses within
an animation sequence The in-between frames or “tweens” could then be filled in to complete the animation, based on those defining features This allowed the valuable time
of the senior animator to be focused on the important parts, whereas the work of drawing the intermediate frames could be divided among the junior colleagues
In computer animation today, a human animator still defines the keyframes, but the
data in between is interpolated by the computer An example of keyframe interpolation
is shown in Figure 4.1 The keyframes are values that the animated property has at specific points in time, and the computer applies an interpolation function to these data points to produce the intermediate values The data itself can be anything from
Trang 3time
F i g u r e 4.1:Keyframe values (points) and interpolated data (curve).
positions and orientations to color and lighting parameters, as long as it can be
represented numerically to the computer
Expressing this mathematically, we have the set of N keyframes K = (k0,k1, ,k N−1)
and an interpolation functionf The value of the animated property can then be
evalu-ated at any time t by evaluating f (K, t); we can also say that we sample the animated value
at different times t Using different functions f, we can vary the characteristics of the
interpolated data, as well as the computational complexity of evaluating the function
The main benefit of keyframe animation today is perhaps not the time saved in producing
the animation, but the memory saved by the keyframe representation Data need only be
stored at the keyframe locations, and since much of the animation data required can be
produced from fairly sparse keyframes, keyframe animation is much more space-efficient
than storing the data for each frame A related benefit is that the keyframe rate need not
be tied to the display rate; once you have your keyframe sequence, you can play it back at
any rate, speeding up or slowing down as necessary
4.1.1 INTERPOLATION
In practice, interpolation is usually implemented in a piecewise manner: each interpolated
segment, separated by two adjacent keyframes, is computed on its own, and the slope
or curvature of the adjacent segments has no effect on the result In some schemes, the
interpolation parameters depend on the keyframes of the adjacent segments as well, but
once those parameters are known, each segment is still interpolated as a separate entity
Interpolating an entire keyframe sequence amounts to identifying the segment we are
interested in, by finding the keyframes surrounding our sampling time t, and computing
the interpolated value for that segment only Let us call the values of our chosen keyframes
simplya and b Those will be the desired values at the beginning and end of the segment,
Trang 4f step (s) = a. (4.1) Instead, as shown in Figure 4.2, the interpolated value always remains at that of the previous keyframe This is very easy from a computational perspective, but as seen in the figure, it produces a discontinuous result that is ill-suited to animating most aspects
of a visual scene—for example, a character jumping from one place to another is not what
we typically expect of animation The step function can still have its uses: switching light sources on and off at preprogrammed times is one intuitive application
Going toward smoother motion, we need to take into account more than one keyframe
Using both keyframes, we can linearly interpolate, or lerp, between them:
time
Step interpolation.
Trang 5time
F i g u r e 4.3:Linear interpolation, or lerp.
As we can see in Figure 4.3, lerp actually connects the keyframes without sudden jumps
However, it is immediately obvious that the result is not smooth: the direction of our
inter-polated line changes abruptly at each keyframe, and the visual effect is very similar This,
again, defies any expectations of physically based motion, where some manner of inertia
is to be expected Lerping is still computationally very cheap, which makes it well suited
to ramping things such as light levels up and down at various speeds, and it can be used to
approximate nonlinear motion by adding more keyframes Of course, any purely linear
motion at constant speed is a natural application, but such motions are quite uncommon
in practice—consider, for example, city traffic during rush hour, or the individual limbs
and joints of a walking human
For more life-like animation, we need a function that can provide some degree of ease-in
and ease-out: instead of jumping into and out of motion, short periods of acceleration
and deceleration make the animation appear much more natural Also, since much of
the animation in computer graphics is not linear, being able to represent curved motion
is another feature in the wish list It would seem that changing the linear segments of
Figure 4.3 into curved ones would solve both problems, and indeed it does—in a number
of different flavors There are many different formulations for producing curves, each
with its individual characteristics that make it better suited for some particular tasks and
less well for others In the following, we will only cover what is relevant to using and
understanding M3G in the later chapters of this book
Trang 6our keyframe sequence similarly to Figure 4.4.
As illustrated in Figure 4.5, each Hermite curve segment is controlled by tangent vectors
at both ends in addition to the actual keyframes If you think of interpolating the position
of an object using a Hermite curve, the tangent vectors are essentially the velocity—speed and direction—of the object at the endpoints of the curve
When discussing lerp, we mentioned that natural objects do not jump from one position
to another, and neither do they change velocity abruptly For natural-looking motion,
we therefore want not only the position, but also the velocity to be continuous There-fore, when interpolating across multiple segments, we want to align the tangent vec-tors between neighboring segments such that the velocity remains constant Since our keyframes are not necessarily spaced uniformly in time, we cannot trivially use the same tangent vector for both segments connected to a keyframe; instead, the tangents
time
F i g u r e 4.4: Curve interpolation.
Trang 7x
y
c
a91
b92
b91
c92
b
a
F i g u r e 4.5: Two Hermite curve segments and their tangent vectors; the end tangent of each
seg-ment is reversed for clarity In order to illustrate the tangent vectors, the coordinate system is different
from Figures 4.2 to 4.4, and time is not plotted at all Note that the relative magnitude of tangents
may have to be scaled to different magnitudes in order to maintain smooth velocity
[KB84, AMH02]
In order to compute the tangent vectorsb−andb+using the three-keyframe sequence
to take into account the durations of the adjacent interpolation segments Let us denote
the time differences betweenb and its adjacent keyframes by Δt abandΔt bc Our tangent
values will then be [KB84, AMH02]
and
wherebis a finite central difference over keyframeb:
The tangents for keyframesa and c are computed in a similar manner from their adjacent
keyframes
Trang 8M =
⎡
⎢
⎢
⎣
−3 3 −2 −1
⎤
⎥
⎥
As we have mentioned, there are numerous other classes of curves and splines used in computer graphics One type of spline commonly used in animation and modeling tools
is an extension of Catmull-Rom splines called Kochanek-Bartels splines [KB84] They
are also known as TCB splines after the tension, continuity, and bias parameters they add
to control the shape of the curve at each keyframe However, for the purposes of this book, it is sufficient to know that it is possible to approximate the shape of any other curve using Catmull-Rom curves, to an arbitrary degree, by adding more keyframes For further reading on other kinds of curves, again, refer to more comprehensive computer graphics textbooks [FvFH90, AMH02, WW92]
4.1.2 QUATERNIONS
Interpolating positions and most other parameters is easy to understand: you can plot the keyframe values on paper, draw a line or curve in between, and pick any point on
the line for an intuitive interpolated position You can interpolate each of the x, y, and z
coordinates independently and get the correct result Interpolating orientations, however,
is not quite as intuitive
Most computer animation today, M3G included, uses unit quaternions to represent
ori-entation, as described in Section 2.3.1 As a quick recap, a unit quaternion is a four-vector
[x y z w] of length one where the first three imaginary components relate to an axis
of rotation and the last, real, component relates to a rotation angle
Each and every quaternion that we are interested in will always rest on a four-dimensional sphere that has a radius of one Imagine that the unit sphere of quaternions has a North
North pole stands for the initial position of your object, before any rotation has been applied to it Each quaternion elsewhere on the surface of the sphere represents a rotation away from the initial position The farther you go from the North pole, the more you rotate Walking along the shortest path possible along the surface will rotate along the
Trang 9shortest path (and about a single axis) between two orientations However, moving by
any number of degrees on the 4D sphere will rotate twice that number in 3D space—refer
to Equation 2.21 for the proof Therefore, if you reach the South pole, your 3D object will
have rotated a full 360 degrees, back to where it started from; but it is important to realize
that in quaternion space, we could not be farther away from the initial position!
To interpolate along the surface of the 4D sphere, spherical linear interpolation or slerp
can be used Assuming two unit quaternion keyframesˆa and ˆb, with the interpolation arc
angleθ defined such that cos θ = ˆa · ˆb, slerp is defined as:
f slerp (s) = slerp(s: ˆa, ˆb) = ˆa sin((1 − s)θ) + ˆb sin(sθ)
Each quaternionˆq has a counterpart −ˆq, on exactly the opposite side of the unit sphere,
which results in exactly the same orientation As long as you are dealing with rotations
of 180 degrees or less, you can optimize your slerp routine a bit by explicitly flipping
the signs on one of the quaternions so that they land on the same hemisphere of the 4D
unit sphere This is what many code examples on quaternion interpolation do, and it will
work as long as all you want is to interpolate along the shortest path between two 3D
orientations However, sometimes you may want to interpolate along the longer path, in
the opposite direction, or blend between more than two orientations, and in such a case
using the whole 4D unit sphere is required
Also, using the proper slerp without any sign mangling, you can actually rotate by
up to 360 degrees between two keyframes, so there is more power available to you
that way In any case, be warned that unless you know very well what you are doing,
you will be better off using the full-blown slerp for interpolation On the other hand,
you will also need to take greater care when exporting the animations from your tools;
some of these only return the orientation keyframes as matrices, requiring a conversion
back to quaternions, and you will need to decide between the quaternions ˆq and −ˆq
for each matrix However, this small headache will enable the rest of the animation
pipeline to work correctly also when more advanced features than simple two-keyframe
interpolation are introduced
Slerp for quaternions has the same problem as lerp for positions: you get instant changes
in angular velocity at keyframes The solution is also similar: use curved interpolation
The equivalent of splines for quaternions is often dubbed squad We omit the associated
hairy math here, but for more details, refer to the paper by Shoemake [Sho87] or a
graph-ics book that treats the subject [WW92, AMH02] Suffice it to say that squad will
inter-polate rotations like spline interinter-polates positions, albeit with increased computational
intensity In practice, slerp or even lerp followed by renormalization (Equation (2.4)) is
often sufficient for purposes such as character animation, and regular spline
interpola-tion can be leveraged for most use cases Squad can still be useful for achieving perfectly
smooth camera interpolation, for example
Trang 104.2.1 MORPHING
A straightforward way to animate a mesh is to do as the cartoonists do: define keyframes
that comprise the essential poses of an animation, then interpolate or morph between
those A simple example is shown in Figure 4.6 The way we can do this is to define a set
of alternative vertex array definitions M0, M1, M2, , M N−1 so that each M irepresents the vertex coordinates (and other related data, such as normals and texture coordinates) for one keyframe The mesh topology or list of primitives is only specified once
To interpolate between the mesh keyframes, we can just lerp between any two of them if the keyframes represent poses on a timeline Better yet, we can think of our keyframes as
alternative shapes, or morph targets, that we can arbitrarily blend between:
where the w iare weights we can freely assign to each of the morph targets Note that using weights outside of the[0, 1] range, it is also possible to extrapolate the predefined shapes
An alternative formulation is to add the weighted morph targets to a base shape:
This emphasizes the role of some base shape that is being modified through morphing, but mathematically, both formulations express the same thing
F i g u r e 4.6: Morphing some of the vertex positions between the two base shapes on the left and the right Note that the number of vertices remains the same.