Search code examples
c++quaternionsautodeskfbx

FBXSDK, using Quaternions to set rotation keys?


I am trying to write a file save application using the Autodesk FBXSDK. I have this working fine using Euler rotations, but I need to update it to use quaternions.

The relevant function is:

bool CreateScene(FbxScene* pScene, double lFocalLength, int startFrame)
{
    //Create Camera
    FbxNode* lMyCameraNode = FbxNode::Create(pScene, "p_camera");
    //connect camera node to root node
    FbxNode* lRootNode = pScene->GetRootNode();
    lRootNode->ConnectSrcObject(lMyCameraNode);
    FbxCamera* lMyCamera = FbxCamera::Create(pScene, "Root_camera");
    lMyCameraNode->SetNodeAttribute(lMyCamera);


    // Create an animation stack
    FbxAnimStack* myAnimStack = FbxAnimStack::Create(pScene, "My stack");

    // Create the base layer (this is mandatory)
    FbxAnimLayer* pAnimLayer = FbxAnimLayer::Create(pScene, "Layer0");

    myAnimStack->AddMember(pAnimLayer);

    // Get the camera’s curve node for local translation.

    FbxAnimCurveNode* myAnimCurveNodeRot = lMyCameraNode->LclRotation.GetCurveNode(pAnimLayer, true);

    //create curve nodes
    FbxAnimCurve* myRotXCurve = NULL;   
    FbxAnimCurve* myRotYCurve = NULL;
    FbxAnimCurve* myRotZCurve = NULL;

    FbxTime lTime;                         // For the start and stop keys.  int lKeyIndex = 0;                // Index for the keys that define the curve



    // Get the animation curve for local rotation of the camera.
    myRotXCurve = lMyCameraNode->LclRotation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_X, true);
    myRotYCurve = lMyCameraNode->LclRotation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Y, true);
    myRotZCurve = lMyCameraNode->LclRotation.GetCurve(pAnimLayer, FBXSDK_CURVENODE_COMPONENT_Z, true);


    //This to add keys, per frame.  
    float frameNumber = startFrame;

    for (int i = 0; i < rec.size(); i++)
    {
        lTime.SetFrame(frameNumber);  //frame number

        //rx
        lKeyIndex = myRotXCurve->KeyAdd(lTime);
        myRotXCurve->KeySet(lKeyIndex, lTime, recRotX[i], FbxAnimCurveDef::eInterpolationLinear);

        //ry
        lKeyIndex = myRotYCurve->KeyAdd(lTime);
        myRotYCurve->KeySet(lKeyIndex, lTime, recRotY[i], FbxAnimCurveDef::eInterpolationLinear);

        //rz
        lKeyIndex = myRotZCurve->KeyAdd(lTime);
        myRotZCurve->KeySet(lKeyIndex, lTime, recRotZ[i], FbxAnimCurveDef::eInterpolationLinear);

        frameNumber += 1;

    }

    return true;
}

I would ideally like to pass in quaternion data here, instead of the euler x,y,z values. Is this possible with the fbxsdk? or do I need to convert my quaternion data first, and continue to pass in eulers? Thank you.


Solution

  • You always need to go back to Euler angles, as you can only get animation curves for the XYZ rotation. The only thing you have control over is the rotation order.

    However, you can use FbxQuaternion for your calculations, then use .DecomposeSphericalXYZ() to get XYZ Euler angles.