Loading and displaying .X files without DirectX
3. Animating the modelAll that is left is to parse and display the animation sets of our model. The example code in Sample3.zip builds on the previous one to support animation information. 3.1. Design3.1.1. DescriptionsAnimating a skinned model is as simple as animating a cartoon character. The lead cartoonist draws key animation frames. Each of these key frames represents an important character pose at a precise time. If we were to look at key animation frames of the mouse Jerry (from Tom & Jerry), we might see 3 key frames:
The other cartoonists will use these key frames to draw all the intermediate frames (meaning they will draw frames 1 to 4 and 6 to 14). The animation in a 3D model is only composed of key frames with a timing identification. The Key frame is a picture at a precise moment of the skeleton position. So a key frame can be represented by a set of local transform matrix describing all the bones orientation in character space. The local transform matrix can also be decomposed into:
Thus a local bone transformation can be either represented by a matrix or by a set of vectors and quaternions. The programmer must calculate all the intermediate key frames by interpolating the key local bone transformations in relation to the time elapsed between two keyframes. The interpolation method is linear for matrices and vectors: Interpolation = A + (B - A) * FrameID / DeltaKey Where A and B are two key frames separated by DeltaKey frames, and FrameID is the frame number between both key frames. Quaternions use spherical linear interpolation (aka SLERP): Interpolation = A*[sin((1-Elapsed) * Angle)/sin(Angle)] + B * [sin (Elapsed * Angle) / sin(Angle)] Where A and B are two key quaternion frames, Angle is the total angle between both quaternions and Elapsed = FrameID / DeltaKey. In DirectX .X file, the animations are grouped into Animation Sets blocks. Within each Animation Set there is an Animation block for each animated bone. Each animation block is composed of one or many Animation Key blocks depending on the representation of the local bone transformations. The table below gives the correspondence between Key types and local bone transformation representations:
That's all we should know about skinned model animations. Now let's define our list of desired functionalities:
3.1.2. Code designThe design to store animation sets is straightforward: The MaxKey member variable in the animation set class stores the last key frame timing index. Each animation set stores a list of animation description. An animation description corresponds to a bone. Inside an animation description, there is a list of each type of transformation representation possible. A transformation structure follows the same template as the Rotate Key below: typedef struct { Uint32 Time; Frm::Quaternion Rotation } RotateKey;The responsibility to calculate the new local transformation matrix is given to our Object3D class: Whoa! That looks complex. If we compare with the design in chapter 2, we see that Object3D takes an additional pointer to an animation set: the Object3D stores the current animation set played. It also has a new method MapAnimation. This method is called when the user switches animations to change the current animation set pointer and update the ObjectBone class. The ObjectBone class gets a pointer to the animation class corresponding to its bone name reference. The Object3D method MapAnimation maps the animation to the ObjectBone. ObjectBone also gets a new method CalcAnimation that calculates the local transformation matrix from the current animation timing. So far, so good. 3.2. Implementation3.2.1. Parsing the fileWe add to the main parsing loop a new method to process animations sets. The pseudo-code:
Get the Animation Set name If there is no name, we assign a name. While we have not reached the end of the block
Else avoid the block (AvoidTemplate) Now let's process animation blocks:
While we have not reached the end of the block
If it is an open brace, we read in the Bone name Else avoid the block (AvoidTemplate) There are animation key blocks inside each animation. We process the animation key block:
Read in the number of transformations stored in the animationkey Switch according to the type of transformation
Read in the Timing Read in the transformation data Push the transformation key to the Animation corresponding transformation key. Now we are ready. 3.2.2. Displaying a single animation setDisplaying the animated model is another matter. We setup as before in sample 2. The difference resides in that we call two new methods of Object3D for animation:
The drawing steps are almost the same:
Calculation the animation set local transform matrices Update the skinned mesh. The new step is the calculation of each bone local transform matrices. This step calls a recursive private method CalcAnimation, which:
For each children ObjectBone
The animation method of the ObjectBone does the following: If no animation instance is linked to that bone, bail out.
If we are at the last matrix index use the last matrix as a transform matrix Else interpolate the transform matrix between this matrix index and the next using the Time count parameter.
If there are rotations
If we are at the last rotation index multiply use the last quaternion Else interpolate the rotation quaternion between this rotation index and the next using the Time count parameter. Convert quaternion to a rotation matrix Multiply the transform matrix by the rotation matrix obtained
If we are at the last scaling index multiply use the last vector Else interpolate the scaling vector between this scaling index and the next using the Time count parameter. Convert the scaling vector to a scaling matrix Multiply the transform matrix by the scaling matrix obtained If there are translations Advance the internal translation animation index according to the main Time count If we are at the last translation index multiply use the last translation vector Else interpolate the translation vector between this translation index and the next using the Time count parameter. Convert the translation vector to a translation matrix Multiply the transform matrix by the translation matrix obtained As you can see, we either use an interpolated matrix or a combined transformation matrix. Be aware that matrix multiplication is not commutative: the multiplication of rotation, scaling and translation matrices must always be done in that order. Now that we have our new local transform matrices, we can calculate our final matrices as usual and obtain our animated skinned mesh. ConclusionWhew, that was a long tutorial. I hope you liked it. You are free to use the sample code as you see fit. Enjoy! Ghostly yours,
References[MSDN]
[WML]
[LUNA]
[XBDEV]
[ADAMS]
|
|