Animating the camera
 
 
 

In this section, we’ll examine how CubeCreator animates the camera. The camera, which is aimed at a marker near the center of a group of cubes, sweeps back and up while remaining aimed at the marker. Here is AnimateCamera():

AnimateCamera(lCameraNode, gTakeName);
 
// The camera moves on the X and Y axes.
void AnimateCamera(KFbxNode* pCameraNode, KString pTakeName)
{

Declare an animation curve (FCurve), a time object, and an animation key index. The key index is 0 for the first key, 1 for the second key, and so forth.

    KFCurve* lCurve = NULL;
    KTime lTime;
    int lKeyIndex = 0;

Here, we create one take node for this scene. A node (KFbxNode object) is a container for take nodes. A take node (KFbxTakeNode object) is in turn a container for animation data. Animation data are stored as function curves, often called FCurves or animation curves).

We give the take node the name that we set previously.

    pCameraNode->CreateTakeNode(pTakeName.Buffer());

A scene can have many takes. Only one of these takes can be the current take. CubeCreator’s scene has only one take, but we explicitly must set it as the current take.

    pCamera->SetCurrentTakeNode(pTakeName.Buffer());

The next statement does the following:

    pCamera->LclTranslation.GetKFCurveNode(true, pTakeName.Buffer());

GetKFCurveNode may not be an obvious name for a method to create FCurves, but if true is its first parameter, it will create a set of FCurves if the node (in this case, pCameraNode) does not already have FCurves. For local translation, the default values for the X, Y, and Z channels are 0, 0, and 0.

There are three FCurves each for translation: one for translation along the X-axis, one for translation along the Y-axis, and one for translation along the Z-axis. Similarly, there are three FCurves each for rotation and scaling.

Here are some defined symbols from kfcurvenode.h:

#define KFCURVENODE_TRANSFORM "Transform"
#define KFCURVENODE_T "T"
#define KFCURVENODE_T_X "X"
#define KFCURVENODE_T_Y "Y"
#define KFCURVENODE_T_Z "Z"
#define KFCURVENODE_R "R"
#define KFCURVENODE_R_X "X"
#define KFCURVENODE_R_Y "Y"
#define KFCURVENODE_R_Z "Z"
#define KFCURVENODE_R_W "W"
#define KFCURVENODE_S "S"
#define KFCURVENODE_S_X "X"
#define KFCURVENODE_S_Y "Y"
#define KFCURVENODE_S_Z "Z"

We need to set up the animation curve for translation along the X-axis. We start by getting the animation curve with its default values:

// X translation.
lCurve = pCamera->LclTranslation.GetKFCurve(KFCURVENODE_T_X, pTakeName.Buffer());
if (lCurve)
{

The animation begins immediately (at 0.0 seconds), and ends at 20.0 seconds: 0.0 and 20.0 are keyframe values.

During that time, any node that is animated by this curve (in this case, the camera node) will move from its initial displacement along the X-axis (0.0 units, the third parameter of Keyset() ), to its final displacement (500.0 units).

The velocity of the displacement will be the same at the beginning of the animation as at the end (KFCURVE_INTERPOLATION_LINEAR).

    lCurve->KeyModifyBegin();
    lTime.SetSecondDouble(0.0);
    lKeyIndex = lCurve->KeyAdd(lTime);
    lCurve->KeySet(lKeyIndex, lTime, 0.0, KFCURVE_INTERPOLATION_LINEAR);
    lTime.SetSecondDouble(20.0);
    lKeyIndex = lCurve->KeyAdd(lTime);
    lCurve->KeySet(lKeyIndex, lTime, 500.0);
     lCurve->KeyModifyEnd();
}

The code is similar for translation along the Y-axis. There is no code for the Z-axis: accordingly, the camera will not move along the Z-axis. Nor is there rotation or scaling for the Z-axis.