Pivot/main.cxx

/**************************************************************************************

 Copyright (C) 2010 Autodesk, Inc. and/or its licensors.
 All Rights Reserved.

 The coded instructions, statements, computer programs, and/or related material 
 (collectively the "Data") in these files contain unpublished information 
 proprietary to Autodesk, Inc. and/or its licensors, which is protected by 
 Canada and United States of America federal copyright law and by international 
 treaties. 
 
 The Data may not be disclosed or distributed to third parties, in whole or in
 part, without the prior written consent of Autodesk, Inc. ("Autodesk").

 THE DATA IS PROVIDED "AS IS" AND WITHOUT WARRANTY.
 ALL WARRANTIES ARE EXPRESSLY EXCLUDED AND DISCLAIMED. AUTODESK MAKES NO
 WARRANTY OF ANY KIND WITH RESPECT TO THE DATA, EXPRESS, IMPLIED OR ARISING
 BY CUSTOM OR TRADE USAGE, AND DISCLAIMS ANY IMPLIED WARRANTIES OF TITLE, 
 NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE OR USE. 
 WITHOUT LIMITING THE FOREGOING, AUTODESK DOES NOT WARRANT THAT THE OPERATION
 OF THE DATA WILL BE UNINTERRUPTED OR ERROR FREE. 
 
 IN NO EVENT SHALL AUTODESK, ITS AFFILIATES, PARENT COMPANIES, LICENSORS
 OR SUPPLIERS ("AUTODESK GROUP") BE LIABLE FOR ANY LOSSES, DAMAGES OR EXPENSES
 OF ANY KIND (INCLUDING WITHOUT LIMITATION PUNITIVE OR MULTIPLE DAMAGES OR OTHER
 SPECIAL, DIRECT, INDIRECT, EXEMPLARY, INCIDENTAL, LOSS OF PROFITS, REVENUE
 OR DATA, COST OF COVER OR CONSEQUENTIAL LOSSES OR DAMAGES OF ANY KIND),
 HOWEVER CAUSED, AND REGARDLESS OF THE THEORY OF LIABILITY, WHETHER DERIVED
 FROM CONTRACT, TORT (INCLUDING, BUT NOT LIMITED TO, NEGLIGENCE), OR OTHERWISE,
 ARISING OUT OF OR RELATING TO THE DATA OR ITS USE OR ANY OTHER PERFORMANCE,
 WHETHER OR NOT AUTODESK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH LOSS
 OR DAMAGE. 

**************************************************************************************/

//
// Illustrates how to get/set pivots, how to convert pivots
// and how to query local and global transform.
// 
//Steps:
//  1. Initialize FBX SDK object.
//  2. Create default animation stack and animation layer.
//  3. Create a pyramid mesh and attach it to a node.
//  4. Set pivots.
//  5. Add animation to the pyramid node.
//  6. Evaluate the local and global transform.
//  7. Save the scene before pivot converting.
//  8. Convert the animation to reset pivots.
//  9. Save the scene after pivot converting.
//

#include <fbxsdk.h>
#include "../Common/Common.h"
#include "../Common/AnimationUtility.h"
#include "../Common/GeometryUtility.h"

const char * SAMPLE_FILENAME_BEFORE_CONVECTION = "pivot_before_convection.fbx";
const char * SAMPLE_FILENAME_AFTER_CONVECTION = "pivot_after_convection.fbx";

const char * PYRAMID_NAME = "Pyramid";

const double KEY_TIME[] = {0.0, 0.5, 1.0};
const float KEY_VALUE[] = {0.0, 90.0, 180.0};

int main(int argc, char** argv)
{
    KFbxSdkManager* lSdkManager = NULL;
    KFbxScene* lScene = NULL;

    // Prepare the FBX SDK.
    InitializeSdkObjects(lSdkManager, lScene);

    // Create a animation stack and layer for the scene
    KFbxAnimStack * lAnimStack;
    KFbxAnimLayer * lAnimLayer = CreateDefaultAnimStackAndLayer(lScene, lAnimStack);

    // Create a pyramid mesh whose bottom width is 4 and height is 4.
    // Attach this pyramid to a node and as a child of the root node of the scene
    KFbxNode * lPyramidNode = CreatePyramid(lScene, PYRAMID_NAME, 4, 4);

    // Enable pivot
    lPyramidNode->SetRotationActive(true);
    // Set the rotation pivot at the center of the pyramid
    lPyramidNode->SetRotationPivot(KFbxNode::eSOURCE_SET, KFbxVector4(0, 2, 0));
    // Add a post rotation for the pyramid
    lPyramidNode->SetPostRotation(KFbxNode::eSOURCE_SET, KFbxVector4(0, 0, -90));

    // Animate the Y channel of the local rotation
    KFbxAnimCurve * lAnimCurve = lPyramidNode->LclRotation.GetCurve<KFbxAnimCurve>(lAnimLayer, "Y", true);
    if (lAnimCurve)
    {
        const int lKeyCount = sizeof(KEY_TIME)/sizeof(double);
        for (int lKeyIndex = 0; lKeyIndex < lKeyCount; ++lKeyIndex)
        {
            KTime lTime;
            KFbxAnimCurveKey lKey;
            lTime.SetSecondDouble(KEY_TIME[lKeyIndex]);
            lKey.Set(lTime, KEY_VALUE[lKeyIndex]);
            lAnimCurve->KeyAdd(lTime, lKey);
        }
    }

    // Query the local transform and global transform of the pyramid node at 0.5 second
    KTime lTime;
    lTime.SetSecondDouble(0.5);
    KFbxXMatrix lLocalTransform = lPyramidNode->EvaluateLocalTransform(lTime);
    KFbxXMatrix lGlobalTransform = lPyramidNode->EvaluateGlobalTransform(lTime);

    // Save the scene before pivot converting
    SaveScene(lSdkManager, lScene, SAMPLE_FILENAME_BEFORE_CONVECTION);

    // Set the target of pivot converting
    // Reset the rotation pivot and post rotation, and maintain the animation
    lPyramidNode->SetPivotState(KFbxNode::eSOURCE_SET, KFbxNode::ePIVOT_STATE_ACTIVE);
    lPyramidNode->SetPivotState(KFbxNode::eDESTINATION_SET, KFbxNode::ePIVOT_STATE_ACTIVE);
    lPyramidNode->SetPostRotation(KFbxNode::eDESTINATION_SET, KFbxVector4(0, 0, 0));
    lPyramidNode->SetRotationPivot(KFbxNode::eDESTINATION_SET, KFbxVector4(0, 0, 0));

    // Convert the animation between source pivot set and destination pivot set with a frame rate of 30 per second
    lScene->GetRootNode()->ConvertPivotAnimationRecursive(lAnimStack->GetName(), KFbxNode::eDESTINATION_SET, 30.0);

    // Copy the rotation pivot and post rotation from destination set to source set in order to save them in file
    lPyramidNode->SetRotationPivot(KFbxNode::eSOURCE_SET, KFbxVector4(0, 0, 0));
    lPyramidNode->SetPostRotation(KFbxNode::eSOURCE_SET, KFbxVector4(0, 0, 0));

    // Save the scene after pivot converting
    SaveScene(lSdkManager, lScene, SAMPLE_FILENAME_AFTER_CONVECTION);

    // Destroy all objects created by the FBX SDK.
    DestroySdkObjects(lSdkManager);
    return 0;
}